Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateBacklinkNamespace
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 updateSkippedMessage
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
30
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
24require_once __DIR__ . '/Maintenance.php';
25
26/**
27 * Maintenance script to populate *_from_namespace fields
28 *
29 * @ingroup Maintenance
30 */
31class 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->getPrimaryDB();
48
49        $this->output( "Updating *_from_namespace fields in links tables.\n" );
50
51        $start = $this->getOption( 'lastUpdatedId' );
52        if ( !$start ) {
53            $start = $db->newSelectQueryBuilder()
54                ->select( 'MIN(page_id)' )
55                ->from( 'page' )
56                ->caller( __METHOD__ )->fetchField();
57        }
58        if ( !$start ) {
59            $this->output( "Nothing to do." );
60            return false;
61        }
62        $end = $db->newSelectQueryBuilder()
63            ->select( 'MAX(page_id)' )
64            ->from( 'page' )
65            ->caller( __METHOD__ )->fetchField();
66        $batchSize = $this->getBatchSize();
67
68        # Do remaining chunk
69        $end += $batchSize - 1;
70        $blockStart = $start;
71        $blockEnd = $start + $batchSize - 1;
72        while ( $blockEnd <= $end ) {
73            $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
74            $res = $db->newSelectQueryBuilder()
75                ->select( [ 'page_id', 'page_namespace' ] )
76                ->from( 'page' )
77                ->where( [
78                    $db->expr( 'page_id', '>=', (int)$blockStart ),
79                    $db->expr( 'page_id', '<=', (int)$blockEnd ),
80                ] )
81                ->caller( __METHOD__ )->fetchResultSet();
82            foreach ( $res as $row ) {
83                $db->newUpdateQueryBuilder()
84                    ->update( 'pagelinks' )
85                    ->set( [ 'pl_from_namespace' => $row->page_namespace ] )
86                    ->where( [ 'pl_from' => $row->page_id ] )
87                    ->caller( __METHOD__ )
88                    ->execute();
89                $db->newUpdateQueryBuilder()
90                    ->update( 'templatelinks' )
91                    ->set( [ 'tl_from_namespace' => $row->page_namespace ] )
92                    ->where( [ 'tl_from' => $row->page_id ] )
93                    ->caller( __METHOD__ )
94                    ->execute();
95                $db->newUpdateQueryBuilder()
96                    ->update( 'imagelinks' )
97                    ->set( [ 'il_from_namespace' => $row->page_namespace ] )
98                    ->where( [ 'il_from' => $row->page_id ] )
99                    ->caller( __METHOD__ )
100                    ->execute();
101            }
102            $blockStart += $batchSize - 1;
103            $blockEnd += $batchSize - 1;
104            $this->waitForReplication();
105        }
106        return true;
107    }
108}
109
110$maintClass = PopulateBacklinkNamespace::class;
111require_once RUN_MAINTENANCE_IF_MAIN;