ImportReporter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use MediaWiki\MediaWikiServices;
  21. /**
  22. * Reporting callback
  23. * @ingroup SpecialPage
  24. */
  25. class ImportReporter extends ContextSource {
  26. private $reason = false;
  27. private $logTags = [];
  28. private $mOriginalLogCallback = null;
  29. private $mOriginalPageOutCallback = null;
  30. private $mLogItemCount = 0;
  31. private $mPageCount;
  32. private $mIsUpload;
  33. private $mInterwiki;
  34. /**
  35. * @param WikiImporter $importer
  36. * @param bool $upload
  37. * @param string $interwiki
  38. * @param string|bool $reason
  39. */
  40. function __construct( $importer, $upload, $interwiki, $reason = false ) {
  41. $this->mOriginalPageOutCallback =
  42. $importer->setPageOutCallback( [ $this, 'reportPage' ] );
  43. $this->mOriginalLogCallback =
  44. $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
  45. $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
  46. $this->mPageCount = 0;
  47. $this->mIsUpload = $upload;
  48. $this->mInterwiki = $interwiki;
  49. $this->reason = $reason;
  50. }
  51. /**
  52. * Sets change tags to apply to the import log entry and null revision.
  53. *
  54. * @param array $tags
  55. * @since 1.29
  56. */
  57. public function setChangeTags( array $tags ) {
  58. $this->logTags = $tags;
  59. }
  60. function open() {
  61. $this->getOutput()->addHTML( "<ul>\n" );
  62. }
  63. function reportNotice( $msg, array $params ) {
  64. $this->getOutput()->addHTML(
  65. Html::element( 'li', [], $this->msg( $msg, $params )->text() )
  66. );
  67. }
  68. function reportLogItem( ...$args ) {
  69. $this->mLogItemCount++;
  70. if ( is_callable( $this->mOriginalLogCallback ) ) {
  71. call_user_func_array( $this->mOriginalLogCallback, $args );
  72. }
  73. }
  74. /**
  75. * @param Title $title
  76. * @param ForeignTitle $foreignTitle
  77. * @param int $revisionCount
  78. * @param int $successCount
  79. * @param array $pageInfo
  80. * @return void
  81. */
  82. public function reportPage( $title, $foreignTitle, $revisionCount,
  83. $successCount, $pageInfo ) {
  84. call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
  85. if ( $title === null ) {
  86. # Invalid or non-importable title; a notice is already displayed
  87. return;
  88. }
  89. $this->mPageCount++;
  90. $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
  91. if ( $successCount > 0 ) {
  92. // <bdi> prevents jumbling of the versions count
  93. // in RTL wikis in case the page title is LTR
  94. $this->getOutput()->addHTML(
  95. "<li>" . $linkRenderer->makeLink( $title ) . " " .
  96. "<bdi>" .
  97. $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
  98. "</bdi>" .
  99. "</li>\n"
  100. );
  101. $logParams = [ '4:number:count' => $successCount ];
  102. if ( $this->mIsUpload ) {
  103. $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
  104. $successCount )->inContentLanguage()->text();
  105. $action = 'upload';
  106. } else {
  107. $pageTitle = $foreignTitle->getFullText();
  108. $fullInterwikiPrefix = $this->mInterwiki;
  109. Hooks::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
  110. $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
  111. $interwiki = '[[:' . $interwikiTitleStr . ']]';
  112. $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
  113. $successCount )->params( $interwiki )->inContentLanguage()->text();
  114. $action = 'interwiki';
  115. $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
  116. }
  117. if ( $this->reason ) {
  118. $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
  119. . $this->reason;
  120. }
  121. $comment = $detail; // quick
  122. $dbw = wfGetDB( DB_MASTER );
  123. $latest = $title->getLatestRevID();
  124. $nullRevision = Revision::newNullRevision(
  125. $dbw,
  126. $title->getArticleID(),
  127. $comment,
  128. true,
  129. $this->getUser()
  130. );
  131. $nullRevId = null;
  132. if ( !is_null( $nullRevision ) ) {
  133. $nullRevId = $nullRevision->insertOn( $dbw );
  134. $page = WikiPage::factory( $title );
  135. # Update page record
  136. $page->updateRevisionOn( $dbw, $nullRevision );
  137. Hooks::run(
  138. 'NewRevisionFromEditComplete',
  139. [ $page, $nullRevision, $latest, $this->getUser() ]
  140. );
  141. }
  142. // Create the import log entry
  143. $logEntry = new ManualLogEntry( 'import', $action );
  144. $logEntry->setTarget( $title );
  145. $logEntry->setComment( $this->reason );
  146. $logEntry->setPerformer( $this->getUser() );
  147. $logEntry->setParameters( $logParams );
  148. // Make sure the null revision will be tagged as well
  149. $logEntry->setAssociatedRevId( $nullRevId );
  150. if ( count( $this->logTags ) ) {
  151. $logEntry->addTags( $this->logTags );
  152. }
  153. $logid = $logEntry->insert();
  154. $logEntry->publish( $logid );
  155. } else {
  156. $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
  157. $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
  158. }
  159. }
  160. function close() {
  161. $out = $this->getOutput();
  162. if ( $this->mLogItemCount > 0 ) {
  163. $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
  164. $out->addHTML( Xml::tags( 'li', null, $msg ) );
  165. } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
  166. $out->addHTML( "</ul>\n" );
  167. return Status::newFatal( 'importnopages' );
  168. }
  169. $out->addHTML( "</ul>\n" );
  170. return Status::newGood( $this->mPageCount );
  171. }
  172. }