ApiQueryTranslationAids.php

Go to the documentation of this file.
00001 <?php
00016 class ApiTranslationAids extends ApiBase {
00017     public function execute() {
00018         $params = $this->extractRequestParams();
00019 
00020         $title = Title::newFromText( $params['title'] );
00021         if ( !$title ) {
00022             $this->dieUsage( 'Invalid title', 'invalidtitle' );
00023         }
00024 
00025         $handle = new MessageHandle( $title );
00026         if ( !$handle->isValid() ) {
00027             $this->dieUsage(
00028                 'Title does not correspond to a translatable message',
00029                 'nomessagefortitle'
00030             );
00031         }
00032 
00033         if ( strval( $params['group'] ) !== '' ) {
00034             $group = MessageGroups::getGroup( $params['group'] );
00035         } else {
00036             $group = $handle->getGroup();
00037         }
00038 
00039         if ( !$group ) {
00040             $this->dieUsage( 'Invalid group', 'invalidgroup' );
00041         }
00042 
00043         $data = array();
00044         $times = array();
00045 
00046         $props = $params['prop'];
00047 
00048         $types = $group->getTranslationAids();
00049         $result = $this->getResult();
00050         foreach ( $props as $type ) {
00051             // Do not proceed if translation aid is not supported for this message group
00052             if ( !isset( $types[$type] ) ) {
00053                 continue;
00054             }
00055 
00056             $start = microtime( true );
00057             $class = $types[$type];
00058             $obj = new $class( $group, $handle, $this );
00059 
00060             try {
00061                 $aid = $obj->getData();
00062             } catch ( TranslationHelperException $e ) {
00063                 $aid = array( 'error' => $e->getMessage() );
00064             }
00065 
00066             if ( isset( $aid['**'] ) ) {
00067                 $result->setIndexedTagName( $aid, $aid['**'] );
00068                 unset( $aid['**'] );
00069             }
00070 
00071             $data[$type] = $aid;
00072             $times[$type] = round( microtime( true ) - $start, 3 );
00073         }
00074 
00075         $result->addValue( null, 'helpers', $data );
00076         $result->addValue( null, 'times', $times );
00077     }
00078 
00079     public function getAllowedParams() {
00080         $props = array_keys( TranslationAid::getTypes() );
00081         wfRunHooks( 'TranslateTranslationAids', array( &$props ) );
00082 
00083         return array(
00084             'title' => array(
00085                 ApiBase::PARAM_TYPE => 'string',
00086                 ApiBase::PARAM_REQUIRED => true,
00087             ),
00088             'group' => array(
00089                 ApiBase::PARAM_TYPE => 'string',
00090             ),
00091             'prop' => array(
00092                 ApiBase::PARAM_DFLT => implode( '|', $props ),
00093                 ApiBase::PARAM_TYPE => $props,
00094                 ApiBase::PARAM_ISMULTI => true,
00095             ),
00096         );
00097     }
00098 
00099     public function getParamDescription() {
00100         return array(
00101             'title' => 'Full title of a known message',
00102             'group' => 'Message group the message belongs to. If empty then ' .
00103                 'primary group is used.',
00104             'prop' => 'Which translation helpers to include.',
00105         );
00106     }
00107 
00108     public function getDescription() {
00109         return 'Query all translations aids';
00110     }
00111 
00112     public function getPossibleErrors() {
00113         return array_merge( parent::getPossibleErrors(), array(
00114             array( 'code' => 'invalidtitle', 'info' => 'The given title is invalid' ),
00115             array( 'code' => 'invalidgroup', 'info' => 'The given or guessed group is invalid' ),
00116             array(
00117                 'code' => 'nomessagefortitle',
00118                 'info' => 'Title does not correspond to a translatable message'
00119             ),
00120         ) );
00121     }
00122 
00123     protected function getExamples() {
00124         return array(
00125             "api.php?action=translationaids&title=MediaWiki:January/fi",
00126         );
00127     }
00128 
00129     public function getVersion() {
00130         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00131     }
00132 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3