ApiQueryMessageTranslations.php

Go to the documentation of this file.
00001 <?php
00016 class ApiQueryMessageTranslations extends ApiQueryBase {
00017 
00018     public function __construct( $query, $moduleName ) {
00019         parent::__construct( $query, $moduleName, 'mt' );
00020     }
00021 
00022     public function getCacheMode( $params ) {
00023         return 'public';
00024     }
00025 
00032     public static function getTranslations( MessageHandle $handle ) {
00033         $namespace = $handle->getTitle()->getNamespace();
00034         $base = $handle->getKey();
00035 
00036         $dbr = wfGetDB( DB_SLAVE );
00037 
00038         $res = $dbr->select( 'page',
00039             array( 'page_namespace', 'page_title' ),
00040             array(
00041                 'page_namespace' => $namespace,
00042                 'page_title ' . $dbr->buildLike( "$base/", $dbr->anyString() ),
00043             ),
00044             __METHOD__,
00045             array(
00046                 'ORDER BY' => 'page_title',
00047                 'USE INDEX' => 'name_title',
00048             )
00049         );
00050 
00051         $titles = array();
00052         foreach ( $res as $row ) {
00053             $titles[] = $row->page_title;
00054         }
00055 
00056         if ( $titles === array() ) {
00057             return array();
00058         }
00059 
00060         $pageInfo = TranslateUtils::getContents( $titles, $namespace );
00061 
00062         return $pageInfo;
00063     }
00064 
00065     public function execute() {
00066         $params = $this->extractRequestParams();
00067 
00068         $title = Title::newFromText( $params['title'] );
00069         if ( !$title ) {
00070             $this->dieUsage( 'Invalid title', 'invalidtitle' );
00071         }
00072 
00073         $handle = new MessageHandle( $title );
00074         if ( !$handle->isValid() ) {
00075             $this->dieUsage(
00076                 'Title does not correspond to a translatable message',
00077                 'nomessagefortitle'
00078             );
00079         }
00080 
00081         $namespace = $title->getNamespace();
00082         $pageInfo = self::getTranslations( $handle );
00083 
00084         $result = $this->getResult();
00085         $count = 0;
00086 
00087         foreach ( $pageInfo as $key => $info ) {
00088             if ( ++$count <= $params['offset'] ) {
00089                 continue;
00090             }
00091 
00092             $tTitle = Title::makeTitle( $namespace, $key );
00093             $tHandle = new MessageHandle( $tTitle );
00094 
00095             $data = array(
00096                 'title' => $tTitle->getPrefixedText(),
00097                 'language' => $tHandle->getCode(),
00098                 'lasttranslator' => $info[1],
00099             );
00100 
00101             $fuzzy = MessageHandle::hasFuzzyString( $info[0] ) || $tHandle->isFuzzy();
00102 
00103             if ( $fuzzy ) {
00104                 $data['fuzzy'] = 'fuzzy';
00105             }
00106 
00107             $translation = str_replace( TRANSLATE_FUZZY, '', $info[0] );
00108             $result->setContent( $data, $translation );
00109 
00110             $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $data );
00111             if ( !$fit ) {
00112                 $this->setContinueEnumParameter( 'offset', $count );
00113                 break;
00114             }
00115         }
00116 
00117         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'message' );
00118     }
00119 
00120     public function getAllowedParams() {
00121         return array(
00122             'title' => array(
00123                 ApiBase::PARAM_TYPE => 'string',
00124                 ApiBase::PARAM_REQUIRED => true,
00125             ),
00126             'offset' => array(
00127                 ApiBase::PARAM_DFLT => 0,
00128                 ApiBase::PARAM_TYPE => 'integer',
00129             ),
00130         );
00131     }
00132 
00133     public function getParamDescription() {
00134         return array(
00135             'title' => 'Full title of a known message',
00136         );
00137     }
00138 
00139     public function getDescription() {
00140         return 'Query all translations for a single message';
00141     }
00142 
00143     public function getPossibleErrors() {
00144         return array_merge( parent::getPossibleErrors(), array(
00145             array( 'code' => 'invalidtitle', 'info' => 'The given title is invalid' ),
00146             array(
00147                 'code' => 'nomessagefortitle',
00148                 'info' => 'Title does not correspond to a translatable message'
00149             ),
00150         ) );
00151     }
00152 
00153     protected function getExamples() {
00154         return array(
00155             "api.php?action=query&meta=messagetranslations&mttitle=MediaWiki:January " .
00156                 "List of translations in the wiki for MediaWiki:January",
00157         );
00158     }
00159 
00160     public function getVersion() {
00161         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00162     }
00163 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3