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;
35 * @param WikiImporter $importer
37 * @param string $interwiki
38 * @param string|bool $reason
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;
53 * Sets change tags to apply to the import log entry and null revision.
58 public function setChangeTags( array $tags ) {
59 $this->logTags
= $tags;
63 $this->getOutput()->addHTML( "<ul>\n" );
66 function reportNotice( $msg, array $params ) {
67 $this->getOutput()->addHTML(
68 Html
::element( 'li', [], $this->msg( $msg, $params )->text() )
72 function reportLogItem( ...$args ) {
73 $this->mLogItemCount++
;
74 if ( is_callable( $this->mOriginalLogCallback
) ) {
75 call_user_func_array( $this->mOriginalLogCallback
, $args );
81 * @param ForeignTitle $foreignTitle
82 * @param int $revisionCount
83 * @param int $successCount
84 * @param array $pageInfo
87 public function reportPage( $title, $foreignTitle, $revisionCount,
88 $successCount, $pageInfo ) {
89 call_user_func_array( $this->mOriginalPageOutCallback
, func_get_args() );
91 if ( $title === null ) {
92 # Invalid or non-importable title; a notice is already displayed
97 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
98 if ( $successCount > 0 ) {
99 // <bdi> prevents jumbling of the versions count
100 // in RTL wikis in case the page title is LTR
101 $this->getOutput()->addHTML(
102 "<li>" . $linkRenderer->makeLink( $title ) . " " .
104 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
109 $logParams = [ '4:number:count' => $successCount ];
110 if ( $this->mIsUpload
) {
111 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
112 $successCount )->inContentLanguage()->text();
115 $pageTitle = $foreignTitle->getFullText();
116 $fullInterwikiPrefix = $this->mInterwiki
;
117 Hooks
::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
119 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
120 $interwiki = '[[:' . $interwikiTitleStr . ']]';
121 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
122 $successCount )->params( $interwiki )->inContentLanguage()->text();
123 $action = 'interwiki';
124 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
126 if ( $this->reason
) {
127 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
131 $comment = $detail; // quick
132 $dbw = wfGetDB( DB_MASTER
);
133 $latest = $title->getLatestRevID();
134 $nullRevision = Revision
::newNullRevision(
136 $title->getArticleID(),
143 if ( !is_null( $nullRevision ) ) {
144 $nullRevId = $nullRevision->insertOn( $dbw );
145 $page = WikiPage
::factory( $title );
147 $page->updateRevisionOn( $dbw, $nullRevision );
149 'NewRevisionFromEditComplete',
150 [ $page, $nullRevision, $latest, $this->getUser() ]
154 // Create the import log entry
155 $logEntry = new ManualLogEntry( 'import', $action );
156 $logEntry->setTarget( $title );
157 $logEntry->setComment( $this->reason
);
158 $logEntry->setPerformer( $this->getUser() );
159 $logEntry->setParameters( $logParams );
160 // Make sure the null revision will be tagged as well
161 $logEntry->setAssociatedRevId( $nullRevId );
162 if ( count( $this->logTags
) ) {
163 $logEntry->setTags( $this->logTags
);
165 $logid = $logEntry->insert();
166 $logEntry->publish( $logid );
168 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
169 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
174 $out = $this->getOutput();
175 if ( $this->mLogItemCount
> 0 ) {
176 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
177 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
178 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
179 $out->addHTML( "</ul>\n" );
181 return Status
::newFatal( 'importnopages' );
183 $out->addHTML( "</ul>\n" );
185 return Status
::newGood( $this->mPageCount
);