Merge "Print chained exceptions when maintenance script fails."
[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->addDescription( 'Populate the *_from_namespace fields' );
35 $this->addOption( 'lastUpdatedId', "Highest page_id with updated links", false, true );
36 }
37
38 protected function getUpdateKey() {
39 return 'populate *_from_namespace';
40 }
41
42 protected function updateSkippedMessage() {
43 return '*_from_namespace column of backlink tables already populated.';
44 }
45
46 public function doDBUpdates() {
47 $db = $this->getDB( DB_MASTER );
48
49 $this->output( "Updating *_from_namespace fields in links tables.\n" );
50
51 $start = $this->getOption( 'lastUpdatedId' );
52 if ( !$start ) {
53 $start = $db->selectField( 'page', 'MIN(page_id)', '', __METHOD__ );
54 }
55 if ( !$start ) {
56 $this->output( "Nothing to do." );
57 return false;
58 }
59 $end = $db->selectField( 'page', 'MAX(page_id)', '', __METHOD__ );
60 $batchSize = $this->getBatchSize();
61
62 # Do remaining chunk
63 $end += $batchSize - 1;
64 $blockStart = $start;
65 $blockEnd = $start + $batchSize - 1;
66 while ( $blockEnd <= $end ) {
67 $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
68 $cond = "page_id BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd;
69 $res = $db->select( 'page', [ 'page_id', 'page_namespace' ], $cond, __METHOD__ );
70 foreach ( $res as $row ) {
71 $db->update( 'pagelinks',
72 [ 'pl_from_namespace' => $row->page_namespace ],
73 [ 'pl_from' => $row->page_id ],
74 __METHOD__
75 );
76 $db->update( 'templatelinks',
77 [ 'tl_from_namespace' => $row->page_namespace ],
78 [ 'tl_from' => $row->page_id ],
79 __METHOD__
80 );
81 $db->update( 'imagelinks',
82 [ 'il_from_namespace' => $row->page_namespace ],
83 [ 'il_from' => $row->page_id ],
84 __METHOD__
85 );
86 }
87 $blockStart += $batchSize - 1;
88 $blockEnd += $batchSize - 1;
89 wfWaitForSlaves();
90 }
91 return true;
92 }
93 }
94
95 $maintClass = PopulateBacklinkNamespace::class;
96 require_once RUN_MAINTENANCE_IF_MAIN;