Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CleanupPageLang
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
42
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
 processRow
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 updatePageLang
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Clean up deprecated language codes in page_lang
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// @codeCoverageIgnoreStart
25require_once __DIR__ . '/TableCleanup.php';
26// @codeCoverageIgnoreEnd
27
28use MediaWiki\Language\LanguageCode;
29
30/**
31 * Maintenance script to clean up deprecated language codes in page_lang
32 *
33 * @ingroup Maintenance
34 */
35class CleanupPageLang extends TableCleanup {
36    public function __construct() {
37        parent::__construct();
38        $this->addDescription( 'Script to clean up deprecated language codes in page_lang' );
39        $this->setBatchSize( 1000 );
40    }
41
42    /**
43     * @param stdClass $row
44     */
45    protected function processRow( $row ) {
46        $oldPageLang = $row->page_lang;
47        if ( $oldPageLang === null ) {
48            // Page has no page language
49            $this->progress( 0 );
50            return;
51        }
52
53        $newPageLang = LanguageCode::replaceDeprecatedCodes( $oldPageLang );
54        if ( $newPageLang === $oldPageLang ) {
55            // Page language is unchanged
56            $this->progress( 0 );
57            return;
58        }
59
60        $this->updatePageLang( $row, $oldPageLang, $newPageLang );
61        $this->progress( 1 );
62    }
63
64    /**
65     * @param stdClass $row
66     * @param string $oldPageLang
67     * @param string $newPageLang
68     */
69    private function updatePageLang( $row, $oldPageLang, $newPageLang ) {
70        if ( $this->dryrun ) {
71            $this->output( "DRY RUN: would update page_lang on $row->page_id from $oldPageLang to $newPageLang.\n" );
72        } else {
73            $this->output( "Update page_lang on $row->page_id from $oldPageLang to $newPageLang.\n" );
74            $this->getPrimaryDB()
75                ->newUpdateQueryBuilder()
76                ->update( 'page' )
77                ->set( [ 'page_lang' => $newPageLang ] )
78                ->where( [ 'page_id' => $row->page_id ] )
79                ->caller( __METHOD__ )->execute();
80        }
81    }
82}
83
84// @codeCoverageIgnoreStart
85$maintClass = CleanupPageLang::class;
86require_once RUN_MAINTENANCE_IF_MAIN;
87// @codeCoverageIgnoreEnd