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