Merge "Add link to PD help translatable pages from sidebar"
[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 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script to correct wrong values in the `page_latest` field
31 * in the database.
32 *
33 * @ingroup Maintenance
34 */
35 class AttachLatest extends Maintenance {
36
37 public function __construct() {
38 parent::__construct();
39 $this->addOption( "fix", "Actually fix the entries, will dry run otherwise" );
40 $this->addOption( "regenerate-all",
41 "Regenerate the page_latest field for all records in table page" );
42 $this->mDescription = "Fix page_latest entries in the page table";
43 }
44
45 public function execute() {
46 $this->output( "Looking for pages with page_latest set to 0...\n" );
47 $dbw = wfGetDB( DB_MASTER );
48 $conds = array( 'page_latest' => 0 );
49 if ( $this->hasOption( 'regenerate-all' ) ) {
50 $conds = '';
51 }
52 $result = $dbw->select( 'page',
53 array( 'page_id', 'page_namespace', 'page_title' ),
54 $conds,
55 __METHOD__ );
56
57 $n = 0;
58 foreach ( $result as $row ) {
59 $pageId = intval( $row->page_id );
60 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
61 $name = $title->getPrefixedText();
62 $latestTime = $dbw->selectField( 'revision',
63 'MAX(rev_timestamp)',
64 array( 'rev_page' => $pageId ),
65 __METHOD__ );
66 if ( !$latestTime ) {
67 $this->output( wfWikiID() . " $pageId [[$name]] can't find latest rev time?!\n" );
68 continue;
69 }
70
71 $revision = Revision::loadFromTimestamp( $dbw, $title, $latestTime );
72 if ( is_null( $revision ) ) {
73 $this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, can't find revision id\n" );
74 continue;
75 }
76 $id = $revision->getId();
77 $this->output( wfWikiID() . " $pageId [[$name]] latest time $latestTime, rev id $id\n" );
78 if ( $this->hasOption( 'fix' ) ) {
79 $page = WikiPage::factory( $title );
80 $page->updateRevisionOn( $dbw, $revision );
81 }
82 $n++;
83 }
84 $this->output( "Done! Processed $n pages.\n" );
85 if ( !$this->hasOption( 'fix' ) ) {
86 $this->output( "This was a dry run; rerun with --fix to update page_latest.\n" );
87 }
88 }
89 }
90
91 $maintClass = "AttachLatest";
92 require_once RUN_MAINTENANCE_IF_MAIN;