r50391 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50390‎ | r50391 | r50392 >
Date:21:29, 9 May 2009
Author:minuteelectron
Status:deferred
Tags:
Comment:
* Translate: OpenLayers writer and reader improvements.
Modified paths:
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/ffs/OpenLayers.php (added) (history)
  • /trunk/extensions/Translate/ffs/OpenLayersJS.php (deleted) (history)
  • /trunk/extensions/Translate/groups/OpenLayers.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/groups/OpenLayers.php
@@ -0,0 +1,45 @@
 2+<?php
 3+
 4+/**
 5+ * Support for OpenLayers.
 6+ *
 7+ * @author Robert Leverington <robert@rhl.me.uk>
 8+ * @copyright Copyright � 2009 Robert Leverington
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+class OpenLayersMessageGroup extends MessageGroup {
 13+ protected $label = 'OpenLayers (slippy map)';
 14+ protected $id = 'out-openlayers';
 15+ protected $type = 'openlayers';
 16+
 17+ protected $fileDir = '__BUG__';
 18+
 19+ public function getPath() { return $this->fileDir; }
 20+ public function setPath( $value ) { $this->fileDir = $value; }
 21+
 22+ protected $codeMap = array(
 23+ );
 24+
 25+ protected $optional = array(
 26+ );
 27+
 28+ public function getMessageFile( $code ) {
 29+ if( isset( $this->codeMap[ $code ] ) ) {
 30+ $code = $this->codeMap[ $code ];
 31+ }
 32+ return "$code.js";
 33+ }
 34+
 35+ protected function getFileLocation( $code ) {
 36+ return $this->fileDir . '/' . $this->getMessageFile( $code );
 37+ }
 38+
 39+ public function getReader( $code ) {
 40+ return new OpenLayersFormatReader( $this->getFileLocation( $code ) );
 41+ }
 42+
 43+ public function getWriter() {
 44+ return new OpenLayersFormatWriter( $this );
 45+ }
 46+}
\ No newline at end of file
Index: trunk/extensions/Translate/_autoload.php
@@ -51,6 +51,8 @@
5252 $wgAutoloadClasses['JavaFormatWriter'] = $dir . 'ffs/Java.php';
5353 $wgAutoloadClasses['PhpVariablesFormatReader'] = $dir . 'ffs/PhpVariables.php';
5454 $wgAutoloadClasses['PhpVariablesFormatWriter'] = $dir . 'ffs/PhpVariables.php';
 55+$wgAutoloadClasses['OpenLayersFormatReader'] = $dir . 'ffs/OpenLayers.php';
 56+$wgAutoloadClasses['OpenLayersFormatWriter'] = $dir . 'ffs/OpenLayers.php';
5557 $wgAutoloadClasses['XliffFormatWriter'] = $dir . 'ffs/Xliff.php';
5658
5759 # utils
@@ -75,6 +77,7 @@
7678 $wgAutoloadClasses['FreeColMessageGroup'] = $dir . 'groups/FreeCol.php';
7779 $wgAutoloadClasses['MantisMessageGroup'] = $dir . 'groups/Mantis.php';
7880 $wgAutoloadClasses['NoccMessageGroup'] = $dir . 'groups/Nocc.php';
 81+$wgAutoloadClasses['OpenLayersMessageGroup'] = $dir . 'groups/OpenLayers.php';
7982
8083 # tag
8184 #$wgAutoloadClasses['RenderJob'] = $dir . 'tag/RenderJob.php';
Index: trunk/extensions/Translate/ffs/OpenLayersJS.php
@@ -1,70 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * *INCOMPLETE* OpenLayers JavaScript language class file format handler.
6 - *
7 - * @author Robert Leverington
8 - * @copyright Copyright � 2009 Robert Leverington
9 - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
10 - * @file
11 - */
12 -
13 -class JavaScriptFormatReader extends SimpleFormatReader {
14 -
15 - private function leftTrim( $string ) {
16 - $string = ltrim( $string );
17 - $string = ltrim( $string, '"' );
18 - return $string;
19 - }
20 -
21 - /**
22 - * Parse OpenLayer JavaScript language class.
23 - * Known issues:
24 - * - It is a requirement for key names to be enclosed in single
25 - * quotation marks, and for messages to be enclosed in double.
26 - * - The last key-value pair must have a comma at the end.
27 - * - Uses seperate $this->leftTrim() function, this is undersired.
28 - */
29 - protected function parseMessages() {
30 - $this->filename = dirname( __FILE__ ) . '/en.js';
31 - $data = file_get_contents( $this->filename );
32 -
33 - // Just get relevant data.
34 - $dataStart = strpos( $data, '{' );
35 - $dataEnd = strrpos( $data, '}' );
36 - $data = substr( $data, $dataStart + 1, $dataEnd - $dataStart - 1);
37 - // Strip comments.
38 - $data = preg_replace( '#^(\s*?)//(.*?)$#m', '', $data );
39 - // Break in to message segements for further parsing.
40 - $data = explode( '",', $data );
41 -
42 - $messages = array();
43 - // Process each segment.
44 - foreach( $data as $segment ) {
45 - // Remove excess quote mark at beginning.
46 - $segment = substr( $segment, 1 );
47 - // Add back trailing quote.
48 - $segment .= '"';
49 - // Concatenate seperate strings.
50 - $segment = explode( '" +', $segment );
51 - $segment = array_map( array( $this, 'leftTrim' ), $segment );
52 - $segment = implode( $segment );
53 - #$segment = preg_replace( '#\" \+(.*?)\"#m', '', $segment );
54 - // Break in to key and message.
55 - $segments = explode( '\':', $segment );
56 - $key = $segments[ 0 ];
57 - unset( $segments[ 0 ] );
58 - $value = implode( $segments );
59 - // Strip excess whitespace from both.
60 - $key = trim( $key );
61 - $value = trim( $value );
62 - // Remove quotation marks and syntax.
63 - $key = substr( $key, 1 );
64 - $value = substr( $value, 1, -1 );
65 - $messages[ $key ] = $value;
66 - }
67 -
68 - return $messages;
69 - }
70 -
71 -}
Index: trunk/extensions/Translate/ffs/OpenLayers.php
@@ -0,0 +1,130 @@
 2+<?php
 3+
 4+/**
 5+ * OpenLayers JavaScript language class file format handler.
 6+ *
 7+ * @author Robert Leverington
 8+ * @copyright Copyright � 2009 Robert Leverington
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+class OpenLayersFormatReader extends SimpleFormatReader {
 13+
 14+ private function leftTrim( $string ) {
 15+ $string = ltrim( $string );
 16+ $string = ltrim( $string, '"' );
 17+ return $string;
 18+ }
 19+
 20+ /**
 21+ * Parse OpenLayer JavaScript language class.
 22+ * Known issues:
 23+ * - It is a requirement for key names to be enclosed in single
 24+ * quotation marks, and for messages to be enclosed in double.
 25+ * - The last key-value pair must have a comma at the end.
 26+ * - Uses seperate $this->leftTrim() function, this is undersired.
 27+ * @params $mangler StringMangler
 28+ * @return Array: Messages from file.
 29+ */
 30+ public function parseMessages( StringMangler $mangler ) {
 31+ $data = file_get_contents( $this->filename );
 32+
 33+ // Just get relevant data.
 34+ $dataStart = strpos( $data, '{' );
 35+ $dataEnd = strrpos( $data, '}' );
 36+ $data = substr( $data, $dataStart + 1, $dataEnd - $dataStart - 1);
 37+ // Strip comments.
 38+ $data = preg_replace( '#^(\s*?)//(.*?)$#m', '', $data );
 39+ // Break in to message segements for further parsing.
 40+ $data = explode( '",', $data );
 41+
 42+ $messages = array();
 43+ // Process each segment.
 44+ foreach( $data as $segment ) {
 45+ // Remove excess quote mark at beginning.
 46+ $segment = substr( $segment, 1 );
 47+ // Add back trailing quote.
 48+ $segment .= '"';
 49+ // Concatenate seperate strings.
 50+ $segment = explode( '" +', $segment );
 51+ $segment = array_map( array( $this, 'leftTrim' ), $segment );
 52+ $segment = implode( $segment );
 53+ #$segment = preg_replace( '#\" \+(.*?)\"#m', '', $segment );
 54+ // Break in to key and message.
 55+ $segments = explode( '\':', $segment );
 56+ $key = $segments[ 0 ];
 57+ unset( $segments[ 0 ] );
 58+ $value = implode( $segments );
 59+ // Strip excess whitespace from both.
 60+ $key = trim( $key );
 61+ $value = trim( $value );
 62+ // Remove quotation marks and syntax.
 63+ $key = substr( $key, 1 );
 64+ $value = substr( $value, 1, -1 );
 65+ $messages[ $key ] = $value;
 66+ }
 67+
 68+ // Remove extraneous key that is sometimes present.
 69+ unset( $messages[ 0 ] );
 70+
 71+ return $messages;
 72+ }
 73+
 74+}
 75+
 76+class OpenLayersFormatWriter extends SimpleFormatWriter {
 77+
 78+ /**
 79+ * Export a languages messages.
 80+ * @param $target File handler.
 81+ * @param $collection MessageCollection.
 82+ */
 83+ protected function exportLanguage( $target, MessageCollection $collection ) {
 84+ $code = $collection->code;
 85+ $names = Language::getLanguageNames();
 86+ $name = $names[ $code ];
 87+
 88+ // Generate list of authors for comment.
 89+ $authors = $collection->getAuthors();
 90+ $authorList = '';
 91+ foreach( $authors as $author ) {
 92+ $authorList .= " * - $author\n";
 93+ }
 94+
 95+ // Generate header and write.
 96+ $header = <<<EOT
 97+/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 98+ * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 99+ * full text of the license. */
 100+
 101+/* Translators (post 2008):
 102+$authorList */
 103+
 104+/**
 105+ * @requires OpenLayers/Lang.js
 106+ */
 107+
 108+/**
 109+ * Namespace: OpenLayers.Lang["$code"]
 110+ * Dictionary for $name. Keys for entries are used in calls to
 111+ * <OpenLayers.Lang.translate>. Entry bodies are normal strings or
 112+ * strings formatted for use with <OpenLayers.String.format> calls.
 113+ */
 114+OpenLayers.Lang.$code = {
 115+
 116+
 117+EOT;
 118+ fwrite( $target, $header );
 119+
 120+ // Get and write messages.
 121+ foreach( $collection as $message ) {
 122+ $message->infile = str_replace( '"', '\"', $message->infile );
 123+ $line = " '{$message->key}': \"{$message->infile}\",\n\n";
 124+ fwrite( $target, $line );
 125+ }
 126+
 127+ // File terminator.
 128+ fwrite( $target, '};' );
 129+ }
 130+
 131+}
Property changes on: trunk/extensions/Translate/ffs/OpenLayers.php
___________________________________________________________________
Name: svn:eol-style
1132 + native

Status & tagging log