Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 private $mPageCount;
34 private $mIsUpload;
35 private $mInterwiki;
36
37 /**
38 * @param WikiImporter $importer
39 * @param bool $upload
40 * @param string $interwiki
41 * @param string|bool $reason
42 */
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;
53 }
54
55 /**
56 * Sets change tags to apply to the import log entry and null revision.
57 *
58 * @param array $tags
59 * @since 1.29
60 */
61 public function setChangeTags( array $tags ) {
62 $this->logTags = $tags;
63 }
64
65 function open() {
66 $this->getOutput()->addHTML( "<ul>\n" );
67 }
68
69 function reportNotice( $msg, array $params ) {
70 $this->getOutput()->addHTML(
71 Html::element( 'li', [], $this->msg( $msg, $params )->text() )
72 );
73 }
74
75 function reportLogItem( ...$args ) {
76 $this->mLogItemCount++;
77 if ( is_callable( $this->mOriginalLogCallback ) ) {
78 call_user_func_array( $this->mOriginalLogCallback, $args );
79 }
80 }
81
82 /**
83 * @param Title $title
84 * @param ForeignTitle $foreignTitle
85 * @param int $revisionCount
86 * @param int $successCount
87 * @param array $pageInfo
88 * @return void
89 */
90 public function reportPage( $title, $foreignTitle, $revisionCount,
91 $successCount, $pageInfo ) {
92 call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
93
94 if ( $title === null ) {
95 # Invalid or non-importable title; a notice is already displayed
96 return;
97 }
98
99 $this->mPageCount++;
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 ) . " " .
106 "<bdi>" .
107 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
108 "</bdi>" .
109 "</li>\n"
110 );
111
112 $logParams = [ '4:number:count' => $successCount ];
113 if ( $this->mIsUpload ) {
114 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
115 $successCount )->inContentLanguage()->text();
116 $action = 'upload';
117 } else {
118 $pageTitle = $foreignTitle->getFullText();
119 $fullInterwikiPrefix = $this->mInterwiki;
120 Hooks::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
121
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;
128 }
129 if ( $this->reason ) {
130 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
131 . $this->reason;
132 }
133
134 $comment = $detail; // quick
135 $dbw = wfGetDB( DB_MASTER );
136 $latest = $title->getLatestRevID();
137 $nullRevision = Revision::newNullRevision(
138 $dbw,
139 $title->getArticleID(),
140 $comment,
141 true,
142 $this->getUser()
143 );
144
145 $nullRevId = null;
146 if ( !is_null( $nullRevision ) ) {
147 $nullRevId = $nullRevision->insertOn( $dbw );
148 $page = WikiPage::factory( $title );
149 # Update page record
150 $page->updateRevisionOn( $dbw, $nullRevision );
151 Hooks::run(
152 'NewRevisionFromEditComplete',
153 [ $page, $nullRevision, $latest, $this->getUser() ]
154 );
155 }
156
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 );
167 }
168 $logid = $logEntry->insert();
169 $logEntry->publish( $logid );
170 } else {
171 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
172 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
173 }
174 }
175
176 function close() {
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" );
183
184 return Status::newFatal( 'importnopages' );
185 }
186 $out->addHTML( "</ul>\n" );
187
188 return Status::newGood( $this->mPageCount );
189 }
190 }