Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
AddSwhids | |
0.00% |
0 / 68 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getQuery | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
30 | |||
createWbItem | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @ingroup Maintenance |
19 | */ |
20 | |
21 | use DataValues\StringValue; |
22 | use DataValues\TimeValue; |
23 | use MediaWiki\Extension\MathSearch\Swh\Swhid; |
24 | use MediaWiki\User\User; |
25 | use Wikibase\DataModel\Entity\ItemId; |
26 | use Wikibase\DataModel\Entity\NumericPropertyId; |
27 | use Wikibase\DataModel\Services\Lookup\EntityLookup; |
28 | use Wikibase\DataModel\Services\Statement\GuidGenerator; |
29 | use Wikibase\DataModel\Snak\PropertyValueSnak; |
30 | use Wikibase\Lib\Store\EntityStore; |
31 | use Wikibase\Repo\WikibaseRepo; |
32 | |
33 | require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
34 | |
35 | class AddSwhids extends Maintenance { |
36 | |
37 | /** @var EntityLookup */ |
38 | private $entityLookup; |
39 | /** @var EntityStore */ |
40 | private $entityStore; |
41 | /** @var GuidGenerator */ |
42 | private $guidGenerator; |
43 | /** @var User */ |
44 | private $mwUser; |
45 | |
46 | public function __construct() { |
47 | parent::__construct(); |
48 | $this->addDescription( "Script to get the SoftWare Heritage IDentifier (SWHID) from a git url" ); |
49 | $this->addOption( |
50 | 'repo', 'The repoURL.', false, true, "r" |
51 | ); |
52 | $this->addOption( 'force', 'force depositing', false, false, 'f' ); |
53 | $this->requireExtension( 'MathSearch' ); |
54 | $this->requireExtension( 'LinkedWiki' ); |
55 | } |
56 | |
57 | private function getQuery() { |
58 | return <<<SPARQL |
59 | PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/> |
60 | |
61 | SELECT ?item ?repo |
62 | WHERE { |
63 | ?item wdt:P339 ?repo. |
64 | FILTER NOT EXISTS { ?item wdt:P1454 ?x } |
65 | } |
66 | SPARQL; |
67 | } |
68 | |
69 | public function execute() { |
70 | $services = $this->getServiceContainer(); |
71 | $configFactory = $services->getConfigFactory()->makeConfig( 'wgLinkedWiki' ); |
72 | $rf = $services->getHttpRequestFactory(); |
73 | $configDefault = $configFactory->get( "SPARQLServiceByDefault" ); |
74 | $arrEndpoint = ToolsParser::newEndpoint( $configDefault, null ); |
75 | $sp = $arrEndpoint["endpoint"]; |
76 | $this->entityLookup = WikibaseRepo::getEntityLookup(); |
77 | $this->entityStore = WikibaseRepo::getEntityStore(); |
78 | $this->guidGenerator = new GuidGenerator(); |
79 | $this->mwUser = $services->getUserFactory()->newFromName( 'swh import' ); |
80 | $exists = ( $this->mwUser->idForName() !== 0 ); |
81 | if ( !$exists ) { |
82 | $services->getAuthManager()->autoCreateUser( |
83 | $this->mwUser, |
84 | MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_MAINT, |
85 | false |
86 | ); |
87 | } |
88 | $rs = $sp->query( $this->getQuery() ); |
89 | foreach ( $rs['result']['rows'] as $row ) { |
90 | $url = $row['repo']; |
91 | $instance = new Swhid( $rf, $url ); |
92 | $instance->fetchOrSave(); |
93 | if ( $instance->getSnapshot() !== null ) { |
94 | $qID = preg_replace( '/.*Q(\d+)$/', '$1', $row['item'] ); |
95 | $this->createWbItem( $qID, $instance->getSnapshot(), $url, $instance->getSnapshotDate() ); |
96 | } |
97 | if ( $instance->getStatus() === 429 ) { |
98 | sleep( $instance->getWait() ); |
99 | } |
100 | } |
101 | } |
102 | |
103 | public function createWbItem( $qID, $swhid, $url, $pit ) { |
104 | global $wgMathSearchPropertySwhid, |
105 | $wgMathSearchPropertyScrUrl, |
106 | $wgMathSearchPropertyPointInTime; |
107 | $mainSnak = new PropertyValueSnak( |
108 | NumericPropertyId::newFromNumber( $wgMathSearchPropertySwhid ), |
109 | new StringValue( $swhid ) ); |
110 | $snakUrl = new PropertyValueSnak( |
111 | NumericPropertyId::newFromNumber( $wgMathSearchPropertyScrUrl ), |
112 | new StringValue( $url ) ); |
113 | $time = new DateTimeImmutable( $pit ); |
114 | // Currently DAY is the maximal precision and the time must be 0:00:00 T57755 |
115 | $date = new TimeValue( |
116 | $time->format( '\+Y-m-d\T00:00:00\Z' ), |
117 | 0, 0, 0, |
118 | TimeValue::PRECISION_DAY, |
119 | TimeValue::CALENDAR_GREGORIAN |
120 | ); |
121 | $snakTime = new PropertyValueSnak( |
122 | NumericPropertyId::newFromNumber( $wgMathSearchPropertyPointInTime ), |
123 | $date ); |
124 | $item = $this->entityLookup->getEntity( ItemId::newFromNumber( $qID ) ); |
125 | $statements = $item->getStatements(); |
126 | $statements->addNewStatement( |
127 | $mainSnak, |
128 | [ $snakUrl, $snakTime ], |
129 | null, |
130 | $this->guidGenerator->newGuid( $item->getId() ) ); |
131 | $item->setStatements( $statements ); |
132 | $this->entityStore->saveEntity( $item, |
133 | "SWHID from Software Heritage", |
134 | $this->mwUser, |
135 | EDIT_FORCE_BOT ); |
136 | } |
137 | |
138 | } |
139 | |
140 | $maintClass = AddSwhids::class; |
141 | /** @noinspection PhpIncludeInspection */ |
142 | require_once RUN_MAINTENANCE_IF_MAIN; |