build: Upgrade mediawiki-codesniffer from 26.0.0 to 28.0.0
[lhc/web/wiklou.git] / maintenance / attachLatest.php
1 <?php
2 /**
3 * Corrects wrong values in the `page_latest` field in the database.
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 * @ingroup Maintenance
25 */
26
27 use MediaWiki\MediaWikiServices;
28
29 require_once __DIR__ . '/Maintenance.php';
30
31 /**
32 * Maintenance script to correct wrong values in the `page_latest` field
33 * in the database.
34 *
35 * @ingroup Maintenance
36 */
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' );
44 }
45
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' ) ) {
51 $conds = '';
52 }
53 $result = $dbw->select( 'page',
54 [ 'page_id', 'page_namespace', 'page_title' ],
55 $conds,
56 __METHOD__ );
57
58 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
59 $dbDomain = $lbFactory->getLocalDomainID();
60
61 $n = 0;
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',
67 'MAX(rev_timestamp)',
68 [ 'rev_page' => $pageId ],
69 __METHOD__ );
70 if ( !$latestTime ) {
71 $this->output( "$dbDomain $pageId [[$name]] can't find latest rev time?!\n" );
72 continue;
73 }
74
75 $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
76 if ( is_null( $revision ) ) {
77 $this->output(
78 "$dbDomain $pageId [[$name]] latest time $latestTime, can't find revision id\n"
79 );
80 continue;
81 }
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();
88 }
89 $n++;
90 }
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" );
94 }
95 }
96 }
97
98 $maintClass = AttachLatest::class;
99 require_once RUN_MAINTENANCE_IF_MAIN;