Completed
Push — master ( f44503...1def0f )
by
unknown
06:52
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,21 +86,21 @@  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 );
89
+		$extensionJsonFile = __DIR__.'/../extension.json';
90
+		$extensionJsonText = file_get_contents($extensionJsonFile);
91
+		$extensionJson = json_decode($extensionJsonText, /* assoc = */ true);
92 92
 		// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
93
-		$wikidataEntityIds = $this->getEntitiesToImport( $extensionJson['config'], $this->getConfig() );
93
+		$wikidataEntityIds = $this->getEntitiesToImport($extensionJson['config'], $this->getConfig());
94 94
 
95
-		foreach ( $wikidataEntityIds as $key => $wikidataEntityId ) {
96
-			$localEntityId = $this->importEntityFromWikidata( $wikidataEntityId );
95
+		foreach ($wikidataEntityIds as $key => $wikidataEntityId) {
96
+			$localEntityId = $this->importEntityFromWikidata($wikidataEntityId);
97 97
 			$configUpdates[$key] = [
98 98
 				'wikidata' => $wikidataEntityId,
99 99
 				'local' => $localEntityId,
100 100
 			];
101 101
 		}
102 102
 
103
-		$this->outputConfigUpdates( $configUpdates );
103
+		$this->outputConfigUpdates($configUpdates);
104 104
 	}
105 105
 
106 106
 	/**
@@ -108,18 +108,18 @@  discard block
 block discarded – undo
108 108
 	 * @param Config $wikiConfig
109 109
 	 * @return string[]
110 110
 	 */
111
-	private function getEntitiesToImport( array $extensionJsonConfig, Config $wikiConfig ) {
111
+	private function getEntitiesToImport(array $extensionJsonConfig, Config $wikiConfig) {
112 112
 		$wikidataEntityIds = [];
113 113
 
114
-		foreach ( $extensionJsonConfig as $key => $value ) {
115
-			if ( !preg_match( '/Id$/', $key ) ) {
114
+		foreach ($extensionJsonConfig as $key => $value) {
115
+			if (!preg_match('/Id$/', $key)) {
116 116
 				continue;
117 117
 			}
118 118
 
119 119
 			$wikidataEntityId = $value['value'];
120
-			$localEntityId = $wikiConfig->get( $key );
120
+			$localEntityId = $wikiConfig->get($key);
121 121
 
122
-			if ( $localEntityId === $wikidataEntityId ) {
122
+			if ($localEntityId === $wikidataEntityId) {
123 123
 				$wikidataEntityIds[$key] = $wikidataEntityId;
124 124
 			}
125 125
 		}
@@ -131,10 +131,10 @@  discard block
 block discarded – undo
131 131
 	 * @param string $wikidataEntityId
132 132
 	 * @return string local entity ID
133 133
 	 */
134
-	private function importEntityFromWikidata( $wikidataEntityId ) {
134
+	private function importEntityFromWikidata($wikidataEntityId) {
135 135
 		$wikidataEntityUrl = "https://www.wikidata.org/wiki/Special:EntityData/$wikidataEntityId.json";
136
-		$wikidataEntitiesJson = file_get_contents( $wikidataEntityUrl );
137
-		return $this->importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson );
136
+		$wikidataEntitiesJson = file_get_contents($wikidataEntityUrl);
137
+		return $this->importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson);
138 138
 	}
139 139
 
140 140
 	/**
@@ -142,24 +142,24 @@  discard block
 block discarded – undo
142 142
 	 * @param string $wikidataEntitiesJson
143 143
 	 * @return string local entity ID
144 144
 	 */
145
-	private function importEntityFromJson( $wikidataEntityId, $wikidataEntitiesJson ) {
145
+	private function importEntityFromJson($wikidataEntityId, $wikidataEntitiesJson) {
146 146
 		// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
147
-		$wikidataEntityArray = json_decode( $wikidataEntitiesJson, true )['entities'][$wikidataEntityId];
148
-		$wikidataEntity = $this->entityDeserializer->deserialize( $wikidataEntityArray );
147
+		$wikidataEntityArray = json_decode($wikidataEntitiesJson, true)['entities'][$wikidataEntityId];
148
+		$wikidataEntity = $this->entityDeserializer->deserialize($wikidataEntityArray);
149 149
 
150
-		$wikidataEntity->setId( null );
150
+		$wikidataEntity->setId(null);
151 151
 
152
-		if ( $wikidataEntity instanceof StatementListProvider ) {
152
+		if ($wikidataEntity instanceof StatementListProvider) {
153 153
 			$wikidataEntity->getStatements()->clear();
154 154
 		}
155 155
 
156
-		if ( $wikidataEntity instanceof Item ) {
157
-			$wikidataEntity->setSiteLinkList( new SiteLinkList() );
156
+		if ($wikidataEntity instanceof Item) {
157
+			$wikidataEntity->setSiteLinkList(new SiteLinkList());
158 158
 		}
159 159
 
160
-		if ( $this->getOption( 'dry-run', false ) ) {
161
-			$wikidataEntityJson = json_encode( $this->entitySerializer->serialize( $wikidataEntity ) );
162
-			$this->output( $wikidataEntityJson . "\n" );
160
+		if ($this->getOption('dry-run', false)) {
161
+			$wikidataEntityJson = json_encode($this->entitySerializer->serialize($wikidataEntity));
162
+			$this->output($wikidataEntityJson."\n");
163 163
 			return "-$wikidataEntityId";
164 164
 		}
165 165
 
@@ -172,12 +172,12 @@  discard block
 block discarded – undo
172 172
 			)->getEntity();
173 173
 
174 174
 			return $localEntity->getId()->getSerialization();
175
-		} catch ( StorageException $storageException ) {
176
-			return $this->storageExceptionToEntityId( $storageException );
175
+		} catch (StorageException $storageException) {
176
+			return $this->storageExceptionToEntityId($storageException);
177 177
 		}
178 178
 	}
179 179
 
180
-	private function storageExceptionToEntityId( StorageException $storageException ) {
180
+	private function storageExceptionToEntityId(StorageException $storageException) {
181 181
 		$message = $storageException->getMessage();
182 182
 		// example messages:
183 183
 		// * Item [[Item:Q475|Q475]] already has label "as references"
@@ -187,25 +187,25 @@  discard block
 block discarded – undo
187 187
 		// * Property [[Property:P694|P694]] already has label "instance of"
188 188
 		//   associated with language code en.
189 189
 		$pattern = '/[[|]([^][|]*)]] already has label .* associated with language code/';
190
-		if ( preg_match( $pattern, $message, $matches ) ) {
190
+		if (preg_match($pattern, $message, $matches)) {
191 191
 			return $matches[1];
192 192
 		} else {
193 193
 			throw $storageException;
194 194
 		}
195 195
 	}
196 196
 
197
-	private function outputConfigUpdates( array $configUpdates ) {
198
-		$configFormat = $this->getOption( 'config-format', 'globals' );
199
-		switch ( $configFormat ) {
197
+	private function outputConfigUpdates(array $configUpdates) {
198
+		$configFormat = $this->getOption('config-format', 'globals');
199
+		switch ($configFormat) {
200 200
 			case 'globals':
201
-				$this->outputConfigUpdatesGlobals( $configUpdates );
201
+				$this->outputConfigUpdatesGlobals($configUpdates);
202 202
 				break;
203 203
 			case 'wgConf':
204
-				$this->outputConfigUpdatesWgConf( $configUpdates );
204
+				$this->outputConfigUpdatesWgConf($configUpdates);
205 205
 				break;
206 206
 			default:
207
-				$this->error( "Invalid config format \"$configFormat\", using \"globals\"" );
208
-				$this->outputConfigUpdatesGlobals( $configUpdates );
207
+				$this->error("Invalid config format \"$configFormat\", using \"globals\"");
208
+				$this->outputConfigUpdatesGlobals($configUpdates);
209 209
 				break;
210 210
 		}
211 211
 	}
@@ -213,22 +213,22 @@  discard block
 block discarded – undo
213 213
 	/**
214 214
 	 * @param array[] $configUpdates
215 215
 	 */
216
-	private function outputConfigUpdatesGlobals( array $configUpdates ) {
217
-		foreach ( $configUpdates as $key => $value ) {
218
-			$localValueCode = var_export( $value['local'], true );
219
-			$this->output( "\$wg$key = $localValueCode;\n" );
216
+	private function outputConfigUpdatesGlobals(array $configUpdates) {
217
+		foreach ($configUpdates as $key => $value) {
218
+			$localValueCode = var_export($value['local'], true);
219
+			$this->output("\$wg$key = $localValueCode;\n");
220 220
 		}
221 221
 	}
222 222
 
223 223
 	/**
224 224
 	 * @param array[] $configUpdates
225 225
 	 */
226
-	private function outputConfigUpdatesWgConf( array $configUpdates ) {
227
-		foreach ( $configUpdates as $key => $value ) {
228
-			$keyCode = var_export( "wg$key", true );
229
-			$wikidataValueCode = var_export( $value['wikidata'], true );
230
-			$localValueCode = var_export( $value['local'], true );
231
-			$wikiIdCode = var_export( wfWikiID(), true );
226
+	private function outputConfigUpdatesWgConf(array $configUpdates) {
227
+		foreach ($configUpdates as $key => $value) {
228
+			$keyCode = var_export("wg$key", true);
229
+			$wikidataValueCode = var_export($value['wikidata'], true);
230
+			$localValueCode = var_export($value['local'], true);
231
+			$wikiIdCode = var_export(wfWikiID(), true);
232 232
 			$block = <<< EOF
233 233
 $keyCode => [
234 234
 	'default' => $wikidataValueCode,
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
 
238 238
 
239 239
 EOF;
240
-			$this->output( $block );
240
+			$this->output($block);
241 241
 		}
242 242
 	}
243 243
 
Please login to merge, or discard this patch.
src/ConstraintCheck/ItemIdSnakValue.php 1 patch
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 	 * @param ItemId $itemId
45 45
 	 * @return self
46 46
 	 */
47
-	public static function fromItemId( ItemId $itemId ) {
47
+	public static function fromItemId(ItemId $itemId) {
48 48
 		$ret = new self;
49 49
 		$ret->itemId = $itemId;
50 50
 		return $ret;
@@ -80,14 +80,14 @@  discard block
 block discarded – undo
80 80
 	 * @throws InvalidArgumentException
81 81
 	 * @return self
82 82
 	 */
83
-	public static function fromSnak( Snak $snak ) {
84
-		switch ( true ) {
83
+	public static function fromSnak(Snak $snak) {
84
+		switch (true) {
85 85
 			case $snak instanceof PropertyValueSnak:
86 86
 				$dataValue = $snak->getDataValue();
87
-				if ( $dataValue instanceof EntityIdValue ) {
87
+				if ($dataValue instanceof EntityIdValue) {
88 88
 					$itemId = $dataValue->getEntityId();
89
-					if ( $itemId instanceof ItemId ) {
90
-						return self::fromItemId( $itemId );
89
+					if ($itemId instanceof ItemId) {
90
+						return self::fromItemId($itemId);
91 91
 					}
92 92
 				}
93 93
 				break;
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 				return self::noValue();
98 98
 		}
99 99
 
100
-		throw new InvalidArgumentException( 'Snak must contain item ID value or be a somevalue / novalue snak' );
100
+		throw new InvalidArgumentException('Snak must contain item ID value or be a somevalue / novalue snak');
101 101
 	}
102 102
 
103 103
 	/**
@@ -135,8 +135,8 @@  discard block
 block discarded – undo
135 135
 	 * @return ItemId
136 136
 	 */
137 137
 	public function getItemId() {
138
-		if ( !$this->isValue() ) {
139
-			throw new DomainException( 'This value does not contain an item ID.' );
138
+		if (!$this->isValue()) {
139
+			throw new DomainException('This value does not contain an item ID.');
140 140
 		}
141 141
 		return $this->itemId;
142 142
 	}
@@ -148,14 +148,14 @@  discard block
 block discarded – undo
148 148
 	 * @param Snak $snak
149 149
 	 * @return bool
150 150
 	 */
151
-	public function matchesSnak( Snak $snak ) {
152
-		switch ( true ) {
151
+	public function matchesSnak(Snak $snak) {
152
+		switch (true) {
153 153
 			case $snak instanceof PropertyValueSnak:
154 154
 				$dataValue = $snak->getDataValue();
155 155
 				return $this->isValue() &&
156 156
 					$dataValue instanceof EntityIdValue &&
157 157
 					$dataValue->getEntityId() instanceof ItemId &&
158
-					$dataValue->getEntityId()->equals( $this->getItemId() );
158
+					$dataValue->getEntityId()->equals($this->getItemId());
159 159
 			case $snak instanceof PropertySomeValueSnak:
160 160
 				return $this->isSomeValue();
161 161
 			case $snak instanceof PropertyNoValueSnak:
Please login to merge, or discard this patch.