r80360 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80359‎ | r80360 | r80361 >
Date:00:01, 15 January 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Modified the special words API query module and the calling JS to work with batches and fixed a regex escaping issue
Modified paths:
  • /trunk/extensions/LiveTranslate/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/LiveTranslate/api/ApiLiveTranslate.php (modified) (history)
  • /trunk/extensions/LiveTranslate/api/ApiQueryLiveTranslate.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php (modified) (history)
  • /trunk/extensions/LiveTranslate/includes/ext.livetranslate.js (modified) (history)

Diff [purge]

Index: trunk/extensions/LiveTranslate/RELEASE-NOTES
@@ -7,7 +7,8 @@
88 === Version 0.5 ===
99 2011-01-xx
1010
11 -*
 11+* Modified the LT API to work with batches, so translation doesn't break for big translation memories.
 12+* Fixed regex escaping issue that caused translations to stop when encountering certain characters in special words.
1213
1314 === Version 0.4 ===
1415 2011-01-14
Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js
@@ -44,40 +44,15 @@
4545 }
4646
4747 /**
48 - * Queries the special words in the source language, finds them in the page,
49 - * and wraps the into notranslate spans. Then initiates the translation process.
 48+ * Disables the translation button and then either kicks of insertion of
 49+ * notranslate spans around special words, or when this already happened,
 50+ * the actual translation process.
5051 */
5152 setupTranslationFeatures = function() {
5253 $( this ).attr( "disabled", true ).text( mediaWiki.msg( 'livetranslate-button-translating' ) );
5354
5455 if ( originalHtml === false ) {
55 - $.getJSON(
56 - wgScriptPath + '/api.php',
57 - {
58 - 'action': 'query',
59 - 'format': 'json',
60 - 'list': 'livetranslate',
61 - 'ltlanguage': currentLang
62 - },
63 - function( data ) {
64 - if ( data.words ) {
65 - insertNoTranslateTags( data.words );
66 - }
67 - else if ( data.error && data.error.info ) {
68 - alert( data.error.info );
69 - }
70 - else {
71 - for ( i in data ) {
72 - alert( mediaWiki.msg( 'livetranslate-dictionary-error' ) );
73 - break;
74 - }
75 - }
76 -
77 - originalHtml = $( '#bodyContent' ).html();
78 -
79 - initiateTranslating();
80 - }
81 - );
 56+ obtainAndInsertTranslations( -1 );
8257 }
8358 else {
8459 initiateTranslating();
@@ -85,6 +60,52 @@
8661 }
8762
8863 /**
 64+ * Queries a batch of special words in the source language, finds them in the page,
 65+ * and wraps the into notranslate spans. If there are no more words, the translation
 66+ * process is initaiated, otherwise the function calls itself again.
 67+ */
 68+ function obtainAndInsertTranslations( offset ) {
 69+ var requestArgs = {
 70+ 'action': 'query',
 71+ 'format': 'json',
 72+ 'list': 'livetranslate',
 73+ 'ltlanguage': currentLang,
 74+ };
 75+
 76+ if ( offset > 0 ) {
 77+ requestArgs['ltcontinue'] = offset;
 78+ }
 79+
 80+ $.getJSON(
 81+ wgScriptPath + '/api.php',
 82+ requestArgs,
 83+ function( data ) {
 84+ if ( data.words ) {
 85+ insertNoTranslateTags( data.words );
 86+ }
 87+ else if ( data.error && data.error.info ) {
 88+ alert( data.error.info );
 89+ }
 90+ else {
 91+ for ( i in data ) {
 92+ alert( mediaWiki.msg( 'livetranslate-dictionary-error' ) );
 93+ break;
 94+ }
 95+ }
 96+
 97+ originalHtml = $( '#bodyContent' ).html();
 98+
 99+ if ( data['query-continue'] ) {
 100+ obtainAndInsertTranslations( data['query-continue'].livetranslate.ltcontinue );
 101+ }
 102+ else {
 103+ initiateTranslating();
 104+ }
 105+ }
 106+ );
 107+ }
 108+
 109+ /**
89110 * Initiates the translation process.
90111 * First all special words are found and send to the local API,
91112 * and then replaced by their translation in the response. Then
@@ -134,6 +155,18 @@
135156 $( '#ltrevertbutton' ).click( showOriginal );
136157
137158 /**
 159+ * Regex text escaping function.
 160+ * Borrowed from http://simonwillison.net/2006/Jan/20/escape/
 161+ */
 162+ RegExp.escape = function(text) {
 163+ if (!arguments.callee.sRE) {
 164+ var specials = [ '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\' ];
 165+ arguments.callee.sRE = new RegExp( '(\\' + specials.join('|\\') + ')', 'g' );
 166+ }
 167+ return text.replace(arguments.callee.sRE, '\\$1');
 168+ }
 169+
 170+ /**
138171 * Inserts notranslate spans around the words specified in the passed array in the page content.
139172 *
140173 * @param {Array} words
@@ -141,7 +174,7 @@
142175 function insertNoTranslateTags( words ) {
143176 for ( i in words ) {
144177 $( '#bodyContent *' ).replaceText(
145 - new RegExp( "\\b" + words[i] + "\\b", "g" ),
 178+ new RegExp( "\\b" + RegExp.escape( words[i] ) + "\\b", "g" ),
146179 function( str ) {
147180 return '<span class="notranslate">' + str + '</span>'
148181 }
Index: trunk/extensions/LiveTranslate/includes/LiveTranslate_Functions.php
@@ -143,34 +143,6 @@
144144 }
145145
146146 /**
147 - * Gets a list of all special words in a language.
148 - *
149 - * @since 0.1
150 - *
151 - * @param string $language
152 - *
153 - * @return array
154 - */
155 - public static function getSpecialWordsForLang( $language ) {
156 - $dbr = wfGetDB( DB_SLAVE );
157 -
158 - $words = array();
159 -
160 - // TODO: fix index
161 - $res = $dbr->query(
162 - 'SELECT DISTINCT word_translation FROM ' .
163 - $dbr->tableName( 'live_translate' ) .
164 - ' WHERE word_language = ' . $dbr->addQuotes( $language )
165 - );
166 -
167 - while ( $translation = $dbr->fetchObject( $res ) ) {
168 - $words[] = $translation->word_translation;
169 - }
170 -
171 - return $words;
172 - }
173 -
174 - /**
175147 * Returns a PHP version of the JavaScript google.language.Languages enum of the Google Translate v1 API.
176148 * @see https://code.google.com/apis/language/translate/v1/getting_started.html#LangNameArray
177149 *
Index: trunk/extensions/LiveTranslate/api/ApiQueryLiveTranslate.php
@@ -26,8 +26,40 @@
2727 $this->dieUsageMsg( array( 'missingparam', 'language' ) );
2828 }
2929
30 - $specialWords = LiveTranslateFunctions::getSpecialWordsForLang( $params['language'] );
 30+ $this->addTables( 'live_translate' );
3131
 32+ $this->addFields( array(
 33+ 'word_id',
 34+ 'word_translation'
 35+ ) );
 36+
 37+ $this->addWhere( array(
 38+ 'word_language' => $params['language']
 39+ ) );
 40+
 41+ if ( !is_null( $params['continue'] ) ) {
 42+ $dbr = wfGetDB( DB_SLAVE );
 43+ $this->addWhere( 'word_id >= ' . $dbr->addQuotes( $params['continue'] ) );
 44+ }
 45+
 46+ $this->addOption( 'LIMIT', $params['limit'] + 1 );
 47+ $this->addOption( 'ORDER BY', 'word_id ASC' );
 48+
 49+ $words = $this->select( __METHOD__ );
 50+ $specialWords = array();
 51+ $count = 0;
 52+
 53+ while ( $word = $words->fetchObject() ) {
 54+ if ( ++$count > $params['limit'] ) {
 55+ // We've reached the one extra which shows that
 56+ // there are additional pages to be had. Stop here...
 57+ $this->setContinueEnumParameter( 'continue', $word->word_id );
 58+ break;
 59+ }
 60+
 61+ $specialWords[] = $word->word_translation;
 62+ }
 63+
3264 $toggeledSpecials = array();
3365
3466 foreach ( $specialWords as $word ) {
@@ -57,6 +89,14 @@
5890 ApiBase::PARAM_TYPE => 'string',
5991 //ApiBase::PARAM_REQUIRED => true,
6092 ),
 93+ 'limit' => array(
 94+ ApiBase :: PARAM_DFLT => 500,
 95+ ApiBase :: PARAM_TYPE => 'limit',
 96+ ApiBase :: PARAM_MIN => 1,
 97+ ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
 98+ ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
 99+ ),
 100+ 'continue' => null,
61101 );
62102 }
63103
@@ -67,6 +107,8 @@
68108 public function getParamDescription() {
69109 return array (
70110 'language' => 'The language for which to return special words',
 111+ 'continue' => 'Offset number from where to continue the query',
 112+ 'limit' => 'Max amount of words to return',
71113 );
72114 }
73115
Index: trunk/extensions/LiveTranslate/api/ApiLiveTranslate.php
@@ -99,7 +99,7 @@
100100 ApiBase::PARAM_TYPE => 'string',
101101 ApiBase::PARAM_ISMULTI => true,
102102 //ApiBase::PARAM_REQUIRED => true,
103 - ),
 103+ ),
104104 );
105105 }
106106

Status & tagging log