Completed
Push — master ( 1e59d7...89bb83 )
by
unknown
02:51
created
maintenance/ImportConstraintEntities.php 1 patch
Spacing   +52 added lines, -52 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,7 +75,7 @@  discard block
 block discarded – undo
75 75
 		$this->entitySerializer = $repo->getAllTypesEntitySerializer();
76 76
 		$this->entityDeserializer = $repo->getInternalFormatEntityDeserializer();
77 77
 		$this->entityStore = $repo->getEntityStore();
78
-		$this->user = User::newSystemUser( 'WikibaseQualityConstraints importer' );
78
+		$this->user = User::newSystemUser('WikibaseQualityConstraints importer');
79 79
 	}
80 80
 
81 81
 	public function execute() {
@@ -83,20 +83,20 @@  discard block
 block discarded – undo
83 83
 
84 84
 		$configUpdates = [];
85 85
 
86
-		$extensionJsonFile = __DIR__ . '/../extension.json';
87
-		$extensionJsonText = file_get_contents( $extensionJsonFile );
88
-		$extensionJson = json_decode( $extensionJsonText, /* assoc = */ true );
89
-		$wikidataEntityIds = $this->getEntitiesToImport( $extensionJson['config'], $this->getConfig() );
86
+		$extensionJsonFile = __DIR__.'/../extension.json';
87
+		$extensionJsonText = file_get_contents($extensionJsonFile);
88
+		$extensionJson = json_decode($extensionJsonText, /* assoc = */ true);
89
+		$wikidataEntityIds = $this->getEntitiesToImport($extensionJson['config'], $this->getConfig());
90 90
 
91
-		foreach ( $wikidataEntityIds as $key => $wikidataEntityId ) {
92
-			$localEntityId = $this->importEntityFromWikidata( $wikidataEntityId );
91
+		foreach ($wikidataEntityIds as $key => $wikidataEntityId) {
92
+			$localEntityId = $this->importEntityFromWikidata($wikidataEntityId);
93 93
 			$configUpdates[$key] = [
94 94
 				'wikidata' => $wikidataEntityId,
95 95
 				'local' => $localEntityId,
96 96
 			];
97 97
 		}
98 98
 
99
-		$this->outputConfigUpdates( $configUpdates );
99
+		$this->outputConfigUpdates($configUpdates);
100 100
 	}
101 101
 
102 102
 	/**
@@ -104,18 +104,18 @@  discard block
 block discarded – undo
104 104
 	 * @param Config $wikiConfig
105 105
 	 * @return string[]
106 106
 	 */
107
-	private function getEntitiesToImport( array $extensionJsonConfig, Config $wikiConfig ) {
107
+	private function getEntitiesToImport(array $extensionJsonConfig, Config $wikiConfig) {
108 108
 		$wikidataEntityIds = [];
109 109
 
110
-		foreach ( $extensionJsonConfig as $key => $value ) {
111
-			if ( !preg_match( '/Id$/', $key ) ) {
110
+		foreach ($extensionJsonConfig as $key => $value) {
111
+			if (!preg_match('/Id$/', $key)) {
112 112
 				continue;
113 113
 			}
114 114
 
115 115
 			$wikidataEntityId = $value['value'];
116
-			$localEntityId = $wikiConfig->get( $key );
116
+			$localEntityId = $wikiConfig->get($key);
117 117
 
118
-			if ( $localEntityId === $wikidataEntityId ) {
118
+			if ($localEntityId === $wikidataEntityId) {
119 119
 				$wikidataEntityIds[$key] = $wikidataEntityId;
120 120
 			}
121 121
 		}
@@ -127,10 +127,10 @@  discard block
 block discarded – undo
127 127
 	 * @param string $wikidataEntityId
128 128
 	 * @return string local entity ID
129 129
 	 */
130
-	private function importEntityFromWikidata( $wikidataEntityId ) {
130
+	private function importEntityFromWikidata($wikidataEntityId) {
131 131
 		$wikidataEntityUrl = "https://www.wikidata.org/wiki/Special:EntityData/$wikidataEntityId.json";
132
-		$wikidataEntitiesJson = file_get_contents( $wikidataEntityUrl );
133
-		return $this->importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson );
132
+		$wikidataEntitiesJson = file_get_contents($wikidataEntityUrl);
133
+		return $this->importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson);
134 134
 	}
135 135
 
136 136
 	/**
@@ -138,23 +138,23 @@  discard block
 block discarded – undo
138 138
 	 * @param string $wikidataEntitiesJson
139 139
 	 * @return string local entity ID
140 140
 	 */
141
-	private function importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson ) {
142
-		$wikidataEntityArray = json_decode( $wikidataEntitiesJson, true )['entities'][$wikidataEntityId];
143
-		$wikidataEntity = $this->entityDeserializer->deserialize( $wikidataEntityArray );
141
+	private function importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson) {
142
+		$wikidataEntityArray = json_decode($wikidataEntitiesJson, true)['entities'][$wikidataEntityId];
143
+		$wikidataEntity = $this->entityDeserializer->deserialize($wikidataEntityArray);
144 144
 
145
-		$wikidataEntity->setId( null );
145
+		$wikidataEntity->setId(null);
146 146
 
147
-		if ( $wikidataEntity instanceof StatementListProvider ) {
147
+		if ($wikidataEntity instanceof StatementListProvider) {
148 148
 			$wikidataEntity->getStatements()->clear();
149 149
 		}
150 150
 
151
-		if ( $wikidataEntity instanceof Item ) {
152
-			$wikidataEntity->setSiteLinkList( new SiteLinkList() );
151
+		if ($wikidataEntity instanceof Item) {
152
+			$wikidataEntity->setSiteLinkList(new SiteLinkList());
153 153
 		}
154 154
 
155
-		if ( $this->getOption( 'dry-run', false ) ) {
156
-			$wikidataEntityJson = json_encode( $this->entitySerializer->serialize( $wikidataEntity ) );
157
-			$this->output( $wikidataEntityJson . "\n" );
155
+		if ($this->getOption('dry-run', false)) {
156
+			$wikidataEntityJson = json_encode($this->entitySerializer->serialize($wikidataEntity));
157
+			$this->output($wikidataEntityJson."\n");
158 158
 			return "-$wikidataEntityId";
159 159
 		}
160 160
 
@@ -167,14 +167,14 @@  discard block
 block discarded – undo
167 167
 			)->getEntity();
168 168
 
169 169
 			return $localEntity->getId()->getSerialization();
170
-		} catch ( StorageException $storageException ) {
170
+		} catch (StorageException $storageException) {
171 171
 			$message = $storageException->getMessage();
172 172
 			// example message:
173 173
 			// * Item [[Item:Q475|Q475]] already has label "as references" associated with language code en, using the same description text.
174 174
 			// note that the label and language code may vary (conflicts in any language),
175 175
 			// and that the item link may or may not be in the main namespace
176 176
 			$pattern = '/[[|]([^]|]*)]] already has label .* using the same description text/';
177
-			if ( preg_match( $pattern, $message, $matches ) ) {
177
+			if (preg_match($pattern, $message, $matches)) {
178 178
 				return $matches[1];
179 179
 			} else {
180 180
 				throw $storageException;
@@ -182,35 +182,35 @@  discard block
 block discarded – undo
182 182
 		}
183 183
 	}
184 184
 
185
-	private function outputConfigUpdates( array $configUpdates ) {
186
-		$configFormat = $this->getOption( 'config-format', 'globals' );
187
-		switch ( $configFormat ) {
185
+	private function outputConfigUpdates(array $configUpdates) {
186
+		$configFormat = $this->getOption('config-format', 'globals');
187
+		switch ($configFormat) {
188 188
 			case 'globals':
189
-				$this->outputConfigUpdatesGlobals( $configUpdates );
189
+				$this->outputConfigUpdatesGlobals($configUpdates);
190 190
 				break;
191 191
 			case 'wgConf':
192
-				$this->outputConfigUpdatesWgConf( $configUpdates );
192
+				$this->outputConfigUpdatesWgConf($configUpdates);
193 193
 				break;
194 194
 			default:
195
-				$this->error( "Invalid config format \"$configFormat\", using \"globals\"" );
196
-				$this->outputConfigUpdatesGlobals( $configUpdates );
195
+				$this->error("Invalid config format \"$configFormat\", using \"globals\"");
196
+				$this->outputConfigUpdatesGlobals($configUpdates);
197 197
 				break;
198 198
 		}
199 199
 	}
200 200
 
201
-	private function outputConfigUpdatesGlobals( array $configUpdates ) {
202
-		foreach ( $configUpdates as $key => $value ) {
203
-			$localValueCode = var_export( $value['local'], true );
204
-			$this->output( "\$wg$key = $localValueCode;\n" );
201
+	private function outputConfigUpdatesGlobals(array $configUpdates) {
202
+		foreach ($configUpdates as $key => $value) {
203
+			$localValueCode = var_export($value['local'], true);
204
+			$this->output("\$wg$key = $localValueCode;\n");
205 205
 		}
206 206
 	}
207 207
 
208
-	private function outputConfigUpdatesWgConf( array $configUpdates ) {
209
-		foreach ( $configUpdates as $key => $value ) {
210
-			$keyCode = var_export( $key, true );
211
-			$wikidataValueCode = var_export( $value['wikidata'], true );
212
-			$localValueCode = var_export( $value['local'], true );
213
-			$wikiIdCode = var_export( wfWikiID(), true );
208
+	private function outputConfigUpdatesWgConf(array $configUpdates) {
209
+		foreach ($configUpdates as $key => $value) {
210
+			$keyCode = var_export($key, true);
211
+			$wikidataValueCode = var_export($value['wikidata'], true);
212
+			$localValueCode = var_export($value['local'], true);
213
+			$wikiIdCode = var_export(wfWikiID(), true);
214 214
 			$block = <<< EOF
215 215
 $keyCode => [
216 216
 	'default' => $wikidataValueCode,
@@ -219,7 +219,7 @@  discard block
 block discarded – undo
219 219
 
220 220
 
221 221
 EOF;
222
-			$this->output( $block );
222
+			$this->output($block);
223 223
 		}
224 224
 	}
225 225
 
Please login to merge, or discard this patch.