xmlbulkexport.class.inc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // Copyright (C) 2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Bulk export: XML export
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class XMLBulkExport extends BulkExport
  25. {
  26. public function DisplayUsage(Page $oP)
  27. {
  28. $oP->p(" * xml format options:");
  29. $oP->p(" *\tThere are no options for the XML format.");
  30. }
  31. public function EnumFormParts()
  32. {
  33. return array_merge(parent::EnumFormParts(), array('xml_options' => array('xml_no_options')));
  34. }
  35. public function DisplayFormPart(WebPage $oP, $sPartId)
  36. {
  37. switch($sPartId)
  38. {
  39. case 'xml_options':
  40. $sNoLocalizeChecked = (utils::ReadParam('no_localize', 0) == 1) ? ' checked ' : '';
  41. $sLinksetChecked = (utils::ReadParam('linksets', 0) == 1) ? ' checked ' : '';
  42. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:XMLOptions').'</legend>');
  43. $oP->add('<table>');
  44. $oP->add('<tr>');
  45. $oP->add('<td><input type="checkbox" id="xml_no_localize" name="no_localize" value="1"'.$sNoLocalizeChecked.'><label for="xml_no_localize"> '.Dict::S('Core:BulkExport:OptionNoLocalize').'</label></td>');
  46. $oP->add('</tr>');
  47. $oP->add('<tr>');
  48. $oP->add('<td><input type="checkbox" id="xml_linksets" name="linksets" value="1"'.$sLinksetChecked.'><label for="xml_linksets"> '.Dict::S('Core:BulkExport:OptionLinkSets').'</label></td>');
  49. $oP->add('</tr>');
  50. $oP->add('</table>');
  51. $oP->add('</fieldset>');
  52. break;
  53. default:
  54. return parent:: DisplayFormPart($oP, $sPartId);
  55. }
  56. }
  57. public function ReadParameters()
  58. {
  59. parent::ReadParameters();
  60. $this->aStatusInfo['localize'] = (utils::ReadParam('no_localize', 0) != 1);
  61. $this->aStatusInfo['linksets'] = (utils::ReadParam('linksets', 0) == 1);
  62. }
  63. protected function GetSampleData($oObj, $sAttCode)
  64. {
  65. if ($oObj)
  66. {
  67. $sRet = $oObj->GetAsXML($sAttCode);
  68. }
  69. else
  70. {
  71. $sRet = '';
  72. }
  73. return $sRet;
  74. }
  75. public function GetHeader()
  76. {
  77. $oSet = new DBObjectSet($this->oSearch);
  78. $this->aStatusInfo['position'] = 0;
  79. $this->aStatusInfo['total'] = $oSet->Count();
  80. $sData = "<"."?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Set>\n";
  81. return $sData;
  82. }
  83. public function GetNextChunk(&$aStatus)
  84. {
  85. $sRetCode = 'run';
  86. $iPercentage = 0;
  87. $iCount = 0;
  88. $sData = '';
  89. $oSet = new DBObjectSet($this->oSearch);
  90. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  91. $bLocalize = $this->aStatusInfo['localize'];
  92. $aClasses = $this->oSearch->GetSelectedClasses();
  93. $aAuthorizedClasses = array();
  94. foreach($aClasses as $sAlias => $sClassName)
  95. {
  96. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  97. {
  98. $aAuthorizedClasses[$sAlias] = $sClassName;
  99. }
  100. }
  101. $aAttribs = array();
  102. $aList = array();
  103. $aList[$sAlias] = MetaModel::GetZListItems($sClassName, 'details');
  104. $iPreviousTimeLimit = ini_get('max_execution_time');
  105. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  106. while ($aObjects = $oSet->FetchAssoc())
  107. {
  108. set_time_limit($iLoopTimeLimit);
  109. if (count($aAuthorizedClasses) > 1)
  110. {
  111. $sData .= "<Row>\n";
  112. }
  113. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  114. {
  115. $oObj = $aObjects[$sAlias];
  116. if (is_null($oObj))
  117. {
  118. $sData .= "<$sClassName alias=\"$sAlias\" id=\"null\">\n";
  119. }
  120. else
  121. {
  122. $sClassName = get_class($oObj);
  123. $sData .= "<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n";
  124. }
  125. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  126. {
  127. if ($oAttDef->IsLinkSet() && !$this->aStatusInfo['linksets'])
  128. {
  129. // Skip link sets
  130. continue;
  131. }
  132. if (is_null($oObj))
  133. {
  134. $sData .= "<$sAttCode>null</$sAttCode>\n";
  135. }
  136. else
  137. {
  138. if ($oAttDef->IsWritable() )
  139. {
  140. $sValue = $oObj->GetAsXML($sAttCode, $bLocalize);
  141. $sData .= "<$sAttCode>$sValue</$sAttCode>\n";
  142. }
  143. }
  144. }
  145. $sData .= "</$sClassName>\n";
  146. }
  147. if (count($aAuthorizedClasses) > 1)
  148. {
  149. $sData .= "</Row>\n";
  150. }
  151. $iCount++;
  152. }
  153. set_time_limit($iPreviousTimeLimit);
  154. $this->aStatusInfo['position'] += $this->iChunkSize;
  155. if ($this->aStatusInfo['total'] == 0)
  156. {
  157. $iPercentage = 100;
  158. }
  159. else
  160. {
  161. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  162. }
  163. if ($iCount < $this->iChunkSize)
  164. {
  165. $sRetCode = 'done';
  166. }
  167. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  168. return $sData;
  169. }
  170. public function GetFooter()
  171. {
  172. $sData = "</Set>\n";
  173. return $sData;
  174. }
  175. public function GetSupportedFormats()
  176. {
  177. return array('xml' => Dict::S('Core:BulkExport:XMLFormat'));
  178. }
  179. public function GetMimeType()
  180. {
  181. return 'text/xml';
  182. }
  183. public function GetFileExtension()
  184. {
  185. return 'xml';
  186. }
  187. }