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