MediaWiki  master
ImportReporter.php
Go to the documentation of this file.
1 <?php
21 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
23 
29  use ProtectedHookAccessorTrait;
30 
31  private $reason = false;
32  private $logTags = [];
33  private $mOriginalLogCallback = null;
34  private $mOriginalPageOutCallback = 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  private function reportNotice( $msg, array $params ) {
73  $this->getOutput()->addHTML(
74  Html::element( 'li', [], $this->msg( $msg, $params )->text() )
75  );
76  }
77 
78  private 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  $services = MediaWikiServices::getInstance();
104  $linkRenderer = $services->getLinkRenderer();
105  if ( $successCount > 0 ) {
106  // <bdi> prevents jumbling of the versions count
107  // in RTL wikis in case the page title is LTR
108  $this->getOutput()->addHTML(
109  "<li>" . $linkRenderer->makeLink( $title ) . " " .
110  "<bdi>" .
111  $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
112  "</bdi>" .
113  "</li>\n"
114  );
115 
116  $logParams = [ '4:number:count' => $successCount ];
117  if ( $this->mIsUpload ) {
118  $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
119  $successCount )->inContentLanguage()->text();
120  $action = 'upload';
121  } else {
122  $pageTitle = $foreignTitle->getFullText();
123  $fullInterwikiPrefix = $this->mInterwiki;
124  $this->getHookRunner()->onImportLogInterwikiLink(
125  $fullInterwikiPrefix, $pageTitle );
126 
127  $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
128  $interwiki = '[[:' . $interwikiTitleStr . ']]';
129  $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
130  $successCount )->params( $interwiki )->inContentLanguage()->text();
131  $action = 'interwiki';
132  $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
133  }
134  if ( $this->reason ) {
135  $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
136  . $this->reason;
137  }
138 
139  $comment = CommentStoreComment::newUnsavedComment( $detail );
140  $dbw = wfGetDB( DB_MASTER );
141  $revStore = $services->getRevisionStore();
142  $latest = $title->getLatestRevID();
143  $nullRevRecord = $revStore->newNullRevision(
144  $dbw,
145  $title,
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  $page = $services->getWikiPageFactory()->newFromTitle( $title );
156 
157  // Update page record
158  $page->updateRevisionOn( $dbw, $inserted );
159 
160  $fakeTags = [];
161  $this->getHookRunner()->onRevisionFromEditComplete(
162  $page, $inserted, $latest, $this->getUser(), $fakeTags
163  );
164 
165  // Hook is hard deprecated since 1.35
166  if ( $this->getHookContainer()->isRegistered( 'NewRevisionFromEditComplete' ) ) {
167  // Only create Revision object if needed
168  $nullRevision = new Revision( $inserted );
169  $this->getHookRunner()->onNewRevisionFromEditComplete(
170  $page,
171  $nullRevision,
172  $latest,
173  $this->getUser(),
174  $fakeTags
175  );
176  }
177  }
178 
179  // Create the import log entry
180  $logEntry = new ManualLogEntry( 'import', $action );
181  $logEntry->setTarget( $title );
182  $logEntry->setComment( $this->reason );
183  $logEntry->setPerformer( $this->getUser() );
184  $logEntry->setParameters( $logParams );
185  // Make sure the null revision will be tagged as well
186  $logEntry->setAssociatedRevId( $nullRevId );
187  if ( count( $this->logTags ) ) {
188  $logEntry->addTags( $this->logTags );
189  }
190  $logid = $logEntry->insert();
191  $logEntry->publish( $logid );
192  } else {
193  $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
194  $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
195  }
196  }
197 
198  public function close() {
199  $out = $this->getOutput();
200  if ( $this->mLogItemCount > 0 ) {
201  $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
202  $out->addHTML( Xml::tags( 'li', null, $msg ) );
203  } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
204  $out->addHTML( "</ul>\n" );
205 
206  return Status::newFatal( 'importnopages' );
207  }
208  $out->addHTML( "</ul>\n" );
209 
210  return Status::newGood( $this->mPageCount );
211  }
212 }
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:67
ImportReporter\$mIsUpload
$mIsUpload
Definition: ImportReporter.php:37
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:166
ImportReporter\$mOriginalPageOutCallback
$mOriginalPageOutCallback
Definition: ImportReporter.php:34
ImportReporter\$mInterwiki
$mInterwiki
Definition: ImportReporter.php:38
ImportReporter
Reporting callback.
Definition: ImportReporter.php:28
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:134
ImportReporter\reportLogItem
reportLogItem(... $args)
Definition: ImportReporter.php:78
Revision
Definition: Revision.php:40
ImportReporter\$logTags
$logTags
Definition: ImportReporter.php:32
ImportReporter\reportPage
reportPage( $title, $foreignTitle, $revisionCount, $successCount, $pageInfo)
Definition: ImportReporter.php:93
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2466
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:124
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:31
$args
if( $line===false) $args
Definition: mcc.php:124
ImportReporter\__construct
__construct( $importer, $upload, $interwiki, $reason=false)
Definition: ImportReporter.php:46
$title
$title
Definition: testCompression.php:38
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$revStore
$revStore
Definition: testCompression.php:55
ImportReporter\$mPageCount
$mPageCount
Definition: ImportReporter.php:36
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
ImportReporter\$mOriginalLogCallback
$mOriginalLogCallback
Definition: ImportReporter.php:33
ImportReporter\reportNotice
reportNotice( $msg, array $params)
Definition: ImportReporter.php:72
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
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:198
ImportReporter\$reason
$reason
Definition: ImportReporter.php:31
ImportReporter\$mLogItemCount
$mLogItemCount
Definition: ImportReporter.php:35
ImportReporter\setChangeTags
setChangeTags(array $tags)
Sets change tags to apply to the import log entry and null revision.
Definition: ImportReporter.php:64
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:43
ImportReporter\open
open()
Definition: ImportReporter.php:68
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234