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