Completed
Push — master ( 51aa7f...a0da98 )
by
unknown
02:47 queued 12s
created
maintenance/ImportConstraintEntities.php 1 patch
Spacing   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -17,10 +17,10 @@  discard block
 block discarded – undo
17 17
 use Wikibase\Repo\WikibaseRepo;
18 18
 
19 19
 // @codeCoverageIgnoreStart
20
-$basePath = getenv( "MW_INSTALL_PATH" ) !== false
21
-	? getenv( "MW_INSTALL_PATH" ) : __DIR__ . "/../../..";
20
+$basePath = getenv("MW_INSTALL_PATH") !== false
21
+	? getenv("MW_INSTALL_PATH") : __DIR__."/../../..";
22 22
 
23
-require_once $basePath . "/maintenance/Maintenance.php";
23
+require_once $basePath."/maintenance/Maintenance.php";
24 24
 // @codeCoverageIgnoreEnd
25 25
 
26 26
 /**
@@ -59,20 +59,20 @@  discard block
 block discarded – undo
59 59
 		parent::__construct();
60 60
 
61 61
 		$this->addDescription(
62
-			'Import entities needed for constraint checks ' .
62
+			'Import entities needed for constraint checks '.
63 63
 			'from Wikidata into the local repository.'
64 64
 		);
65 65
 		$this->addOption(
66 66
 			'config-format',
67
-			'The format in which the resulting configuration will be omitted: ' .
68
-			'"globals" for directly settings global variables, suitable for inclusion in LocalSettings.php (default), ' .
67
+			'The format in which the resulting configuration will be omitted: '.
68
+			'"globals" for directly settings global variables, suitable for inclusion in LocalSettings.php (default), '.
69 69
 			'or "wgConf" for printing parts of arrays suitable for inclusion in $wgConf->settings.'
70 70
 		);
71 71
 		$this->addOption(
72 72
 			'dry-run',
73 73
 			'Don’t actually import entities, just print which ones would be imported.'
74 74
 		);
75
-		$this->requireExtension( 'WikibaseQualityConstraints' );
75
+		$this->requireExtension('WikibaseQualityConstraints');
76 76
 	}
77 77
 
78 78
 	/**
@@ -80,12 +80,12 @@  discard block
 block discarded – undo
80 80
 	 */
81 81
 	private function setupServices() {
82 82
 		$services = MediaWikiServices::getInstance();
83
-		$this->entitySerializer = WikibaseRepo::getAllTypesEntitySerializer( $services );
84
-		$this->entityDeserializer = WikibaseRepo::getInternalFormatEntityDeserializer( $services );
85
-		$this->entityStore = WikibaseRepo::getEntityStore( $services );
83
+		$this->entitySerializer = WikibaseRepo::getAllTypesEntitySerializer($services);
84
+		$this->entityDeserializer = WikibaseRepo::getInternalFormatEntityDeserializer($services);
85
+		$this->entityStore = WikibaseRepo::getEntityStore($services);
86 86
 		$this->httpRequestFactory = $services->getHttpRequestFactory();
87
-		if ( !$this->getOption( 'dry-run', false ) ) {
88
-			$this->user = User::newSystemUser( 'WikibaseQualityConstraints importer' );
87
+		if (!$this->getOption('dry-run', false)) {
88
+			$this->user = User::newSystemUser('WikibaseQualityConstraints importer');
89 89
 		}
90 90
 	}
91 91
 
@@ -94,21 +94,21 @@  discard block
 block discarded – undo
94 94
 
95 95
 		$configUpdates = [];
96 96
 
97
-		$extensionJsonFile = __DIR__ . '/../extension.json';
98
-		$extensionJsonText = file_get_contents( $extensionJsonFile );
99
-		$extensionJson = json_decode( $extensionJsonText, /* assoc = */ true );
97
+		$extensionJsonFile = __DIR__.'/../extension.json';
98
+		$extensionJsonText = file_get_contents($extensionJsonFile);
99
+		$extensionJson = json_decode($extensionJsonText, /* assoc = */ true);
100 100
 		// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
101
-		$wikidataEntityIds = $this->getEntitiesToImport( $extensionJson['config'], $this->getConfig() );
101
+		$wikidataEntityIds = $this->getEntitiesToImport($extensionJson['config'], $this->getConfig());
102 102
 
103
-		foreach ( $wikidataEntityIds as $key => $wikidataEntityId ) {
104
-			$localEntityId = $this->importEntityFromWikidata( $wikidataEntityId );
103
+		foreach ($wikidataEntityIds as $key => $wikidataEntityId) {
104
+			$localEntityId = $this->importEntityFromWikidata($wikidataEntityId);
105 105
 			$configUpdates[$key] = [
106 106
 				'wikidata' => $wikidataEntityId,
107 107
 				'local' => $localEntityId,
108 108
 			];
109 109
 		}
110 110
 
111
-		$this->outputConfigUpdates( $configUpdates );
111
+		$this->outputConfigUpdates($configUpdates);
112 112
 	}
113 113
 
114 114
 	/**
@@ -116,18 +116,18 @@  discard block
 block discarded – undo
116 116
 	 * @param Config $wikiConfig
117 117
 	 * @return string[]
118 118
 	 */
119
-	private function getEntitiesToImport( array $extensionJsonConfig, Config $wikiConfig ) {
119
+	private function getEntitiesToImport(array $extensionJsonConfig, Config $wikiConfig) {
120 120
 		$wikidataEntityIds = [];
121 121
 
122
-		foreach ( $extensionJsonConfig as $key => $value ) {
123
-			if ( !preg_match( '/Id$/', $key ) ) {
122
+		foreach ($extensionJsonConfig as $key => $value) {
123
+			if (!preg_match('/Id$/', $key)) {
124 124
 				continue;
125 125
 			}
126 126
 
127 127
 			$wikidataEntityId = $value['value'];
128
-			$localEntityId = $wikiConfig->get( $key );
128
+			$localEntityId = $wikiConfig->get($key);
129 129
 
130
-			if ( $localEntityId === $wikidataEntityId ) {
130
+			if ($localEntityId === $wikidataEntityId) {
131 131
 				$wikidataEntityIds[$key] = $wikidataEntityId;
132 132
 			}
133 133
 		}
@@ -139,10 +139,10 @@  discard block
 block discarded – undo
139 139
 	 * @param string $wikidataEntityId
140 140
 	 * @return string local entity ID
141 141
 	 */
142
-	private function importEntityFromWikidata( $wikidataEntityId ) {
142
+	private function importEntityFromWikidata($wikidataEntityId) {
143 143
 		$wikidataEntityUrl = "https://www.wikidata.org/wiki/Special:EntityData/$wikidataEntityId.json";
144
-		$wikidataEntitiesJson = $this->httpRequestFactory->get( $wikidataEntityUrl, [], __METHOD__ );
145
-		return $this->importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson );
144
+		$wikidataEntitiesJson = $this->httpRequestFactory->get($wikidataEntityUrl, [], __METHOD__);
145
+		return $this->importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson);
146 146
 	}
147 147
 
148 148
 	/**
@@ -150,24 +150,24 @@  discard block
 block discarded – undo
150 150
 	 * @param string $wikidataEntitiesJson
151 151
 	 * @return string local entity ID
152 152
 	 */
153
-	private function importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson ) {
153
+	private function importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson) {
154 154
 		// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
155
-		$wikidataEntityArray = json_decode( $wikidataEntitiesJson, true )['entities'][$wikidataEntityId];
156
-		$wikidataEntity = $this->entityDeserializer->deserialize( $wikidataEntityArray );
155
+		$wikidataEntityArray = json_decode($wikidataEntitiesJson, true)['entities'][$wikidataEntityId];
156
+		$wikidataEntity = $this->entityDeserializer->deserialize($wikidataEntityArray);
157 157
 
158
-		$wikidataEntity->setId( null );
158
+		$wikidataEntity->setId(null);
159 159
 
160
-		if ( $wikidataEntity instanceof StatementListProvider ) {
160
+		if ($wikidataEntity instanceof StatementListProvider) {
161 161
 			$wikidataEntity->getStatements()->clear();
162 162
 		}
163 163
 
164
-		if ( $wikidataEntity instanceof Item ) {
165
-			$wikidataEntity->setSiteLinkList( new SiteLinkList() );
164
+		if ($wikidataEntity instanceof Item) {
165
+			$wikidataEntity->setSiteLinkList(new SiteLinkList());
166 166
 		}
167 167
 
168
-		if ( $this->getOption( 'dry-run', false ) ) {
169
-			$wikidataEntityJson = json_encode( $this->entitySerializer->serialize( $wikidataEntity ) );
170
-			$this->output( $wikidataEntityJson . "\n" );
168
+		if ($this->getOption('dry-run', false)) {
169
+			$wikidataEntityJson = json_encode($this->entitySerializer->serialize($wikidataEntity));
170
+			$this->output($wikidataEntityJson."\n");
171 171
 			return "-$wikidataEntityId";
172 172
 		}
173 173
 
@@ -180,12 +180,12 @@  discard block
 block discarded – undo
180 180
 			)->getEntity();
181 181
 
182 182
 			return $localEntity->getId()->getSerialization();
183
-		} catch ( StorageException $storageException ) {
184
-			return $this->storageExceptionToEntityId( $storageException );
183
+		} catch (StorageException $storageException) {
184
+			return $this->storageExceptionToEntityId($storageException);
185 185
 		}
186 186
 	}
187 187
 
188
-	private function storageExceptionToEntityId( StorageException $storageException ) {
188
+	private function storageExceptionToEntityId(StorageException $storageException) {
189 189
 		$message = $storageException->getMessage();
190 190
 		// example messages:
191 191
 		// * Item [[Item:Q475|Q475]] already has label "as references"
@@ -195,25 +195,25 @@  discard block
 block discarded – undo
195 195
 		// * Property [[Property:P694|P694]] already has label "instance of"
196 196
 		//   associated with language code en.
197 197
 		$pattern = '/[[|]([^][|]*)]] already has label .* associated with language code/';
198
-		if ( preg_match( $pattern, $message, $matches ) ) {
198
+		if (preg_match($pattern, $message, $matches)) {
199 199
 			return $matches[1];
200 200
 		} else {
201 201
 			throw $storageException;
202 202
 		}
203 203
 	}
204 204
 
205
-	private function outputConfigUpdates( array $configUpdates ) {
206
-		$configFormat = $this->getOption( 'config-format', 'globals' );
207
-		switch ( $configFormat ) {
205
+	private function outputConfigUpdates(array $configUpdates) {
206
+		$configFormat = $this->getOption('config-format', 'globals');
207
+		switch ($configFormat) {
208 208
 			case 'globals':
209
-				$this->outputConfigUpdatesGlobals( $configUpdates );
209
+				$this->outputConfigUpdatesGlobals($configUpdates);
210 210
 				break;
211 211
 			case 'wgConf':
212
-				$this->outputConfigUpdatesWgConf( $configUpdates );
212
+				$this->outputConfigUpdatesWgConf($configUpdates);
213 213
 				break;
214 214
 			default:
215
-				$this->error( "Invalid config format \"$configFormat\", using \"globals\"" );
216
-				$this->outputConfigUpdatesGlobals( $configUpdates );
215
+				$this->error("Invalid config format \"$configFormat\", using \"globals\"");
216
+				$this->outputConfigUpdatesGlobals($configUpdates);
217 217
 				break;
218 218
 		}
219 219
 	}
@@ -221,22 +221,22 @@  discard block
 block discarded – undo
221 221
 	/**
222 222
 	 * @param array[] $configUpdates
223 223
 	 */
224
-	private function outputConfigUpdatesGlobals( array $configUpdates ) {
225
-		foreach ( $configUpdates as $key => $value ) {
226
-			$localValueCode = var_export( $value['local'], true );
227
-			$this->output( "\$wg$key = $localValueCode;\n" );
224
+	private function outputConfigUpdatesGlobals(array $configUpdates) {
225
+		foreach ($configUpdates as $key => $value) {
226
+			$localValueCode = var_export($value['local'], true);
227
+			$this->output("\$wg$key = $localValueCode;\n");
228 228
 		}
229 229
 	}
230 230
 
231 231
 	/**
232 232
 	 * @param array[] $configUpdates
233 233
 	 */
234
-	private function outputConfigUpdatesWgConf( array $configUpdates ) {
235
-		foreach ( $configUpdates as $key => $value ) {
236
-			$keyCode = var_export( "wg$key", true );
237
-			$wikidataValueCode = var_export( $value['wikidata'], true );
238
-			$localValueCode = var_export( $value['local'], true );
239
-			$wikiIdCode = var_export( wfWikiID(), true );
234
+	private function outputConfigUpdatesWgConf(array $configUpdates) {
235
+		foreach ($configUpdates as $key => $value) {
236
+			$keyCode = var_export("wg$key", true);
237
+			$wikidataValueCode = var_export($value['wikidata'], true);
238
+			$localValueCode = var_export($value['local'], true);
239
+			$wikiIdCode = var_export(wfWikiID(), true);
240 240
 			$block = <<< EOF
241 241
 $keyCode => [
242 242
 	'default' => $wikidataValueCode,
@@ -245,7 +245,7 @@  discard block
 block discarded – undo
245 245
 
246 246
 
247 247
 EOF;
248
-			$this->output( $block );
248
+			$this->output($block);
249 249
 		}
250 250
 	}
251 251
 
Please login to merge, or discard this patch.