Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulatePPSortKey
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 3
90
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
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
56
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Populate the pp_sortkey fields in the page_props table
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 * Usage:
28 *  populatePPSortKey.php
29 */
30class PopulatePPSortKey extends LoggedUpdateMaintenance {
31    public function __construct() {
32        parent::__construct();
33        $this->addDescription( 'Populate the pp_sortkey field' );
34        $this->setBatchSize( 100 );
35    }
36
37    protected function doDBUpdates() {
38        $dbw = $this->getPrimaryDB();
39
40        $lastProp = null;
41        $lastPageValue = 0;
42
43        $lastRowCount = 0;
44        $editedRowCount = 0;
45
46        $this->output( "Populating page_props.pp_sortkey...\n" );
47        while ( true ) {
48            $queryBuilder = $dbw->newSelectQueryBuilder()
49                ->select( [ 'pp_propname', 'pp_page', 'pp_sortkey', 'pp_value' ] )
50                ->from( 'page_props' )
51                ->where( [ 'pp_sortkey' => null ] )
52                ->orderBy( [ 'pp_page', 'pp_propname' ] )
53                ->limit( $this->getBatchSize() );
54            if ( $lastPageValue !== 0 ) {
55                $queryBuilder->andWhere( $dbw->buildComparison( '>', [
56                    'pp_page' => $lastPageValue,
57                    'pp_propname' => $lastProp,
58                ] ) );
59            }
60
61            $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
62
63            if ( $res->numRows() === 0 ) {
64                break;
65            }
66
67            $this->beginTransaction( $dbw, __METHOD__ );
68
69            foreach ( $res as $row ) {
70                if ( !is_numeric( $row->pp_value ) ) {
71                    continue;
72                }
73                $dbw->newUpdateQueryBuilder()
74                    ->update( 'page_props' )
75                    ->set( [ 'pp_sortkey' => $row->pp_value ] )
76                    ->where( [
77                        'pp_page' => $row->pp_page,
78                        'pp_propname' => $row->pp_propname
79                    ] )
80                    ->caller( __METHOD__ )
81                    ->execute();
82                $editedRowCount++;
83            }
84
85            if ( $editedRowCount !== $lastRowCount ) {
86                $this->output( "Updated " . $editedRowCount . " rows\n" );
87                $lastRowCount = $editedRowCount;
88            }
89
90            $this->commitTransaction( $dbw, __METHOD__ );
91
92            // We need to get the last element's page ID
93            // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
94            $lastPageValue = $row->pp_page;
95            // And the propname...
96            // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
97            $lastProp = $row->pp_propname;
98        }
99
100        $this->output( "Populating page_props.pp_sortkey complete.\n" );
101        $this->output( "Updated a total of $editedRowCount rows\n" );
102        return true;
103    }
104
105    protected function getUpdateKey() {
106        return 'populate pp_sortkey';
107    }
108}
109
110$maintClass = PopulatePPSortKey::class;
111require_once RUN_MAINTENANCE_IF_MAIN;