ApiTranslationStash.php

Go to the documentation of this file.
00001 <?php
00015 class ApiTranslationStash extends ApiBase {
00016     public function execute() {
00017         $params = $this->extractRequestParams();
00018 
00019         // The user we are operating on, not necessarly the user making the request
00020         $user = $this->getUser();
00021 
00022         if ( isset( $params['username'] ) ){
00023             if ( $this->getUser()->isAllowed( 'translate-sandboxmanage' ) ) {
00024                 $user = User::newFromName( $params['username'] );
00025                 if ( !$user ) {
00026                     $this->dieUsageMsg( array( 'invalidparam', 'username' ) );
00027                 }
00028             } else {
00029                 $this->dieUsageMsg( array( 'invalidparam', 'username' ) );
00030             }
00031         }
00032 
00033         $stash = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
00034         $action = $params['subaction'];
00035 
00036         if ( $action === 'add' ) {
00037             if ( !isset( $params['title'] ) ) {
00038                 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
00039             }
00040             if ( !isset( $params['translation'] ) ) {
00041                 $this->dieUsageMsg( array( 'missingparam', 'translation' ) );
00042             }
00043 
00044             // @todo: Return value of Title::newFromText not checked
00045             $translation = new StashedTranslation(
00046                 $user,
00047                 Title::newFromText( $params['title'] ),
00048                 $params['translation'],
00049                 FormatJson::decode( $params['metadata'], true )
00050             );
00051             $stash->addTranslation( $translation );
00052         }
00053 
00054         if ( $action === 'query' ) {
00055             $output['translations'] = array();
00056 
00057             $translations = $stash->getTranslations( $user );
00058             foreach( $translations as $translation ) {
00059                 $output['translations'][] = $this->formatTranslation( $translation );
00060             }
00061         }
00062 
00063         // If we got this far, nothing has failed
00064         $output['result'] = 'ok';
00065         $this->getResult()->addValue( null, $this->getModuleName(), $output );
00066     }
00067 
00068     protected function formatTranslation( StashedTranslation $translation ) {
00069         $title = $translation->getTitle();
00070         $handle = new MessageHandle( $title );
00071 
00072         // Prepare for the worst
00073         $definition = '';
00074         $comparison = '';
00075         if ( $handle->isValid() ) {
00076             $groupId = MessageIndex::getPrimaryGroupId( $handle );
00077             $group = MessageGroups::getGroup( $groupId );
00078 
00079             $key = $handle->getKey();
00080 
00081             $definition = $group->getMessage( $key, $group->getSourceLanguage() );
00082             $comparison = $group->getMessage( $key, $handle->getCode() );
00083         }
00084 
00085         return array(
00086             'title' => $title->getPrefixedText(),
00087             'definition' => $definition,
00088             'translation' => $translation->getValue(),
00089             'comparison' => $comparison,
00090             'metadata' => $translation->getMetadata(),
00091         );
00092     }
00093 
00094 
00095     public function isWriteMode() {
00096         return true;
00097     }
00098 
00099     public function getTokenSalt() {
00100         return 'translationstash';
00101     }
00102 
00103     public static function getToken() {
00104         $user = RequestContext::getMain()->getUser();
00105 
00106         return $user->getEditToken( 'translationstash' );
00107     }
00108 
00109     public static function injectTokenFunction( &$list ) {
00110         $list['translationstash'] = array( __CLASS__, 'getToken' );
00111 
00112         return true;
00113     }
00114 
00115     public function getAllowedParams() {
00116         return array(
00117             'subaction' => array(
00118                 ApiBase::PARAM_TYPE => array( 'add', 'query' ),
00119                 ApiBase::PARAM_REQUIRED => true,
00120             ),
00121             'title' => array(
00122                 ApiBase::PARAM_TYPE => 'string',
00123             ),
00124             'translation' => array(
00125                 ApiBase::PARAM_TYPE => 'string',
00126             ),
00127             'metadata' => array(
00128                 ApiBase::PARAM_TYPE => 'string',
00129                 ApiBase::PARAM_DFLT => null,
00130             ),
00131             'token' => array(
00132                 ApiBase::PARAM_TYPE => 'string',
00133                 ApiBase::PARAM_REQUIRED => true,
00134             ),
00135             'username' => array(
00136                 ApiBase::PARAM_TYPE => 'string',
00137             ),
00138         );
00139     }
00140 
00141     public function getParamDescription() {
00142         $action = TranslateUtils::getTokenAction( 'translationstash' );
00143 
00144         return array(
00145             'subaction' => 'Action',
00146             'title' => 'Title of the translation unit page',
00147             'translation' => 'Translation made by the user',
00148             'metadata' => 'Json object',
00149             'token' => "A token previously acquired with $action",
00150             'username' => 'Optionally the user whose stash to get. '
00151                 . 'Only priviledged users can do this',
00152         );
00153     }
00154 
00155     public function getDescription() {
00156         return 'Add translations to stash';
00157     }
00158 
00159     public function getExamples() {
00160         return array(
00161             "api.php?action=translationstash&subaction=add&title=MediaWiki:Jan/fi&" .
00162                 "translation=tammikuu&metadata={}",
00163             "api.php?action=translationstash&subaction=query",
00164         );
00165     }
00166 
00167     // BC for old MW
00168     public function getVersion() {
00169         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00170     }
00171 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3