| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 22 |
use MediaWiki\Context\ContextSource; |
| 23 |
use MediaWiki\Context\IContextSource; |
| 24 |
use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
| 25 |
use MediaWiki\Html\Html; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Page\PageIdentity; |
| 28 |
use MediaWiki\Status\Status; |
| 29 |
use MediaWiki\Title\ForeignTitle; |
| 30 |
use MediaWiki\Xml\Xml; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class ImportReporter extends ContextSource { |
| 37 |
use ProtectedHookAccessorTrait; |
| 38 |
|
| 39 |
|
| 40 |
private $reason; |
| 41 |
|
| 42 |
private $logTags = []; |
| 43 |
|
| 44 |
private $mOriginalLogCallback; |
| 45 |
|
| 46 |
private $mOriginalPageOutCallback; |
| 47 |
|
| 48 |
private $mLogItemCount = 0; |
| 49 |
|
| 50 |
private $mPageCount = 0; |
| 51 |
|
| 52 |
private $mIsUpload; |
| 53 |
|
| 54 |
private $mInterwiki; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( $importer, $upload, $interwiki, $reason = "", ?IContextSource $context = null ) { |
| 64 |
if ( $context ) { |
| 65 |
$this->setContext( $context ); |
| 66 |
} else { |
| 67 |
wfDeprecated( __METHOD__ . ' without $context', '1.42' ); |
| 68 |
} |
| 69 |
$this->mOriginalPageOutCallback = |
| 70 |
$importer->setPageOutCallback( [ $this, 'reportPage' ] ); |
| 71 |
$this->mOriginalLogCallback = |
| 72 |
$importer->setLogItemCallback( [ $this, 'reportLogItem' ] ); |
| 73 |
$importer->setNoticeCallback( [ $this, 'reportNotice' ] ); |
| 74 |
$this->mIsUpload = $upload; |
| 75 |
$this->mInterwiki = $interwiki; |
| 76 |
$this->reason = is_string( $reason ) ? $reason : ""; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function setChangeTags( array $tags ) { |
| 86 |
$this->logTags = $tags; |
| 87 |
} |
| 88 |
|
| 89 |
public function open() { |
| 90 |
$this->getOutput()->addHTML( "<ul>\n" ); |
| 91 |
} |
| 92 |
|
| 93 |
public function reportNotice( $msg, array $params ) { |
| 94 |
$this->getOutput()->addHTML( |
| 95 |
Html::element( 'li', [], $this->msg( $msg, $params )->text() ) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
public function reportLogItem( ...$args ) { |
| 100 |
$this->mLogItemCount++; |
| 101 |
if ( is_callable( $this->mOriginalLogCallback ) ) { |
| 102 |
call_user_func_array( $this->mOriginalLogCallback, $args ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function reportPage( ?PageIdentity $pageIdentity, $foreignTitle, $revisionCount, |
| 115 |
$successCount, $pageInfo ) { |
| 116 |
call_user_func_array( $this->mOriginalPageOutCallback, func_get_args() ); |
| 117 |
|
| 118 |
if ( $pageIdentity === null ) { |
| 119 |
|
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$this->mPageCount++; |
| 124 |
$services = MediaWikiServices::getInstance(); |
| 125 |
$linkRenderer = $services->getLinkRenderer(); |
| 126 |
if ( $successCount > 0 ) { |
| 127 |
|
| 128 |
|
| 129 |
$this->getOutput()->addHTML( |
| 130 |
"<li>" . $linkRenderer->makeLink( $pageIdentity ) . " " . |
| 131 |
"<bdi>" . |
| 132 |
$this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() . |
| 133 |
"</bdi>" . |
| 134 |
"</li>\n" |
| 135 |
); |
| 136 |
|
| 137 |
$logParams = [ '4:number:count' => $successCount ]; |
| 138 |
if ( $this->mIsUpload ) { |
| 139 |
$detail = $this->msg( 'import-logentry-upload-detail' )->numParams( |
| 140 |
$successCount )->inContentLanguage()->text(); |
| 141 |
$action = 'upload'; |
| 142 |
} else { |
| 143 |
$pageTitle = $foreignTitle->getFullText(); |
| 144 |
$fullInterwikiPrefix = $this->mInterwiki; |
| 145 |
$this->getHookRunner()->onImportLogInterwikiLink( |
| 146 |
$fullInterwikiPrefix, $pageTitle ); |
| 147 |
|
| 148 |
$interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle; |
| 149 |
$interwiki = '[[:' . $interwikiTitleStr . ']]'; |
| 150 |
$detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams( |
| 151 |
$successCount )->params( $interwiki )->inContentLanguage()->text(); |
| 152 |
$action = 'interwiki'; |
| 153 |
$logParams['5:title-link:interwiki'] = $interwikiTitleStr; |
| 154 |
} |
| 155 |
if ( $this->reason ) { |
| 156 |
$detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text() |
| 157 |
. $this->reason; |
| 158 |
} |
| 159 |
|
| 160 |
$comment = CommentStoreComment::newUnsavedComment( $detail ); |
| 161 |
$dbw = $services->getConnectionProvider()->getPrimaryDatabase(); |
| 162 |
$revStore = $services->getRevisionStore(); |
| 163 |
$nullRevRecord = $revStore->newNullRevision( |
| 164 |
$dbw, |
| 165 |
$pageIdentity, |
| 166 |
$comment, |
| 167 |
true, |
| 168 |
$this->getUser() |
| 169 |
); |
| 170 |
|
| 171 |
$nullRevId = null; |
| 172 |
if ( $nullRevRecord !== null ) { |
| 173 |
$inserted = $revStore->insertRevisionOn( $nullRevRecord, $dbw ); |
| 174 |
$nullRevId = $inserted->getId(); |
| 175 |
$parentRevId = $inserted->getParentId(); |
| 176 |
$page = $services->getWikiPageFactory()->newFromTitle( $pageIdentity ); |
| 177 |
|
| 178 |
|
| 179 |
$page->updateRevisionOn( $dbw, $inserted ); |
| 180 |
|
| 181 |
$fakeTags = []; |
| 182 |
$this->getHookRunner()->onRevisionFromEditComplete( |
| 183 |
$page, $inserted, $parentRevId, $this->getUser(), $fakeTags |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
$logEntry = new ManualLogEntry( 'import', $action ); |
| 189 |
$logEntry->setTarget( $pageIdentity ); |
| 190 |
$logEntry->setComment( $this->reason ); |
| 191 |
$logEntry->setPerformer( $this->getUser() ); |
| 192 |
$logEntry->setParameters( $logParams ); |
| 193 |
|
| 194 |
|
| 195 |
$logEntry->setAssociatedRevId( $nullRevId ); |
| 196 |
if ( count( $this->logTags ) ) { |
| 197 |
$logEntry->addTags( $this->logTags ); |
| 198 |
} |
| 199 |
$logid = $logEntry->insert(); |
| 200 |
$logEntry->publish( $logid ); |
| 201 |
} else { |
| 202 |
$this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $pageIdentity ) . " " . |
| 203 |
$this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
public function close() { |
| 208 |
$out = $this->getOutput(); |
| 209 |
if ( $this->mLogItemCount > 0 ) { |
| 210 |
$msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse(); |
| 211 |
$out->addHTML( Xml::tags( 'li', null, $msg ) ); |
| 212 |
} elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) { |
| 213 |
$out->addHTML( "</ul>\n" ); |
| 214 |
|
| 215 |
return Status::newFatal( 'importnopages' ); |
| 216 |
} |
| 217 |
$out->addHTML( "</ul>\n" ); |
| 218 |
|
| 219 |
return Status::newGood( $this->mPageCount ); |
| 220 |
} |
| 221 |
} |