Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
FetchIdsFromWd | |
0.00% |
0 / 53 |
|
0.00% |
0 / 2 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 52 |
|
0.00% |
0 / 1 |
110 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\MathSearch\Graph\Job; |
4 | |
5 | use DataValues\StringValue; |
6 | use MediaWiki\Extension\MathSearch\Graph\Query; |
7 | use MediaWiki\MediaWikiServices; |
8 | use Throwable; |
9 | use Wikibase\DataModel\Entity\ItemId; |
10 | use Wikibase\DataModel\Entity\NumericPropertyId; |
11 | use Wikibase\DataModel\Services\Statement\GuidGenerator; |
12 | use Wikibase\DataModel\Snak\PropertyValueSnak; |
13 | use Wikibase\DataModel\Statement\StatementList; |
14 | use Wikibase\Repo\WikibaseRepo; |
15 | |
16 | class FetchIdsFromWd extends GraphJob { |
17 | public function __construct( $params ) { |
18 | parent::__construct( 'FetchIdsFromWd', $params ); |
19 | } |
20 | |
21 | public function run(): bool { |
22 | $query = Query::getQueryForWdId(); |
23 | $rs = Query::getQueryEndpoint()->query( $query ); |
24 | if ( !$rs ) { |
25 | self::getLog()->info( "No results retrieved!\n" ); |
26 | return false; |
27 | } |
28 | self::getLog()->info( "Retrieved " . count( $rs['result']['rows'] ) . " results.\n" ); |
29 | |
30 | $user = $this->getUser(); |
31 | $store = WikibaseRepo::getEntityStore(); |
32 | $lookup = WikibaseRepo::getEntityLookup(); |
33 | $guidGenerator = new GuidGenerator(); |
34 | foreach ( $rs['result']['rows'] as $row ) { |
35 | try { |
36 | $qid = $row['qid']; |
37 | unset( $row['qid'] ); |
38 | self::getLog()->info( "Update Properties for Q$qid." ); |
39 | $item = $lookup->getEntity( ItemId::newFromNumber( $qid ) ); |
40 | if ( $item === null ) { |
41 | self::getLog()->error( "Item Q$qid not found." ); |
42 | continue; |
43 | } |
44 | /** @var StatementList $statements */ |
45 | $statements = $item->getStatements(); |
46 | $changed = false; |
47 | foreach ( $row as $colname => $colval ) { |
48 | if ( !str_starts_with( $colname, 'P' ) || !is_numeric( substr( $colname, 1 ) ) ) { |
49 | self::getLog() |
50 | ->warning( "Result column '$colname' does not start with P followed by a number." ); |
51 | continue; |
52 | } |
53 | $pid = NumericPropertyId::newFromNumber( substr( $colname, 1 ) ); |
54 | $pidStatements = $statements->getByPropertyId( $pid ); |
55 | if ( !$pidStatements->isEmpty() ) { |
56 | self::getLog()->info( "$colname of Zbl Q$qid already set." ); |
57 | continue; |
58 | } |
59 | $changed = true; |
60 | $mainSnak = new PropertyValueSnak( |
61 | $pid, |
62 | new StringValue( $colval ) ); |
63 | $statements->addNewStatement( |
64 | $mainSnak, |
65 | [], |
66 | null, |
67 | $guidGenerator->newGuid( $item->getId() ) ); |
68 | } |
69 | if ( !$changed ) { |
70 | self::getLog()->info( "No changes for Zbl Q$qid." ); |
71 | continue; |
72 | } |
73 | $item->setStatements( $statements ); |
74 | |
75 | $store->saveEntity( $item, "Add wikidata reference.", $user, |
76 | EDIT_FORCE_BOT ); |
77 | } catch ( Throwable $ex ) { |
78 | self::getLog()->error( "Skip page processing page Q$qid.", [ $ex ] ); |
79 | self::getLog()->info( $ex->getMessage() ); |
80 | self::getLog()->info( $ex->getTraceAsString() ); |
81 | } |
82 | } |
83 | // reschedule job (maybe there are more results) |
84 | $jobQueueGroup = MediaWikiServices::getInstance()->getJobQueueGroup(); |
85 | $jobQueueGroup->lazyPush( new FetchIdsFromWd( $this->params ) ); |
86 | return true; |
87 | } |
88 | } |