Completed
Push — master ( 0bbafa...cd7720 )
by
unknown
01:41
created
maintenance/ImportConstraintEntities.php 1 patch
Spacing   +56 added lines, -56 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,20 +52,20 @@  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(
65 65
 			'dry-run',
66 66
 			'Don’t actually import entities, just print which ones would be imported.'
67 67
 		);
68
-		$this->requireExtension( 'WikibaseQualityConstraints' );
68
+		$this->requireExtension('WikibaseQualityConstraints');
69 69
 	}
70 70
 
71 71
 	/**
@@ -76,8 +76,8 @@  discard block
 block discarded – undo
76 76
 		$this->entitySerializer = $repo->getAllTypesEntitySerializer();
77 77
 		$this->entityDeserializer = $repo->getInternalFormatEntityDeserializer();
78 78
 		$this->entityStore = $repo->getEntityStore();
79
-		if ( !$this->getOption( 'dry-run', false ) ) {
80
-			$this->user = User::newSystemUser( 'WikibaseQualityConstraints importer' );
79
+		if (!$this->getOption('dry-run', false)) {
80
+			$this->user = User::newSystemUser('WikibaseQualityConstraints importer');
81 81
 		}
82 82
 	}
83 83
 
@@ -86,20 +86,20 @@  discard block
 block discarded – undo
86 86
 
87 87
 		$configUpdates = [];
88 88
 
89
-		$extensionJsonFile = __DIR__ . '/../extension.json';
90
-		$extensionJsonText = file_get_contents( $extensionJsonFile );
91
-		$extensionJson = json_decode( $extensionJsonText, /* assoc = */ true );
92
-		$wikidataEntityIds = $this->getEntitiesToImport( $extensionJson['config'], $this->getConfig() );
89
+		$extensionJsonFile = __DIR__.'/../extension.json';
90
+		$extensionJsonText = file_get_contents($extensionJsonFile);
91
+		$extensionJson = json_decode($extensionJsonText, /* assoc = */ true);
92
+		$wikidataEntityIds = $this->getEntitiesToImport($extensionJson['config'], $this->getConfig());
93 93
 
94
-		foreach ( $wikidataEntityIds as $key => $wikidataEntityId ) {
95
-			$localEntityId = $this->importEntityFromWikidata( $wikidataEntityId );
94
+		foreach ($wikidataEntityIds as $key => $wikidataEntityId) {
95
+			$localEntityId = $this->importEntityFromWikidata($wikidataEntityId);
96 96
 			$configUpdates[$key] = [
97 97
 				'wikidata' => $wikidataEntityId,
98 98
 				'local' => $localEntityId,
99 99
 			];
100 100
 		}
101 101
 
102
-		$this->outputConfigUpdates( $configUpdates );
102
+		$this->outputConfigUpdates($configUpdates);
103 103
 	}
104 104
 
105 105
 	/**
@@ -107,18 +107,18 @@  discard block
 block discarded – undo
107 107
 	 * @param Config $wikiConfig
108 108
 	 * @return string[]
109 109
 	 */
110
-	private function getEntitiesToImport( array $extensionJsonConfig, Config $wikiConfig ) {
110
+	private function getEntitiesToImport(array $extensionJsonConfig, Config $wikiConfig) {
111 111
 		$wikidataEntityIds = [];
112 112
 
113
-		foreach ( $extensionJsonConfig as $key => $value ) {
114
-			if ( !preg_match( '/Id$/', $key ) ) {
113
+		foreach ($extensionJsonConfig as $key => $value) {
114
+			if (!preg_match('/Id$/', $key)) {
115 115
 				continue;
116 116
 			}
117 117
 
118 118
 			$wikidataEntityId = $value['value'];
119
-			$localEntityId = $wikiConfig->get( $key );
119
+			$localEntityId = $wikiConfig->get($key);
120 120
 
121
-			if ( $localEntityId === $wikidataEntityId ) {
121
+			if ($localEntityId === $wikidataEntityId) {
122 122
 				$wikidataEntityIds[$key] = $wikidataEntityId;
123 123
 			}
124 124
 		}
@@ -130,10 +130,10 @@  discard block
 block discarded – undo
130 130
 	 * @param string $wikidataEntityId
131 131
 	 * @return string local entity ID
132 132
 	 */
133
-	private function importEntityFromWikidata( $wikidataEntityId ) {
133
+	private function importEntityFromWikidata($wikidataEntityId) {
134 134
 		$wikidataEntityUrl = "https://www.wikidata.org/wiki/Special:EntityData/$wikidataEntityId.json";
135
-		$wikidataEntitiesJson = file_get_contents( $wikidataEntityUrl );
136
-		return $this->importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson );
135
+		$wikidataEntitiesJson = file_get_contents($wikidataEntityUrl);
136
+		return $this->importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson);
137 137
 	}
138 138
 
139 139
 	/**
@@ -141,23 +141,23 @@  discard block
 block discarded – undo
141 141
 	 * @param string $wikidataEntitiesJson
142 142
 	 * @return string local entity ID
143 143
 	 */
144
-	private function importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson ) {
145
-		$wikidataEntityArray = json_decode( $wikidataEntitiesJson, true )['entities'][$wikidataEntityId];
146
-		$wikidataEntity = $this->entityDeserializer->deserialize( $wikidataEntityArray );
144
+	private function importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson) {
145
+		$wikidataEntityArray = json_decode($wikidataEntitiesJson, true)['entities'][$wikidataEntityId];
146
+		$wikidataEntity = $this->entityDeserializer->deserialize($wikidataEntityArray);
147 147
 
148
-		$wikidataEntity->setId( null );
148
+		$wikidataEntity->setId(null);
149 149
 
150
-		if ( $wikidataEntity instanceof StatementListProvider ) {
150
+		if ($wikidataEntity instanceof StatementListProvider) {
151 151
 			$wikidataEntity->getStatements()->clear();
152 152
 		}
153 153
 
154
-		if ( $wikidataEntity instanceof Item ) {
155
-			$wikidataEntity->setSiteLinkList( new SiteLinkList() );
154
+		if ($wikidataEntity instanceof Item) {
155
+			$wikidataEntity->setSiteLinkList(new SiteLinkList());
156 156
 		}
157 157
 
158
-		if ( $this->getOption( 'dry-run', false ) ) {
159
-			$wikidataEntityJson = json_encode( $this->entitySerializer->serialize( $wikidataEntity ) );
160
-			$this->output( $wikidataEntityJson . "\n" );
158
+		if ($this->getOption('dry-run', false)) {
159
+			$wikidataEntityJson = json_encode($this->entitySerializer->serialize($wikidataEntity));
160
+			$this->output($wikidataEntityJson."\n");
161 161
 			return "-$wikidataEntityId";
162 162
 		}
163 163
 
@@ -170,12 +170,12 @@  discard block
 block discarded – undo
170 170
 			)->getEntity();
171 171
 
172 172
 			return $localEntity->getId()->getSerialization();
173
-		} catch ( StorageException $storageException ) {
174
-			return $this->storageExceptionToEntityId( $storageException );
173
+		} catch (StorageException $storageException) {
174
+			return $this->storageExceptionToEntityId($storageException);
175 175
 		}
176 176
 	}
177 177
 
178
-	private function storageExceptionToEntityId( StorageException $storageException ) {
178
+	private function storageExceptionToEntityId(StorageException $storageException) {
179 179
 		$message = $storageException->getMessage();
180 180
 		// example messages:
181 181
 		// * Item [[Item:Q475|Q475]] already has label "as references"
@@ -185,42 +185,42 @@  discard block
 block discarded – undo
185 185
 		// * Property [[Property:P694|P694]] already has label "instance of"
186 186
 		//   associated with language code en.
187 187
 		$pattern = '/[[|]([^][|]*)]] already has label .* associated with language code/';
188
-		if ( preg_match( $pattern, $message, $matches ) ) {
188
+		if (preg_match($pattern, $message, $matches)) {
189 189
 			return $matches[1];
190 190
 		} else {
191 191
 			throw $storageException;
192 192
 		}
193 193
 	}
194 194
 
195
-	private function outputConfigUpdates( array $configUpdates ) {
196
-		$configFormat = $this->getOption( 'config-format', 'globals' );
197
-		switch ( $configFormat ) {
195
+	private function outputConfigUpdates(array $configUpdates) {
196
+		$configFormat = $this->getOption('config-format', 'globals');
197
+		switch ($configFormat) {
198 198
 			case 'globals':
199
-				$this->outputConfigUpdatesGlobals( $configUpdates );
199
+				$this->outputConfigUpdatesGlobals($configUpdates);
200 200
 				break;
201 201
 			case 'wgConf':
202
-				$this->outputConfigUpdatesWgConf( $configUpdates );
202
+				$this->outputConfigUpdatesWgConf($configUpdates);
203 203
 				break;
204 204
 			default:
205
-				$this->error( "Invalid config format \"$configFormat\", using \"globals\"" );
206
-				$this->outputConfigUpdatesGlobals( $configUpdates );
205
+				$this->error("Invalid config format \"$configFormat\", using \"globals\"");
206
+				$this->outputConfigUpdatesGlobals($configUpdates);
207 207
 				break;
208 208
 		}
209 209
 	}
210 210
 
211
-	private function outputConfigUpdatesGlobals( array $configUpdates ) {
212
-		foreach ( $configUpdates as $key => $value ) {
213
-			$localValueCode = var_export( $value['local'], true );
214
-			$this->output( "\$wg$key = $localValueCode;\n" );
211
+	private function outputConfigUpdatesGlobals(array $configUpdates) {
212
+		foreach ($configUpdates as $key => $value) {
213
+			$localValueCode = var_export($value['local'], true);
214
+			$this->output("\$wg$key = $localValueCode;\n");
215 215
 		}
216 216
 	}
217 217
 
218
-	private function outputConfigUpdatesWgConf( array $configUpdates ) {
219
-		foreach ( $configUpdates as $key => $value ) {
220
-			$keyCode = var_export( "wg$key", true );
221
-			$wikidataValueCode = var_export( $value['wikidata'], true );
222
-			$localValueCode = var_export( $value['local'], true );
223
-			$wikiIdCode = var_export( wfWikiID(), true );
218
+	private function outputConfigUpdatesWgConf(array $configUpdates) {
219
+		foreach ($configUpdates as $key => $value) {
220
+			$keyCode = var_export("wg$key", true);
221
+			$wikidataValueCode = var_export($value['wikidata'], true);
222
+			$localValueCode = var_export($value['local'], true);
223
+			$wikiIdCode = var_export(wfWikiID(), true);
224 224
 			$block = <<< EOF
225 225
 $keyCode => [
226 226
 	'default' => $wikidataValueCode,
@@ -229,7 +229,7 @@  discard block
 block discarded – undo
229 229
 
230 230
 
231 231
 EOF;
232
-			$this->output( $block );
232
+			$this->output($block);
233 233
 		}
234 234
 	}
235 235
 
Please login to merge, or discard this patch.