ApiQueryMessageCollection.php

Go to the documentation of this file.
00001 <?php
00016 class ApiQueryMessageCollection extends ApiQueryGeneratorBase {
00017 
00018     public function __construct( $query, $moduleName ) {
00019         parent::__construct( $query, $moduleName, 'mc' );
00020     }
00021 
00022     public function execute() {
00023         $this->run();
00024     }
00025 
00026     public function getCacheMode( $params ) {
00027         return 'public';
00028     }
00029 
00030     public function executeGenerator( $resultPageSet ) {
00031         $this->run( $resultPageSet );
00032     }
00033 
00037     private function run( $resultPageSet = null ) {
00038         $params = $this->extractRequestParams();
00039 
00040         $group = MessageGroups::getGroup( $params['group'] );
00041         if ( !$group ) {
00042             $this->dieUsageMsg( array( 'missingparam', 'mcgroup' ) );
00043         }
00044 
00045         if ( MessageGroups::isDynamic( $group ) ) {
00049             $group->setLanguage( $params['language'] );
00050         }
00051 
00052         $result = $this->getResult();
00053 
00054         $languages = $group->getTranslatableLanguages();
00055 
00056         if ( $languages !== null && !isset( $languages[$params['language']] ) ) {
00057             $this->dieUsage(
00058                 'Translation to this language is disabled',
00059                 'translate-language-disabled'
00060             );
00061         }
00062 
00063         $messages = $group->initCollection( $params['language'] );
00064 
00065         foreach ( $params['filter'] as $filter ) {
00066             $value = null;
00067             if ( strpos( $filter, ':' ) !== false ) {
00068                 list( $filter, $value ) = explode( ':', $filter, 2 );
00069             }
00070             /* The filtering params here are swapped wrt MessageCollection.
00071              * There (fuzzy) means do not show fuzzy, which is the same as !fuzzy
00072              * here and fuzzy here means (fuzzy, false) there. */
00073             if ( $filter[0] === '!' ) {
00074                 $messages->filter( substr( $filter, 1 ), true, $value );
00075             } else {
00076                 $messages->filter( $filter, false, $value );
00077             }
00078         }
00079 
00080         $resultSize = count( $messages );
00081         $offsets = $messages->slice( $params['offset'], $params['limit'] );
00082         $batchSize = count( $messages );
00083         list( /*$backwardsOffset*/, $forwardsOffset, $startOffset ) = $offsets;
00084 
00085         $result->addValue(
00086             array( 'query', 'metadata' ),
00087             'state',
00088             self::getWorkflowState( $group->getId(), $params['language'] )
00089         );
00090 
00091         $result->addValue( array( 'query', 'metadata' ), 'resultsize', $resultSize );
00092         $result->addValue(
00093             array( 'query', 'metadata' ),
00094             'remaining',
00095             $resultSize - $startOffset - $batchSize
00096         );
00097 
00098         $messages->loadTranslations();
00099 
00100         $pages = array();
00101 
00102         if ( $forwardsOffset !== false ) {
00103             $this->setContinueEnumParameter( 'offset', $forwardsOffset );
00104         }
00105 
00106         $props = array_flip( $params['prop'] );
00107 
00108         foreach ( $messages->keys() as $mkey => $title ) {
00109             if ( is_null( $resultPageSet ) ) {
00110                 $data = $this->extractMessageData( $result, $props, $messages[$mkey] );
00111                 $data['title'] = $title->getPrefixedText();
00112 
00113                 $result->addValue( array( 'query', $this->getModuleName() ), null, $data );
00114             } else {
00115                 $pages[] = $title;
00116             }
00117         }
00118 
00119         if ( is_null( $resultPageSet ) ) {
00120             $result->setIndexedTagName_internal(
00121                 array( 'query', $this->getModuleName() ),
00122                 'message'
00123             );
00124         } else {
00125             $resultPageSet->populateFromTitles( $pages );
00126         }
00127     }
00128 
00135     public function extractMessageData( $result, $props, $message ) {
00136         $data['key'] = $message->key();
00137 
00138         if ( isset( $props['definition'] ) ) {
00139             $data['definition'] = $message->definition();
00140         }
00141         if ( isset( $props['translation'] ) ) {
00142             // Remove !!FUZZY!! from translation if present.
00143             $translation = $message->translation();
00144             if ( $translation !== null ) {
00145                 $translation = str_replace( TRANSLATE_FUZZY, '', $translation );
00146             }
00147             $data['translation'] = $translation;
00148         }
00149         if ( isset( $props['tags'] ) ) {
00150             $data['tags'] = $message->getTags();
00151             $result->setIndexedTagName( $data['tags'], 'tag' );
00152         }
00153         // BC
00154         if ( isset( $props['revision'] ) ) {
00155             $data['revision'] = $message->getProperty( 'revision' );
00156         }
00157         if ( isset( $props['properties'] ) ) {
00158             foreach ( $message->getPropertyNames() as $prop ) {
00159                 $data['properties'][$prop] = $message->getProperty( $prop );
00160                 $result->setIndexedTagName_recursive( $data['properties'], 'val' );
00161             }
00162         }
00163 
00164         return $data;
00165     }
00166 
00174     protected static function getWorkflowState( $groupId, $language ) {
00175         $dbr = wfGetDB( DB_SLAVE );
00176 
00177         return $dbr->selectField(
00178             'translate_groupreviews',
00179             'tgr_state',
00180             array(
00181                 'tgr_group' => $groupId,
00182                 'tgr_lang' => $language
00183             ),
00184             __METHOD__
00185         );
00186     }
00187 
00188     public function getAllowedParams() {
00189         return array(
00190             'group' => array(
00191                 ApiBase::PARAM_TYPE => 'string',
00192                 ApiBase::PARAM_REQUIRED => true,
00193             ),
00194             'language' => array(
00195                 ApiBase::PARAM_TYPE => 'string',
00196                 ApiBase::PARAM_DFLT => 'en',
00197             ),
00198             'limit' => array(
00199                 ApiBase::PARAM_DFLT => 500,
00200                 ApiBase::PARAM_TYPE => 'limit',
00201                 ApiBase::PARAM_MIN => 1,
00202                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00203                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00204             ),
00205             'offset' => array(
00206                 ApiBase::PARAM_DFLT => '',
00207                 ApiBase::PARAM_TYPE => 'string',
00208             ),
00209             'filter' => array(
00210                 ApiBase::PARAM_TYPE => 'string',
00211                 ApiBase::PARAM_DFLT => '!optional|!ignored',
00212                 ApiBase::PARAM_ISMULTI => true,
00213             ),
00214             'prop' => array(
00215                 ApiBase::PARAM_TYPE => array(
00216                     'definition',
00217                     'translation',
00218                     'tags',
00219                     'revision',
00220                     'properties'
00221                 ),
00222                 ApiBase::PARAM_DFLT => 'definition|translation',
00223                 ApiBase::PARAM_ISMULTI => true,
00224             ),
00225         );
00226     }
00227 
00228     public function getParamDescription() {
00229         return array(
00230             'group' => 'Message group',
00231             'language' => 'Language code',
00232             'offset' => 'Integer or key offset for start',
00233             'limit' => 'How many messages to show (after filtering)',
00234             'prop' => array(
00235                 'Which properties to get',
00236                 'definition  - message definition',
00237                 'translation - current translation (without !!FUZZY!! string if any, ' .
00238                     'use the tags to check for outdated or broken translations)',
00239                 'tags        - message tags, like optional, ignored and fuzzy',
00240                 'properties  - message properties, like status, revision, ' .
00241                     'last-translator. Can vary between messages.',
00242                 'revision    - deprecated! use properties!',
00243             ),
00244             'filter' => array(
00245                 'Message collection filters. Use ! to negate condition. For example ' .
00246                     '!fuzzy means list only all non-fuzzy messages. Filters are ' .
00247                     'applied in the order given.',
00248                 'fuzzy             - messages with fuzzy tag',
00249                 'optional          - messages which should be translated only if ' .
00250                     'changes are necessary',
00251                 'ignored           - messages which are never translated',
00252                 'hastranslation    - messages which have a translation regardless if it ' .
00253                     'is fuzzy or not',
00254                 'translated        - messages which have a translation which is not fuzzy',
00255                 'changed           - messages which has been translated or changed since ' .
00256                     'last export',
00257                 'reviewer:#        - messages where given userid # is among reviewers',
00258                 'last-translator:# - messages where given userid # is the last translator',
00259             ),
00260         );
00261     }
00262 
00263     public function getDescription() {
00264         return 'Query MessageCollection about translations';
00265     }
00266 
00267     protected function getExamples() {
00268         $group = 'page-Example';
00269 
00270         return array(
00271             'api.php?action=query&meta=siteinfo&siprop=languages List of supported languages',
00272             "api.php?action=query&list=messagecollection&mcgroup=$group " .
00273                 "List of non-optional message definitions for group $group",
00274             "api.php?action=query&list=messagecollection&mcgroup=$group&mclanguage=fi&" .
00275                 "mcprop=definition|translation|tags&mcfilter=optional " .
00276                 "List of optional messages in Finnish with tags for group $group",
00277             "api.php?action=query&generator=messagecollection&gmcgroup=$group" .
00278                 "&gmclanguage=nl&prop=revisions " .
00279                 "More information about latest translation revisions for group $group",
00280         );
00281     }
00282 
00283     public function getVersion() {
00284         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00285     }
00286 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3