xmlbulkexport.class.inc.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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(" *\tno_localize: set to 1 to retrieve non-localized values (for instance for ENUM values). Default is 0 (= localized values)");
  30. $oP->p(" *\tlinksets: set to 1 to retrieve links to related objects (1-N or N-N relations). Default is 0 (= only scalar fields)");
  31. }
  32. public function EnumFormParts()
  33. {
  34. return array_merge(parent::EnumFormParts(), array('xml_options' => array('xml_no_options')));
  35. }
  36. public function DisplayFormPart(WebPage $oP, $sPartId)
  37. {
  38. switch($sPartId)
  39. {
  40. case 'xml_options':
  41. $sNoLocalizeChecked = (utils::ReadParam('no_localize', 0) == 1) ? ' checked ' : '';
  42. $sLinksetChecked = (utils::ReadParam('linksets', 0) == 1) ? ' checked ' : '';
  43. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:XMLOptions').'</legend>');
  44. $oP->add('<table>');
  45. $oP->add('<tr>');
  46. $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>');
  47. $oP->add('</tr>');
  48. $oP->add('<tr>');
  49. $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>');
  50. $oP->add('</tr>');
  51. $oP->add('</table>');
  52. $oP->add('</fieldset>');
  53. break;
  54. default:
  55. return parent:: DisplayFormPart($oP, $sPartId);
  56. }
  57. }
  58. public function ReadParameters()
  59. {
  60. parent::ReadParameters();
  61. $this->aStatusInfo['localize'] = (utils::ReadParam('no_localize', 0) != 1);
  62. $this->aStatusInfo['linksets'] = (utils::ReadParam('linksets', 0) == 1);
  63. }
  64. protected function GetSampleData($oObj, $sAttCode)
  65. {
  66. if ($oObj)
  67. {
  68. $sRet = $oObj->GetAsXML($sAttCode);
  69. }
  70. else
  71. {
  72. $sRet = '';
  73. }
  74. return $sRet;
  75. }
  76. public function GetHeader()
  77. {
  78. $oSet = new DBObjectSet($this->oSearch);
  79. $this->aStatusInfo['position'] = 0;
  80. $this->aStatusInfo['total'] = $oSet->Count();
  81. $sData = "<"."?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n<Set>\n";
  82. return $sData;
  83. }
  84. public function GetNextChunk(&$aStatus)
  85. {
  86. $sRetCode = 'run';
  87. $iPercentage = 0;
  88. $iCount = 0;
  89. $sData = '';
  90. $oSet = new DBObjectSet($this->oSearch);
  91. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  92. $bLocalize = $this->aStatusInfo['localize'];
  93. $aClasses = $this->oSearch->GetSelectedClasses();
  94. $aAuthorizedClasses = array();
  95. $aClass2Attributes = array();
  96. foreach($aClasses as $sAlias => $sClassName)
  97. {
  98. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  99. {
  100. $aAuthorizedClasses[$sAlias] = $sClassName;
  101. $aAttributes = array();
  102. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  103. {
  104. if ($oAttDef->IsLinkSet() && !$this->aStatusInfo['linksets'])
  105. {
  106. continue;
  107. }
  108. if (!$oAttDef->IsWritable())
  109. {
  110. continue;
  111. }
  112. $aAttributes[$sAttCode] = $oAttDef;
  113. if ($oAttDef->IsExternalKey())
  114. {
  115. foreach(MetaModel::ListAttributeDefs($sClassName) as $sSubAttCode=>$oSubAttDef)
  116. {
  117. if ($oSubAttDef->IsExternalField() && ($oSubAttDef->GetKeyAttCode() == $sAttCode))
  118. {
  119. $aAttributes[$sAttCode.'_friendlyname'] = MetaModel::GetAttributeDef($sClassName, $sAttCode.'_friendlyname');
  120. $aAttributes[$sSubAttCode] = $oSubAttDef;
  121. }
  122. }
  123. }
  124. }
  125. $aClass2Attributes[$sAlias] = $aAttributes;
  126. }
  127. }
  128. $iPreviousTimeLimit = ini_get('max_execution_time');
  129. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  130. while ($aObjects = $oSet->FetchAssoc())
  131. {
  132. set_time_limit($iLoopTimeLimit);
  133. if (count($aAuthorizedClasses) > 1)
  134. {
  135. $sData .= "<Row>\n";
  136. }
  137. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  138. {
  139. $oObj = $aObjects[$sAlias];
  140. if (is_null($oObj))
  141. {
  142. $sData .= "<$sClassName alias=\"$sAlias\" id=\"null\">\n";
  143. }
  144. else
  145. {
  146. $sClassName = get_class($oObj);
  147. $sData .= "<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n";
  148. }
  149. foreach($aClass2Attributes[$sAlias] as $sAttCode=>$oAttDef)
  150. {
  151. if (is_null($oObj))
  152. {
  153. $sData .= "<$sAttCode>null</$sAttCode>\n";
  154. }
  155. else
  156. {
  157. $sValue = $oObj->GetAsXML($sAttCode, $bLocalize);
  158. $sData .= "<$sAttCode>$sValue</$sAttCode>\n";
  159. }
  160. }
  161. $sData .= "</$sClassName>\n";
  162. }
  163. if (count($aAuthorizedClasses) > 1)
  164. {
  165. $sData .= "</Row>\n";
  166. }
  167. $iCount++;
  168. }
  169. set_time_limit($iPreviousTimeLimit);
  170. $this->aStatusInfo['position'] += $this->iChunkSize;
  171. if ($this->aStatusInfo['total'] == 0)
  172. {
  173. $iPercentage = 100;
  174. }
  175. else
  176. {
  177. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  178. }
  179. if ($iCount < $this->iChunkSize)
  180. {
  181. $sRetCode = 'done';
  182. }
  183. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  184. return $sData;
  185. }
  186. public function GetFooter()
  187. {
  188. $sData = "</Set>\n";
  189. return $sData;
  190. }
  191. public function GetSupportedFormats()
  192. {
  193. return array('xml' => Dict::S('Core:BulkExport:XMLFormat'));
  194. }
  195. public function GetMimeType()
  196. {
  197. return 'text/xml';
  198. }
  199. public function GetFileExtension()
  200. {
  201. return 'xml';
  202. }
  203. }