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