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.
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.
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
21 use MediaWiki\MediaWikiServices
;
25 * @ingroup SpecialPage
27 class ImportReporter
extends ContextSource
{
28 private $reason = false;
29 private $logTags = [];
30 private $mOriginalLogCallback = null;
31 private $mOriginalPageOutCallback = null;
32 private $mLogItemCount = 0;
38 * @param WikiImporter $importer
40 * @param string $interwiki
41 * @param string|bool $reason
43 function __construct( $importer, $upload, $interwiki, $reason = false ) {
44 $this->mOriginalPageOutCallback
=
45 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
46 $this->mOriginalLogCallback
=
47 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
48 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
49 $this->mPageCount
= 0;
50 $this->mIsUpload
= $upload;
51 $this->mInterwiki
= $interwiki;
52 $this->reason
= $reason;
56 * Sets change tags to apply to the import log entry and null revision.
61 public function setChangeTags( array $tags ) {
62 $this->logTags
= $tags;
66 $this->getOutput()->addHTML( "<ul>\n" );
69 function reportNotice( $msg, array $params ) {
70 $this->getOutput()->addHTML(
71 Html
::element( 'li', [], $this->msg( $msg, $params )->text() )
75 function reportLogItem( ...$args ) {
76 $this->mLogItemCount++
;
77 if ( is_callable( $this->mOriginalLogCallback
) ) {
78 call_user_func_array( $this->mOriginalLogCallback
, $args );
84 * @param ForeignTitle $foreignTitle
85 * @param int $revisionCount
86 * @param int $successCount
87 * @param array $pageInfo
90 public function reportPage( $title, $foreignTitle, $revisionCount,
91 $successCount, $pageInfo ) {
92 call_user_func_array( $this->mOriginalPageOutCallback
, func_get_args() );
94 if ( $title === null ) {
95 # Invalid or non-importable title; a notice is already displayed
100 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
101 if ( $successCount > 0 ) {
102 // <bdi> prevents jumbling of the versions count
103 // in RTL wikis in case the page title is LTR
104 $this->getOutput()->addHTML(
105 "<li>" . $linkRenderer->makeLink( $title ) . " " .
107 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
112 $logParams = [ '4:number:count' => $successCount ];
113 if ( $this->mIsUpload
) {
114 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
115 $successCount )->inContentLanguage()->text();
118 $pageTitle = $foreignTitle->getFullText();
119 $fullInterwikiPrefix = $this->mInterwiki
;
120 Hooks
::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
122 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
123 $interwiki = '[[:' . $interwikiTitleStr . ']]';
124 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
125 $successCount )->params( $interwiki )->inContentLanguage()->text();
126 $action = 'interwiki';
127 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
129 if ( $this->reason
) {
130 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
134 $comment = $detail; // quick
135 $dbw = wfGetDB( DB_MASTER
);
136 $latest = $title->getLatestRevID();
137 $nullRevision = Revision
::newNullRevision(
139 $title->getArticleID(),
146 if ( !is_null( $nullRevision ) ) {
147 $nullRevId = $nullRevision->insertOn( $dbw );
148 $page = WikiPage
::factory( $title );
150 $page->updateRevisionOn( $dbw, $nullRevision );
152 'NewRevisionFromEditComplete',
153 [ $page, $nullRevision, $latest, $this->getUser() ]
157 // Create the import log entry
158 $logEntry = new ManualLogEntry( 'import', $action );
159 $logEntry->setTarget( $title );
160 $logEntry->setComment( $this->reason
);
161 $logEntry->setPerformer( $this->getUser() );
162 $logEntry->setParameters( $logParams );
163 // Make sure the null revision will be tagged as well
164 $logEntry->setAssociatedRevId( $nullRevId );
165 if ( count( $this->logTags
) ) {
166 $logEntry->addTags( $this->logTags
);
168 $logid = $logEntry->insert();
169 $logEntry->publish( $logid );
171 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
172 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
177 $out = $this->getOutput();
178 if ( $this->mLogItemCount
> 0 ) {
179 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
180 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
181 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
182 $out->addHTML( "</ul>\n" );
184 return Status
::newFatal( 'importnopages' );
186 $out->addHTML( "</ul>\n" );
188 return Status
::newGood( $this->mPageCount
);