mOriginalPageOutCallback =
$importer->setPageOutCallback( [ $this, 'reportPage' ] );
$this->mOriginalLogCallback =
$importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
$importer->setNoticeCallback( [ $this, 'reportNotice' ] );
$this->mPageCount = 0;
$this->mIsUpload = $upload;
$this->mInterwiki = $interwiki;
$this->reason = $reason;
}
/**
* Sets change tags to apply to the import log entry and null revision.
*
* @param array $tags
* @since 1.29
*/
public function setChangeTags( array $tags ) {
$this->logTags = $tags;
}
public function open() {
$this->getOutput()->addHTML( "
\n" );
}
private function reportNotice( $msg, array $params ) {
$this->getOutput()->addHTML(
Html::element( 'li', [], $this->msg( $msg, $params )->text() )
);
}
private function reportLogItem( ...$args ) {
$this->mLogItemCount++;
if ( is_callable( $this->mOriginalLogCallback ) ) {
call_user_func_array( $this->mOriginalLogCallback, $args );
}
}
/**
* @param Title $title
* @param ForeignTitle $foreignTitle
* @param int $revisionCount
* @param int $successCount
* @param array $pageInfo
* @return void
*/
public function reportPage( $title, $foreignTitle, $revisionCount,
$successCount, $pageInfo ) {
call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() );
if ( $title === null ) {
# Invalid or non-importable title; a notice is already displayed
return;
}
$this->mPageCount++;
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
if ( $successCount > 0 ) {
// prevents jumbling of the versions count
// in RTL wikis in case the page title is LTR
$this->getOutput()->addHTML(
"- " . $linkRenderer->makeLink( $title ) . " " .
"" .
$this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
"" .
"
\n"
);
$logParams = [ '4:number:count' => $successCount ];
if ( $this->mIsUpload ) {
$detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
$successCount )->inContentLanguage()->text();
$action = 'upload';
} else {
$pageTitle = $foreignTitle->getFullText();
$fullInterwikiPrefix = $this->mInterwiki;
$this->getHookRunner()->onImportLogInterwikiLink(
$fullInterwikiPrefix, $pageTitle );
$interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
$interwiki = '[[:' . $interwikiTitleStr . ']]';
$detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
$successCount )->params( $interwiki )->inContentLanguage()->text();
$action = 'interwiki';
$logParams['5:title-link:interwiki'] = $interwikiTitleStr;
}
if ( $this->reason ) {
$detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
. $this->reason;
}
$comment = CommentStoreComment::newUnsavedComment( $detail );
$dbw = wfGetDB( DB_MASTER );
$revStore = MediaWikiServices::getInstance()->getRevisionStore();
$latest = $title->getLatestRevID();
$nullRevRecord = $revStore->newNullRevision(
$dbw,
$title,
$comment,
true,
$this->getUser()
);
$nullRevId = null;
if ( $nullRevRecord !== null ) {
$inserted = $revStore->insertRevisionOn( $nullRevRecord, $dbw );
$nullRevId = $inserted->getId();
$page = WikiPage::factory( $title );
// Update page record
$page->updateRevisionOn( $dbw, $inserted );
$fakeTags = [];
$this->getHookRunner()->onRevisionFromEditComplete(
$page, $inserted, $latest, $this->getUser(), $fakeTags
);
// Hook is hard deprecated since 1.35
if ( $this->getHookContainer()->isRegistered( 'NewRevisionFromEditComplete' ) ) {
// Only create Revision object if needed
$nullRevision = new Revision( $inserted );
$this->getHookRunner()->onNewRevisionFromEditComplete(
$page,
$nullRevision,
$latest,
$this->getUser(),
$fakeTags
);
}
}
// Create the import log entry
$logEntry = new ManualLogEntry( 'import', $action );
$logEntry->setTarget( $title );
$logEntry->setComment( $this->reason );
$logEntry->setPerformer( $this->getUser() );
$logEntry->setParameters( $logParams );
// Make sure the null revision will be tagged as well
$logEntry->setAssociatedRevId( $nullRevId );
if ( count( $this->logTags ) ) {
$logEntry->addTags( $this->logTags );
}
$logid = $logEntry->insert();
$logEntry->publish( $logid );
} else {
$this->getOutput()->addHTML( "- " . $linkRenderer->makeKnownLink( $title ) . " " .
$this->msg( 'import-nonewrevisions' )->escaped() . "
\n" );
}
}
public function close() {
$out = $this->getOutput();
if ( $this->mLogItemCount > 0 ) {
$msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
$out->addHTML( Xml::tags( 'li', null, $msg ) );
} elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
$out->addHTML( "
\n" );
return Status::newFatal( 'importnopages' );
}
$out->addHTML( "\n" );
return Status::newGood( $this->mPageCount );
}
}