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\MediaWikiServices; |
25 | use Wikibase\DataModel\Entity\ItemId; |
26 | use Wikibase\DataModel\Entity\NumericPropertyId; |
27 | use Wikibase\DataModel\Services\Statement\GuidGenerator; |
28 | use Wikibase\DataModel\Snak\PropertyValueSnak; |
29 | use Wikibase\Repo\WikibaseRepo; |
30 | |
31 | require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
32 | |
33 | class AddSwhids extends Maintenance { |
34 | |
35 | private $entityLookup; |
36 | private $entityStore; |
37 | private $guidGenerator; |
38 | private $mwUser; |
39 | |
40 | public function __construct() { |
41 | parent::__construct(); |
42 | $this->addDescription( "Script to get the SoftWare Heritage IDentifier (SWHID) from a git url" ); |
43 | $this->addOption( |
44 | 'repo', 'The repoURL.', false, true, "r" |
45 | ); |
46 | $this->addOption( 'force', 'force depositing', false, false, 'f' ); |
47 | $this->requireExtension( 'MathSearch' ); |
48 | $this->requireExtension( 'LinkedWiki' ); |
49 | } |
50 | |
51 | private function getQuery() { |
52 | return <<<SPARQL |
53 | PREFIX wdt: <https://portal.mardi4nfdi.de/prop/direct/> |
54 | |
55 | SELECT ?item ?repo |
56 | WHERE { |
57 | ?item wdt:P339 ?repo. |
58 | FILTER NOT EXISTS { ?item wdt:P1454 ?x } |
59 | } |
60 | SPARQL; |
61 | } |
62 | |
63 | public function execute() { |
64 | $configFactory = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'wgLinkedWiki' ); |
65 | $rf = MediaWikiServices::getInstance()->getHttpRequestFactory(); |
66 | $configDefault = $configFactory->get( "SPARQLServiceByDefault" ); |
67 | $arrEndpoint = ToolsParser::newEndpoint( $configDefault, null ); |
68 | $sp = $arrEndpoint["endpoint"]; |
69 | $this->entityLookup = WikibaseRepo::getEntityLookup(); |
70 | $this->entityStore = WikibaseRepo::getEntityStore(); |
71 | $this->guidGenerator = new GuidGenerator(); |
72 | $this->mwUser = |
73 | MediaWikiServices::getInstance()->getUserFactory()->newFromName( 'swh import' ); |
74 | $exists = ( $this->mwUser->idForName() !== 0 ); |
75 | if ( !$exists ) { |
76 | MediaWikiServices::getInstance()->getAuthManager()->autoCreateUser( |
77 | $this->mwUser, |
78 | MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_MAINT, |
79 | false |
80 | ); |
81 | } |
82 | $rs = $sp->query( $this->getQuery() ); |
83 | foreach ( $rs['result']['rows'] as $row ) { |
84 | $url = $row['repo']; |
85 | $instance = new Swhid( $rf, $url ); |
86 | $instance->fetchOrSave(); |
87 | if ( $instance->getSnapshot() !== null ) { |
88 | $qID = preg_replace( '/.*Q(\d+)$/', '$1', $row['item'] ); |
89 | $this->createWbItem( $qID, $instance->getSnapshot(), $url, $instance->getSnapshotDate() ); |
90 | } |
91 | if ( $instance->getStatus() === 429 ) { |
92 | sleep( $instance->getWait() ); |
93 | } |
94 | } |
95 | } |
96 | |
97 | public function createWbItem( $qID, $swhid, $url, $pit ) { |
98 | global $wgMathSearchPropertySwhid, |
99 | $wgMathSearchPropertyScrUrl, |
100 | $wgMathSearchPropertyPointInTime; |
101 | $mainSnak = new PropertyValueSnak( |
102 | NumericPropertyId::newFromNumber( $wgMathSearchPropertySwhid ), |
103 | new StringValue( $swhid ) ); |
104 | $snakUrl = new PropertyValueSnak( |
105 | NumericPropertyId::newFromNumber( $wgMathSearchPropertyScrUrl ), |
106 | new StringValue( $url ) ); |
107 | $time = new DateTimeImmutable( $pit ); |
108 | // Currently DAY is the maximal precision and the time must be 0:00:00 T57755 |
109 | $date = new TimeValue( |
110 | $time->format( '\+Y-m-d\T00:00:00\Z' ), |
111 | 0, 0, 0, |
112 | TimeValue::PRECISION_DAY, |
113 | TimeValue::CALENDAR_GREGORIAN |
114 | ); |
115 | $snakTime = new PropertyValueSnak( |
116 | NumericPropertyId::newFromNumber( $wgMathSearchPropertyPointInTime ), |
117 | $date ); |
118 | $item = $this->entityLookup->getEntity( ItemId::newFromNumber( $qID ) ); |
119 | $statements = $item->getStatements(); |
120 | $statements->addNewStatement( |
121 | $mainSnak, |
122 | [ $snakUrl, $snakTime ], |
123 | null, |
124 | $this->guidGenerator->newGuid( $item->getId() ) ); |
125 | $item->setStatements( $statements ); |
126 | $this->entityStore->saveEntity( $item, |
127 | "SWHID from Software Heritage", |
128 | $this->mwUser, |
129 | EDIT_FORCE_BOT ); |
130 | } |
131 | |
132 | } |
133 | |
134 | $maintClass = AddSwhids::class; |
135 | /** @noinspection PhpIncludeInspection */ |
136 | require_once RUN_MAINTENANCE_IF_MAIN; |