Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.42% |
72 / 93 |
|
14.29% |
1 / 7 |
CRAP | |
0.00% |
0 / 1 |
ImportReporter | |
77.42% |
72 / 93 |
|
14.29% |
1 / 7 |
23.16 | |
0.00% |
0 / 1 |
__construct | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
3.01 | |||
setChangeTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
open | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
reportNotice | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
reportLogItem | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
reportPage | |
86.15% |
56 / 65 |
|
0.00% |
0 / 1 |
7.13 | |||
close | |
55.56% |
5 / 9 |
|
0.00% |
0 / 1 |
5.40 |
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\CommentStore\CommentStoreComment; |
22 | use MediaWiki\Context\ContextSource; |
23 | use MediaWiki\Context\IContextSource; |
24 | use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
25 | use MediaWiki\Html\Html; |
26 | use MediaWiki\MediaWikiServices; |
27 | use MediaWiki\Page\PageIdentity; |
28 | use MediaWiki\Status\Status; |
29 | use MediaWiki\Title\ForeignTitle; |
30 | use MediaWiki\Xml\Xml; |
31 | |
32 | /** |
33 | * Reporting callback |
34 | * @ingroup SpecialPage |
35 | */ |
36 | class ImportReporter extends ContextSource { |
37 | use ProtectedHookAccessorTrait; |
38 | |
39 | /** @var string */ |
40 | private $reason; |
41 | /** @var string[] */ |
42 | private $logTags = []; |
43 | /** @var callable|null */ |
44 | private $mOriginalLogCallback; |
45 | /** @var callable|null */ |
46 | private $mOriginalPageOutCallback; |
47 | /** @var int */ |
48 | private $mLogItemCount = 0; |
49 | /** @var int */ |
50 | private $mPageCount = 0; |
51 | /** @var bool */ |
52 | private $mIsUpload; |
53 | /** @var string */ |
54 | private $mInterwiki; |
55 | |
56 | /** |
57 | * @param WikiImporter $importer |
58 | * @param bool $upload |
59 | * @param string $interwiki |
60 | * @param string|bool $reason |
61 | * @param IContextSource|null $context |
62 | */ |
63 | public function __construct( $importer, $upload, $interwiki, $reason = "", ?IContextSource $context = null ) { |
64 | if ( $context ) { |
65 | $this->setContext( $context ); |
66 | } else { |
67 | wfDeprecated( __METHOD__ . ' without $context', '1.42' ); |
68 | } |
69 | $this->mOriginalPageOutCallback = |
70 | $importer->setPageOutCallback( [ $this, 'reportPage' ] ); |
71 | $this->mOriginalLogCallback = |
72 | $importer->setLogItemCallback( [ $this, 'reportLogItem' ] ); |
73 | $importer->setNoticeCallback( [ $this, 'reportNotice' ] ); |
74 | $this->mIsUpload = $upload; |
75 | $this->mInterwiki = $interwiki; |
76 | $this->reason = is_string( $reason ) ? $reason : ""; |
77 | } |
78 | |
79 | /** |
80 | * Sets change tags to apply to the import log entry and null revision. |
81 | * |
82 | * @param string[] $tags |
83 | * @since 1.29 |
84 | */ |
85 | public function setChangeTags( array $tags ) { |
86 | $this->logTags = $tags; |
87 | } |
88 | |
89 | public function open() { |
90 | $this->getOutput()->addHTML( "<ul>\n" ); |
91 | } |
92 | |
93 | public function reportNotice( $msg, array $params ) { |
94 | $this->getOutput()->addHTML( |
95 | Html::element( 'li', [], $this->msg( $msg, $params )->text() ) |
96 | ); |
97 | } |
98 | |
99 | public function reportLogItem( ...$args ) { |
100 | $this->mLogItemCount++; |
101 | if ( is_callable( $this->mOriginalLogCallback ) ) { |
102 | call_user_func_array( $this->mOriginalLogCallback, $args ); |
103 | } |
104 | } |
105 | |
106 | /** |
107 | * @param ?PageIdentity $pageIdentity |
108 | * @param ForeignTitle $foreignTitle |
109 | * @param int $revisionCount |
110 | * @param int $successCount |
111 | * @param array $pageInfo |
112 | * @return void |
113 | */ |
114 | public function reportPage( ?PageIdentity $pageIdentity, $foreignTitle, $revisionCount, |
115 | $successCount, $pageInfo ) { |
116 | call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() ); |
117 | |
118 | if ( $pageIdentity === null ) { |
119 | # Invalid or non-importable title; a notice is already displayed |
120 | return; |
121 | } |
122 | |
123 | $this->mPageCount++; |
124 | $services = MediaWikiServices::getInstance(); |
125 | $linkRenderer = $services->getLinkRenderer(); |
126 | if ( $successCount > 0 ) { |
127 | // <bdi> prevents jumbling of the versions count |
128 | // in RTL wikis in case the page title is LTR |
129 | $this->getOutput()->addHTML( |
130 | "<li>" . $linkRenderer->makeLink( $pageIdentity ) . " " . |
131 | "<bdi>" . |
132 | $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() . |
133 | "</bdi>" . |
134 | "</li>\n" |
135 | ); |
136 | |
137 | $logParams = [ '4:number:count' => $successCount ]; |
138 | if ( $this->mIsUpload ) { |
139 | $detail = $this->msg( 'import-logentry-upload-detail' )->numParams( |
140 | $successCount )->inContentLanguage()->text(); |
141 | $action = 'upload'; |
142 | } else { |
143 | $pageTitle = $foreignTitle->getFullText(); |
144 | $fullInterwikiPrefix = $this->mInterwiki; |
145 | $this->getHookRunner()->onImportLogInterwikiLink( |
146 | $fullInterwikiPrefix, $pageTitle ); |
147 | |
148 | $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle; |
149 | $interwiki = '[[:' . $interwikiTitleStr . ']]'; |
150 | $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams( |
151 | $successCount )->params( $interwiki )->inContentLanguage()->text(); |
152 | $action = 'interwiki'; |
153 | $logParams['5:title-link:interwiki'] = $interwikiTitleStr; |
154 | } |
155 | if ( $this->reason ) { |
156 | $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() |
157 | . $this->reason; |
158 | } |
159 | |
160 | $comment = CommentStoreComment::newUnsavedComment( $detail ); |
161 | $dbw = $services->getConnectionProvider()->getPrimaryDatabase(); |
162 | $revStore = $services->getRevisionStore(); |
163 | $nullRevRecord = $revStore->newNullRevision( |
164 | $dbw, |
165 | $pageIdentity, |
166 | $comment, |
167 | true, |
168 | $this->getUser() |
169 | ); |
170 | |
171 | $nullRevId = null; |
172 | if ( $nullRevRecord !== null ) { |
173 | $inserted = $revStore->insertRevisionOn( $nullRevRecord, $dbw ); |
174 | $nullRevId = $inserted->getId(); |
175 | $parentRevId = $inserted->getParentId(); |
176 | $page = $services->getWikiPageFactory()->newFromTitle( $pageIdentity ); |
177 | |
178 | // Update page record |
179 | $page->updateRevisionOn( $dbw, $inserted ); |
180 | |
181 | $fakeTags = []; |
182 | $this->getHookRunner()->onRevisionFromEditComplete( |
183 | $page, $inserted, $parentRevId, $this->getUser(), $fakeTags |
184 | ); |
185 | } |
186 | |
187 | // Create the import log entry |
188 | $logEntry = new ManualLogEntry( 'import', $action ); |
189 | $logEntry->setTarget( $pageIdentity ); |
190 | $logEntry->setComment( $this->reason ); |
191 | $logEntry->setPerformer( $this->getUser() ); |
192 | $logEntry->setParameters( $logParams ); |
193 | // Make sure the null revision will be tagged as well |
194 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T303637 |
195 | $logEntry->setAssociatedRevId( $nullRevId ); |
196 | if ( count( $this->logTags ) ) { |
197 | $logEntry->addTags( $this->logTags ); |
198 | } |
199 | $logid = $logEntry->insert(); |
200 | $logEntry->publish( $logid ); |
201 | } else { |
202 | $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $pageIdentity ) . " " . |
203 | $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" ); |
204 | } |
205 | } |
206 | |
207 | public function close() { |
208 | $out = $this->getOutput(); |
209 | if ( $this->mLogItemCount > 0 ) { |
210 | $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse(); |
211 | $out->addHTML( Xml::tags( 'li', null, $msg ) ); |
212 | } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) { |
213 | $out->addHTML( "</ul>\n" ); |
214 | |
215 | return Status::newFatal( 'importnopages' ); |
216 | } |
217 | $out->addHTML( "</ul>\n" ); |
218 | |
219 | return Status::newGood( $this->mPageCount ); |
220 | } |
221 | } |