MediaWiki master
attachLatest.php
Go to the documentation of this file.
1<?php
30
31// @codeCoverageIgnoreStart
32require_once __DIR__ . '/Maintenance.php';
33// @codeCoverageIgnoreEnd
34
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// @codeCoverageIgnoreStart
110$maintClass = AttachLatest::class;
111require_once RUN_MAINTENANCE_IF_MAIN;
112// @codeCoverageIgnoreEnd
$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.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers 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.
Represents a title within MediaWiki.
Definition Title.php:78
Interface for database access objects.