Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SetProfileType
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace MediaWiki\Extension\MathSearch\Graph\Job;
4
5use Throwable;
6use Wikibase\DataModel\Entity\EntityIdValue;
7use Wikibase\DataModel\Entity\ItemId;
8use Wikibase\DataModel\Entity\NumericPropertyId;
9use Wikibase\DataModel\Services\Statement\GuidGenerator;
10use Wikibase\DataModel\Snak\PropertyValueSnak;
11use Wikibase\DataModel\Statement\StatementList;
12use Wikibase\Repo\WikibaseRepo;
13
14class SetProfileType extends GraphJob {
15    public function __construct( $params ) {
16        parent::__construct( 'SetProfileType', $params );
17    }
18
19    public function run(): bool {
20        global $wgMathSearchPropertyProfileType;
21        $user = $this->getUser();
22        $store = WikibaseRepo::getEntityStore();
23        $lookup = WikibaseRepo::getEntityLookup();
24        $guidGenerator = new GuidGenerator();
25        $pProfileType = NumericPropertyId::newFromNumber( $wgMathSearchPropertyProfileType );
26        $mainSnak = new PropertyValueSnak(
27            $pProfileType,
28            new EntityIdValue( new ItemId( $this->params['qType'] ) ) );
29        foreach ( $this->params['rows'] as $qid ) {
30            try {
31                self::getLog()->info( "Add profile type for $qid." );
32                $item = $lookup->getEntity( ItemId::newFromNumber( $qid ) );
33                if ( $item === null ) {
34                    self::getLog()->error( "Item Q$qid not found." );
35                    continue;
36                }
37                /** @var StatementList $statements */
38                $statements = $item->getStatements();
39                $profileTypeStatements = $statements->getByPropertyId( $pProfileType );
40                if ( !$profileTypeStatements->isEmpty() ) {
41                    if ( $this->params['overwrite'] ) {
42                        foreach ( $profileTypeStatements->getIterator() as $snak ) {
43                            $statements->removeStatementsWithGuid( $snak->getGuid() );
44                        }
45                    } else {
46                        self::getLog()->info( "Skip page Q$qid." );
47                        continue;
48                    }
49                }
50                $statements->addNewStatement(
51                    $mainSnak,
52                    [],
53                    null,
54                    $guidGenerator->newGuid( $item->getId() ) );
55                $item->setStatements( $statements );
56                $store->saveEntity( $item, "Set profile property.", $user,
57                    EDIT_FORCE_BOT );
58            } catch ( Throwable $ex ) {
59                self::getLog()->error( "Skip page processing page Q$qid.", [ $ex ] );
60            }
61        }
62
63        return true;
64    }
65}