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
24 * @ingroup Maintenance
27 use MediaWiki\MediaWikiServices
;
29 require_once __DIR__
. '/Maintenance.php';
32 * Maintenance script to correct wrong values in the `page_latest` field
35 * @ingroup Maintenance
37 class AttachLatest
extends Maintenance
{
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' );
46 public function execute() {
47 $this->output( "Looking for pages with page_latest set to 0...\n" );
48 $dbw = $this->getDB( DB_MASTER
);
49 $conds = [ 'page_latest' => 0 ];
50 if ( $this->hasOption( 'regenerate-all' ) ) {
53 $result = $dbw->select( 'page',
54 [ 'page_id', 'page_namespace', 'page_title' ],
58 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
59 $dbDomain = $lbFactory->getLocalDomainID();
62 foreach ( $result as $row ) {
63 $pageId = intval( $row->page_id
);
64 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
65 $name = $title->getPrefixedText();
66 $latestTime = $dbw->selectField( 'revision',
68 [ 'rev_page' => $pageId ],
71 $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
75 $revision = Revision
::loadFromTimestamp( $dbw, $title, $latestTime );
76 if ( is_null( $revision ) ) {
78 "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
82 $id = $revision->getId();
83 $this->output( "$dbDomain $pageId [[$name]] latest time $latestTime, rev id $id\n" );
84 if ( $this->hasOption( 'fix' ) ) {
85 $page = WikiPage
::factory( $title );
86 $page->updateRevisionOn( $dbw, $revision );
87 $lbFactory->waitForReplication();
91 $this->output( "Done! Processed $n pages.\n" );
92 if ( !$this->hasOption( 'fix' ) ) {
93 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
98 $maintClass = AttachLatest
::class;
99 require_once RUN_MAINTENANCE_IF_MAIN
;