/** * @param string $data Full file contents * @param string $filename Full path to file for debugging * @return string[] Sections indexed by language code, or 0 for header section * @throws MWException */ protected function splitSections($data, $filename = 'unknown') { $data = SimpleFFS::fixNewLines($data); $splitter = '$messages = array();'; $pos = strpos($data, $splitter); if ($pos === false) { throw new MWException("MWEFFS1: File {$filename}: splitter not found"); } $offset = $pos + strlen($splitter); $header = substr($data, 0, $offset); $pattern = '(?: /\\*\\* .*? \\*/ \\n )? (?: \\$.*? \\n\\);(?:\\n\\n|\\s+\\z) )'; $regexp = "~{$pattern}~xsu"; $matches = array(); preg_match_all($regexp, $data, $matches, PREG_SET_ORDER, $offset); $sections = array(); $sections[] = $header; foreach ($matches as $data) { $pattern = "\\\$messages\\['([a-z-]+)'\\]"; $regexp = "~{$pattern}~su"; $matches = array(); if (!preg_match($regexp, $data[0], $matches)) { throw new MWException("MWEFFS2: File {$filename}: malformed section: {$data[0]}"); } $code = $matches[1]; // Normalize number of newlines after each section $sections[$code] = rtrim($data[0]); } return $sections; }
/** * @param $group FileBasedMessageGroup */ public function __construct(FileBasedMessageGroup $group) { parent::__construct($group); if (isset($this->extra['keySeparator'])) { $this->keySeparator = $this->extra['keySeparator']; } }
/** * @param $var * @return array * @throws MWException */ public function parseSections( $var ) { if ( $this->filename === false ) { return array( '', array() ); } $data = file_get_contents( $this->filename ) . "\n"; $data = SimpleFFS::fixNewLines( $data ); $headerP = " .*? # Ungreedily eat header \\$$var \s* = \s* array \s* \( \s* \) \s*;"; /* * x to have nice syntax * u for utf-8 * s for dot matches newline */ $fileStructure = "~^($headerP)(.*)~xsu"; $matches = array(); if ( !preg_match( $fileStructure, $data, $matches ) ) { throw new MWException( "Unable to parse file structure" ); } list( , $header, $data ) = $matches; $sectionP = '(?: /\*\* .*? \*/ )? (?: .*? \n\);\n\n )'; $codeP = "\\$$var\[' (.*?) '\]"; $sectionMatches = array(); if ( !preg_match_all( "~$sectionP~xsu", $data, $sectionMatches, PREG_SET_ORDER ) ) { throw new MWException( "Unable to parse sections" ); } $sections = array(); $unknown = array(); foreach ( $sectionMatches as $data ) { $code = array(); if ( !preg_match( "~$codeP~xsu", $data[0], $code ) ) { echo "Malformed section:\n$data[0]"; $unknown[] = $data[0]; } else { $sections[$code[1]] = $data[0]; } } if ( $unknown ) { $sections[] = implode( "\n", $unknown ); } return array( $header, $sections ); }