r50512 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50511‎ | r50512 | r50513 >
Date:09:44, 12 May 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
* Do not prepend fuzzy mark if no translation
* Fixed some call-time pass by references
* Backwards compatibility to 1.14
* Fix marking for translation when marking would create a new revision
* Schema updates was broken, it is not possible to add tags because the databases are not created at that time yet. Made an ugly hack to add them on next request.
* Fixed some hooks to check $wgTranslateMessageNamespaces to avoid checks for groups
* Removed the assignment of pagetranslation right for staff by default
* Edit tools were not displayed without explicit loadgroup, because the method to check groups for translatable pages was never used.
Modified paths:
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/PageTranslation.i18n.php (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/TranslateUtils.php (modified) (history)
  • /trunk/extensions/Translate/tag/PageTranslationHooks.php (modified) (history)
  • /trunk/extensions/Translate/tag/SpecialPageTranslation.php (modified) (history)
  • /trunk/extensions/Translate/tag/TPSection.php (modified) (history)
  • /trunk/extensions/Translate/tag/TranslatablePage.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageIndex.php (modified) (history)
  • /trunk/extensions/Translate/utils/ToolBox.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/MessageGroups.php
@@ -649,7 +649,10 @@
650650 foreach ( $messages as $key => $m ) {
651651 $rev = $page->getTransrev( $key .'/' . $messages->code );
652652 if ( $rev === false ) {
653 - $m->database = TRANSLATE_FUZZY . $m->database;
 653+ if ( !$m->database === null ) {
 654+ //TODO: fixme, ugly ugly ugly
 655+ $m->database = TRANSLATE_FUZZY . $m->database;
 656+ }
654657 continue;
655658 }
656659 foreach ( $markedRevs as $r ) {
Index: trunk/extensions/Translate/tag/TranslatablePage.php
@@ -105,7 +105,7 @@
106106 $len = strlen($matches[2][0][0]); // len of the content
107107 $end = $start + $len;
108108
109 - $ret = $this->sectionise( &$sections, substr( $contents, $start, $len ) );
 109+ $ret = $this->sectionise( $sections, substr( $contents, $start, $len ) );
110110
111111 $tagPlaceHolders[$ph] =
112112 self::index_replace( $contents, $ret, $start, $end );
@@ -142,7 +142,7 @@
143143 return substr( $string, 0, $start ) . $rep . substr( $string, $end );
144144 }
145145
146 - protected function sectionise( $sections, $text ) {
 146+ protected function sectionise( &$sections, $text ) {
147147 $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
148148 $parts = preg_split( '~(\s*\n\n\s*|\s*$)~', $text, -1, $flags );
149149
@@ -197,12 +197,13 @@
198198 }
199199
200200 protected function addTag( $tag, $revision, $value = null ) {
 201+
201202 $dbw = wfGetDB( DB_MASTER );
202203
203 - // Can this be done in one query?
204 - $id = $dbw->selectField( 'revtag_type', 'rtt_id',
205 - array( 'rtt_name' => $tag ), __METHOD__ );
 204+ $id = $this->getTagId( $tag );
206205
 206+ if ( is_object($revision) ) throw new MWException('Got object, excepted id');
 207+
207208 $conds = array(
208209 'rt_page' => $this->getTitle()->getArticleId(),
209210 'rt_type' => $id,
Index: trunk/extensions/Translate/tag/SpecialPageTranslation.php
@@ -67,7 +67,7 @@
6868
6969 // This will modify the sections to include name property
7070 $error = false;
71 - $sections = $this->checkInput( $page, &$error );
 71+ $sections = $this->checkInput( $page, $error );
7272 // Non-fatal error which prevents saving
7373 if ( $error === false && $wgRequest->wasPosted() ) {
7474 $err = $this->markForTranslation( $page, $sections );
@@ -198,7 +198,13 @@
199199 }
200200
201201 global $wgLang;
202 - return $wgLang->semicolonList( $actions );
 202+ if ( method_exists( $wgLang, 'semicolonList' ) ) {
 203+ // BC for <1.15
 204+ $actionText .= $wgLang->semicolonList( $actions );
 205+ } else {
 206+ $actionText = implode( '; ', $actions );
 207+ }
 208+ return $actionText;
203209 }
204210
205211 public function checkInput( TranslatablePage $page, &$error = false ) {
@@ -302,6 +308,13 @@
303309 if ( !$status->isOK() ) return array( 'tpt-edit-failed', $status->getWikiText() );
304310
305311 $newrevision = $status->value['revision'];
 312+
 313+ // In theory it is either null or Revision object,
 314+ // never revision object with null id, but who knows
 315+ if ( $newrevision instanceof Revision ) {
 316+ $newrevision = $newrevision->getId();
 317+ }
 318+
306319 if ( $newrevision === null ) {
307320 // Probably a no-change edit, so no new revision was assigned
308321 $newrevision = $page->getTitle()->getLatestRevId();
@@ -323,10 +336,8 @@
324337 $dbw = wfGetDB( DB_MASTER );
325338 $dbw->delete( 'translate_sections', array( 'trs_page' => $page->getTitle()->getArticleId() ), __METHOD__ );
326339 $ok = $dbw->insert( 'translate_sections', $inserts, __METHOD__ );
327 - if ( $ok === false ) return array( 'tpt-insert-failed' );
328340
329341 $page->addMarkedTag( $newrevision, $changed );
330 -
331342 // Re-generate caches
332343 MessageIndex::cache( NS_TRANSLATIONS );
333344 $page->getTranslationPercentages( true );
Index: trunk/extensions/Translate/tag/PageTranslationHooks.php
@@ -288,23 +288,12 @@
289289 return true;
290290 }
291291
292 - public static function onLoadExtensionSchemaUpdates() {
 292+ public static function schemaUpdates() {
293293 global $wgExtNewTables;
294294 $dir = dirname( __FILE__ ) . '/..';
295295 $wgExtNewTables[] = array( 'translate_sections', "$dir/translate.sql" );
296296 $wgExtNewTables[] = array( 'revtag_type', "$dir/revtags.sql" );
297297
298 - // Add our tags if they are not registered yet
299 - // tp:tag is called also the ready tag
300 - $tags = array( 'tp:mark', 'tp:tag', 'tp:transver' );
301 -
302 - $dbw = wfGetDB( DB_MASTER );
303 - foreach ( $tags as $tag ) {
304 - // TODO: use insert ignore
305 - $field = array( 'rtt_name' => $tag );
306 - $ret = $dbw->selectField( 'revtag_type', 'rtt_name', $field, __METHOD__ );
307 - if ( $ret !== $tag ) $dbw->insert( 'revtag_type', $field, __METHOD__ );
308 - }
309298 return true;
310299 }
311300
@@ -368,7 +357,12 @@
369358
370359 if ( !count($actions) ) return;
371360 $legend = "<div style=\"font-size: x-small; text-align: center\">";
372 - $legend .= $wgLang->semicolonList( $actions );
 361+ if ( method_exists( $wgLang, 'semicolonList' ) ) {
 362+ // BC for <1.15
 363+ $legend .= $wgLang->semicolonList( $actions );
 364+ } else {
 365+ $legend .= implode( '; ', $actions );
 366+ }
373367 $legend .= '</div><hr />';
374368
375369 global $wgOut;
Index: trunk/extensions/Translate/tag/TPSection.php
@@ -25,7 +25,7 @@
2626 $rep = "\\1 $header\n";
2727 $count = 0;
2828
29 - $text = preg_replace( $re, $rep, $this->text, 1, &$count );
 29+ $text = preg_replace( $re, $rep, $this->text, 1, $count );
3030 if ( $count === 0 ) {
3131 $text = $header . "\n" . $this->text;
3232 }
Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -13,8 +13,11 @@
1414 const MSG = 'translate-edit-';
1515
1616 static function addNavigation( &$outputpage, &$text ) {
17 - global $wgUser, $wgTitle;
 17+ global $wgUser, $wgTitle, $wgTranslateMessageNamespaces;
1818 $ns = $wgTitle->getNamespace();
 19+
 20+ if ( !in_array( $ns, $wgTranslateMessageNamespaces) ) return true;
 21+
1922 list( $key, $code ) = self::figureMessage( $wgTitle );
2023
2124 $group = self::getMessageGroup( $ns, $key );
@@ -104,6 +107,10 @@
105108
106109
107110 static function addTools( $object ) {
 111+ global $wgTranslateMessageNamespaces;
 112+ $ns = $object->mTitle->getNamespace();
 113+ if ( !in_array( $ns, $wgTranslateMessageNamespaces) ) return true;
 114+
108115 $object->editFormTextTop .= self::editBoxes( $object );
109116 global $wgMessageCache, $wgLang;
110117 return true;
Index: trunk/extensions/Translate/Translate.php
@@ -212,6 +212,16 @@
213213 // Page translation hooks
214214 global $wgHooks;
215215
 216+ // Database schema
 217+ $wgHooks['LoadExtensionSchemaUpdates'][] = 'PageTranslationHooks::schemaUpdates';
 218+
 219+ // Do not activate hooks if not setup properly
 220+ if ( !efTranslateCheckPT() ) {
 221+ $wgEnablePageTranslation = false;
 222+ return true;
 223+ }
 224+
 225+
216226 // Register our css, is there a better place for this?
217227 $wgHooks['OutputPageBeforeHTML'][] = 'PageTranslationHooks::injectCss';
218228
@@ -238,13 +248,54 @@
239249
240250 $wgHooks['ArticleViewHeader'][] = 'PageTranslationHooks::test';
241251
242 - // Database schema
243 - $wgHooks['LoadExtensionSchemaUpdates'][] = 'PageTranslationHooks::onLoadExtensionSchemaUpdates';
 252+ }
244253
 254+}
 255+
 256+
 257+function efTranslateCheckPT() {
 258+ global $wgHooks, $wgMemc;
 259+
 260+ $version = 2;
 261+ global $wgMemc;
 262+ $memcKey = wfMemcKey( 'pt' );
 263+ $ok = $wgMemc->get( $memcKey );
 264+
 265+ wfLoadExtensionMessages( 'PageTranslation' );
 266+ if ( $ok === $version ) {
 267+ return true;
245268 }
246269
 270+
 271+ // Add our tags if they are not registered yet
 272+ // tp:tag is called also the ready tag
 273+ $tags = array( 'tp:mark', 'tp:tag', 'tp:transver' );
 274+
 275+ $dbw = wfGetDB( DB_MASTER );
 276+ if ( !$dbw->tableExists('revtag_type') ) {
 277+ $wgHooks['SiteNoticeAfter'][] = array('efTranslateCheckWarn', wfMsg( 'tpt-install' ) );
 278+ return false;
 279+ }
 280+
 281+ foreach ( $tags as $tag ) {
 282+ // TODO: use insert ignore
 283+ $field = array( 'rtt_name' => $tag );
 284+ $ret = $dbw->selectField( 'revtag_type', 'rtt_name', $field, __METHOD__ );
 285+ if ( $ret !== $tag ) $dbw->insert( 'revtag_type', $field, __METHOD__ );
 286+ }
 287+
 288+ $wgMemc->set( $memcKey, $version );
 289+
 290+ return true;
247291 }
248292
 293+function efTranslateCheckWarn( $msg, &$sitenotice ) {
 294+ global $wgOut;
 295+ $sitenotice = $msg;
 296+ $wgOut->enableClientCache( false );
 297+ return true;
 298+}
 299+
249300 function efTranslateInitTags( $parser ) {
250301 // For nice language list in-page
251302 $parser->setHook( 'languages', array( 'PageTranslationHooks', 'languages' ) );
@@ -256,5 +307,3 @@
257308 function STDOUT() {}
258309 function STDERR() {}
259310 }
260 -
261 -$wgGroupPermissions['staff' ]['pagetranslation'] = true;
Index: trunk/extensions/Translate/PageTranslation.i18n.php
@@ -23,7 +23,7 @@
2424
2525 # Specific page on the special page
2626 'tpt-badtitle' => 'Page name given ($1) is not a valid title',
27 - 'tpt-oldrevision' => '$2 is not the latest version of the page $1.
 27+ 'tpt-oldrevision' => '$2 is not the latest version of the page [[$1]].
2828 Only latest versions can be marked for translation.',
2929 'tpt-notsuitable' => 'Page $1 is not suitable for translation.
3030 Make sure it has <nowiki><translate></nowiki> tags and has a valid syntax.',
@@ -35,7 +35,6 @@
3636 Before marking this version for translation, check that the changes to sections are minimised to avoid unnecessary work for translators.',
3737 'tpt-mark-summary' => 'Marked this version for translation',
3838 'tpt-edit-failed' => 'Could not update the page: $1',
39 - 'tpt-insert-failed' => 'Could not add sections to the database.',
4039 'tpt-already-marked' => 'The latest version of this page has already been marked for translation.',
4140
4241 # Page list on the special page
@@ -58,7 +57,10 @@
5958 'tpt-target-page' => 'This page cannot be updated manually.
6059 This page is a translation of page [[$1]] and the translation can be updated using [$2 the translation tool].',
6160 'tpt-unknown-page' => 'This namespace is reserved for content page translations.
62 -The page you are trying to edit does not seem to correspond any page marked for translation.'
 61+The page you are trying to edit does not seem to correspond any page marked for translation.',
 62+
 63+ 'tpt-install' => 'Run php maintenance/update.php or web install to enable page translation feature.',
 64+ 'tpt-install-ok' => 'Add <tt>$wgPageTranslationNoCheck = true;</tt> to your LocalSettings.php.'
6365 );
6466
6567 /** Arabic (العربية)
Index: trunk/extensions/Translate/utils/MessageIndex.php
@@ -30,10 +30,8 @@
3131 }
3232
3333 public static function cache( $namespace = null ) {
34 - if ( $namespace !== null ) {
35 - $namepace = MWNamespace::getCanonicalName( $namespace );
36 - if ( $namespace === false ) return null;
37 - }
 34+ $namespace = self::checkNs( $namespace );
 35+ if ( $namespace === null ) return null;
3836
3937 $groups = MessageGroups::singleton()->getGroups();
4038
@@ -100,6 +98,9 @@
10199 }
102100
103101 protected static function index( $namespace ) {
 102+ $namespace = self::checkNs( $namespace );
 103+ if ( $namespace === null ) return null;
 104+
104105 global $wgMemc;
105106 $memcKey = wfMemcKey( 'messageindex', $namespace );
106107 $cache = unserialize( $wgMemc->get($memcKey) );
@@ -107,9 +108,17 @@
108109 // Missing? Update it
109110 if ( !is_array($cache) ) self::cache( $namespace );
110111 $cache = unserialize( $wgMemc->get($memcKey) );
111 - if ( !is_array($cache) ) throw new MWException( "Caching failed" );
 112+ if ( !is_array($cache) ) throw new MWException( "Caching failed: $namespace" );
112113
113114 return $cache;
114115
115116 }
 117+
 118+ protected static function checkNs( $namespace ) {
 119+ if ( $namespace !== null ) {
 120+ $namepace = MWNamespace::getCanonicalName( $namespace );
 121+ if ( $namespace === false ) return null;
 122+ }
 123+ return $namespace;
 124+ }
116125 }
\ No newline at end of file
Index: trunk/extensions/Translate/utils/ToolBox.php
@@ -8,8 +8,12 @@
99 * actually is a translatable/translated message.
1010 */
1111 static function toolboxAllTranslations( &$skin ) {
12 - global $wgTitle;
 12+ global $wgTitle, $wgTranslateMessageNamespaces;
1313
 14+ $ns = $wgTitle->getNamespace();
 15+ if ( !in_array( $ns, $wgTranslateMessageNamespaces) ) return true;
 16+
 17+
1418 $inMessageGroup = TranslateUtils::messageKeyToGroup( $wgTitle->getNamespace(), $wgTitle->getBaseText() );
1519
1620 if ( $inMessageGroup ) {
Index: trunk/extensions/Translate/TranslateUtils.php
@@ -296,9 +296,16 @@
297297 }
298298
299299 public static function messageKeyToGroup( $namespace, $key ) {
300 - $key = self::normaliseKey( $namespace, $key );
 300+ $normkey = self::normaliseKey( $namespace, $key );
301301 $index = self::messageIndex();
302 - return @$index[$key];
 302+ $group = @$index[$normkey];
 303+
 304+ global $wgEnablePageTranslation;
 305+ if ( $wgEnablePageTranslation && !$group ) {
 306+ $group = MessageIndex::messageToGroup( $namespace, $key );
 307+ }
 308+ return $group;
 309+
303310 }
304311
305312 public static function normaliseKey( $namespace, $key ) {

Status & tagging log