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