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  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
104  if ( $successCount > 0 ) {
105  // <bdi> prevents jumbling of the versions count
106  // in RTL wikis in case the page title is LTR
107  $this->getOutput()->addHTML(
108  "<li>" . $linkRenderer->makeLink( $title ) . " " .
109  "<bdi>" .
110  $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
111  "</bdi>" .
112  "</li>\n"
113  );
114 
115  $logParams = [ '4:number:count' => $successCount ];
116  if ( $this->mIsUpload ) {
117  $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
118  $successCount )->inContentLanguage()->text();
119  $action = 'upload';
120  } else {
121  $pageTitle = $foreignTitle->getFullText();
122  $fullInterwikiPrefix = $this->mInterwiki;
123  $this->getHookRunner()->onImportLogInterwikiLink(
124  $fullInterwikiPrefix, $pageTitle );
125 
126  $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
127  $interwiki = '[[:' . $interwikiTitleStr . ']]';
128  $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
129  $successCount )->params( $interwiki )->inContentLanguage()->text();
130  $action = 'interwiki';
131  $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
132  }
133  if ( $this->reason ) {
134  $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
135  . $this->reason;
136  }
137 
138  $comment = CommentStoreComment::newUnsavedComment( $detail );
139  $dbw = wfGetDB( DB_MASTER );
140  $revStore = MediaWikiServices::getInstance()->getRevisionStore();
141  $latest = $title->getLatestRevID();
142  $nullRevRecord = $revStore->newNullRevision(
143  $dbw,
144  $title,
145  $comment,
146  true,
147  $this->getUser()
148  );
149 
150  $nullRevId = null;
151  if ( $nullRevRecord !== null ) {
152  $inserted = $revStore->insertRevisionOn( $nullRevRecord, $dbw );
153  $nullRevId = $inserted->getId();
154  $page = WikiPage::factory( $title );
155 
156  // Update page record
157  $page->updateRevisionOn( $dbw, $inserted );
158 
159  $fakeTags = [];
160  $this->getHookRunner()->onRevisionFromEditComplete(
161  $page, $inserted, $latest, $this->getUser(), $fakeTags
162  );
163 
164  // Hook is hard deprecated since 1.35
165  if ( $this->getHookContainer()->isRegistered( 'NewRevisionFromEditComplete' ) ) {
166  // Only create Revision object if needed
167  $nullRevision = new Revision( $inserted );
168  $this->getHookRunner()->onNewRevisionFromEditComplete(
169  $page,
170  $nullRevision,
171  $latest,
172  $this->getUser(),
173  $fakeTags
174  );
175  }
176  }
177 
178  // Create the import log entry
179  $logEntry = new ManualLogEntry( 'import', $action );
180  $logEntry->setTarget( $title );
181  $logEntry->setComment( $this->reason );
182  $logEntry->setPerformer( $this->getUser() );
183  $logEntry->setParameters( $logParams );
184  // Make sure the null revision will be tagged as well
185  $logEntry->setAssociatedRevId( $nullRevId );
186  if ( count( $this->logTags ) ) {
187  $logEntry->addTags( $this->logTags );
188  }
189  $logid = $logEntry->insert();
190  $logEntry->publish( $logid );
191  } else {
192  $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
193  $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
194  }
195  }
196 
197  public function close() {
198  $out = $this->getOutput();
199  if ( $this->mLogItemCount > 0 ) {
200  $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
201  $out->addHTML( Xml::tags( 'li', null, $msg ) );
202  } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
203  $out->addHTML( "</ul>\n" );
204 
205  return Status::newFatal( 'importnopages' );
206  }
207  $out->addHTML( "</ul>\n" );
208 
209  return Status::newGood( $this->mPageCount );
210  }
211 }
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:163
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
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:159
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:2459
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:130
ImportReporter\close
close()
Definition: ImportReporter.php:197
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:42
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:231