blob: 43e4db25ab5e398cc8e0981cbe0f113fbad2c1cf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<?php
/**
* @file
* @author Niklas Laxström
* @license GPL-2.0-or-later
*/
namespace LocalisationUpdate;
/**
* Reads MediaWiki PHP i18n files.
*/
class PHPReader implements Reader {
/// @var string Language tag
protected $code;
public function __construct( $code = null ) {
$this->code = $code;
}
/**
* @param string $contents
*
* @return array
*/
public function parse( $contents ) {
if ( strpos( $contents, '$messages' ) === false ) {
// This happens for some core languages that only have a fallback.
return [];
}
$php = $this->cleanupFile( $contents );
$reader = new \QuickArrayReader( "<?php $php" );
$messages = $reader->getVar( 'messages' );
if ( $this->code ) {
return [ $this->code => $messages ];
}
// Assuming that the array is keyed by language codes
return $messages;
}
/**
* Removes all unneeded content from a file and returns it.
*
* @param string $contents String
* @return string PHP code without PHP tags
*/
protected function cleanupFile( $contents ) {
// We hate the windows vs linux linebreaks.
$contents = preg_replace( '/\r\n?/', "\n", $contents );
// We only want message arrays.
$results = [];
preg_match_all( '/\$messages(?:.*\s)*?\);/', $contents, $results );
// But we want them all in one string.
return implode( "\n\n", $results[0] );
}
}
|