MediaWiki master
attachLatest.php
Go to the documentation of this file.
1<?php
28
29require_once __DIR__ . '/Maintenance.php';
30
38 public function __construct() {
39 parent::__construct();
40 $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
41 $this->addOption( "regenerate-all",
42 "Regenerate the page_latest field for all records in table page" );
43 $this->addDescription( 'Fix page_latest entries in the page table' );
44 }
45
46 public function execute() {
47 $this->output( "Looking for pages with page_latest set to 0...\n" );
48 $dbw = $this->getPrimaryDB();
49 $conds = [ 'page_latest' => 0 ];
50 if ( $this->hasOption( 'regenerate-all' ) ) {
51 $conds = '';
52 }
53 $result = $dbw->newSelectQueryBuilder()
54 ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
55 ->from( 'page' )
56 ->where( $conds )
57 ->caller( __METHOD__ )
58 ->fetchResultSet();
59
60 $services = $this->getServiceContainer();
61 $dbDomain = $services->getDBLoadBalancerFactory()->getLocalDomainID();
62 $wikiPageFactory = $services->getWikiPageFactory();
63 $revisionLookup = $services->getRevisionLookup();
64
65 $n = 0;
66 foreach ( $result as $row ) {
67 $pageId = intval( $row->page_id );
68 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
69 $name = $title->getPrefixedText();
70 $latestTime = $dbw->newSelectQueryBuilder()
71 ->select( 'MAX(rev_timestamp)' )
72 ->from( 'revision' )
73 ->where( [ 'rev_page' => $pageId ] )
74 ->caller( __METHOD__ )
75 ->fetchField();
76 if ( !$latestTime ) {
77 $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
78 continue;
79 }
80
81 $revRecord = $revisionLookup->getRevisionByTimestamp( $title, $latestTime, IDBAccessObject::READ_LATEST );
82 if ( $revRecord === null ) {
83 $this->output(
84 "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
85 );
86 continue;
87 }
88
89 $id = $revRecord->getId();
90 $this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" );
91 if ( $this->hasOption( 'fix' ) ) {
92 $page = $wikiPageFactory->newFromTitle( $title );
93 $page->updateRevisionOn( $dbw, $revRecord );
94 $this->waitForReplication();
95 }
96 $n++;
97 }
98 $this->output( "Done! Processed $n pages.\n" );
99 if ( !$this->hasOption( 'fix' ) ) {
100 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
101 }
102 }
103}
104
105$maintClass = AttachLatest::class;
106require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
Maintenance script to correct wrong values in the page_latest field in the database.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Represents a title within MediaWiki.
Definition Title.php:78