r99762 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99761‎ | r99762 | r99763 >
Date:09:54, 14 October 2011
Author:nikerabbit
Status:resolved (Comments)
Tags:
Comment:
New API module: messagetranslations
Modified paths:
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/api/ApiQueryMessageTranslations.php (added) (history)

Diff [purge]

Index: trunk/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: trunk/extensions/Translate/README
@@ -29,6 +29,8 @@
3030 http://translatewiki.net/docs/Translate/html/
3131
3232 == Change log ==
 33+* 2011-10-14
 34+- New API module: messagetranslations
3335 * 2011-10-12
3436 - Multiple bug fixes and improvements to translatable page moving feature
3537 * 2011-10-07
Index: trunk/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: trunk/extensions/Translate/api/ApiQueryMessageTranslations.php
@@ -0,0 +1,131 @@
 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+
 71+ $tTitle = Title::makeTitle( $namespace, $key );
 72+ $tHandle = new MessageHandle( $tTitle );
 73+
 74+ $data = array(
 75+ 'title' => $tTitle->getPrefixedText(),
 76+ 'language' => $tHandle->getCode(),
 77+ 'lasttranslator' => $info[1],
 78+ );
 79+
 80+ $fuzzy = MessageHandle::hasFuzzyString( $info[0] ) || $tHandle->isFuzzy();
 81+
 82+ if ( $fuzzy ) {
 83+ $data['fuzzy'] = 'fuzzy';
 84+ }
 85+
 86+ $translation = str_replace( TRANSLATE_FUZZY, '', $info[0] );
 87+ $result->setContent( $data, $translation );
 88+
 89+ $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $data );
 90+ if ( !$fit ) {
 91+ $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
 92+ break;
 93+ }
 94+
 95+ }
 96+
 97+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
 98+ }
 99+
 100+ public function getAllowedParams() {
 101+ return array(
 102+ 'title' => array(
 103+ ApiBase::PARAM_TYPE => 'string',
 104+ ApiBase::PARAM_REQUIRED => true,
 105+ ),
 106+ 'offset' => array(
 107+ ApiBase::PARAM_DFLT => 0,
 108+ ApiBase::PARAM_TYPE => 'integer',
 109+ ),
 110+ );
 111+ }
 112+
 113+ public function getParamDescription() {
 114+ return array(
 115+ 'title' => 'Full title of a known message',
 116+ );
 117+ }
 118+
 119+ public function getDescription() {
 120+ return 'Query all translations for a single message';
 121+ }
 122+
 123+ protected function getExamples() {
 124+ return array(
 125+ "api.php?action=query&meta=messagetranslations&title=MediaWiki:January List of translations in the wiki for MediaWiki:January",
 126+ );
 127+ }
 128+
 129+ public function getVersion() {
 130+ return __CLASS__ . ': $Id$';
 131+ }
 132+}
Property changes on: trunk/extensions/Translate/api/ApiQueryMessageTranslations.php
___________________________________________________________________
Added: svn:eol-style
1133 + native
Added: svn:keywords
2134 + Id

Follow-up revisions

RevisionCommit summaryAuthorDate
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
r100004MFT r99673: possible fix for bug 31653: Translation page updates might not ha...siebrand23:12, 16 October 2011

Comments

#Comment by Siebrand (talk | contribs)   12:19, 14 October 2011

I was wondering if it might make sense to add a limiter paramter for this page, so that only a given array of translations will be returned, if available.

#Comment by Nikerabbit (talk | contribs)   12:26, 14 October 2011

Imho no, there is offset (which is not handled, oops) for the rare case that translations are so big they do not all fit in.

#Comment by Siebrand (talk | contribs)   12:25, 14 October 2011

The example in the API documentation is incorrect. For http://translatewiki.net/w/api.php?action=query&meta=messagetranslations&title=MediaWiki:January which is mentioned on http://translatewiki.net/w/api.php, I'm getting:

  <error code="mtnotitle" info="The title parameter must be set" xml:space="preserve">

Status & tagging log