MediaWiki master
ImportReporter.php
Go to the documentation of this file.
1<?php
23use 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 ( $this->mOriginalLogCallback )( ...$args );
103 }
104 }
105
114 public function reportPage( ?PageIdentity $pageIdentity, $foreignTitle, $revisionCount,
115 $successCount, $pageInfo ) {
116 ( $this->mOriginalPageOutCallback )( $pageIdentity, $foreignTitle, $revisionCount, $successCount, $pageInfo );
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 $wikiPage = $services->getWikiPageFactory()->newFromTitle( $pageIdentity );
161 $dummyRevRecord = $wikiPage->newPageUpdater( $this->getUser() )
162 ->setCause( PageUpdater::CAUSE_IMPORT )
163 ->saveDummyRevision( $detail, EDIT_SILENT | EDIT_MINOR );
164
165 // Create the import log entry
166 $logEntry = new ManualLogEntry( 'import', $action );
167 $logEntry->setTarget( $pageIdentity );
168 $logEntry->setComment( $this->reason );
169 $logEntry->setPerformer( $this->getUser() );
170 $logEntry->setParameters( $logParams );
171 // Make sure the null revision will be tagged as well
172 $logEntry->setAssociatedRevId( $dummyRevRecord->getId() );
173 if ( count( $this->logTags ) ) {
174 $logEntry->addTags( $this->logTags );
175 }
176 $logid = $logEntry->insert();
177 $logEntry->publish( $logid );
178 } else {
179 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $pageIdentity ) . " " .
180 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
181 }
182 }
183
184 public function close() {
185 $out = $this->getOutput();
186 if ( $this->mLogItemCount > 0 ) {
187 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
188 $out->addHTML( Html::rawElement( 'li', [], $msg ) );
189 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
190 $out->addHTML( "</ul>\n" );
191
192 return Status::newFatal( 'importnopages' );
193 }
194 $out->addHTML( "</ul>\n" );
195
196 return Status::newGood( $this->mPageCount );
197 }
198}
const EDIT_SILENT
Do not notify other users (e.g.
Definition Defines.php:137
const EDIT_MINOR
Mark this edit minor, if the user is allowed to do so.
Definition Defines.php:134
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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)
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:57
Class for creating new log entries and inserting them into the database.
Service locator for MediaWiki core services.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Controller-like object for creating and updating pages by creating new revisions.
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.