| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Cache\CacheKeyHelper; |
| 22 |
use MediaWiki\Linker\LinkTarget; |
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use MediaWiki\Page\PageReference; |
| 25 |
use MediaWiki\Page\WikiPageFactory; |
| 26 |
use MediaWiki\Parser\MagicWordFactory; |
| 27 |
use MediaWiki\Revision\RevisionLookup; |
| 28 |
use MediaWiki\Revision\SlotRecord; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use MediaWiki\User\User; |
| 31 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class DoubleRedirectJob extends Job { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public const MAX_DR_JOBS_COUNTER = 10000; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private $redirTitle; |
| 50 |
|
| 51 |
|
| 52 |
private static $user; |
| 53 |
|
| 54 |
|
| 55 |
private $revisionLookup; |
| 56 |
|
| 57 |
|
| 58 |
private $magicWordFactory; |
| 59 |
|
| 60 |
|
| 61 |
private $wikiPageFactory; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function __construct( |
| 76 |
PageReference $page, |
| 77 |
array $params, |
| 78 |
RevisionLookup $revisionLookup, |
| 79 |
MagicWordFactory $magicWordFactory, |
| 80 |
WikiPageFactory $wikiPageFactory |
| 81 |
) { |
| 82 |
parent::__construct( 'fixDoubleRedirect', $page, $params ); |
| 83 |
$this->redirTitle = Title::newFromText( $params['redirTitle'] ); |
| 84 |
$this->revisionLookup = $revisionLookup; |
| 85 |
$this->magicWordFactory = $magicWordFactory; |
| 86 |
$this->wikiPageFactory = $wikiPageFactory; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static function fixRedirects( $reason, $redirTitle ) { |
| 97 |
|
| 98 |
$services = MediaWikiServices::getInstance(); |
| 99 |
$dbw = $services->getConnectionProvider()->getPrimaryDatabase(); |
| 100 |
$res = $dbw->newSelectQueryBuilder() |
| 101 |
->select( [ 'page_namespace', 'page_title' ] ) |
| 102 |
->from( 'redirect' ) |
| 103 |
->join( 'page', null, 'page_id = rd_from' ) |
| 104 |
->where( [ 'rd_namespace' => $redirTitle->getNamespace(), 'rd_title' => $redirTitle->getDBkey() ] ) |
| 105 |
->andWhere( [ 'rd_interwiki' => '' ] ) |
| 106 |
->caller( __METHOD__ )->fetchResultSet(); |
| 107 |
if ( !$res->numRows() ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
$jobs = []; |
| 111 |
$jobQueueGroup = $services->getJobQueueGroup(); |
| 112 |
foreach ( $res as $row ) { |
| 113 |
$title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
| 114 |
if ( !$title || !$title->canExist() ) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
$jobs[] = new self( |
| 119 |
$title, |
| 120 |
[ |
| 121 |
'reason' => $reason, |
| 122 |
'redirTitle' => $services->getTitleFormatter() |
| 123 |
->getPrefixedDBkey( $redirTitle ) |
| 124 |
], |
| 125 |
$services->getRevisionLookup(), |
| 126 |
$services->getMagicWordFactory(), |
| 127 |
$services->getWikiPageFactory() |
| 128 |
); |
| 129 |
|
| 130 |
if ( count( $jobs ) > self::MAX_DR_JOBS_COUNTER ) { |
| 131 |
$jobQueueGroup->push( $jobs ); |
| 132 |
$jobs = []; |
| 133 |
} |
| 134 |
} |
| 135 |
$jobQueueGroup->push( $jobs ); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function run() { |
| 142 |
if ( !$this->redirTitle ) { |
| 143 |
$this->setLastError( 'Invalid title' ); |
| 144 |
|
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
if ( !$this->title->canExist() ) { |
| 149 |
|
| 150 |
$this->setLastError( 'Cannot edit title' ); |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
$targetRev = $this->revisionLookup |
| 156 |
->getRevisionByTitle( $this->title, 0, IDBAccessObject::READ_LATEST ); |
| 157 |
if ( !$targetRev ) { |
| 158 |
wfDebug( __METHOD__ . ": target redirect already deleted, ignoring" ); |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
$content = $targetRev->getContent( SlotRecord::MAIN ); |
| 163 |
$currentDest = $content ? $content->getRedirectTarget() : null; |
| 164 |
if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) { |
| 165 |
wfDebug( __METHOD__ . ": Redirect has changed since the job was queued" ); |
| 166 |
|
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
$mw = $this->magicWordFactory->get( 'staticredirect' ); |
| 172 |
if ( $content->matchMagicWord( $mw ) ) { |
| 173 |
wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__" ); |
| 174 |
|
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
$newTitle = self::getFinalDestination( $this->redirTitle ); |
| 180 |
if ( !$newTitle ) { |
| 181 |
wfDebug( __METHOD__ . |
| 182 |
": skipping: single redirect, circular redirect or invalid redirect destination" ); |
| 183 |
|
| 184 |
return true; |
| 185 |
} |
| 186 |
if ( $newTitle->equals( $this->redirTitle ) ) { |
| 187 |
|
| 188 |
|
| 189 |
wfDebug( __METHOD__ . " : skipping, already good" ); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
$newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(), |
| 194 |
$currentDest->getFragment(), $newTitle->getInterwiki() ); |
| 195 |
|
| 196 |
|
| 197 |
$newContent = $content->updateRedirect( $newTitle ); |
| 198 |
|
| 199 |
if ( $newContent->equals( $content ) ) { |
| 200 |
$this->setLastError( 'Content unchanged???' ); |
| 201 |
|
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
$user = $this->getUser(); |
| 206 |
if ( !$user ) { |
| 207 |
$this->setLastError( 'Invalid user' ); |
| 208 |
|
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
global $wgUser; |
| 215 |
$oldUser = $wgUser; |
| 216 |
$wgUser = $user; |
| 217 |
$article = $this->wikiPageFactory->newFromTitle( $this->title ); |
| 218 |
|
| 219 |
|
| 220 |
$reason = wfMessage( 'double-redirect-fixed-' . $this->params['reason'], |
| 221 |
$this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText() |
| 222 |
)->inContentLanguage()->text(); |
| 223 |
|
| 224 |
$flags = EDIT_UPDATE | EDIT_SUPPRESS_RC | EDIT_INTERNAL | EDIT_MINOR; |
| 225 |
$article->doUserEditContent( $newContent, $user, $reason, $flags ); |
| 226 |
$wgUser = $oldUser; |
| 227 |
|
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static function getFinalDestination( $title ) { |
| 240 |
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 241 |
|
| 242 |
|
| 243 |
$seenTitles = []; |
| 244 |
$dest = false; |
| 245 |
|
| 246 |
while ( true ) { |
| 247 |
$titleText = CacheKeyHelper::getKeyForPage( $title ); |
| 248 |
if ( isset( $seenTitles[$titleText] ) ) { |
| 249 |
wfDebug( __METHOD__, "Circular redirect detected, aborting" ); |
| 250 |
|
| 251 |
return false; |
| 252 |
} |
| 253 |
$seenTitles[$titleText] = true; |
| 254 |
|
| 255 |
if ( $title->isExternal() ) { |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
break; |
| 261 |
} |
| 262 |
$row = $dbw->newSelectQueryBuilder() |
| 263 |
->select( [ 'rd_namespace', 'rd_title', 'rd_interwiki' ] ) |
| 264 |
->from( 'redirect' ) |
| 265 |
->join( 'page', null, 'page_id = rd_from' ) |
| 266 |
->where( [ 'page_namespace' => $title->getNamespace() ] ) |
| 267 |
->andWhere( [ 'page_title' => $title->getDBkey() ] ) |
| 268 |
->caller( __METHOD__ )->fetchRow(); |
| 269 |
if ( !$row ) { |
| 270 |
|
| 271 |
break; |
| 272 |
} else { |
| 273 |
$dest = $title = Title::makeTitle( |
| 274 |
$row->rd_namespace, |
| 275 |
$row->rd_title, |
| 276 |
'', |
| 277 |
$row->rd_interwiki |
| 278 |
); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return $dest; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
private function getUser() { |
| 293 |
if ( !self::$user ) { |
| 294 |
$username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text(); |
| 295 |
self::$user = User::newFromName( $username ); |
| 296 |
|
| 297 |
if ( self::$user && !self::$user->isRegistered() ) { |
| 298 |
self::$user->addToDatabase(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return self::$user; |
| 303 |
} |
| 304 |
} |