MediaWiki REL1_37
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 = [];
36 private $mLogItemCount = 0;
37 private $mPageCount;
38 private $mIsUpload;
39 private $mInterwiki;
40
47 public function __construct( $importer, $upload, $interwiki, $reason = false ) {
48 $this->mOriginalPageOutCallback =
49 $importer->setPageOutCallback( [ $this, 'reportPage' ] );
50 $this->mOriginalLogCallback =
51 $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
52 $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
53 $this->mPageCount = 0;
54 $this->mIsUpload = $upload;
55 $this->mInterwiki = $interwiki;
56 $this->reason = $reason;
57 }
58
65 public function setChangeTags( array $tags ) {
66 $this->logTags = $tags;
67 }
68
69 public function open() {
70 $this->getOutput()->addHTML( "<ul>\n" );
71 }
72
73 private function reportNotice( $msg, array $params ) {
74 $this->getOutput()->addHTML(
75 Html::element( 'li', [], $this->msg( $msg, $params )->text() )
76 );
77 }
78
79 private function reportLogItem( ...$args ) {
80 $this->mLogItemCount++;
81 if ( is_callable( $this->mOriginalLogCallback ) ) {
82 call_user_func_array( $this->mOriginalLogCallback, $args );
83 }
84 }
85
94 public function reportPage( ?PageIdentity $pageIdentity, $foreignTitle, $revisionCount,
95 $successCount, $pageInfo ) {
96 call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
97
98 if ( $pageIdentity === null ) {
99 # Invalid or non-importable title; a notice is already displayed
100 return;
101 }
102
103 $this->mPageCount++;
104 $services = MediaWikiServices::getInstance();
105 $linkRenderer = $services->getLinkRenderer();
106 if ( $successCount > 0 ) {
107 // <bdi> prevents jumbling of the versions count
108 // in RTL wikis in case the page title is LTR
109 $this->getOutput()->addHTML(
110 "<li>" . $linkRenderer->makeLink( $pageIdentity ) . " " .
111 "<bdi>" .
112 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
113 "</bdi>" .
114 "</li>\n"
115 );
116
117 $logParams = [ '4:number:count' => $successCount ];
118 if ( $this->mIsUpload ) {
119 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
120 $successCount )->inContentLanguage()->text();
121 $action = 'upload';
122 } else {
123 $pageTitle = $foreignTitle->getFullText();
124 $fullInterwikiPrefix = $this->mInterwiki;
125 $this->getHookRunner()->onImportLogInterwikiLink(
126 $fullInterwikiPrefix, $pageTitle );
127
128 $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
129 $interwiki = '[[:' . $interwikiTitleStr . ']]';
130 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
131 $successCount )->params( $interwiki )->inContentLanguage()->text();
132 $action = 'interwiki';
133 $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
134 }
135 if ( $this->reason ) {
136 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
138 }
139
140 $comment = CommentStoreComment::newUnsavedComment( $detail );
141 $dbw = wfGetDB( DB_PRIMARY );
142 $revStore = $services->getRevisionStore();
143 $nullRevRecord = $revStore->newNullRevision(
144 $dbw,
145 $pageIdentity,
146 $comment,
147 true,
148 $this->getUser()
149 );
150
151 $nullRevId = null;
152 if ( $nullRevRecord !== null ) {
153 $inserted = $revStore->insertRevisionOn( $nullRevRecord, $dbw );
154 $nullRevId = $inserted->getId();
155 $parentRevId = $inserted->getParentId();
156 $page = $services->getWikiPageFactory()->newFromTitle( $pageIdentity );
157
158 // Update page record
159 $page->updateRevisionOn( $dbw, $inserted );
160
161 $fakeTags = [];
162 $this->getHookRunner()->onRevisionFromEditComplete(
163 $page, $inserted, $parentRevId, $this->getUser(), $fakeTags
164 );
165 }
166
167 // Create the import log entry
168 $logEntry = new ManualLogEntry( 'import', $action );
169 $logEntry->setTarget( TitleValue::castPageToLinkTarget( $pageIdentity ) );
170 $logEntry->setComment( $this->reason );
171 $logEntry->setPerformer( $this->getUser() );
172 $logEntry->setParameters( $logParams );
173 // Make sure the null revision will be tagged as well
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)
__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.
Interface for objects (potentially) representing an editable wiki page.
if( $line===false) $args
Definition mcc.php:124
const DB_PRIMARY
Definition defines.php:27