MediaWiki  master
ImportReporter.php
Go to the documentation of this file.
1 <?php
21 use 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()
137  . $this->reason;
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 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:67
ImportReporter\$mIsUpload
$mIsUpload
Definition: ImportReporter.php:38
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
ImportReporter\$mOriginalPageOutCallback
$mOriginalPageOutCallback
Definition: ImportReporter.php:35
ImportReporter\$mInterwiki
$mInterwiki
Definition: ImportReporter.php:39
ImportReporter
Reporting callback.
Definition: ImportReporter.php:29
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ImportReporter\reportPage
reportPage(?PageIdentity $pageIdentity, $foreignTitle, $revisionCount, $successCount, $pageInfo)
Definition: ImportReporter.php:94
ImportReporter\reportLogItem
reportLogItem(... $args)
Definition: ImportReporter.php:79
TitleValue\castPageToLinkTarget
static castPageToLinkTarget(?PageReference $page)
Casts a PageReference to a LinkTarget.
Definition: TitleValue.php:124
ImportReporter\$logTags
$logTags
Definition: ImportReporter.php:33
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2200
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:126
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:33
$args
if( $line===false) $args
Definition: mcc.php:124
ImportReporter\__construct
__construct( $importer, $upload, $interwiki, $reason=false)
Definition: ImportReporter.php:47
$revStore
$revStore
Definition: testCompression.php:55
ImportReporter\$mPageCount
$mPageCount
Definition: ImportReporter.php:37
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:197
ImportReporter\$mOriginalLogCallback
$mOriginalLogCallback
Definition: ImportReporter.php:34
ImportReporter\reportNotice
reportNotice( $msg, array $params)
Definition: ImportReporter.php:73
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Xml\tags
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:132
ImportReporter\close
close()
Definition: ImportReporter.php:186
ImportReporter\$reason
$reason
Definition: ImportReporter.php:32
ImportReporter\$mLogItemCount
$mLogItemCount
Definition: ImportReporter.php:36
ImportReporter\setChangeTags
setChangeTags(array $tags)
Sets change tags to apply to the import log entry and null revision.
Definition: ImportReporter.php:65
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:44
ImportReporter\open
open()
Definition: ImportReporter.php:69
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232