| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
use MediaWiki\Maintenance\Maintenance; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 30 |
|
| 31 |
|
| 32 |
require_once __DIR__ . '/Maintenance.php'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class AttachLatest extends Maintenance { |
| 42 |
public function __construct() { |
| 43 |
parent::__construct(); |
| 44 |
$this->addOption( "fix", "Actually fix the entries, will dry run otherwise" ); |
| 45 |
$this->addOption( "regenerate-all", |
| 46 |
"Regenerate the page_latest field for all records in table page" ); |
| 47 |
$this->addDescription( 'Fix page_latest entries in the page table' ); |
| 48 |
} |
| 49 |
|
| 50 |
public function execute() { |
| 51 |
$this->output( "Looking for pages with page_latest set to 0...\n" ); |
| 52 |
$dbw = $this->getPrimaryDB(); |
| 53 |
$conds = [ 'page_latest' => 0 ]; |
| 54 |
if ( $this->hasOption( 'regenerate-all' ) ) { |
| 55 |
$conds = []; |
| 56 |
} |
| 57 |
$result = $dbw->newSelectQueryBuilder() |
| 58 |
->select( [ 'page_id', 'page_namespace', 'page_title' ] ) |
| 59 |
->from( 'page' ) |
| 60 |
->where( $conds ) |
| 61 |
->caller( __METHOD__ ) |
| 62 |
->fetchResultSet(); |
| 63 |
|
| 64 |
$services = $this->getServiceContainer(); |
| 65 |
$dbDomain = $services->getDBLoadBalancerFactory()->getLocalDomainID(); |
| 66 |
$wikiPageFactory = $services->getWikiPageFactory(); |
| 67 |
$revisionLookup = $services->getRevisionLookup(); |
| 68 |
|
| 69 |
$n = 0; |
| 70 |
foreach ( $result as $row ) { |
| 71 |
$pageId = intval( $row->page_id ); |
| 72 |
$title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 73 |
$name = $title->getPrefixedText(); |
| 74 |
$latestTime = $dbw->newSelectQueryBuilder() |
| 75 |
->select( 'MAX(rev_timestamp)' ) |
| 76 |
->from( 'revision' ) |
| 77 |
->where( [ 'rev_page' => $pageId ] ) |
| 78 |
->caller( __METHOD__ ) |
| 79 |
->fetchField(); |
| 80 |
if ( !$latestTime ) { |
| 81 |
$this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" ); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
$revRecord = $revisionLookup->getRevisionByTimestamp( $title, $latestTime, IDBAccessObject::READ_LATEST ); |
| 86 |
if ( $revRecord === null ) { |
| 87 |
$this->output( |
| 88 |
"$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n" |
| 89 |
); |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
$id = $revRecord->getId(); |
| 94 |
$this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" ); |
| 95 |
if ( $this->hasOption( 'fix' ) ) { |
| 96 |
$page = $wikiPageFactory->newFromTitle( $title ); |
| 97 |
$page->updateRevisionOn( $dbw, $revRecord ); |
| 98 |
$this->waitForReplication(); |
| 99 |
} |
| 100 |
$n++; |
| 101 |
} |
| 102 |
$this->output( "Done! Processed $n pages.\n" ); |
| 103 |
if ( !$this->hasOption( 'fix' ) ) { |
| 104 |
$this->output( "This was a dry run; rerun with --fix to update page_latest.\n" ); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
$maintClass = AttachLatest::class; |
| 111 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 112 |
|