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
NormalizeDoi
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 2
56
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
 run
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\MathSearch\Graph\Job;
4
5use DataValues\StringValue;
6use Throwable;
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 NormalizeDoi extends GraphJob {
15    private int $pDoi;
16
17    public function __construct( $params ) {
18        global $wgMathSearchPropertyDoi;
19        parent::__construct( 'NormalizeDoi', $params );
20        $this->pDoi = $this->params['pDoi'] ?? $wgMathSearchPropertyDoi;
21    }
22
23    public function run(): bool {
24        $user = $this->getUser();
25        $store = WikibaseRepo::getEntityStore();
26        $lookup = WikibaseRepo::getEntityLookup();
27        $guidGenerator = new GuidGenerator();
28        $pDOI = NumericPropertyId::newFromNumber( $this->pDoi );
29        foreach ( $this->params['rows'] as $qid => $doi ) {
30            try {
31                self::getLog()->info( "Update DOI 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                $doiStatements = $statements->getByPropertyId( $pDOI );
40                if ( $doiStatements->count() !== 1 ) {
41                    self::getLog()->error( "Skip page processing page Q$qid. Only 1 DOI is supported" );
42                    continue;
43                }
44                foreach ( $doiStatements->getIterator() as $snak ) {
45                    $statements->removeStatementsWithGuid( $snak->getGuid() );
46                }
47                $mainSnak = new PropertyValueSnak(
48                    $pDOI,
49                    new StringValue( strtoupper( $doi ) ) );
50                $statements->addNewStatement(
51                    $mainSnak,
52                    [],
53                    null,
54                    $guidGenerator->newGuid( $item->getId() ) );
55                $item->setStatements( $statements );
56                $store->saveEntity( $item, "Normalize DOI.", $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}