Merge "Special:Allpages replace table with unordered list"
[lhc/web/wiklou.git] / maintenance / populateBacklinkNamespace.php
1 <?php
2 /**
3 * Optional upgrade script to populate *_from_namespace fields
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to populate *_from_namespace fields
28 *
29 * @ingroup Maintenance
30 */
31 class PopulateBacklinkNamespace extends LoggedUpdateMaintenance {
32 public function __construct() {
33 parent::__construct();
34 $this->mDescription = "Populate the *_from_namespace fields";
35 }
36
37 protected function getUpdateKey() {
38 return 'populate *_from_namespace';
39 }
40
41 protected function updateSkippedMessage() {
42 return '*_from_namespace column of backlink tables already populated.';
43 }
44
45 public function doDBUpdates() {
46 $force = $this->getOption( 'force' );
47
48 $db = $this->getDB( DB_MASTER );
49
50 $this->output( "Updating *_from_namespace fields in links tables.\n" );
51
52 $start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
53 if ( !$start ) {
54 $this->output( "Nothing to do." );
55 return false;
56 }
57 $end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
58
59 # Do remaining chunk
60 $end += $this->mBatchSize - 1;
61 $blockStart = $start;
62 $blockEnd = $start + $this->mBatchSize - 1;
63 while ( $blockEnd <= $end ) {
64 $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
65 $cond = "page_id BETWEEN $blockStart AND $blockEnd";
66 $res = $db->select( 'page', array( 'page_id', 'page_namespace' ), $cond, __METHOD__ );
67 foreach ( $res as $row ) {
68 $db->update( 'pagelinks',
69 array( 'pl_from_namespace' => $row->page_namespace ),
70 array( 'pl_from' => $row->page_id ),
71 __METHOD__
72 );
73 $db->update( 'templatelinks',
74 array( 'tl_from_namespace' => $row->page_namespace ),
75 array( 'tl_from' => $row->page_id ),
76 __METHOD__
77 );
78 $db->update( 'imagelinks',
79 array( 'il_from_namespace' => $row->page_namespace ),
80 array( 'il_from' => $row->page_id ),
81 __METHOD__
82 );
83 }
84 $blockStart += $this->mBatchSize - 1;
85 $blockEnd += $this->mBatchSize - 1;
86 wfWaitForSlaves();
87 }
88 return true;
89 }
90 }
91
92 $maintClass = "PopulateBacklinkNamespace";
93 require_once RUN_MAINTENANCE_IF_MAIN;