r79921 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79920‎ | r79921 | r79922 >
Date:23:19, 9 January 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Added new table to keep track of translation memories and implemented (but did not test yet) TMX parser
Modified paths:
  • /trunk/extensions/LiveTranslate/LiveTranslate.hooks.php (modified) (history)
  • /trunk/extensions/LiveTranslate/LiveTranslate.sql (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LT_TMParser.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LT_TMUnit.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LT_TMXParser.php (modified) (history)
  • /trunk/extensions/LiveTranslate/sql (added) (history)
  • /trunk/extensions/LiveTranslate/sql/LT_IndexWordTranslation.sql (added) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/LiveTranslate.sql
@@ -1,6 +1,6 @@
22 -- MySQL version of the database schema for the Live Translate extension.
33
 4+-- Special translations table.
45 CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/live_translate (
56 word_id INT(8) unsigned NOT NULL,
67 word_language VARCHAR(255) NOT NULL,
@@ -8,4 +8,9 @@
99 word_primary INT(1) unsigned NOT NULL
1010 ) /*$wgDBTableOptions*/;
1111
12 -CREATE INDEX word_translation ON /*$wgDBprefix*/live_translate (word_id, word_language);
\ No newline at end of file
 12+-- Table to keep track of translation memories for the special words.
 13+CREATE TABLE IF NOT EXISTS /*$wgDBprefix*/live_translate_memories (
 14+ memory_id INT(4) unsigned NOT NULL auto_increment PRIMARY KEY,
 15+ memory_type INT(2) unsigned NOT NULL,
 16+ memory_location VARCHAR(255) NOT NULL
 17+) /*$wgDBTableOptions*/;
\ No newline at end of file
Index: trunk/extensions/LiveTranslate/sql/LT_IndexWordTranslation.sql
@@ -0,0 +1 @@
 2+CREATE INDEX word_translation ON /*$wgDBprefix*/live_translate (word_id, word_language);
Property changes on: trunk/extensions/LiveTranslate/sql/LT_IndexWordTranslation.sql
___________________________________________________________________
Added: svn:eol-style
13 + native
Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php
@@ -136,11 +136,23 @@
137137 if ( $wgDBtype == 'mysql' ) {
138138 // Set up the current schema.
139139 if ( $updater === null ) {
140 - global $wgExtNewTables;
 140+ global $wgExtNewTables, $wgExtNewIndexes;
141141 $wgExtNewTables[] = array(
142142 'livetranslate',
143 - $egLiveTranslateIP . '/LiveTranslate.sql'
 143+ $egLiveTranslateIP . '/LiveTranslate.sql',
 144+ true
144145 );
 146+ $wgExtNewTables[] = array(
 147+ 'livetranslatememories',
 148+ $egLiveTranslateIP . '/LiveTranslate.sql',
 149+ true
 150+ );
 151+ $wgExtNewIndexes[] = array(
 152+ 'live_translate',
 153+ 'word_translation',
 154+ $egLiveTranslateIP . '/sql/LT_IndexWordTranslation.sql',
 155+ true
 156+ );
145157 }
146158 else {
147159 $updater->addExtensionUpdate( array(
@@ -148,7 +160,20 @@
149161 'livetranslate',
150162 $egLiveTranslateIP . '/LiveTranslate.sql',
151163 true
 164+ ) );
 165+ $updater->addExtensionUpdate( array(
 166+ 'addTable',
 167+ 'livetranslatememories',
 168+ $egLiveTranslateIP . '/LiveTranslate.sql',
 169+ true
152170 ) );
 171+ $updater->addExtensionUpdate( array(
 172+ 'addIndex',
 173+ 'live_translate',
 174+ 'word_translation',
 175+ $egLiveTranslateIP . '/sql/LT_IndexWordTranslation.sql',
 176+ true
 177+ ) );
153178 }
154179 }
155180
Index: trunk/extensions/LiveTranslate/includes/LT_TMXParser.php
@@ -10,15 +10,171 @@
1111 *
1212 * @licence GNU GPL v3
1313 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ * @author Nicola Asuni < http://evolt.org/node/60511 >
1415 */
1516 class LTTMXParser extends LTTMParser {
1617
1718 /**
 19+ * XML parser object.
 20+ *
 21+ * @since 0.4
 22+ *
 23+ * @var resource
 24+ */
 25+ protected $parser;
 26+
 27+ /**
 28+ * The translation memory the parser will store translations in before returning them.
 29+ *
 30+ * @since 0.4
 31+ *
 32+ * @var LTTranslationMemory
 33+ */
 34+ protected $tm;
 35+
 36+ /**
 37+ * The current translation unit.
 38+ *
 39+ * @since 0.4
 40+ *
 41+ * @var LTTMUnit
 42+ */
 43+ protected $tu;
 44+
 45+ /**
 46+ * The language of the current translation unit variant (tuv) node.
 47+ *
 48+ * @since 0.4
 49+ *
 50+ * @var string
 51+ */
 52+ protected $currentLanguage;
 53+
 54+ /**
 55+ * A string to build up translations when a single variant contains multiple segments.
 56+ *
 57+ * @since 0.4
 58+ *
 59+ * @var string
 60+ */
 61+ protected $currentTranslation;
 62+
 63+ /**
 64+ * Boolean to keep track of if the XML parser is inside a segment node.
 65+ *
 66+ * @since 0.4
 67+ *
 68+ * @var boolean
 69+ */
 70+ protected $insideSegment;
 71+
 72+ /**
 73+ * Constructor.
 74+ *
 75+ * @since 0.4
 76+ */
 77+ public function __construct() {
 78+ parent::__construct();
 79+
 80+ $this->parser = xml_parser_create( $this );
 81+
 82+ xml_parser_set_option( $this->parser, XML_OPTION_CASE_FOLDING, 0 );
 83+
 84+ xml_set_object( $this->parser, $this );
 85+ xml_set_element_handler( $this->parser, "startElementHandler", "endElementHandler" );
 86+ xml_set_character_data_handler( $this->parser, "segmentContentHandler" );
 87+ }
 88+
 89+ /**
 90+ * Destructor.
 91+ *
 92+ * @since 0.4
 93+ */
 94+ public function __destruct() {
 95+ xml_parser_free( $this->parser );
 96+ }
 97+
 98+ /**
1899 * (non-PHPdoc)
19100 * @see LTTMParser::parse()
20101 */
21102 public function parse( $text ) {
 103+ $this->tm = new LTTranslationMemory();
22104
 105+ // Attempt to parse the TMX.
 106+ if( !xml_parse( $this->parser, $text ) ) {
 107+ // xml_error_string(xml_get_error_code($this->parser))
 108+ // xml_get_current_line_number($this->parser))
 109+ }
 110+
 111+ return $this->tm;
23112 }
24113
 114+ /**
 115+ * Sets the start element handler function for the XML parser parser.start_element_handler.
 116+ *
 117+ * @param resource $parser The first parameter, parser, is a reference to the XML parser calling the handler.
 118+ * @param string $name The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
 119+ * @param array $attribs The third parameter, attribs, contains an associative array with the element's attributes (if any). The keys of this array are the attribute names, the values are the attribute values. Attribute names are case-folded on the same criteria as element names. Attribute values are not case-folded. The original order of the attributes can be retrieved by walking through attribs the normal way, using each(). The first key in the array was the first attribute, and so on.
 120+ */
 121+ protected function startElementHandler( $parser, $name, $attribs ) {
 122+ switch( strtolower( $name ) ) {
 123+ case 'tu':
 124+ // A new translation unit node has been entered, so create a new translation unit object.
 125+ $this->tu = new LTTMUnit();
 126+ break;
 127+ case 'tuv':
 128+ if ( array_key_exists( 'xml:lang', $attribs ) ) {
 129+ $this->currentLanguage = $attribs['xml:lang'];
 130+ }
 131+ else {
 132+ // TODO: ignore node or give warning
 133+ }
 134+ break;
 135+ case 'seg':
 136+ $this->currentTranslation = '';
 137+ $this->insideSegment = true;
 138+ break;
 139+ }
 140+ }
 141+
 142+ /**
 143+ * Sets the end element handler function for the XML parser parser.end_element_handler.
 144+ *
 145+ * @since 0.4
 146+ *
 147+ * @param resource $parser The first parameter, parser, is a reference to the XML parser calling the handler.
 148+ * @param string $name The second parameter, name, contains the name of the element for which this handler is called. If case-folding is in effect for this parser, the element name will be in uppercase letters.
 149+ */
 150+ protected function endElementHandler($parser, $name) {
 151+ switch( strtolower( $name ) ) {
 152+ case 'tu':
 153+ // We are leaving the translation unit node, so add the translation unit to the translation memory.
 154+ if ( $this->tu->hasVariants() ) {
 155+ $this->tm->addTranslationUnit( $this->tu );
 156+ }
 157+ break;
 158+ case 'tuv':
 159+ $this->tu->addVariant( $this->currentLanguage, $this->currentTranslation );
 160+ break;
 161+ case 'seg':
 162+ $this->insideSegment = false;
 163+ break;
 164+ }
 165+ }
 166+
 167+ /**
 168+ * Sets the character data handler function for the XML parser parser.handler.
 169+ *
 170+ * @since 0.4
 171+ *
 172+ * @param resource $parser The first parameter, parser, is a reference to the XML parser calling the handler.
 173+ * @param string $data The second parameter, data, contains the character data as a string.
 174+ */
 175+ protected function segmentContentHandler( $parser, $data ) {
 176+ if ( $this->insideSegment ) {
 177+ $this->currentTranslation .= $data;
 178+ }
 179+ }
 180+
25181 }
Index: trunk/extensions/LiveTranslate/includes/LT_TMParser.php
@@ -33,4 +33,7 @@
3434 */
3535 public abstract function parse( $text );
3636
 37+
 38+ // file_get_contents
 39+
3740 }
Index: trunk/extensions/LiveTranslate/includes/LT_TMUnit.php
@@ -74,4 +74,15 @@
7575 }
7676 }
7777
 78+ /**
 79+ * Returns if the translation unit has any variants. If not, it can probably be ignored.
 80+ *
 81+ * @since 0.4
 82+ *
 83+ * @return boolean
 84+ */
 85+ public function hasVariants() {
 86+ return count( $this->variants ) > 0;
 87+ }
 88+
7889 }

Status & tagging log