xmlbulkexport.class.inc.php 6.4 KB

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