ApiHardMessages.php

Go to the documentation of this file.
00001 <?php
00018 class ApiHardMessages extends ApiBase {
00019     protected static $right = 'translate';
00020 
00021     public function execute() {
00022         if ( !$this->getUser()->isAllowed( self::$right ) ) {
00023             $this->dieUsage( 'Permission denied', 'permissiondenied' );
00024         }
00025 
00026         $params = $this->extractRequestParams();
00027         $title = Title::newFromText( $params['title'] );
00028 
00029         if ( !$title ) {
00030             $this->dieUsage( 'Invalid title', 'invalidtitle' );
00031         }
00032 
00033         $handle = new MessageHandle( $title );
00034         if ( !$handle->isValid() ) {
00035             $this->dieUsage( 'Invalid title', 'invalidtitle' );
00036         }
00037 
00038         $baseTitle = Title::makeTitle( $title->getNamespace(),
00039             $handle->getKey() . '/' . $handle->getGroup()->getSourceLanguage() );
00040         $revision = Revision::newFromTitle( $baseTitle );
00041 
00042         if ( !$revision ) {
00043             // This can fail. See https://bugzilla.wikimedia.org/show_bug.cgi?id=43286
00044             $this->dieUsage( 'Invalid revision', 'invalidrevision' );
00045         }
00046 
00047         $count = self::getHardCount( $revision ) + 1;
00048         self::doMarkHard( $revision, $count );
00049 
00050         $output = array(
00051             'title' => $baseTitle->getPrefixedText(),
00052             'pageid' => $revision->getPage(),
00053             'revision' => $revision->getId(),
00054             'count' => $count
00055         );
00056 
00057         $this->getResult()->addValue( null, $this->getModuleName(), $output );
00058     }
00059 
00065     public static function doMarkHard( Revision $revision, $count ) {
00066         $dbw = wfGetDB( DB_MASTER );
00067         $table = 'revtag';
00068 
00069         if ( $count === 1 ) {
00070             $row = array(
00071                 'rt_type' => 'hard',
00072                 'rt_page' => $revision->getPage(),
00073                 'rt_revision' => $revision->getId(),
00074                 'rt_value' => $count
00075             );
00076             $options = array( 'IGNORE' );
00077             $dbw->insert( $table, $row, __METHOD__, $options );
00078         } else {
00079             $dbw->update( $table,
00080                 array(
00081                     'rt_value' => $count
00082                 ),
00083                 array(
00084                     'rt_type' => 'hard',
00085                     'rt_page' => $revision->getPage(),
00086                 ),
00087                 __METHOD__
00088             );
00089         }
00090     }
00091 
00098     public static function getHardCount( Revision $revision ) {
00099         $dbr = wfGetDB( DB_SLAVE );
00100         $res = $dbr->selectField(
00101             'revtag',
00102             'rt_value',
00103             array( 'rt_type = "hard"',
00104                 'rt_page = ' . $revision->getPage(),
00105             ),
00106             __METHOD__
00107         );
00108 
00109         $count = intval( $res );
00110 
00111         return $count;
00112     }
00113 
00114     public function isWriteMode() {
00115         return true;
00116     }
00117 
00118     public function needsToken() {
00119         return true;
00120     }
00121 
00122     public function getAllowedParams() {
00123         return array(
00124             'title' => array(
00125                 ApiBase::PARAM_TYPE => 'string',
00126                 ApiBase::PARAM_REQUIRED => true,
00127             ),
00128             'token' => array(
00129                 ApiBase::PARAM_TYPE => 'string',
00130                 ApiBase::PARAM_REQUIRED => true,
00131             ),
00132         );
00133     }
00134 
00135     public function getParamDescription() {
00136         $action = TranslateUtils::getTokenAction( 'hardmessages' );
00137 
00138         return array(
00139             'title' => 'The title of the message to mark hard',
00140             'token' => "A token previously acquired with $action",
00141         );
00142     }
00143 
00144     public function getDescription() {
00145         return 'Mark translations hard';
00146     }
00147 
00148     public function getPossibleErrors() {
00149         $right = self::$right;
00150 
00151         return array_merge( parent::getPossibleErrors(), array(
00152             array( 'code' => 'permissiondenied', 'info' => "You must have $right right" ),
00153             array( 'code' => 'invalidtitle', 'info' => 'Title $1 is invalid' ),
00154             array( 'code' => 'invalidrevision', 'info' => 'Revision $1 is invalid' ),
00155         ) );
00156     }
00157 
00158     public function getExamples() {
00159         return array(
00160             'api.php?action=hardmessages&title=SampleTitle&token=foo',
00161         );
00162     }
00163 
00164     public function getVersion() {
00165         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00166     }
00167 
00168     public static function getToken() {
00169         $user = RequestContext::getMain()->getUser();
00170         if ( !$user->isAllowed( self::$right ) ) {
00171             return false;
00172         }
00173 
00174         return $user->getEditToken();
00175     }
00176 
00177     public static function injectTokenFunction( &$list ) {
00178         $list['hardmessage'] = array( __CLASS__, 'getToken' );
00179 
00180         return true;
00181     }
00182 
00183     public static function getRight() {
00184         return self::$right;
00185     }
00186 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3