MediaWiki REL1_39
ImportReporter.php
Go to the documentation of this file.
1<?php
21use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
24
30 use ProtectedHookAccessorTrait;
31
32 private $reason;
33 private $logTags = [];
34 private $mOriginalLogCallback;
35 private $mOriginalPageOutCallback;
36 private $mLogItemCount = 0;
37 private $mPageCount = 0;
38 private $mIsUpload;
39 private $mInterwiki;
40
47 public function __construct( $importer, $upload, $interwiki, $reason = "" ) {
48 $this->mOriginalPageOutCallback =
49 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
50 $this->mOriginalLogCallback =
51 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
52 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
53 $this->mIsUpload = $upload;
54 $this->mInterwiki = $interwiki;
55 $this->reason = is_string( $reason ) ? $reason : "";
56 }
57
64 public function setChangeTags( array $tags ) {
65 $this->logTags = $tags;
66 }
67
68 public function open() {
69 $this->getOutput()->addHTML( "<ul>\n" );
70 }
71
72 public function reportNotice( $msg, array $params ) {
73 $this->getOutput()->addHTML(
74 Html::element( 'li', [], $this->msg( $msg, $params )->text() )
75 );
76 }
77
78 public function reportLogItem( ...$args ) {
79 $this->mLogItemCount++;
80 if ( is_callable( $this->mOriginalLogCallback ) ) {
81 call_user_func_array( $this->mOriginalLogCallback, $args );
82 }
83 }
84
93 public function reportPage( ?PageIdentity $pageIdentity, $foreignTitle, $revisionCount,
94 $successCount, $pageInfo ) {
95 call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
96
97 if ( $pageIdentity === null ) {
98 # Invalid or non-importable title; a notice is already displayed
99 return;
100 }
101
102 $this->mPageCount++;
103 $services = MediaWikiServices::getInstance();
104 $linkRenderer = $services->getLinkRenderer();
105 if ( $successCount > 0 ) {
106 // <bdi> prevents jumbling of the versions count
107 // in RTL wikis in case the page title is LTR
108 $this->getOutput()->addHTML(
109 "<li>" . $linkRenderer->makeLink( $pageIdentity ) . " " .
110 "<bdi>" .
111 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
112 "</bdi>" .
113 "</li>\n"
114 );
115
116 $logParams = [ '4:number:count' => $successCount ];
117 if ( $this->mIsUpload ) {
118 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
119 $successCount )->inContentLanguage()->text();
120 $action = 'upload';
121 } else {
122 $pageTitle = $foreignTitle->getFullText();
123 $fullInterwikiPrefix = $this->mInterwiki;
124 $this->getHookRunner()->onImportLogInterwikiLink(
125 $fullInterwikiPrefix, $pageTitle );
126
127 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
128 $interwiki = '[[:' . $interwikiTitleStr . ']]';
129 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
130 $successCount )->params( $interwiki )->inContentLanguage()->text();
131 $action = 'interwiki';
132 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
133 }
134 if ( $this->reason ) {
135 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
136 . $this->reason;
137 }
138
139 $comment = CommentStoreComment::newUnsavedComment( $detail );
140 $dbw = wfGetDB( DB_PRIMARY );
141 $revStore = $services->getRevisionStore();
142 $nullRevRecord = $revStore->newNullRevision(
143 $dbw,
144 $pageIdentity,
145 $comment,
146 true,
147 $this->getUser()
148 );
149
150 $nullRevId = null;
151 if ( $nullRevRecord !== null ) {
152 $inserted = $revStore->insertRevisionOn( $nullRevRecord, $dbw );
153 $nullRevId = $inserted->getId();
154 $parentRevId = $inserted->getParentId();
155 $page = $services->getWikiPageFactory()->newFromTitle( $pageIdentity );
156
157 // Update page record
158 $page->updateRevisionOn( $dbw, $inserted );
159
160 $fakeTags = [];
161 $this->getHookRunner()->onRevisionFromEditComplete(
162 $page, $inserted, $parentRevId, $this->getUser(), $fakeTags
163 );
164 }
165
166 // Create the import log entry
167 $logEntry = new ManualLogEntry( 'import', $action );
168 $logEntry->setTarget( $pageIdentity );
169 $logEntry->setComment( $this->reason );
170 $logEntry->setPerformer( $this->getUser() );
171 $logEntry->setParameters( $logParams );
172 // Make sure the null revision will be tagged as well
173 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T303637
174 $logEntry->setAssociatedRevId( $nullRevId );
175 if ( count( $this->logTags ) ) {
176 $logEntry->addTags( $this->logTags );
177 }
178 $logid = $logEntry->insert();
179 $logEntry->publish( $logid );
180 } else {
181 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $pageIdentity ) . " " .
182 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
183 }
184 }
185
186 public function close() {
187 $out = $this->getOutput();
188 if ( $this->mLogItemCount > 0 ) {
189 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
190 $out->addHTML( Xml::tags( 'li', null, $msg ) );
191 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
192 $out->addHTML( "</ul>\n" );
193
194 return Status::newFatal( 'importnopages' );
195 }
196 $out->addHTML( "</ul>\n" );
197
198 return Status::newGood( $this->mPageCount );
199 }
200}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
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="")
Class for creating new log entries and inserting them into the database.
Service locator for MediaWiki core services.
Interface for objects (potentially) representing an editable wiki page.
if( $line===false) $args
Definition mcc.php:124
const DB_PRIMARY
Definition defines.php:28