MediaWiki REL1_39
attachLatest.php
Go to the documentation of this file.
1<?php
29
30require_once __DIR__ . '/Maintenance.php';
31
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
42 $this->addOption( "regenerate-all",
43 "Regenerate the page_latest field for all records in table page" );
44 $this->addDescription( 'Fix page_latest entries in the page table' );
45 }
46
47 public function execute() {
48 $this->output( "Looking for pages with page_latest set to 0...\n" );
49 $dbw = $this->getDB( DB_PRIMARY );
50 $conds = [ 'page_latest' => 0 ];
51 if ( $this->hasOption( 'regenerate-all' ) ) {
52 $conds = '';
53 }
54 $result = $dbw->newSelectQueryBuilder()
55 ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
56 ->from( 'page' )
57 ->where( $conds )
58 ->caller( __METHOD__ )
59 ->fetchResultSet();
60
61 $services = MediaWikiServices::getInstance();
62 $lbFactory = $services->getDBLoadBalancerFactory();
63 $dbDomain = $lbFactory->getLocalDomainID();
64 $wikiPageFactory = $services->getWikiPageFactory();
65 $revisionLookup = $services->getRevisionLookup();
66
67 $n = 0;
68 foreach ( $result as $row ) {
69 $pageId = intval( $row->page_id );
70 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
71 $name = $title->getPrefixedText();
72 $latestTime = $dbw->newSelectQueryBuilder()
73 ->select( 'MAX(rev_timestamp)' )
74 ->from( 'revision' )
75 ->where( [ 'rev_page' => $pageId ] )
76 ->caller( __METHOD__ )
77 ->fetchField();
78 if ( !$latestTime ) {
79 $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
80 continue;
81 }
82
83 $revRecord = $revisionLookup->getRevisionByTimestamp( $title, $latestTime, RevisionLookup::READ_LATEST );
84 if ( $revRecord === null ) {
85 $this->output(
86 "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
87 );
88 continue;
89 }
90
91 $id = $revRecord->getId();
92 $this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" );
93 if ( $this->hasOption( 'fix' ) ) {
94 $page = $wikiPageFactory->newFromTitle( $title );
95 $page->updateRevisionOn( $dbw, $revRecord );
96 $lbFactory->waitForReplication();
97 }
98 $n++;
99 }
100 $this->output( "Done! Processed $n pages.\n" );
101 if ( !$this->hasOption( 'fix' ) ) {
102 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
103 }
104 }
105}
106
107$maintClass = AttachLatest::class;
108require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
$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.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Service locator for MediaWiki core services.
Service for looking up page revisions.
const DB_PRIMARY
Definition defines.php:28