Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateShortUrlTable
0.00% covered (danger)
0.00%
0 / 37
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
 insertRows
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3use MediaWiki\Maintenance\Maintenance;
4
5// @codeCoverageIgnoreStart
6$IP = getenv( 'MW_INSTALL_PATH' );
7if ( $IP === false ) {
8    $IP = __DIR__ . '/../..';
9}
10require_once "$IP/maintenance/Maintenance.php";
11// @codeCoverageIgnoreEnd
12
13class PopulateShortUrlTable extends Maintenance {
14    public function __construct() {
15        parent::__construct();
16        $this->addDescription( 'Populates ShortUrls Table with all existing articles' );
17        $this->requireExtension( 'ShortUrl' );
18    }
19
20    /**
21     * @param mixed $a
22     */
23    private function insertRows( $a ) {
24        $dbw = $this->getPrimaryDB();
25        $dbw->newInsertQueryBuilder()
26            ->insertInto( 'shorturls' )
27            ->ignore()
28            ->rows( $a )
29            ->caller( __METHOD__ )
30            ->execute();
31    }
32
33    /**
34     * @todo FIXME: Refactor out code in ShortUrlUtils.php so it can be used here
35     */
36    public function execute() {
37        $rowCount = 0;
38        $dbr = $this->getReplicaDB();
39
40        $last_processed_id = 0;
41
42        while ( true ) {
43            $insertBuffer = [];
44            $res = $dbr->newSelectQueryBuilder()
45                ->select( [ 'page_id', 'page_namespace', 'page_title' ] )
46                ->from( 'page' )
47                ->where( $dbr->expr( 'page_id', '>', $last_processed_id ) )
48                ->limit( 100 )
49                ->orderBy( 'page_id' )
50                ->caller( __METHOD__ )
51                ->fetchResultSet();
52            if ( $res->numRows() == 0 ) {
53                break;
54            }
55
56            foreach ( $res as $row ) {
57                $rowCount++;
58
59                $rowData = [
60                    'su_namespace' => $row->page_namespace,
61                    'su_title' => $row->page_title
62                ];
63                $insertBuffer[] = $rowData;
64
65                $last_processed_id = $row->page_id;
66            }
67
68            $this->insertRows( $insertBuffer );
69            $this->waitForReplication();
70            $this->output( $rowCount . " titles done\n" );
71        }
72        $this->output( "Done\n" );
73    }
74}
75
76// @codeCoverageIgnoreStart
77$maintClass = PopulateShortUrlTable::class;
78require_once RUN_MAINTENANCE_IF_MAIN;
79// @codeCoverageIgnoreEnd