| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Utils; |
| 5 |
|
| 6 |
/** |
| 7 |
* This refactors common code in Api and Mock based config computation |
| 8 |
*/ |
| 9 |
class ConfigUtils { |
| 10 |
/** |
| 11 |
* Compute the interwiki map based on raw data (either manually |
| 12 |
* configured or obtaianed via an API) |
| 13 |
* |
| 14 |
* @param array $iwData |
| 15 |
* @return array |
| 16 |
*/ |
| 17 |
public static function computeInterwikiMap( array $iwData ): array { |
| 18 |
$interwikiMap = []; |
| 19 |
$keys = [ |
| 20 |
'prefix' => true, |
| 21 |
'url' => true, |
| 22 |
'protorel' => true, |
| 23 |
'local' => true, |
| 24 |
'localinterwiki' => true, |
| 25 |
'language' => true, |
| 26 |
'extralanglink' => true, |
| 27 |
'linktext' => true, |
| 28 |
]; |
| 29 |
$cb = static function ( $v ) { |
| 30 |
return $v !== false; |
| 31 |
}; |
| 32 |
foreach ( $iwData as $iwEntry ) { |
| 33 |
$iwEntry['language'] = isset( $iwEntry['language'] ); |
| 34 |
// Fix up broken interwiki hrefs that are missing a $1 placeholder |
| 35 |
// Just append the placeholder at the end. |
| 36 |
// This makes sure that the interwikiMatcher adds one match |
| 37 |
// group per URI, and that interwiki links work as expected. |
| 38 |
|
| 39 |
// Not sure why Phan thinks $iwEntry['url'] is a bool |
| 40 |
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal |
| 41 |
if ( strpos( $iwEntry['url'], '$1' ) === false ) { |
| 42 |
$iwEntry['url'] .= '$1'; |
| 43 |
} |
| 44 |
$iwEntry = array_intersect_key( $iwEntry, $keys ); |
| 45 |
$interwikiMap[$iwEntry['prefix']] = array_filter( $iwEntry, $cb ); |
| 46 |
} |
| 47 |
|
| 48 |
return $interwikiMap; |
| 49 |
} |
| 50 |
} |