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( /* ... */ ) {
73 $this->mLogItemCount++
;
74 if ( is_callable( $this->mOriginalLogCallback
) ) {
75 call_user_func_array( $this->mOriginalLogCallback
, func_get_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 $args = func_get_args();
90 call_user_func_array( $this->mOriginalPageOutCallback
, $args );
92 if ( $title === null ) {
93 # Invalid or non-importable title; a notice is already displayed
98 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
99 if ( $successCount > 0 ) {
100 // <bdi> prevents jumbling of the versions count
101 // in RTL wikis in case the page title is LTR
102 $this->getOutput()->addHTML(
103 "<li>" . $linkRenderer->makeLink( $title ) . " " .
105 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
110 $logParams = [ '4:number:count' => $successCount ];
111 if ( $this->mIsUpload
) {
112 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
113 $successCount )->inContentLanguage()->text();
116 $pageTitle = $foreignTitle->getFullText();
117 $fullInterwikiPrefix = $this->mInterwiki
;
118 Hooks
::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
120 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
121 $interwiki = '[[:' . $interwikiTitleStr . ']]';
122 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
123 $successCount )->params( $interwiki )->inContentLanguage()->text();
124 $action = 'interwiki';
125 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
127 if ( $this->reason
) {
128 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
132 $comment = $detail; // quick
133 $dbw = wfGetDB( DB_MASTER
);
134 $latest = $title->getLatestRevID();
135 $nullRevision = Revision
::newNullRevision(
137 $title->getArticleID(),
144 if ( !is_null( $nullRevision ) ) {
145 $nullRevId = $nullRevision->insertOn( $dbw );
146 $page = WikiPage
::factory( $title );
148 $page->updateRevisionOn( $dbw, $nullRevision );
150 'NewRevisionFromEditComplete',
151 [ $page, $nullRevision, $latest, $this->getUser() ]
155 // Create the import log entry
156 $logEntry = new ManualLogEntry( 'import', $action );
157 $logEntry->setTarget( $title );
158 $logEntry->setComment( $this->reason
);
159 $logEntry->setPerformer( $this->getUser() );
160 $logEntry->setParameters( $logParams );
161 // Make sure the null revision will be tagged as well
162 $logEntry->setAssociatedRevId( $nullRevId );
163 if ( count( $this->logTags
) ) {
164 $logEntry->setTags( $this->logTags
);
166 $logid = $logEntry->insert();
167 $logEntry->publish( $logid );
169 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
170 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
175 $out = $this->getOutput();
176 if ( $this->mLogItemCount
> 0 ) {
177 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount
)->parse();
178 $out->addHTML( Xml
::tags( 'li', null, $msg ) );
179 } elseif ( $this->mPageCount
== 0 && $this->mLogItemCount
== 0 ) {
180 $out->addHTML( "</ul>\n" );
182 return Status
::newFatal( 'importnopages' );
184 $out->addHTML( "</ul>\n" );
186 return Status
::newGood( $this->mPageCount
);