r40552 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r40551‎ | r40552 | r40553 >
Date:22:07, 6 September 2008
Author:siebrand
Status:old
Tags:
Comment:
Modified paths:
  • /trunk/extensions/Translate/SpecialTranslationChanges.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/ffs/PhpVariables.php (added) (history)
  • /trunk/extensions/Translate/groups/Mantis.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/groups/Mantis.php
@@ -0,0 +1,74 @@
 2+<?php
 3+
 4+class MantisMessageGroup extends MessageGroup {
 5+ protected $label = 'Mantis (web-based bugtracking system)';
 6+ protected $id = 'out-mantis';
 7+ protected $type = 'mantis';
 8+
 9+ protected $fileDir = '__BUG__';
 10+
 11+ public function getPath() { return $this->fileDir; }
 12+ public function setPath( $value ) { $this->fileDir = $value; }
 13+
 14+ protected $codeMap = array(
 15+ 'bg' => 'bulgarian',
 16+ 'ca' => 'catalan',
 17+ 'cs' => 'czech',
 18+ 'da' => 'danish',
 19+ 'de' => 'german',
 20+ 'el' => 'greek',
 21+ 'en' => 'english',
 22+ 'es' => 'spanish',
 23+ 'et' => 'estonian',
 24+ 'fi' => 'finnish',
 25+ 'fr' => 'french',
 26+ 'he' => 'hebrew',
 27+ 'hr' => 'croatian',
 28+ 'hu' => 'hungarian',
 29+ 'is' => 'icelandic',
 30+ 'it' => 'italian',
 31+ 'ja' => 'japanese',
 32+ 'ko' => 'korean',
 33+ 'lt' => 'lithuanian',
 34+ 'lv' => 'latvian',
 35+ 'nl' => 'dutch',
 36+ 'no' => 'norwegian',
 37+ 'pl' => 'polish',
 38+ 'pt' => 'portuguese_standard',
 39+ 'pt-br' => 'portuguese_brazil',
 40+ 'ro' => 'romanian',
 41+ 'ru' => 'russian',
 42+ 'sk' => 'slovak',
 43+ 'sl' => 'slovene',
 44+ 'sr-ec' => 'serbian',
 45+ 'sv' => 'swedish',
 46+ 'tr' => 'turkish',
 47+ 'uk' => 'ukrainian',
 48+ 'ur' => 'urdu',
 49+ 'zh-hans' => 'chinese_simplified',
 50+ 'zh-hant' => 'chinese_traditional',
 51+ );
 52+
 53+ protected $optional = array(
 54+ 's_sponsorship_process_url',
 55+ );
 56+
 57+ public function getMessageFile( $code ) {
 58+ if ( isset($this->codeMap[$code]) ) {
 59+ $code = $this->codeMap[$code];
 60+ }
 61+ return "strings_$code.txt";
 62+ }
 63+
 64+ protected function getFileLocation( $code ) {
 65+ return $this->fileDir . '/' . $this->getMessageFile( $code );
 66+ }
 67+
 68+ public function getReader( $code ) {
 69+ return new PhpVariablesFormatReader( $this->getFileLocation( $code ) );
 70+ }
 71+
 72+ public function getWriter() {
 73+ return new PhpVariablesFormatWriter( $this );
 74+ }
 75+}
Property changes on: trunk/extensions/Translate/groups/Mantis.php
___________________________________________________________________
Added: svn:eol-style
176 + native
Added: svn:keywords
277 + Id
Index: trunk/extensions/Translate/_autoload.php
@@ -46,6 +46,8 @@
4747 $wgAutoloadClasses['GettextFormatWriter'] = $dir . 'ffs/Gettext.php';
4848 $wgAutoloadClasses['JavaFormatReader'] = $dir . 'ffs/Java.php';
4949 $wgAutoloadClasses['JavaFormatWriter'] = $dir . 'ffs/Java.php';
 50+$wgAutoloadClasses['PhpVariablesFormatReader'] = $dir . 'ffs/PhpVariables.php';
 51+$wgAutoloadClasses['PhpVariablesFormatWriter'] = $dir . 'ffs/PhpVariables.php';
5052 $wgAutoloadClasses['XliffFormatWriter'] = $dir . 'ffs/Xliff.php';
5153
5254 # utils
@@ -64,6 +66,7 @@
6567 # predefined groups
6668 $wgAutoloadClasses['PremadeMediawikiExtensionGroups'] = $dir . 'groups/MediaWikiExtensions.php';
6769 $wgAutoloadClasses['FreeColMessageGroup'] = $dir . 'groups/FreeCol.php';
 70+$wgAutoloadClasses['MantisMessageGroup'] = $dir . 'groups/Mantis.php';
6871
6972 # tag
7073 $wgAutoloadClasses['TranslateTag'] = $dir . 'tag/Tag.php';
Index: trunk/extensions/Translate/SpecialTranslationChanges.php
@@ -77,8 +77,11 @@
7878 }
7979
8080 switch ($group) {
81 - case 'core': $class = 'core'; break;
 81+ case 'core': $class = 'mediawiki'; break;
8282 case 'out-freecol': $class = 'freecol'; break;
 83+ case 'out-mantis': $class = 'mantis'; break;
 84+ case 'out-voctrain': $class = 'voctrain'; break;
 85+ case 'out-zabbix': $class = 'zabbix'; break;
8386 default: $class = 'extension'; break;
8487 }
8588
Index: trunk/extensions/Translate/ffs/PhpVariables.php
@@ -0,0 +1,138 @@
 2+<?php
 3+/**
 4+ * PHP variables file format handler.
 5+ *
 6+ * @author Niklas Laxström
 7+ * @author Siebrand Mazeland
 8+ * @copyright Copyright © 2008, Niklas Laxström, Siebrand Mazeland
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ * @file
 11+ */
 12+
 13+/**
 14+ * Reader for PHP variables files. Not completely general, as it excepts two
 15+ * comment sections at the top, separated by a blank line.
 16+ *
 17+ * Authors in the first section are detected, if prefixed with '# Author: '.
 18+ * Second section (if any) is returned verbatim.
 19+ */
 20+class PhpVariablesFormatReader extends SimpleFormatReader {
 21+
 22+ /**
 23+ * Inherited from SimpleFormatReader, which parses whole header in one pass.
 24+ * Basically the same, with different author prefix and separator between
 25+ * headers and messages.
 26+ *
 27+ * FIXME: possible to refactor to reduce duplication?
 28+ */
 29+ protected function parseHeader() {
 30+ $authors = array();
 31+ $staticHeader = '';
 32+
 33+ if ( $this->filename !== false ) {
 34+ $handle = fopen( $this->filename, "rt" );
 35+ $state = 0;
 36+
 37+ while ( !feof($handle) ) {
 38+ $line = fgets($handle);
 39+
 40+ if ( $state === 0 ) {
 41+ if ( $line === "\n" ) {
 42+ $state = 1;
 43+ continue;
 44+ }
 45+
 46+ $formatPrefix = '# Author: ';
 47+
 48+ $prefixLength = strlen($formatPrefix);
 49+ $prefix = substr( $line, 0, $prefixLength );
 50+ if ( strcasecmp( $prefix, $formatPrefix ) === 0 ) {
 51+ // fgets includes the trailing newline, trim to get rid of it
 52+ $authors[] = trim(substr( $line, $prefixLength ));
 53+ }
 54+ } elseif ( $state === 1 ) {
 55+ if ( $line === "\n" || $line[0] !== '#' ) {
 56+ break; // End of static header, if any
 57+ }
 58+ $staticHeader .= $line;
 59+ }
 60+ }
 61+
 62+ fclose( $handle );
 63+ }
 64+
 65+ $this->authors = $authors;
 66+ $this->staticHeader = $staticHeader;
 67+ }
 68+
 69+ /**
 70+ * Parses messages from lines key=value. Whitespace is trimmer around key and
 71+ * values. New lines inside values have to be escaped as '\n'. Lines which do
 72+ * not have = are ignored. Comments are designated by # at the start of the
 73+ * line only. Values can have = characters, only the first one is considered
 74+ * separator.
 75+ */
 76+ public function parseMessages( StringMangler $mangler ) {
 77+ if ( !file_exists( $this->filename ) ) {
 78+ return null;
 79+ }
 80+
 81+ $data = file_get_contents( $this->filename );
 82+
 83+ $matches = array();
 84+ $regex = '/^\$(.*?)\s*=\s*[\'"](.*?)[\'"];(\s*#.*?)?$/mus';
 85+ preg_match_all( $regex, $data, $matches, PREG_SET_ORDER );
 86+ $messages = array();
 87+ foreach ( $matches as $_ ) {
 88+ $legal = Title::legalChars();
 89+ $key = preg_replace( "/([^$legal]|\\\\)/ue", '\'\x\'.' . "dechex(ord('\\0'))", $_[1] );
 90+ $value = str_replace( array( "\'", "\\\\" ), array( "'", "\\" ), $_[2] );
 91+ $messages[$key] = $value;
 92+ }
 93+ return $messages;
 94+ }
 95+}
 96+
 97+/**
 98+ * Very simple writer for exporting messages to PhpVariables property files from wiki.
 99+ */
 100+class PhpVariablesFormatWriter extends SimpleFormatWriter {
 101+
 102+ /**
 103+ * Inherited. Very simplistic header with timestamp.
 104+ */
 105+ public function makeHeader( $handle, $code ) {
 106+ global $wgSitename;
 107+ list( $name, $native ) = $this->getLanguageNames($code);
 108+ $authors = $this->formatAuthors( '# Author: ', $code );
 109+ $when = wfTimestamp(TS_ISO_8601);
 110+
 111+ fwrite( $handle, <<<HEADER
 112+# Messages for $name ($native)
 113+# Exported from $wgSitename at $when
 114+$authors
 115+
 116+HEADER
 117+ );
 118+ }
 119+
 120+ /**
 121+ * Inherited. Exports messages as lines of format $key = 'value'.
 122+ */
 123+ protected function exportMessages( $handle, MessageCollection $collection ) {
 124+ $mangler = $this->group->getMangler();
 125+ foreach ( $collection->keys() as $item ) {
 126+ $value = $collection[$item]->translation;
 127+ if ( $value === null ) continue;
 128+
 129+ $key = $mangler->unmangle($item);
 130+ $key = stripcslashes( $key );
 131+
 132+ $value = str_replace( TRANSLATE_FUZZY, '', $value );
 133+ $value = addcslashes( $value, "'" );
 134+
 135+ # No pretty alignment here, sorry
 136+ fwrite( $handle, "$$key = '$value';\n" );
 137+ }
 138+ }
 139+}
Property changes on: trunk/extensions/Translate/ffs/PhpVariables.php
___________________________________________________________________
Added: svn:eol-style
1140 + native
Added: svn:keywords
2141 + Id