Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.84% covered (warning)
87.84%
65 / 74
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateCognatePages
94.20% covered (success)
94.20%
65 / 69
50.00% covered (danger)
50.00%
1 / 2
8.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 execute
93.33% covered (success)
93.33%
56 / 60
0.00% covered (danger)
0.00%
0 / 1
7.01
1<?php
2
3namespace Cognate;
4
5use MediaWiki\Maintenance\Maintenance;
6
7if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
8    require_once getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php';
9} else {
10    require_once __DIR__ . '/../../../maintenance/Maintenance.php';
11}
12
13/**
14 * Maintenance script for populating the cognate page and title tables.
15 *
16 * The script will add an entry in the cognate_pages table for every page that
17 * exists in the mediawiki page table as well as adding the needed entries to
18 * the cognate_titles table.
19 *
20 * @license GPL-2.0-or-later
21 * @author Addshore
22 */
23class PopulateCognatePages extends Maintenance {
24
25    public function __construct() {
26        parent::__construct();
27
28        $this->addDescription( 'Populate the Cognate page and title tables' );
29        $this->addOption( 'start', 'The page ID to start from.', false, true );
30        $this->addOption(
31            'clear-first',
32            'Clear entries in the table for the current wiki before population starts.'
33        );
34        $this->setBatchSize( 100 );
35        $this->requireExtension( 'Cognate' );
36    }
37
38    public function execute() {
39        $services = $this->getServiceContainer();
40        $dbName = $services->getMainConfig()->get( 'DBname' );
41        $namespaces = $services->getMainConfig()->get( 'CognateNamespaces' );
42        $namespaces = array_filter(
43            array_map( 'intval', $namespaces ),
44            static function ( $namespace ) {
45                return $namespace >= NS_MAIN && $namespace <= NS_CATEGORY_TALK;
46            }
47        );
48
49        /** @var CognateStore $store */
50        $store = $services->getService( 'CognateStore' );
51
52        if ( $this->hasOption( 'clear-first' ) ) {
53            $this->output( "Clearing cognate_pages table of entries for $dbName.\n" );
54            $store->deletePagesForSite( $dbName );
55        }
56
57        $this->output( "Started processing.\n" );
58        $dbr = $this->getDB( DB_REPLICA );
59
60        $start = $this->getOption( 'start' );
61        if ( $start === null ) {
62            $start = $dbr->newSelectQueryBuilder()
63                ->select( 'MIN(page_id)' )
64                ->from( 'page' )
65                ->caller( __METHOD__ )
66                ->fetchField();
67        }
68        if ( !$start ) {
69            $this->output( "Nothing to do.\n" );
70            return true;
71        }
72
73        $end = $dbr->newSelectQueryBuilder()
74            ->select( 'MAX(page_id)' )
75            ->from( 'page' )
76            ->caller( __METHOD__ )
77            ->fetchField();
78
79        $blockStart = (int)$start;
80        $blockEnd = $blockStart + $this->mBatchSize - 1;
81
82        while ( $blockStart <= $end ) {
83            $rows = $dbr->newSelectQueryBuilder()
84                ->select( [ 'page_namespace', 'page_title' ] )
85                ->from( 'page' )
86                ->where( [
87                    $dbr->expr( 'page_id', '>=', $blockStart ),
88                    $dbr->expr( 'page_id', '<=', $blockEnd ),
89                    'page_namespace' => $namespaces,
90                    'page_is_redirect' => 0,
91                ] )
92                ->caller( __METHOD__ )
93                ->fetchResultSet();
94
95            $titleDetails = [];
96            foreach ( $rows as $row ) {
97                $titleDetails[] = [
98                    'site' => $dbName,
99                    'namespace' => (int)$row->page_namespace,
100                    'title' => $row->page_title,
101                ];
102            }
103
104            $numberOfRows = count( $titleDetails );
105            $store->insertPages( $titleDetails );
106
107            $this->output( "$numberOfRows rows processed.\n" );
108
109            $blockStart += $this->mBatchSize;
110            $blockEnd += $this->mBatchSize;
111            $this->output( "Pass finished.\n" );
112
113            $this->waitForReplication();
114        }
115
116        $this->output( "Done.\n" );
117        return true;
118    }
119
120}
121
122$maintClass = PopulateCognatePages::class;
123require_once RUN_MAINTENANCE_IF_MAIN;