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