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