MediaWiki REL1_35
ImportReporter.php
Go to the documentation of this file.
1<?php
21use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
23
29 use ProtectedHookAccessorTrait;
30
31 private $reason = false;
32 private $logTags = [];
33 private $mOriginalLogCallback = null;
35 private $mLogItemCount = 0;
36 private $mPageCount;
37 private $mIsUpload;
38 private $mInterwiki;
39
46 public function __construct( $importer, $upload, $interwiki, $reason = false ) {
47 $this->mOriginalPageOutCallback =
48 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
49 $this->mOriginalLogCallback =
50 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
51 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
52 $this->mPageCount = 0;
53 $this->mIsUpload = $upload;
54 $this->mInterwiki = $interwiki;
55 $this->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( $title, $foreignTitle, $revisionCount,
94 $successCount, $pageInfo ) {
95 call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
96
97 if ( $title === null ) {
98 # Invalid or non-importable title; a notice is already displayed
99 return;
100 }
101
102 $this->mPageCount++;
103 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
104 if ( $successCount > 0 ) {
105 // <bdi> prevents jumbling of the versions count
106 // in RTL wikis in case the page title is LTR
107 $this->getOutput()->addHTML(
108 "<li>" . $linkRenderer->makeLink( $title ) . " " .
109 "<bdi>" .
110 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
111 "</bdi>" .
112 "</li>\n"
113 );
114
115 $logParams = [ '4:number:count' => $successCount ];
116 if ( $this->mIsUpload ) {
117 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
118 $successCount )->inContentLanguage()->text();
119 $action = 'upload';
120 } else {
121 $pageTitle = $foreignTitle->getFullText();
122 $fullInterwikiPrefix = $this->mInterwiki;
123 $this->getHookRunner()->onImportLogInterwikiLink(
124 $fullInterwikiPrefix, $pageTitle );
125
126 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
127 $interwiki = '[[:' . $interwikiTitleStr . ']]';
128 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
129 $successCount )->params( $interwiki )->inContentLanguage()->text();
130 $action = 'interwiki';
131 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
132 }
133 if ( $this->reason ) {
134 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
136 }
137
138 $comment = CommentStoreComment::newUnsavedComment( $detail );
139 $dbw = wfGetDB( DB_MASTER );
140 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
141 $latest = $title->getLatestRevID();
142 $nullRevRecord = $revStore->newNullRevision(
143 $dbw,
144 $title,
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 $page = WikiPage::factory( $title );
155
156 // Update page record
157 $page->updateRevisionOn( $dbw, $inserted );
158
159 $fakeTags = [];
160 $this->getHookRunner()->onRevisionFromEditComplete(
161 $page, $inserted, $latest, $this->getUser(), $fakeTags
162 );
163
164 // Hook is hard deprecated since 1.35
165 if ( $this->getHookContainer()->isRegistered( 'NewRevisionFromEditComplete' ) ) {
166 // Only create Revision object if needed
167 $nullRevision = new Revision( $inserted );
168 $this->getHookRunner()->onNewRevisionFromEditComplete(
169 $page,
170 $nullRevision,
171 $latest,
172 $this->getUser(),
173 $fakeTags
174 );
175 }
176 }
177
178 // Create the import log entry
179 $logEntry = new ManualLogEntry( 'import', $action );
180 $logEntry->setTarget( $title );
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 $logEntry->setAssociatedRevId( $nullRevId );
186 if ( count( $this->logTags ) ) {
187 $logEntry->addTags( $this->logTags );
188 }
189 $logid = $logEntry->insert();
190 $logEntry->publish( $logid );
191 } else {
192 $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
193 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
194 }
195 }
196
197 public function close() {
198 $out = $this->getOutput();
199 if ( $this->mLogItemCount > 0 ) {
200 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
201 $out->addHTML( Xml::tags( 'li', null, $msg ) );
202 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
203 $out->addHTML( "</ul>\n" );
204
205 return Status::newFatal( 'importnopages' );
206 }
207 $out->addHTML( "</ul>\n" );
208
209 return Status::newGood( $this->mPageCount );
210 }
211}
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 ...
getUser()
Stable to override.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Reporting callback.
reportNotice( $msg, array $params)
reportLogItem(... $args)
reportPage( $title, $foreignTitle, $revisionCount, $successCount, $pageInfo)
__construct( $importer, $upload, $interwiki, $reason=false)
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
if( $line===false) $args
Definition mcc.php:124
const DB_MASTER
Definition defines.php:29