PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/MantisBT/plugins/XmlImportExport/ImportXml.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 184 lines | 112 code | 26 blank | 46 comment | 10 complexity | a43830a0245bc2800def2fcd5e8e6df8 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. # MantisBT - a php based bugtracking system
  3. # Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
  4. # MantisBT is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # MantisBT is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
  16. require_once( 'ImportXml' . DIRECTORY_SEPARATOR . 'Mapper.php' );
  17. require_once( 'ImportXml' . DIRECTORY_SEPARATOR . 'Issue.php' );
  18. class SourceData {
  19. public $version;
  20. public $urlbase;
  21. public $issuelink;
  22. public $notelink;
  23. public $format;
  24. public function get_issue_url( $issue_id ) {
  25. return $this->urlbase . 'view.php?id=' . $issue_id;
  26. }
  27. public function get_note_url( $issue_id, $note_id ) {
  28. return $this->urlbase . 'view.php?id=' . $issue_id . '#c' . $note_id;
  29. }
  30. }
  31. /**
  32. * Perform import from an XML file
  33. */
  34. class ImportXML {
  35. private $source_;
  36. private $reader_;
  37. private $itemsMap_;
  38. private $strategy_;
  39. private $fallback_;
  40. // issues specific options
  41. private $keepCategory_;
  42. private $defaultCategory_;
  43. /**
  44. * Constructor
  45. *
  46. * @param string $filename name of the file to read
  47. * @param string $strategy conversion strategy; one of "renumber", "link" or "disable"
  48. * @param string $fallback alternative conversion strategy when "renumber" does not apply
  49. */
  50. public function __construct( $filename, $strategy, $fallback, $keepCategory, $defaultCategory ) {
  51. $this->source_ = new SourceData;
  52. $this->reader_ = new XMLReader( );
  53. $this->itemsMap_ = new ImportXml_Mapper;
  54. $this->strategy_ = $strategy;
  55. $this->fallback_ = $fallback;
  56. $this->keepCategory_ = $keepCategory;
  57. $this->defaultCategory_ = $defaultCategory;
  58. $this->reader_->open( $filename['tmp_name'] );
  59. }
  60. /**
  61. * Perform import from an XML file
  62. *
  63. * @param string $p_filename name of the file to read
  64. * @param string $p_strategy conversion strategy; one of "renumber", "link" or "disable"
  65. */
  66. public function import( ) {
  67. // Read the <mantis> element and it's attributes
  68. while( $this->reader_->read( ) && $this->reader_->name == 'mantis' ) {
  69. $this->source_->version = $this->reader_->getAttribute( 'version' );
  70. $this->source_->urlbase = $this->reader_->getAttribute( 'urlbase' );
  71. $this->source_->issuelink = $this->reader_->getAttribute( 'issuelink' );
  72. $this->source_->notelink = $this->reader_->getAttribute( 'notelink' );
  73. $this->source_->format = $this->reader_->getAttribute( 'format' );
  74. }
  75. echo 'Importing file, please wait...';
  76. // loop through the elements
  77. while( $this->reader_->read( ) ) {
  78. switch( $this->reader_->nodeType ) {
  79. case XMLReader::ELEMENT:
  80. /* element start */
  81. $t_element_name = $this->reader_->localName;
  82. $t_importer = $this->get_importer_object( $t_element_name );
  83. if( !is_null( $t_importer ) ) {
  84. $t_importer->process( $this->reader_ );
  85. $t_importer->update_map( $this->itemsMap_ );
  86. }
  87. break;
  88. }
  89. }
  90. echo " Done\n";
  91. $importedIssues = $this->itemsMap_->getall( 'issue' );
  92. printf( "Processing cross-references for %s issues...", count( $importedIssues ) );
  93. foreach( $importedIssues as $oldId => $newId ) {
  94. $bugData = bug_get( $newId, true );
  95. $bugLinkRegexp = '/(^|[^\w])(' . preg_quote( $this->source_->issuelink, '/' ) . ')(\d+)\b/e';
  96. $replacement = '"\\1" . $this->getReplacementString( "\\2", "\\3" )';
  97. $bugData->description = preg_replace( $bugLinkRegexp, $replacement, $bugData->description );
  98. $bugData->update( true, true );
  99. }
  100. echo " Done\n";
  101. }
  102. /**
  103. * Compute and return the new link
  104. *
  105. */
  106. private function getReplacementString( $oldLinkTag, $oldId ) {
  107. $linkTag = config_get( 'bug_link_tag' );
  108. $replacement = '';
  109. switch( $this->strategy_ ) {
  110. case 'link':
  111. $replacement = $this->source_->get_issue_url( $oldId );
  112. break;
  113. case 'disable':
  114. $replacement = htmlFullEntities( $oldLinkTag ) . $oldId;
  115. break;
  116. case 'renumber':
  117. if( $this->itemsMap_->exists( 'issue', $oldId ) ) {
  118. // regular renumber
  119. $replacement = $linkTag . $this->itemsMap_->getNewID( 'issue', $oldId );
  120. } else {
  121. // fallback strategy
  122. if( $this->fallback_ == 'link' ) {
  123. $replacement = $this->source_->get_issue_url( $oldId );
  124. }
  125. if( $this->fallback_ == 'disable' ) {
  126. $replacement = htmlFullEntities( $oldLinkTag ) . $oldId;
  127. }
  128. }
  129. break;
  130. default:
  131. echo "Unknown method";
  132. }
  133. //echo "$oldId -> $replacement\n"; // DEBUG
  134. return $replacement;
  135. }
  136. private function get_importer_object( $p_element_name ) {
  137. $importer = null;
  138. switch( $p_element_name ) {
  139. case 'issue':
  140. $importer = new ImportXml_Issue( $this->keepCategory_, $this->defaultCategory_ );
  141. break;
  142. }
  143. return $importer;
  144. }
  145. }
  146. /** candidates for string api **/
  147. /**
  148. * Convert each character of the passed string to the
  149. * corresponding HTML entity.
  150. */
  151. function htmlFullEntities( $string ) {
  152. $chars = str_split( $string );
  153. $escaped = array_map( 'getEntity', $chars );
  154. return implode( '', $escaped );
  155. }
  156. function getEntity( $char ) {
  157. return '&#' . ord( $char ) . ';';
  158. }