MediaWiki master
ImportReporter.php
Go to the documentation of this file.
1<?php
24use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
30
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
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
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
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}
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.
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing an editable wiki page.