xmlbulkexport.class.inc.php 5.0 KB

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