blob: 986d7b52c399dbcf7cee3c61e109663b6d6b1882 (
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
|
<?php
/**
* @file
* @author Niklas Laxström
* @license GPL-2.0+
*/
/**
* Reads MediaWiki PHP i18n files.
*/
class LU_PHPReader implements LU_Reader {
/// @var string Language tag
protected $code;
public function __construct( $code = null ) {
$this->code = $code;
}
public function parse( $contents ) {
if ( strpos( $contents, '$messages' ) === false ) {
// This happens for some core languages that only have a fallback.
return array();
}
$php = $this->cleanupFile( $contents );
$reader = new QuickArrayReader( "<?php $php" );
$messages = $reader->getVar( 'messages' );
if ( $this->code ) {
return array( $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 = array();
preg_match_all( '/\$messages(?:.*\s)*?\);/', $contents, $results );
// But we want them all in one string.
return implode( "\n\n", $results[0] );
}
}
|