Completed
Push — master ( 38fdb2...bd812f )
by
unknown
04:20 queued 02:07
created
maintenance/ImportConstraintEntities.php 1 patch
Spacing   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -15,10 +15,10 @@  discard block
 block discarded – undo
15 15
 use Wikibase\Repo\WikibaseRepo;
16 16
 
17 17
 // @codeCoverageIgnoreStart
18
-$basePath = getenv( "MW_INSTALL_PATH" ) !== false
19
-	? getenv( "MW_INSTALL_PATH" ) : __DIR__ . "/../../..";
18
+$basePath = getenv("MW_INSTALL_PATH") !== false
19
+	? getenv("MW_INSTALL_PATH") : __DIR__."/../../..";
20 20
 
21
-require_once $basePath . "/maintenance/Maintenance.php";
21
+require_once $basePath."/maintenance/Maintenance.php";
22 22
 // @codeCoverageIgnoreEnd
23 23
 
24 24
 /**
@@ -52,13 +52,13 @@  discard block
 block discarded – undo
52 52
 		parent::__construct();
53 53
 
54 54
 		$this->addDescription(
55
-			'Import entities needed for constraint checks ' .
55
+			'Import entities needed for constraint checks '.
56 56
 			'from Wikidata into the local repository.'
57 57
 		);
58 58
 		$this->addOption(
59 59
 			'config-format',
60
-			'The format in which the resulting configuration will be omitted: ' .
61
-			'"globals" for directly settings global variables, suitable for inclusion in LocalSettings.php (default), ' .
60
+			'The format in which the resulting configuration will be omitted: '.
61
+			'"globals" for directly settings global variables, suitable for inclusion in LocalSettings.php (default), '.
62 62
 			'or "wgConf" for printing parts of arrays suitable for inclusion in $wgConf->settings.'
63 63
 		);
64 64
 		$this->addOption(
@@ -75,8 +75,8 @@  discard block
 block discarded – undo
75 75
 		$this->entitySerializer = $repo->getAllTypesEntitySerializer();
76 76
 		$this->entityDeserializer = $repo->getInternalFormatEntityDeserializer();
77 77
 		$this->entityStore = $repo->getEntityStore();
78
-		if ( !$this->getOption( 'dry-run', false ) ) {
79
-			$this->user = User::newSystemUser( 'WikibaseQualityConstraints importer' );
78
+		if (!$this->getOption('dry-run', false)) {
79
+			$this->user = User::newSystemUser('WikibaseQualityConstraints importer');
80 80
 		}
81 81
 	}
82 82
 
@@ -85,20 +85,20 @@  discard block
 block discarded – undo
85 85
 
86 86
 		$configUpdates = [];
87 87
 
88
-		$extensionJsonFile = __DIR__ . '/../extension.json';
89
-		$extensionJsonText = file_get_contents( $extensionJsonFile );
90
-		$extensionJson = json_decode( $extensionJsonText, /* assoc = */ true );
91
-		$wikidataEntityIds = $this->getEntitiesToImport( $extensionJson['config'], $this->getConfig() );
88
+		$extensionJsonFile = __DIR__.'/../extension.json';
89
+		$extensionJsonText = file_get_contents($extensionJsonFile);
90
+		$extensionJson = json_decode($extensionJsonText, /* assoc = */ true);
91
+		$wikidataEntityIds = $this->getEntitiesToImport($extensionJson['config'], $this->getConfig());
92 92
 
93
-		foreach ( $wikidataEntityIds as $key => $wikidataEntityId ) {
94
-			$localEntityId = $this->importEntityFromWikidata( $wikidataEntityId );
93
+		foreach ($wikidataEntityIds as $key => $wikidataEntityId) {
94
+			$localEntityId = $this->importEntityFromWikidata($wikidataEntityId);
95 95
 			$configUpdates[$key] = [
96 96
 				'wikidata' => $wikidataEntityId,
97 97
 				'local' => $localEntityId,
98 98
 			];
99 99
 		}
100 100
 
101
-		$this->outputConfigUpdates( $configUpdates );
101
+		$this->outputConfigUpdates($configUpdates);
102 102
 	}
103 103
 
104 104
 	/**
@@ -106,18 +106,18 @@  discard block
 block discarded – undo
106 106
 	 * @param Config $wikiConfig
107 107
 	 * @return string[]
108 108
 	 */
109
-	private function getEntitiesToImport( array $extensionJsonConfig, Config $wikiConfig ) {
109
+	private function getEntitiesToImport(array $extensionJsonConfig, Config $wikiConfig) {
110 110
 		$wikidataEntityIds = [];
111 111
 
112
-		foreach ( $extensionJsonConfig as $key => $value ) {
113
-			if ( !preg_match( '/Id$/', $key ) ) {
112
+		foreach ($extensionJsonConfig as $key => $value) {
113
+			if (!preg_match('/Id$/', $key)) {
114 114
 				continue;
115 115
 			}
116 116
 
117 117
 			$wikidataEntityId = $value['value'];
118
-			$localEntityId = $wikiConfig->get( $key );
118
+			$localEntityId = $wikiConfig->get($key);
119 119
 
120
-			if ( $localEntityId === $wikidataEntityId ) {
120
+			if ($localEntityId === $wikidataEntityId) {
121 121
 				$wikidataEntityIds[$key] = $wikidataEntityId;
122 122
 			}
123 123
 		}
@@ -129,10 +129,10 @@  discard block
 block discarded – undo
129 129
 	 * @param string $wikidataEntityId
130 130
 	 * @return string local entity ID
131 131
 	 */
132
-	private function importEntityFromWikidata( $wikidataEntityId ) {
132
+	private function importEntityFromWikidata($wikidataEntityId) {
133 133
 		$wikidataEntityUrl = "https://www.wikidata.org/wiki/Special:EntityData/$wikidataEntityId.json";
134
-		$wikidataEntitiesJson = file_get_contents( $wikidataEntityUrl );
135
-		return $this->importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson );
134
+		$wikidataEntitiesJson = file_get_contents($wikidataEntityUrl);
135
+		return $this->importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson);
136 136
 	}
137 137
 
138 138
 	/**
@@ -140,23 +140,23 @@  discard block
 block discarded – undo
140 140
 	 * @param string $wikidataEntitiesJson
141 141
 	 * @return string local entity ID
142 142
 	 */
143
-	private function importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson ) {
144
-		$wikidataEntityArray = json_decode( $wikidataEntitiesJson, true )['entities'][$wikidataEntityId];
145
-		$wikidataEntity = $this->entityDeserializer->deserialize( $wikidataEntityArray );
143
+	private function importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson) {
144
+		$wikidataEntityArray = json_decode($wikidataEntitiesJson, true)['entities'][$wikidataEntityId];
145
+		$wikidataEntity = $this->entityDeserializer->deserialize($wikidataEntityArray);
146 146
 
147
-		$wikidataEntity->setId( null );
147
+		$wikidataEntity->setId(null);
148 148
 
149
-		if ( $wikidataEntity instanceof StatementListProvider ) {
149
+		if ($wikidataEntity instanceof StatementListProvider) {
150 150
 			$wikidataEntity->getStatements()->clear();
151 151
 		}
152 152
 
153
-		if ( $wikidataEntity instanceof Item ) {
154
-			$wikidataEntity->setSiteLinkList( new SiteLinkList() );
153
+		if ($wikidataEntity instanceof Item) {
154
+			$wikidataEntity->setSiteLinkList(new SiteLinkList());
155 155
 		}
156 156
 
157
-		if ( $this->getOption( 'dry-run', false ) ) {
158
-			$wikidataEntityJson = json_encode( $this->entitySerializer->serialize( $wikidataEntity ) );
159
-			$this->output( $wikidataEntityJson . "\n" );
157
+		if ($this->getOption('dry-run', false)) {
158
+			$wikidataEntityJson = json_encode($this->entitySerializer->serialize($wikidataEntity));
159
+			$this->output($wikidataEntityJson."\n");
160 160
 			return "-$wikidataEntityId";
161 161
 		}
162 162
 
@@ -169,54 +169,54 @@  discard block
 block discarded – undo
169 169
 			)->getEntity();
170 170
 
171 171
 			return $localEntity->getId()->getSerialization();
172
-		} catch ( StorageException $storageException ) {
173
-			return $this->storageExceptionToEntityId( $storageException );
172
+		} catch (StorageException $storageException) {
173
+			return $this->storageExceptionToEntityId($storageException);
174 174
 		}
175 175
 	}
176 176
 
177
-	private function storageExceptionToEntityId( StorageException $storageException ) {
177
+	private function storageExceptionToEntityId(StorageException $storageException) {
178 178
 		$message = $storageException->getMessage();
179 179
 		// example messages:
180 180
 		// * Item [[Item:Q475|Q475]] already has label "as references" associated with language code en, using the same description text.
181 181
 		// * Item [[Q475]] already has label "as references" associated with language code en, using the same description text.
182 182
 		// * Property [[Property:P694|P694]] already has label "instance of" associated with language code en.
183 183
 		$pattern = '/[[|]([^][|]*)]] already has label .* associated with language code/';
184
-		if ( preg_match( $pattern, $message, $matches ) ) {
184
+		if (preg_match($pattern, $message, $matches)) {
185 185
 			return $matches[1];
186 186
 		} else {
187 187
 			throw $storageException;
188 188
 		}
189 189
 	}
190 190
 
191
-	private function outputConfigUpdates( array $configUpdates ) {
192
-		$configFormat = $this->getOption( 'config-format', 'globals' );
193
-		switch ( $configFormat ) {
191
+	private function outputConfigUpdates(array $configUpdates) {
192
+		$configFormat = $this->getOption('config-format', 'globals');
193
+		switch ($configFormat) {
194 194
 			case 'globals':
195
-				$this->outputConfigUpdatesGlobals( $configUpdates );
195
+				$this->outputConfigUpdatesGlobals($configUpdates);
196 196
 				break;
197 197
 			case 'wgConf':
198
-				$this->outputConfigUpdatesWgConf( $configUpdates );
198
+				$this->outputConfigUpdatesWgConf($configUpdates);
199 199
 				break;
200 200
 			default:
201
-				$this->error( "Invalid config format \"$configFormat\", using \"globals\"" );
202
-				$this->outputConfigUpdatesGlobals( $configUpdates );
201
+				$this->error("Invalid config format \"$configFormat\", using \"globals\"");
202
+				$this->outputConfigUpdatesGlobals($configUpdates);
203 203
 				break;
204 204
 		}
205 205
 	}
206 206
 
207
-	private function outputConfigUpdatesGlobals( array $configUpdates ) {
208
-		foreach ( $configUpdates as $key => $value ) {
209
-			$localValueCode = var_export( $value['local'], true );
210
-			$this->output( "\$wg$key = $localValueCode;\n" );
207
+	private function outputConfigUpdatesGlobals(array $configUpdates) {
208
+		foreach ($configUpdates as $key => $value) {
209
+			$localValueCode = var_export($value['local'], true);
210
+			$this->output("\$wg$key = $localValueCode;\n");
211 211
 		}
212 212
 	}
213 213
 
214
-	private function outputConfigUpdatesWgConf( array $configUpdates ) {
215
-		foreach ( $configUpdates as $key => $value ) {
216
-			$keyCode = var_export( "wg$key", true );
217
-			$wikidataValueCode = var_export( $value['wikidata'], true );
218
-			$localValueCode = var_export( $value['local'], true );
219
-			$wikiIdCode = var_export( wfWikiID(), true );
214
+	private function outputConfigUpdatesWgConf(array $configUpdates) {
215
+		foreach ($configUpdates as $key => $value) {
216
+			$keyCode = var_export("wg$key", true);
217
+			$wikidataValueCode = var_export($value['wikidata'], true);
218
+			$localValueCode = var_export($value['local'], true);
219
+			$wikiIdCode = var_export(wfWikiID(), true);
220 220
 			$block = <<< EOF
221 221
 $keyCode => [
222 222
 	'default' => $wikidataValueCode,
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
 
226 226
 
227 227
 EOF;
228
-			$this->output( $block );
228
+			$this->output($block);
229 229
 		}
230 230
 	}
231 231
 
Please login to merge, or discard this patch.