r79972 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79971‎ | r79972 | r79973 >
Date:23:24, 10 January 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Making use of the new parsing code and throwing out the old stuff
Modified paths:
  • /trunk/extensions/LiveTranslate/LiveTranslate.hooks.php (modified) (history)
  • /trunk/extensions/LiveTranslate/LiveTranslate.php (modified) (history)
  • /trunk/extensions/LiveTranslate/api/ApiImportTranslationMemories.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LT_GCSVParser.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LT_LTFParser.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LT_TranslationMemory.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php (modified) (history)
  • /trunk/extensions/LiveTranslate/specials/SpecialLiveTranslate.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/LiveTranslate.php
@@ -53,7 +53,7 @@
5454 $incDirIP = $egLiveTranslateIP . '/includes/';
5555 $wgAutoloadClasses['LiveTranslateFunctions'] = $incDirIP . 'LiveTranslate_Functions.php';
5656 $wgAutoloadClasses['LTGCSVParser'] = $incDirIP . 'LT_GCSVParser.php';
57 -$wgAutoloadClasses['LTLTFParser'] = $incDirIP . 'LT_LFTParser.php';
 57+$wgAutoloadClasses['LTLTFParser'] = $incDirIP . 'LT_LTFParser.php';
5858 $wgAutoloadClasses['LTTMParser'] = $incDirIP . 'LT_TMParser.php';
5959 $wgAutoloadClasses['LTTMUnit'] = $incDirIP . 'LT_TMUnit.php';
6060 $wgAutoloadClasses['LTTMXParser'] = $incDirIP . 'LT_TMXParser.php';
Index: trunk/extensions/LiveTranslate/specials/SpecialLiveTranslate.php
@@ -66,7 +66,11 @@
6767 $this->handleSubmission();
6868 }
6969
70 - $this->displayTMConfig( $this->getTMConfigItems() );
 70+ $tms = $this->getTMConfigItems();
 71+
 72+ $this->displayTMConfig( $tms );
 73+
 74+ $this->updateTranslationMemory( $tms );
7175 }
7276
7377 /**
@@ -119,6 +123,28 @@
120124 }
121125
122126 /**
 127+ * Updates the translation memory stored in the db.
 128+ *
 129+ * @since 0.4
 130+ *
 131+ * @param array $tms The current translation memories
 132+ */
 133+ protected function updateTranslationMemory( array $tms ) {
 134+ foreach ( $tms as $tm ) {
 135+ $requestData = array(
 136+ 'action' => 'importtms',
 137+ 'format' => 'json',
 138+ 'source' => $tm->memory_location,
 139+ 'type' => $tm->memory_type,
 140+ );
 141+
 142+ $api = new ApiMain( new FauxRequest( $requestData, true ), true );
 143+ $api->execute();
 144+ $response = $api->getResultData();
 145+ }
 146+ }
 147+
 148+ /**
123149 * Displays the translation memories config table.
124150 *
125151 * @since 0.4
Index: trunk/extensions/LiveTranslate/LiveTranslate.hooks.php
@@ -31,21 +31,23 @@
3232 $currentLang = LiveTranslateFunctions::getCurrentLang( $title );
3333
3434 if ( $title->getFullText() == $egLiveTranslateDirPage ) {
35 - $wordSets = LiveTranslateFunctions::parseTranslations( $article->getContent() );
 35+ $parser = new LTLTFParser();
 36+ $tm = $parser->parse( $article->getContent() );
 37+ $tus = $tm->getTranslationUnits();
3638
37 - if ( count( $wordSets ) == 0 ) {
 39+ if ( count( $tus ) == 0 ) {
3840 $wgOut->addWikiMsg( 'livetranslate-dictionary-empty' );
3941 }
4042 else {
4143 $wgOut->addWikiMsg(
4244 'livetranslate-dictionary-count',
43 - $wgLang->formatNum( count( $wordSets ) ) ,
44 - $wgLang->formatNum( count( $wordSets[0] ) )
 45+ $wgLang->formatNum( count( $tus ) ) ,
 46+ $wgLang->formatNum( count( $tus[0]->getVariants() ) )
4547 );
4648
4749 $notAllowedLanguages = array();
4850
49 - foreach ( $wordSets[0] as $languageCode => $translations ) {
 51+ foreach ( $tus[0]->getVariants() as $languageCode => $translations ) {
5052 if ( !in_array( $languageCode, $egLiveTranslateLanguages ) ) {
5153 $notAllowedLanguages[] = $languageCode;
5254 }
@@ -195,7 +197,16 @@
196198 $title = $article->getTitle();
197199
198200 if ( $title->getFullText() == $egLiveTranslateDirPage ) {
199 - LiveTranslateFunctions::saveTranslations( LiveTranslateFunctions::parseTranslations( $text ) );
 201+ $requestData = array(
 202+ 'action' => 'importtms',
 203+ 'format' => 'json',
 204+ 'source' => $title->getFullText(),
 205+ 'type' => 0,
 206+ );
 207+
 208+ $api = new ApiMain( new FauxRequest( $requestData, true ), true );
 209+ $api->execute();
 210+ $response = $api->getResultData();
200211 }
201212
202213 return true;
Index: trunk/extensions/LiveTranslate/includes/LT_GCSVParser.php
@@ -21,4 +21,4 @@
2222 return new LTTranslationMemory(); // TODO
2323 }
2424
25 -}
\ No newline at end of file
 25+}
Index: trunk/extensions/LiveTranslate/includes/LT_LTFParser.php
@@ -18,8 +18,28 @@
1919 * @see LTTMParser::parse()
2020 */
2121 public function parse( $text ) {
22 - // LiveTranslateFunctions::parseTranslations
23 - return new LTTranslationMemory(); // TODO
 22+ $tm = new LTTranslationMemory();
 23+
 24+ $translationSets = array();
 25+
 26+ $lines = explode( "\n", $text );
 27+ $languages = array_map( 'trim', explode( ',', array_shift( $lines ) ) );
 28+
 29+ foreach ( $lines as $line ) {
 30+ $values = array_map( 'trim', explode( ',', $line ) );
 31+ $tu = new LTTMUnit();
 32+
 33+ foreach ( $values as $nr => $value ) {
 34+ if ( array_key_exists( $nr, $languages ) ) {
 35+ // Add the translation (or translations) (value, array) of the word in the language (key).
 36+ $tu->addVariants( array( $languages[$nr] => array_map( 'trim', explode( '|', $value ) ) ) );
 37+ }
 38+ }
 39+
 40+ $tm->addTranslationUnit( $tu );
 41+ }
 42+
 43+ return $tm;
2444 }
2545
2646 }
\ No newline at end of file
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php
@@ -169,75 +169,8 @@
170170
171171 return $words;
172172 }
173 -
174 - /**
175 - * Parses the provided dictionary content and returns it as an
176 - * array of associative arrays.
177 - *
178 - * @since 0.1
179 - *
180 - * @param string $content
181 - *
182 - * @return array
183 - */
184 - public static function parseTranslations( $content ) {
185 - $translationSets = array();
186 -
187 - $lines = explode( "\n", $content );
188 - $languages = array_map( 'trim', explode( ',', array_shift( $lines ) ) );
189 -
190 - foreach ( $lines as $line ) {
191 - $values = array_map( 'trim', explode( ',', $line ) );
192 -
193 - $translations = array();
194 -
195 - foreach ( $values as $nr => $value ) {
196 - if ( array_key_exists( $nr, $languages ) ) {
197 - // Add the translation (or translations) (value, array) of the word in the language (key).
198 - $translations[$languages[$nr]] = array_map( 'trim', explode( '|', $value ) );
199 - }
200 - }
201 -
202 - $translationSets[] = $translations;
203 - }
204 -
205 - return $translationSets;
206 - }
207173
208174 /**
209 - * Replaces the current translations with the provided ones.
210 - *
211 - * @since 0.1
212 - *
213 - * @param array $translationSets
214 - */
215 - public static function saveTranslations( array $translationSets ) {
216 - $dbw = wfGetDB( DB_MASTER );
217 -
218 - $dbw->query( 'TRUNCATE TABLE ' . $dbw->tableName( 'live_translate' ) );
219 -
220 - foreach ( $translationSets as $wordId => $translationSet ) {
221 - foreach ( $translationSet as $language => $translations ) {
222 - $primary = 1;
223 -
224 - foreach ( $translations as $translation ) {
225 - $dbw->insert(
226 - 'live_translate',
227 - array(
228 - 'word_id' => $wordId,
229 - 'word_language' => $language,
230 - 'word_translation' => $translation,
231 - 'word_primary' => $primary
232 - )
233 - );
234 -
235 - $primary = 0;
236 - }
237 - }
238 - }
239 - }
240 -
241 - /**
242175 * Returns a PHP version of the JavaScript google.language.Languages enum of the Google Translate v1 API.
243176 * @see https://code.google.com/apis/language/translate/v1/getting_started.html#LangNameArray
244177 *
Index: trunk/extensions/LiveTranslate/includes/LT_TranslationMemory.php
@@ -39,7 +39,7 @@
4040 * @param LTTMUnit $tu
4141 */
4242 public function addTranslationUnit( LTTMUnit $tu ) {
43 - $translationUnits[] = $tu;
 43+ $this->translationUnits[] = $tu;
4444 }
4545
4646 /**
Index: trunk/extensions/LiveTranslate/api/ApiImportTranslationMemories.php
@@ -55,8 +55,6 @@
5656 break;
5757 }
5858
59 -
60 -
6159 if ( $tm !== false ) {
6260 $this->doTMImport( $tm );
6361 }
@@ -64,7 +62,34 @@
6563 }
6664
6765 protected function doTMImport( LTTranslationMemory $tm ) {
 66+ $dbw = wfGetDB( DB_MASTER );
6867
 68+ // TODO: move
 69+ $dbw->query( 'TRUNCATE TABLE ' . $dbw->tableName( 'live_translate' ) );
 70+
 71+ $wordId = 0;
 72+
 73+ foreach ( $tm->getTranslationUnits() as $tu ) {
 74+ foreach ( $tu->getVariants() as $language => $translations ) {
 75+ $primary = 1;
 76+
 77+ foreach ( $translations as $translation ) {
 78+ $dbw->insert(
 79+ 'live_translate',
 80+ array(
 81+ 'word_id' => $wordId,
 82+ 'word_language' => $language,
 83+ 'word_translation' => $translation,
 84+ 'word_primary' => $primary
 85+ )
 86+ );
 87+
 88+ $primary = 0;
 89+ }
 90+
 91+ $wordId++;
 92+ }
 93+ }
6994 }
7095
7196 public function getAllowedParams() {

Status & tagging log