r100004 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100003‎ | r100004 | r100005 >
Date:23:12, 16 October 2011
Author:siebrand
Status:ok
Tags:
Comment:
MFT r99673: possible fix for bug 31653: Translation page updates might not happen when there is replication lag
MFT r99762, r99763, r99764: New API module: messagetranslations
Modified paths:
  • /branches/wmf/1.18wmf1/extensions/Translate (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/Translate/MessageCollection.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/Translate/Translate.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/Translate/_autoload.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/Translate/api/ApiQueryMessageTranslations.php (added) (history)
  • /branches/wmf/1.18wmf1/extensions/Translate/tag/RenderJob.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/Translate/tag/TPParse.php (modified) (history)

Diff [purge]

Index: branches/wmf/1.18wmf1/extensions/Translate/MessageCollection.php
@@ -188,11 +188,13 @@
189189
190190 /**
191191 * Loads all message data. Must be called before accessing the messages
192 - * with ArrayAccess or iteration.
 192+ * with ArrayAccess or iteration. Must be called before filtering for
 193+ * $dbtype to have an effect.
 194+ * @param $dbtype One of DB_* constants.
193195 */
194 - public function loadTranslations() {
195 - $this->loadData( $this->keys );
196 - $this->loadInfo( $this->keys );
 196+ public function loadTranslations( $dbtype = DB_SLAVE ) {
 197+ $this->loadData( $this->keys, $dbtype );
 198+ $this->loadInfo( $this->keys, $dbtype );
197199 $this->initMessages();
198200 }
199201
@@ -476,8 +478,9 @@
477479 /**
478480 * Loads existence and fuzzy state for given list of keys.
479481 * @param $keys \list{String} List of keys in database format.
 482+ * @param $dbtype One of DB_* constants.
480483 */
481 - protected function loadInfo( array $keys ) {
 484+ protected function loadInfo( array $keys, $dbtype = DB_SLAVE ) {
482485 if ( $this->dbInfo !== null ) {
483486 return;
484487 }
@@ -488,7 +491,7 @@
489492 return;
490493 }
491494
492 - $dbr = wfGetDB( DB_SLAVE );
 495+ $dbr = wfGetDB( $dbtype );
493496
494497 $tables = array( 'page', 'revtag' );
495498 $fields = array( 'page_title', 'rt_type' );
@@ -509,8 +512,9 @@
510513 /**
511514 * Loads translation for given list of keys.
512515 * @param $keys \list{String} List of keys in database format.
 516+ * @param $dbtype One of DB_* constants.
513517 */
514 - protected function loadData( $keys ) {
 518+ protected function loadData( $keys, $dbtype = DB_SLAVE ) {
515519 if ( $this->dbData !== null ) {
516520 return;
517521 }
@@ -521,7 +525,7 @@
522526 return;
523527 }
524528
525 - $dbr = wfGetDB( DB_SLAVE );
 529+ $dbr = wfGetDB( $dbtype );
526530
527531 $tables = array( 'page', 'revision', 'text' );
528532 $fields = array( 'page_title', 'rev_user_text', 'old_flags', 'old_text' );
Index: branches/wmf/1.18wmf1/extensions/Translate/tag/RenderJob.php
@@ -51,6 +51,7 @@
5252
5353 $group = $page->getMessageGroup();
5454 $collection = $group->initCollection( $code );
 55+ $collection->loadTranslations( DB_MASTER );
5556
5657 $text = $page->getParse()->getTranslationPageText( $collection );
5758
Index: branches/wmf/1.18wmf1/extensions/Translate/tag/TPParse.php
@@ -177,7 +177,7 @@
178178 // For finding the messages
179179 $prefix = $this->title->getPrefixedDBKey() . '/';
180180
181 - if ( $collection instanceOf MessageCollection ) {
 181+ if ( $collection instanceof MessageCollection ) {
182182 $collection->filter( 'hastranslation', false );
183183 $collection->loadTranslations();
184184 }
Index: branches/wmf/1.18wmf1/extensions/Translate/Translate.php
@@ -84,6 +84,7 @@
8585 // API
8686 $wgAPIListModules['messagecollection'] = 'ApiQueryMessageCollection';
8787 $wgAPIMetaModules['messagegroups'] = 'ApiQueryMessageGroups';
 88+$wgAPIMetaModules['messagetranslations'] = 'ApiQueryMessageTranslations';
8889
8990 // Register hooks.
9091 $wgHooks['EditPage::showEditForm:initial'][] = 'TranslateEditAddons::addTools';
Index: branches/wmf/1.18wmf1/extensions/Translate/_autoload.php
@@ -192,4 +192,5 @@
193193 */
194194 $wgAutoloadClasses['ApiQueryMessageCollection'] = $dir . 'api/ApiQueryMessageCollection.php';
195195 $wgAutoloadClasses['ApiQueryMessageGroups'] = $dir . 'api/ApiQueryMessageGroups.php';
 196+$wgAutoloadClasses['ApiQueryMessageTranslations'] = $dir . 'api/ApiQueryMessageTranslations.php';
196197 /**@}*/
Index: branches/wmf/1.18wmf1/extensions/Translate/api/ApiQueryMessageTranslations.php
@@ -0,0 +1,134 @@
 2+<?php
 3+/**
 4+ * Api module for querying message translations.
 5+ *
 6+ * @file
 7+ * @author Niklas Laxström
 8+ * @copyright Copyright © 2011, Niklas Laxström
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+/**
 13+ * Api module for querying message translations.
 14+ *
 15+ * @ingroup API TranslateAPI
 16+ */
 17+class ApiQueryMessageTranslations extends ApiQueryBase {
 18+
 19+ public function __construct( $query, $moduleName ) {
 20+ parent::__construct( $query, $moduleName, 'mt' );
 21+ }
 22+
 23+ public function getCacheMode( $params ) {
 24+ return 'public';
 25+ }
 26+
 27+ public function execute() {
 28+ $params = $this->extractRequestParams();
 29+
 30+ $title = Title::newFromText( $params['title'] );
 31+ if ( !$title ) {
 32+ $this->dieUsage( 'Invalid title' );
 33+ }
 34+
 35+ $handle = new MessageHandle( $title );
 36+ if ( !$handle->isValid() ) {
 37+ $this->dieUsage( 'Title does not correspond to a translatable message' );
 38+ }
 39+
 40+ $base = Title::makeTitle( $title->getNamespace(), $handle->getKey() );
 41+ $namespace = $base->getNamespace();
 42+ $message = $base->getDBKey();
 43+
 44+ $dbr = wfGetDB( DB_SLAVE );
 45+
 46+ $res = $dbr->select( 'page',
 47+ array( 'page_namespace', 'page_title' ),
 48+ array(
 49+ 'page_namespace' => $namespace,
 50+ 'page_title ' . $dbr->buildLike( "$message/", $dbr->anyString() ),
 51+ ),
 52+ __METHOD__,
 53+ array(
 54+ 'ORDER BY' => 'page_title',
 55+ 'USE INDEX' => 'name_title',
 56+ )
 57+ );
 58+
 59+ $titles = array();
 60+ foreach ( $res as $row ) {
 61+ $titles[] = $row->page_title;
 62+ }
 63+ $pageInfo = TranslateUtils::getContents( $titles, $namespace );
 64+
 65+ $result = $this->getResult();
 66+ $pages = array();
 67+ $count = 0;
 68+
 69+ foreach ( $pageInfo as $key => $info ) {
 70+ if ( ++$count <= $params['offset'] ) {
 71+ continue;
 72+ }
 73+
 74+ $tTitle = Title::makeTitle( $namespace, $key );
 75+ $tHandle = new MessageHandle( $tTitle );
 76+
 77+ $data = array(
 78+ 'title' => $tTitle->getPrefixedText(),
 79+ 'language' => $tHandle->getCode(),
 80+ 'lasttranslator' => $info[1],
 81+ );
 82+
 83+ $fuzzy = MessageHandle::hasFuzzyString( $info[0] ) || $tHandle->isFuzzy();
 84+
 85+ if ( $fuzzy ) {
 86+ $data['fuzzy'] = 'fuzzy';
 87+ }
 88+
 89+ $translation = str_replace( TRANSLATE_FUZZY, '', $info[0] );
 90+ $result->setContent( $data, $translation );
 91+
 92+ $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $data );
 93+ if ( !$fit ) {
 94+ $this->setContinueEnumParameter( 'offset', $count );
 95+ break;
 96+ }
 97+
 98+ }
 99+
 100+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
 101+ }
 102+
 103+ public function getAllowedParams() {
 104+ return array(
 105+ 'title' => array(
 106+ ApiBase::PARAM_TYPE => 'string',
 107+ ApiBase::PARAM_REQUIRED => true,
 108+ ),
 109+ 'offset' => array(
 110+ ApiBase::PARAM_DFLT => 0,
 111+ ApiBase::PARAM_TYPE => 'integer',
 112+ ),
 113+ );
 114+ }
 115+
 116+ public function getParamDescription() {
 117+ return array(
 118+ 'title' => 'Full title of a known message',
 119+ );
 120+ }
 121+
 122+ public function getDescription() {
 123+ return 'Query all translations for a single message';
 124+ }
 125+
 126+ protected function getExamples() {
 127+ return array(
 128+ "api.php?action=query&meta=messagetranslations&mttitle=MediaWiki:January List of translations in the wiki for MediaWiki:January",
 129+ );
 130+ }
 131+
 132+ public function getVersion() {
 133+ return __CLASS__ . ': $Id$';
 134+ }
 135+}
Property changes on: branches/wmf/1.18wmf1/extensions/Translate/api/ApiQueryMessageTranslations.php
___________________________________________________________________
Added: svn:eol-style
1136 + native
Added: svn:keywords
2137 + Id
Property changes on: branches/wmf/1.18wmf1/extensions/Translate
___________________________________________________________________
Modified: svn:mergeinfo
3138 Merged /trunk/translatewiki:r99673,99762-99764

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r99673Possible fix for bug 31653nikerabbit08:38, 13 October 2011
r99762New API module: messagetranslationsnikerabbit09:54, 14 October 2011
r99763Followup r99762 - fix examplenikerabbit12:30, 14 October 2011
r99764Followup r99762 - no point adding continue tag if offset is not handled :)nikerabbit12:55, 14 October 2011

Status & tagging log