MediaWiki master
ImportReporter.php
Go to the documentation of this file.
1<?php
24use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
31
37 use ProtectedHookAccessorTrait;
38
40 private $reason;
42 private $logTags = [];
44 private $mOriginalLogCallback;
46 private $mOriginalPageOutCallback;
48 private $mLogItemCount = 0;
50 private $mPageCount = 0;
52 private $mIsUpload;
54 private $mInterwiki;
55
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
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
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}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
array $params
The job parameters.
Reporting callback.
reportNotice( $msg, array $params)
reportPage(?PageIdentity $pageIdentity, $foreignTitle, $revisionCount, $successCount, $pageInfo)
reportLogItem(... $args)
setChangeTags(array $tags)
Sets change tags to apply to the import log entry and null revision.
__construct( $importer, $upload, $interwiki, $reason="", ?IContextSource $context=null)
Class for creating new log entries and inserting them into the database.
Value object for a comment stored by CommentStore.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setContext(IContextSource $context)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
A simple, immutable structure to hold the title of a page on a foreign MediaWiki installation.
Module of static functions for generating XML.
Definition Xml.php:37
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing an editable wiki page.