Update git submodules
[mediawiki.git] / maintenance / attachLatest.php
blobde4940a3aacb0703dd7f6c4786a948d04ad263c0
1 <?php
2 /**
3 * Corrects wrong values in the `page_latest` field in the database.
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
23 * @file
24 * @ingroup Maintenance
27 use MediaWiki\Revision\RevisionLookup;
28 use MediaWiki\Title\Title;
30 require_once __DIR__ . '/Maintenance.php';
32 /**
33 * Maintenance script to correct wrong values in the `page_latest` field
34 * in the database.
36 * @ingroup Maintenance
38 class AttachLatest extends Maintenance {
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' );
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 = '';
54 $result = $dbw->newSelectQueryBuilder()
55 ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
56 ->from( 'page' )
57 ->where( $conds )
58 ->caller( __METHOD__ )
59 ->fetchResultSet();
61 $services = $this->getServiceContainer();
62 $dbDomain = $services->getDBLoadBalancerFactory()->getLocalDomainID();
63 $wikiPageFactory = $services->getWikiPageFactory();
64 $revisionLookup = $services->getRevisionLookup();
66 $n = 0;
67 foreach ( $result as $row ) {
68 $pageId = intval( $row->page_id );
69 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
70 $name = $title->getPrefixedText();
71 $latestTime = $dbw->newSelectQueryBuilder()
72 ->select( 'MAX(rev_timestamp)' )
73 ->from( 'revision' )
74 ->where( [ 'rev_page' => $pageId ] )
75 ->caller( __METHOD__ )
76 ->fetchField();
77 if ( !$latestTime ) {
78 $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
79 continue;
82 $revRecord = $revisionLookup->getRevisionByTimestamp( $title, $latestTime, RevisionLookup::READ_LATEST );
83 if ( $revRecord === null ) {
84 $this->output(
85 "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
87 continue;
90 $id = $revRecord->getId();
91 $this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" );
92 if ( $this->hasOption( 'fix' ) ) {
93 $page = $wikiPageFactory->newFromTitle( $title );
94 $page->updateRevisionOn( $dbw, $revRecord );
95 $this->waitForReplication();
97 $n++;
99 $this->output( "Done! Processed $n pages.\n" );
100 if ( !$this->hasOption( 'fix' ) ) {
101 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
106 $maintClass = AttachLatest::class;
107 require_once RUN_MAINTENANCE_IF_MAIN;