MediaWiki  1.34.0
ImportReporter.php
Go to the documentation of this file.
1 <?php
22 
28  private $reason = false;
29  private $logTags = [];
30  private $mOriginalLogCallback = null;
31  private $mOriginalPageOutCallback = null;
32  private $mLogItemCount = 0;
33  private $mPageCount;
34  private $mIsUpload;
35  private $mInterwiki;
36 
43  function __construct( $importer, $upload, $interwiki, $reason = false ) {
44  $this->mOriginalPageOutCallback =
45  $importer->setPageOutCallback( [ $this, 'reportPage' ] );
46  $this->mOriginalLogCallback =
47  $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
48  $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
49  $this->mPageCount = 0;
50  $this->mIsUpload = $upload;
51  $this->mInterwiki = $interwiki;
52  $this->reason = $reason;
53  }
54 
61  public function setChangeTags( array $tags ) {
62  $this->logTags = $tags;
63  }
64 
65  function open() {
66  $this->getOutput()->addHTML( "<ul>\n" );
67  }
68 
69  function reportNotice( $msg, array $params ) {
70  $this->getOutput()->addHTML(
71  Html::element( 'li', [], $this->msg( $msg, $params )->text() )
72  );
73  }
74 
75  function reportLogItem( ...$args ) {
76  $this->mLogItemCount++;
77  if ( is_callable( $this->mOriginalLogCallback ) ) {
78  call_user_func_array( $this->mOriginalLogCallback, $args );
79  }
80  }
81 
90  public function reportPage( $title, $foreignTitle, $revisionCount,
91  $successCount, $pageInfo ) {
92  call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
93 
94  if ( $title === null ) {
95  # Invalid or non-importable title; a notice is already displayed
96  return;
97  }
98 
99  $this->mPageCount++;
100  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
101  if ( $successCount > 0 ) {
102  // <bdi> prevents jumbling of the versions count
103  // in RTL wikis in case the page title is LTR
104  $this->getOutput()->addHTML(
105  "<li>" . $linkRenderer->makeLink( $title ) . " " .
106  "<bdi>" .
107  $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
108  "</bdi>" .
109  "</li>\n"
110  );
111 
112  $logParams = [ '4:number:count' => $successCount ];
113  if ( $this->mIsUpload ) {
114  $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
115  $successCount )->inContentLanguage()->text();
116  $action = 'upload';
117  } else {
118  $pageTitle = $foreignTitle->getFullText();
119  $fullInterwikiPrefix = $this->mInterwiki;
120  Hooks::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
121 
122  $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
123  $interwiki = '[[:' . $interwikiTitleStr . ']]';
124  $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
125  $successCount )->params( $interwiki )->inContentLanguage()->text();
126  $action = 'interwiki';
127  $logParams['5:title-link:interwiki'] = $interwikiTitleStr;
128  }
129  if ( $this->reason ) {
130  $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
131  . $this->reason;
132  }
133 
134  $comment = $detail; // quick
135  $dbw = wfGetDB( DB_MASTER );
136  $latest = $title->getLatestRevID();
137  $nullRevision = Revision::newNullRevision(
138  $dbw,
139  $title->getArticleID(),
140  $comment,
141  true,
142  $this->getUser()
143  );
144 
145  $nullRevId = null;
146  if ( !is_null( $nullRevision ) ) {
147  $nullRevId = $nullRevision->insertOn( $dbw );
148  $page = WikiPage::factory( $title );
149  # Update page record
150  $page->updateRevisionOn( $dbw, $nullRevision );
151  Hooks::run(
152  'NewRevisionFromEditComplete',
153  [ $page, $nullRevision, $latest, $this->getUser() ]
154  );
155  }
156 
157  // Create the import log entry
158  $logEntry = new ManualLogEntry( 'import', $action );
159  $logEntry->setTarget( $title );
160  $logEntry->setComment( $this->reason );
161  $logEntry->setPerformer( $this->getUser() );
162  $logEntry->setParameters( $logParams );
163  // Make sure the null revision will be tagged as well
164  $logEntry->setAssociatedRevId( $nullRevId );
165  if ( count( $this->logTags ) ) {
166  $logEntry->addTags( $this->logTags );
167  }
168  $logid = $logEntry->insert();
169  $logEntry->publish( $logid );
170  } else {
171  $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
172  $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
173  }
174  }
175 
176  function close() {
177  $out = $this->getOutput();
178  if ( $this->mLogItemCount > 0 ) {
179  $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
180  $out->addHTML( Xml::tags( 'li', null, $msg ) );
181  } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
182  $out->addHTML( "</ul>\n" );
183 
184  return Status::newFatal( 'importnopages' );
185  }
186  $out->addHTML( "</ul>\n" );
187 
188  return Status::newGood( $this->mPageCount );
189  }
190 }
ImportReporter\$mIsUpload
$mIsUpload
Definition: ImportReporter.php:34
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
ImportReporter\$mOriginalPageOutCallback
$mOriginalPageOutCallback
Definition: ImportReporter.php:31
ImportReporter\$mInterwiki
$mInterwiki
Definition: ImportReporter.php:35
ImportReporter
Reporting callback.
Definition: ImportReporter.php:27
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ImportReporter\reportLogItem
reportLogItem(... $args)
Definition: ImportReporter.php:75
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
ImportReporter\$logTags
$logTags
Definition: ImportReporter.php:29
ImportReporter\reportPage
reportPage( $title, $foreignTitle, $revisionCount, $successCount, $pageInfo)
Definition: ImportReporter.php:90
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:29
ImportReporter\__construct
__construct( $importer, $upload, $interwiki, $reason=false)
Definition: ImportReporter.php:43
$title
$title
Definition: testCompression.php:34
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ImportReporter\$mPageCount
$mPageCount
Definition: ImportReporter.php:33
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
ImportReporter\$mOriginalLogCallback
$mOriginalLogCallback
Definition: ImportReporter.php:30
ImportReporter\reportNotice
reportNotice( $msg, array $params)
Definition: ImportReporter.php:69
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
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:176
ImportReporter\$reason
$reason
Definition: ImportReporter.php:28
ImportReporter\$mLogItemCount
$mLogItemCount
Definition: ImportReporter.php:32
$args
if( $line===false) $args
Definition: cdb.php:64
ImportReporter\setChangeTags
setChangeTags(array $tags)
Sets change tags to apply to the import log entry and null revision.
Definition: ImportReporter.php:61
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:37
Revision\newNullRevision
static newNullRevision( $dbw, $pageId, $summary, $minor, $user=null)
Create a new null-revision for insertion into a page's history.
Definition: Revision.php:1000
ImportReporter\open
open()
Definition: ImportReporter.php:65
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200