Merge "Merge last-seen stash data at more points in SpecialWatchlist"
[lhc/web/wiklou.git] / includes / specials / helpers / ImportReporter.php
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
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * Reporting callback
25 * @ingroup SpecialPage
26 */
27 class ImportReporter extends ContextSource {
28 private $reason = false;
29 private $logTags = [];
30 private $mOriginalLogCallback = null;
31 private $mOriginalPageOutCallback = null;
32 private $mLogItemCount = 0;
33
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 /**
53 * Sets change tags to apply to the import log entry and null revision.
54 *
55 * @param array $tags
56 * @since 1.29
57 */
58 public function setChangeTags( array $tags ) {
59 $this->logTags = $tags;
60 }
61
62 function open() {
63 $this->getOutput()->addHTML( "<ul>\n" );
64 }
65
66 function reportNotice( $msg, array $params ) {
67 $this->getOutput()->addHTML(
68 Html::element( 'li', [], $this->msg( $msg, $params )->text() )
69 );
70 }
71
72 function reportLogItem( ...$args ) {
73 $this->mLogItemCount++;
74 if ( is_callable( $this->mOriginalLogCallback ) ) {
75 call_user_func_array( $this->mOriginalLogCallback, $args );
76 }
77 }
78
79 /**
80 * @param Title $title
81 * @param ForeignTitle $foreignTitle
82 * @param int $revisionCount
83 * @param int $successCount
84 * @param array $pageInfo
85 * @return void
86 */
87 public function reportPage( $title, $foreignTitle, $revisionCount,
88 $successCount, $pageInfo ) {
89 call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
90
91 if ( $title === null ) {
92 # Invalid or non-importable title; a notice is already displayed
93 return;
94 }
95
96 $this->mPageCount++;
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 ) . " " .
103 "<bdi>" .
104 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
105 "</bdi>" .
106 "</li>\n"
107 );
108
109 $logParams = [ '4:number:count' => $successCount ];
110 if ( $this->mIsUpload ) {
111 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
112 $successCount )->inContentLanguage()->text();
113 $action = 'upload';
114 } else {
115 $pageTitle = $foreignTitle->getFullText();
116 $fullInterwikiPrefix = $this->mInterwiki;
117 Hooks::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
118
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;
125 }
126 if ( $this->reason ) {
127 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
128 . $this->reason;
129 }
130
131 $comment = $detail; // quick
132 $dbw = wfGetDB( DB_MASTER );
133 $latest = $title->getLatestRevID();
134 $nullRevision = Revision::newNullRevision(
135 $dbw,
136 $title->getArticleID(),
137 $comment,
138 true,
139 $this->getUser()
140 );
141
142 $nullRevId = null;
143 if ( !is_null( $nullRevision ) ) {
144 $nullRevId = $nullRevision->insertOn( $dbw );
145 $page = WikiPage::factory( $title );
146 # Update page record
147 $page->updateRevisionOn( $dbw, $nullRevision );
148 Hooks::run(
149 'NewRevisionFromEditComplete',
150 [ $page, $nullRevision, $latest, $this->getUser() ]
151 );
152 }
153
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 );
164 }
165 $logid = $logEntry->insert();
166 $logEntry->publish( $logid );
167 } else {
168 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
169 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
170 }
171 }
172
173 function close() {
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" );
180
181 return Status::newFatal( 'importnopages' );
182 }
183 $out->addHTML( "</ul>\n" );
184
185 return Status::newGood( $this->mPageCount );
186 }
187 }