MediaWiki master
ImportReporter.php
Go to the documentation of this file.
1<?php
8
11use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
22
28 use ProtectedHookAccessorTrait;
29
31 private $reason;
33 private $logTags = [];
35 private $mOriginalLogCallback;
37 private $mOriginalPageOutCallback;
39 private $mLogItemCount = 0;
41 private $mPageCount = 0;
42
43 public function __construct(
44 WikiImporter $importer,
45 private readonly bool $mIsUpload,
46 private readonly ?string $mInterwiki,
47 string|bool|null $reason = "",
48 ?IContextSource $context = null,
49 ) {
50 if ( $context ) {
51 $this->setContext( $context );
52 } else {
53 wfDeprecated( __METHOD__ . ' without $context', '1.42' );
54 }
55 $this->mOriginalPageOutCallback =
56 $importer->setPageOutCallback( $this->reportPage( ... ) );
57 $this->mOriginalLogCallback =
58 $importer->setLogItemCallback( $this->reportLogItem( ... ) );
59 $importer->setNoticeCallback( $this->reportNotice( ... ) );
60 $this->reason = is_string( $reason ) ? $reason : "";
61 }
62
69 public function setChangeTags( array $tags ) {
70 $this->logTags = $tags;
71 }
72
73 public function open() {
74 $this->getOutput()->addHTML( "<ul>\n" );
75 }
76
82 public function reportNotice( $msg, array $params ) {
83 $this->getOutput()->addHTML(
84 Html::element( 'li', [], $this->msg( $msg, $params )->text() )
85 );
86 }
87
91 public function reportLogItem( ...$args ) {
92 $this->mLogItemCount++;
93 if ( is_callable( $this->mOriginalLogCallback ) ) {
94 ( $this->mOriginalLogCallback )( ...$args );
95 }
96 }
97
106 public function reportPage( ?PageIdentity $pageIdentity, $foreignTitle, $revisionCount,
107 $successCount, $pageInfo ) {
108 ( $this->mOriginalPageOutCallback )( $pageIdentity, $foreignTitle, $revisionCount, $successCount, $pageInfo );
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 = (string)$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 $wikiPage = $services->getWikiPageFactory()->newFromTitle( $pageIdentity );
153 $dummyRevRecord = $wikiPage->newPageUpdater( $this->getUser() )
154 ->setCause( PageUpdater::CAUSE_IMPORT )
155 ->saveDummyRevision( $detail, EDIT_SILENT | EDIT_MINOR );
156
157 // Create the import log entry
158 $logEntry = new ManualLogEntry( 'import', $action );
159 $logEntry->setTarget( $pageIdentity );
160 $logEntry->setComment( $this->reason );
161 $logEntry->setPerformer( $this->getUser() );
162 $logEntry->setParameters( $logParams );
163 // Make sure the dummy revision will be tagged as well
164 $logEntry->setAssociatedRevId( $dummyRevRecord->getId() );
165 if ( count( $this->logTags ) ) {
166 $logEntry->addTags( $this->logTags );
167 }
168 $logid = $logEntry->insert();
169 $logEntry->publish( $logid );
170 } else {
171 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $pageIdentity ) . " " .
172 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
173 }
174 }
175
176 public function close(): Status {
177 $out = $this->getOutput();
178 if ( $this->mLogItemCount > 0 ) {
179 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
180 $out->addHTML( Html::rawElement( 'li', [], $msg ) );
181 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
182 $out->addHTML( "</ul>\n" );
183
184 return Status::newFatal( 'importnopages' );
185 }
186 $out->addHTML( "</ul>\n" );
187
188 return Status::newGood( $this->mPageCount );
189 }
190}
191
193class_alias( ImportReporter::class, 'ImportReporter' );
const EDIT_SILENT
Do not notify other users (e.g.
Definition Defines.php:123
const EDIT_MINOR
Mark this edit minor, if the user is allowed to do so.
Definition Defines.php:120
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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:43
XML file reader for the page data importer.
setPageOutCallback( $callback)
Sets the action to perform as each page in the stream is completed.
setLogItemCallback( $callback)
Sets the action to perform as each log item reached.
setNoticeCallback( $callback)
Set a callback that displays notice messages.
Class for creating new log entries and inserting them into the database.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
__construct(WikiImporter $importer, private readonly bool $mIsUpload, private readonly ?string $mInterwiki, string|bool|null $reason="", ?IContextSource $context=null,)
setChangeTags(array $tags)
Sets change tags to apply to the import log entry and dummy revision.
reportPage(?PageIdentity $pageIdentity, $foreignTitle, $revisionCount, $successCount, $pageInfo)
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
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.
Value object representing a message parameter with one of the types from {.
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing an editable wiki page.
element(SerializerNode $parent, SerializerNode $node, $contents)