Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateProjectsFromSiteMatrix
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 generateSites
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 generateAllowedDomains
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\ReadingLists\Maintenance;
4
5use Generator;
6use Maintenance;
7use MediaWiki\Extension\ReadingLists\Utils;
8use MediaWiki\Extension\SiteMatrix\SiteMatrix;
9use MediaWiki\MediaWikiServices;
10
11require_once getenv( 'MW_INSTALL_PATH' ) !== false
12    ? getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'
13    : __DIR__ . '/../../../maintenance/Maintenance.php';
14
15/**
16 * Maintenance script for populating the reading_list_project table.
17 * If the table is already populated, add new entries (old entries that don't exist anymore are
18 * left in place).
19 * Uses the SiteMatrix extension as the data source.
20 * @ingroup Maintenance
21 */
22class PopulateProjectsFromSiteMatrix extends Maintenance {
23
24    /** @var SiteMatrix */
25    private $siteMatrix;
26
27    public function __construct() {
28        parent::__construct();
29        $this->addDescription(
30            'Populate (or update) the reading_list_project table from SiteMatrix data.' );
31        $this->setBatchSize( 100 );
32        $this->requireExtension( 'ReadingLists' );
33        $this->requireExtension( 'SiteMatrix' );
34    }
35
36    /**
37     * @inheritDoc
38     */
39    public function execute() {
40        // Would be nicer to the put this in the constructor but there extensions are not loaded yet.
41        $this->siteMatrix = new SiteMatrix();
42
43        $dbw = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getPrimaryDatabase(
44            Utils::VIRTUAL_DOMAIN
45        );
46        $inserted = 0;
47
48        $this->output( "populating...\n" );
49        foreach ( $this->generateAllowedDomains() as [ $project ] ) {
50            $dbw->newInsertQueryBuilder()
51                ->insertInto( 'reading_list_project' )
52                ->ignore()
53                ->row( [ 'rlp_project' => $project ] )
54                ->caller( __METHOD__ )->execute();
55            if ( $dbw->affectedRows() ) {
56                $inserted++;
57                if ( $inserted % $this->mBatchSize ) {
58                    $this->waitForReplication();
59                }
60            }
61        }
62        $this->output( "inserted $inserted projects\n" );
63    }
64
65    /**
66     * List all sites known to SiteMatrix.
67     * @return Generator [ language, site ]
68     */
69    private function generateSites() {
70        foreach ( $this->siteMatrix->getSites() as $site ) {
71            foreach ( $this->siteMatrix->getLangList() as $lang ) {
72                if ( !$this->siteMatrix->exist( $lang, $site ) ) {
73                    continue;
74                }
75                yield [ $lang, $site ];
76            }
77        }
78        foreach ( $this->siteMatrix->getSpecials() as $special ) {
79            [ $lang, $site ] = $special;
80            yield [ $lang, $site ];
81        }
82    }
83
84    /**
85     * List all sites that are safe to add to the reading list.
86     * @return Generator [ domain, dbname ]
87     */
88    private function generateAllowedDomains() {
89        foreach ( $this->generateSites() as [ $lang, $site ] ) {
90            $dbName = $this->siteMatrix->getDBName( $lang, $site );
91            $domain = $this->siteMatrix->getCanonicalUrl( $lang, $site );
92
93            if ( $this->siteMatrix->isPrivate( $dbName ) ) {
94                continue;
95            }
96
97            yield [ $domain, $dbName ];
98        }
99    }
100
101}
102
103$maintClass = PopulateProjectsFromSiteMatrix::class;
104require_once RUN_MAINTENANCE_IF_MAIN;