Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreatePreexistingSchemas
0.00% covered (danger)
0.00%
0 / 119
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getSchemasToCreate
0.00% covered (danger)
0.00%
0 / 82
0.00% covered (danger)
0.00%
0 / 1
2
 createSchema
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\Maintenance;
6
7$basePath = getenv( 'MW_INSTALL_PATH' ) !== false
8    ? getenv( 'MW_INSTALL_PATH' )
9    : __DIR__ . '/../../..';
10
11require_once $basePath . '/maintenance/Maintenance.php';
12require_once $basePath . '/extensions/EntitySchema/src/Domain/Storage/IdGenerator.php';
13require_once 'FixedIdGenerator.php';
14
15use EntitySchema\DataAccess\MediaWikiPageUpdaterFactory;
16use EntitySchema\DataAccess\MediaWikiRevisionEntitySchemaInserter;
17use EntitySchema\DataAccess\WatchlistUpdater;
18use Maintenance;
19use MediaWiki\MediaWikiServices;
20use MediaWiki\User\User;
21use RequestContext;
22use RuntimeException;
23
24/**
25 * Maintenance script for creating preexisting EntitySchemas.
26 *
27 * @license GPL-2.0-or-later
28 */
29class CreatePreexistingSchemas extends Maintenance {
30
31    private const LABEL = 'label';
32    private const DESC = 'desc';
33
34    public function __construct() {
35        parent::__construct();
36
37        $this->addDescription(
38            'Create initial EntitySchemas. Prossibly not that useful beyond wikidata.'
39        );
40
41        $this->requireExtension( 'EntitySchema' );
42    }
43
44    public function execute(): void {
45        // "Maintenance script" is in MediaWiki's $wgReservedUsernames
46        $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
47
48        $entities = $this->getSchemasToCreate();
49
50        $this->output( "Starting import...\n\n" );
51
52        foreach ( $entities as $idString => $dataMap ) {
53            $this->createSchema( $idString, $dataMap, $user );
54        }
55
56        $this->output( "Import completed.\n" );
57    }
58
59    private function getSchemasToCreate(): array {
60        return [
61            'E1' => [
62                self::LABEL => 'ShExR',
63                self::DESC => 'Schema of ShEx',
64            ],
65
66            'E2' => [
67                self::LABEL => 'Wikimedia',
68                self::DESC => 'Schema of Wikimedia projects in Wikidata',
69            ],
70
71            'E3' => [
72                self::LABEL => 'Wikidata Item',
73                self::DESC => 'Schema of a Wikidata item',
74            ],
75
76            'E4' => [
77                self::LABEL => 'Labels/Descriptions',
78                self::DESC => 'Schema of labels and descriptions',
79            ],
80
81            'E5' => [
82                self::LABEL => 'Statement',
83                self::DESC => 'Schema of a Statement',
84            ],
85
86            'E6' => [
87                self::LABEL => 'Language mappings',
88                self::DESC => 'Schema for language mappings in Wikidata',
89            ],
90
91            'E7' => [
92                self::LABEL => 'Citation',
93                self::DESC => 'Schema of a Citation',
94            ],
95
96            'E8' => [
97                self::LABEL => 'External RDF',
98                self::DESC => 'Schema of a Citation',
99            ],
100
101            'E9' => [
102                self::LABEL => 'Wikidata-Wikibase',
103                self::DESC => 'Schema linking wikibase and wikidata',
104            ],
105
106            'E40' => [
107                self::LABEL => 'Routes',
108                self::DESC => 'European routes',
109            ],
110
111            'E42' => [
112                self::LABEL => 'author',
113                self::DESC => 'A Schema for authors',
114            ],
115
116            'E123' => [
117                self::LABEL => 'Sandbox Schema',
118                self::DESC => 'An EntitySchema to try things out, not intended for productive use',
119            ],
120
121            'E570' => [
122                self::LABEL => 'recently deceased humans',
123                self::DESC => 'Schema for humans who died recently',
124            ],
125
126            'E734' => [
127                self::LABEL => 'family name',
128                self::DESC => 'basic scheme for family name items',
129            ],
130
131            'E735' => [
132                self::LABEL => 'given name',
133                self::DESC => 'basic schema for given name items',
134            ],
135
136            'E999' => [
137                self::LABEL => 'B0rked',
138                self::DESC => 'Broken schema',
139
140            ],
141
142            'E1234' => [
143                self::LABEL => 'Sandbox Schema',
144                self::DESC => 'An EntitySchema to try things out, not intended for productive use',
145            ],
146
147            'E3300' => [
148                self::LABEL => 'human',
149                self::DESC => 'basic schema for instances of Q5',
150            ],
151
152            'E11424' => [
153                self::LABEL => 'Films',
154                self::DESC => 'basic schema for films',
155            ],
156
157            'E12345' => [
158                self::LABEL => 'Sandbox Schema',
159                self::DESC => 'An EntitySchema to try things out, not intended for productive use',
160            ],
161
162        ];
163    }
164
165    private function createSchema( string $idString, array $dataMap, User $user ): void {
166        $this->output(
167            'Importing Schema with label ' . $dataMap[self::LABEL] . " as EntitySchema $idString... \n"
168        );
169
170        $pageUpdaterFactory = new MediaWikiPageUpdaterFactory( $user );
171
172        $fixedIdGenerator = new FixedIdGenerator( (int)trim( $idString, 'E' ) );
173
174        $services = MediaWikiServices::getInstance();
175        $schemaInserter = new MediaWikiRevisionEntitySchemaInserter(
176            $pageUpdaterFactory,
177            new WatchlistUpdater( $user, NS_ENTITYSCHEMA_JSON ),
178            $fixedIdGenerator,
179            RequestContext::getMain(),
180            $services->getLanguageFactory(),
181            $services->getHookContainer(),
182            $services->getTitleFactory()
183        );
184
185        try {
186            $schemaInserter->insertSchema(
187                'en',
188                $dataMap[self::LABEL] ?? '',
189                $dataMap[self::DESC] ?? '',
190                [],
191                ''
192            );
193        } catch ( RuntimeException $e ) {
194            $this->output(
195                'Failed to save ' . $dataMap[self::LABEL] . " with ID $idString. Moving on... \n"
196            );
197        }
198    }
199
200}
201
202$maintClass = CreatePreexistingSchemas::class;
203require_once RUN_MAINTENANCE_IF_MAIN;