MediaWiki  master
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 }
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setChangeTags(array $tags)
Sets change tags to apply to the import log entry and null revision.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
const DB_MASTER
Definition: defines.php:26
__construct( $importer, $upload, $interwiki, $reason=false)
if( $line===false) $args
Definition: cdb.php:64
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
static tags( $element, $attribs, $contents)
Same as Xml::element(), but does not escape contents.
Definition: Xml.php:130
reportNotice( $msg, array $params)
reportLogItem(... $args)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Reporting callback.
reportPage( $title, $foreignTitle, $revisionCount, $successCount, $pageInfo)
static newNullRevision( $dbw, $pageId, $summary, $minor, $user=null)
Create a new null-revision for insertion into a page&#39;s history.
Definition: Revision.php:994
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200