Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ImportConstraintStatements
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace WikibaseQuality\ConstraintReport\Maintenance;
4
5use Maintenance;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Title\Title;
8use Wikibase\Lib\Store\PropertyInfoLookup;
9use Wikibase\Repo\WikibaseRepo;
10use WikibaseQuality\ConstraintReport\Job\UpdateConstraintsTableJob;
11use Wikimedia\Rdbms\ILBFactory;
12
13// @codeCoverageIgnoreStart
14$basePath = getenv( "MW_INSTALL_PATH" ) !== false
15    ? getenv( "MW_INSTALL_PATH" ) : __DIR__ . "/../../..";
16
17require_once $basePath . "/maintenance/Maintenance.php";
18// @codeCoverageIgnoreEnd
19
20/**
21 * Runs {@link UpdateConstraintsTableJob} once for every property.
22 *
23 * @license GPL-2.0-or-later
24 */
25class ImportConstraintStatements extends Maintenance {
26
27    /**
28     * @var PropertyInfoLookup
29     */
30    private $propertyInfoLookup;
31
32    /** @var ILBFactory */
33    private $lbFactory;
34
35    /**
36     * @var callable
37     * @phan-var callable(string):UpdateConstraintsTableJob
38     */
39    private $newUpdateConstraintsTableJob;
40
41    /**
42     * @var callable
43     * @phan-var callable():void
44     */
45    private $setupServices;
46
47    public function __construct() {
48        parent::__construct();
49        $this->newUpdateConstraintsTableJob = static function ( $propertyIdSerialization ) {
50            return UpdateConstraintsTableJob::newFromGlobalState(
51                Title::newMainPage(),
52                [ 'propertyId' => $propertyIdSerialization ]
53            );
54        };
55
56        $this->addDescription( 'Imports property constraints from statements on properties' );
57        $this->requireExtension( 'WikibaseQualityConstraints' );
58        $this->setBatchSize( 10 );
59
60        // Wikibase classes are not yet loaded, so setup services in a callback run in execute
61        // that can be overridden in tests.
62        $this->setupServices = function () {
63            $services = MediaWikiServices::getInstance();
64            $this->propertyInfoLookup = WikibaseRepo::getStore( $services )->getPropertyInfoLookup();
65            $this->lbFactory = $services->getDBLoadBalancerFactory();
66        };
67    }
68
69    public function execute() {
70        ( $this->setupServices )();
71        if ( !$this->getConfig()->get( 'WBQualityConstraintsEnableConstraintsImportFromStatements' ) ) {
72            $this->error( 'Constraint statements are not enabled. Aborting.' );
73            return;
74        }
75
76        $propertyInfos = $this->propertyInfoLookup->getAllPropertyInfo();
77        $propertyIds = array_keys( $propertyInfos );
78
79        foreach ( array_chunk( $propertyIds, $this->getBatchSize() ) as $propertyIdsChunk ) {
80            foreach ( $propertyIdsChunk as $propertyIdSerialization ) {
81                $this->output( sprintf(
82                    'Importing constraint statements for % 6s... ',
83                    $propertyIdSerialization ),
84                    $propertyIdSerialization
85                );
86                $startTime = microtime( true );
87                $job = call_user_func( $this->newUpdateConstraintsTableJob, $propertyIdSerialization );
88                $job->run();
89                $endTime = microtime( true );
90                $millis = ( $endTime - $startTime ) * 1000;
91                $this->output( sprintf( 'done in % 6.2f ms.', $millis ), $propertyIdSerialization );
92            }
93
94            $this->output( 'Waiting for replication... ', 'waitForReplication' );
95            $startTime = microtime( true );
96            $this->lbFactory->waitForReplication();
97            $endTime = microtime( true );
98            $millis = ( $endTime - $startTime ) * 1000;
99            $this->output( sprintf( 'done in % 6.2f ms.', $millis ), 'waitForReplication' );
100        }
101    }
102
103}
104
105// @codeCoverageIgnoreStart
106$maintClass = ImportConstraintStatements::class;
107require_once RUN_MAINTENANCE_IF_MAIN;
108// @codeCoverageIgnoreEnd