xmlbulkexport.class.inc.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. $sChecked = (utils::ReadParam('no_localize', 0) == 1) ? ' checked ' : '';
  41. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:XMLOptions').'</legend>');
  42. $oP->add('<table>');
  43. $oP->add('<tr>');
  44. $oP->add('<td><input type="checkbox" id="xml_no_localize" name="no_localize" value="1"'.$sChecked.'><label for="xml_no_localize"> '.Dict::S('Core:BulkExport:OptionNoLocalize').'</label></td>');
  45. $oP->add('</tr>');
  46. $oP->add('</table>');
  47. $oP->add('</fieldset>');
  48. break;
  49. default:
  50. return parent:: DisplayFormPart($oP, $sPartId);
  51. }
  52. }
  53. public function ReadParameters()
  54. {
  55. parent::ReadParameters();
  56. $this->aStatusInfo['localize'] = (utils::ReadParam('no_localize', 0) != 1);
  57. }
  58. protected function GetSampleData($oObj, $sAttCode)
  59. {
  60. if ($oObj)
  61. {
  62. $sRet = $oObj->GetAsXML($sAttCode);
  63. }
  64. else
  65. {
  66. $sRet = '';
  67. }
  68. return $sRet;
  69. }
  70. public function GetHeader()
  71. {
  72. $oSet = new DBObjectSet($this->oSearch);
  73. $this->aStatusInfo['position'] = 0;
  74. $this->aStatusInfo['total'] = $oSet->Count();
  75. $sData = "<"."?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Set>\n";
  76. return $sData;
  77. }
  78. public function GetNextChunk(&$aStatus)
  79. {
  80. $sRetCode = 'run';
  81. $iPercentage = 0;
  82. $iCount = 0;
  83. $sData = '';
  84. $oSet = new DBObjectSet($this->oSearch);
  85. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  86. $bLocalize = $this->aStatusInfo['localize'];
  87. $aClasses = $this->oSearch->GetSelectedClasses();
  88. $aAuthorizedClasses = array();
  89. foreach($aClasses as $sAlias => $sClassName)
  90. {
  91. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  92. {
  93. $aAuthorizedClasses[$sAlias] = $sClassName;
  94. }
  95. }
  96. $aAttribs = array();
  97. $aList = array();
  98. $aList[$sAlias] = MetaModel::GetZListItems($sClassName, 'details');
  99. $iPreviousTimeLimit = ini_get('max_execution_time');
  100. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  101. while ($aObjects = $oSet->FetchAssoc())
  102. {
  103. set_time_limit($iLoopTimeLimit);
  104. if (count($aAuthorizedClasses) > 1)
  105. {
  106. $sData .= "<Row>\n";
  107. }
  108. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  109. {
  110. $oObj = $aObjects[$sAlias];
  111. if (is_null($oObj))
  112. {
  113. $sData .= "<$sClassName alias=\"$sAlias\" id=\"null\">\n";
  114. }
  115. else
  116. {
  117. $sClassName = get_class($oObj);
  118. $sData .= "<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n";
  119. }
  120. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  121. {
  122. if (is_null($oObj))
  123. {
  124. $sData .= "<$sAttCode>null</$sAttCode>\n";
  125. }
  126. else
  127. {
  128. if ($oAttDef->IsWritable() )
  129. {
  130. $sValue = $oObj->GetAsXML($sAttCode, $bLocalize);
  131. $sData .= "<$sAttCode>$sValue</$sAttCode>\n";
  132. }
  133. }
  134. }
  135. $sData .= "</$sClassName>\n";
  136. }
  137. if (count($aAuthorizedClasses) > 1)
  138. {
  139. $sData .= "</Row>\n";
  140. }
  141. $iCount++;
  142. }
  143. set_time_limit($iPreviousTimeLimit);
  144. $this->aStatusInfo['position'] += $this->iChunkSize;
  145. if ($this->aStatusInfo['total'] == 0)
  146. {
  147. $iPercentage = 100;
  148. }
  149. else
  150. {
  151. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  152. }
  153. if ($iCount < $this->iChunkSize)
  154. {
  155. $sRetCode = 'done';
  156. }
  157. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  158. return $sData;
  159. }
  160. public function GetFooter()
  161. {
  162. $sData = "</Set>\n";
  163. return $sData;
  164. }
  165. public function GetSupportedFormats()
  166. {
  167. return array('xml' => Dict::S('Core:BulkExport:XMLFormat'));
  168. }
  169. public function GetMimeType()
  170. {
  171. return 'text/xml';
  172. }
  173. public function GetFileExtension()
  174. {
  175. return 'xml';
  176. }
  177. }