Merge "Skin: Make skins aware of their registered skin name"
[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( /* ... */ ) {
73 $this->mLogItemCount++;
74 if ( is_callable( $this->mOriginalLogCallback ) ) {
75 call_user_func_array( $this->mOriginalLogCallback, func_get_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 $args = func_get_args();
90 call_user_func_array( $this->mOriginalPageOutCallback, $args );
91
92 if ( $title === null ) {
93 # Invalid or non-importable title; a notice is already displayed
94 return;
95 }
96
97 $this->mPageCount++;
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 ) . " " .
104 "<bdi>" .
105 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
106 "</bdi>" .
107 "</li>\n"
108 );
109
110 $logParams = [ '4:number:count' => $successCount ];
111 if ( $this->mIsUpload ) {
112 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
113 $successCount )->inContentLanguage()->text();
114 $action = 'upload';
115 } else {
116 $pageTitle = $foreignTitle->getFullText();
117 $fullInterwikiPrefix = $this->mInterwiki;
118 Hooks::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
119
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;
126 }
127 if ( $this->reason ) {
128 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
129 . $this->reason;
130 }
131
132 $comment = $detail; // quick
133 $dbw = wfGetDB( DB_MASTER );
134 $latest = $title->getLatestRevID();
135 $nullRevision = Revision::newNullRevision(
136 $dbw,
137 $title->getArticleID(),
138 $comment,
139 true,
140 $this->getUser()
141 );
142
143 $nullRevId = null;
144 if ( !is_null( $nullRevision ) ) {
145 $nullRevId = $nullRevision->insertOn( $dbw );
146 $page = WikiPage::factory( $title );
147 # Update page record
148 $page->updateRevisionOn( $dbw, $nullRevision );
149 Hooks::run(
150 'NewRevisionFromEditComplete',
151 [ $page, $nullRevision, $latest, $this->getUser() ]
152 );
153 }
154
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 $logid = $logEntry->insert();
162 if ( count( $this->logTags ) ) {
163 $logEntry->setTags( $this->logTags );
164 }
165 // Make sure the null revision will be tagged as well
166 $logEntry->setAssociatedRevId( $nullRevId );
167
168 $logEntry->publish( $logid );
169
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 }