xmlbulkexport.class.inc.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // Copyright (C) 2015-2017 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-2017 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['linksets'] = (utils::ReadParam('linksets', 0) == 1);
  62. }
  63. protected function GetSampleData($oObj, $sAttCode)
  64. {
  65. $sRet = ($sAttCode == 'id') ? $oObj->GetKey() : $oObj->GetAsXML($sAttCode);
  66. return $sRet;
  67. }
  68. public function GetHeader()
  69. {
  70. // Check permissions
  71. foreach($this->oSearch->GetSelectedClasses() as $sAlias => $sClass)
  72. {
  73. if (UserRights::IsActionAllowed($sClass, UR_ACTION_BULK_READ) != UR_ALLOWED_YES)
  74. {
  75. throw new Exception("You do not have enough permissions to bulk read data of class '$sClass' (alias: $sAlias)");
  76. }
  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. $aClasses = $this->oSearch->GetSelectedClasses();
  93. $aAuthorizedClasses = array();
  94. $aClass2Attributes = array();
  95. foreach($aClasses as $sAlias => $sClassName)
  96. {
  97. if (UserRights::IsActionAllowed($sClassName, UR_ACTION_BULK_READ, $oSet) && (UR_ALLOWED_YES || UR_ALLOWED_DEPENDS))
  98. {
  99. $aAuthorizedClasses[$sAlias] = $sClassName;
  100. $aAttributes = array();
  101. foreach(MetaModel::ListAttributeDefs($sClassName) as $sAttCode=>$oAttDef)
  102. {
  103. if ($oAttDef->IsLinkSet() && !$this->aStatusInfo['linksets'])
  104. {
  105. continue;
  106. }
  107. if ($oAttDef->IsExternalField())
  108. {
  109. continue;
  110. }
  111. $aAttributes[$sAttCode] = $oAttDef;
  112. if ($oAttDef->IsExternalKey())
  113. {
  114. foreach(MetaModel::ListAttributeDefs($sClassName) as $sSubAttCode=>$oSubAttDef)
  115. {
  116. if ($oSubAttDef->IsExternalField() && ($oSubAttDef->GetKeyAttCode() == $sAttCode))
  117. {
  118. $aAttributes[$sAttCode.'_friendlyname'] = MetaModel::GetAttributeDef($sClassName, $sAttCode.'_friendlyname');
  119. $aAttributes[$sSubAttCode] = $oSubAttDef;
  120. }
  121. }
  122. }
  123. }
  124. $aClass2Attributes[$sAlias] = $aAttributes;
  125. }
  126. }
  127. $iPreviousTimeLimit = ini_get('max_execution_time');
  128. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  129. while ($aObjects = $oSet->FetchAssoc())
  130. {
  131. set_time_limit($iLoopTimeLimit);
  132. if (count($aAuthorizedClasses) > 1)
  133. {
  134. $sData .= "<Row>\n";
  135. }
  136. foreach($aAuthorizedClasses as $sAlias => $sClassName)
  137. {
  138. $oObj = $aObjects[$sAlias];
  139. if (is_null($oObj))
  140. {
  141. $sData .= "<$sClassName alias=\"$sAlias\" id=\"null\">\n";
  142. }
  143. else
  144. {
  145. $sClassName = get_class($oObj);
  146. $sData .= "<$sClassName alias=\"$sAlias\" id=\"".$oObj->GetKey()."\">\n";
  147. }
  148. foreach($aClass2Attributes[$sAlias] as $sAttCode=>$oAttDef)
  149. {
  150. if (is_null($oObj))
  151. {
  152. $sData .= "<$sAttCode>null</$sAttCode>\n";
  153. }
  154. else
  155. {
  156. $sValue = $oObj->GetAsXML($sAttCode, $this->bLocalizeOutput);
  157. $sData .= "<$sAttCode>$sValue</$sAttCode>\n";
  158. }
  159. }
  160. $sData .= "</$sClassName>\n";
  161. }
  162. if (count($aAuthorizedClasses) > 1)
  163. {
  164. $sData .= "</Row>\n";
  165. }
  166. $iCount++;
  167. }
  168. set_time_limit($iPreviousTimeLimit);
  169. $this->aStatusInfo['position'] += $this->iChunkSize;
  170. if ($this->aStatusInfo['total'] == 0)
  171. {
  172. $iPercentage = 100;
  173. }
  174. else
  175. {
  176. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  177. }
  178. if ($iCount < $this->iChunkSize)
  179. {
  180. $sRetCode = 'done';
  181. }
  182. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  183. return $sData;
  184. }
  185. public function GetFooter()
  186. {
  187. $sData = "</Set>\n";
  188. return $sData;
  189. }
  190. public function GetSupportedFormats()
  191. {
  192. return array('xml' => Dict::S('Core:BulkExport:XMLFormat'));
  193. }
  194. public function GetMimeType()
  195. {
  196. return 'text/xml';
  197. }
  198. public function GetFileExtension()
  199. {
  200. return 'xml';
  201. }
  202. }