ApiTranslateSandbox.php

Go to the documentation of this file.
00001 <?php
00014 class ApiTranslateSandbox extends ApiBase {
00015 
00016     public function execute() {
00017         global $wgTranslateUseSandbox;
00018         if ( !$wgTranslateUseSandbox ) {
00019             $this->dieUsage( 'Sandbox feature is not in use', 'sandboxdisabled' );
00020         }
00021 
00022         $params = $this->extractRequestParams();
00023         switch ( $params['do'] ) {
00024             case 'create':
00025                 $this->doCreate();
00026                 break;
00027             case 'delete':
00028                 $this->doDelete();
00029                 break;
00030             case 'promote':
00031                 $this->doPromote();
00032                 break;
00033             case 'remind':
00034                 $this->doRemind();
00035                 break;
00036         }
00037     }
00038 
00039     protected function doCreate() {
00040         $params = $this->extractRequestParams();
00041 
00042         // Do validations
00043         foreach ( explode( '|', 'username|password|email' ) as $field ) {
00044             if ( !isset( $params[$field] ) ) {
00045                 $this->dieUsage( "Missing parameter $field", 'missingparam' );
00046             }
00047         }
00048 
00049         $username = $params['username'];
00050         if ( User::getCanonicalName( $username, 'creatable' ) === false ) {
00051             $this->dieUsage( "User name is not acceptable", 'invalidusername' );
00052         }
00053 
00054         $user = User::newFromName( $username );
00055         if ( $user->getID() !== 0 ) {
00056             $this->dieUsage( "User name is in use", 'nonfreeusername' );
00057         }
00058 
00059         $password = $params['password'];
00060         if ( !$user->isValidPassword( $password ) ) {
00061             $this->dieUsage( "Password is not acceptable", 'invalidpassword' );
00062         }
00063 
00064         $email = $params['email'];
00065         if ( !Sanitizer::validateEmail( $email ) ) {
00066             $this->dieUsage( "Email is not acceptable", 'invalidemail' );
00067         }
00068 
00069         $user = TranslateSandbox::addUser( $username, $email, $password );
00070         $output = array( 'user' => array(
00071             'name' => $user->getName(),
00072             'id' => $user->getId(),
00073         ) );
00074 
00075         $this->getResult()->addValue( null, $this->getModuleName(), $output );
00076     }
00077 
00078     protected function doDelete() {
00079         if ( !$this->getUser()->isAllowed( 'translate-sandboxmanage' ) ) {
00080             $this->dieUsage( 'Access denied', 'missingperms' );
00081         }
00082 
00083         $params = $this->extractRequestParams();
00084 
00085         foreach ( $params['userid'] as $user ) {
00086             $user = User::newFromId( $user );
00087 
00088             try {
00089                 TranslateSandbox::deleteUser( $user );
00090             } catch ( MWException $e ) {
00091                 $this->dieUsage( $e->getMessage(), 'invalidparam' );
00092             }
00093         }
00094     }
00095 
00096     protected function doPromote() {
00097         if ( !$this->getUser()->isAllowed( 'translate-sandboxmanage' ) ) {
00098             $this->dieUsage( 'Access denied', 'missingperms' );
00099         }
00100 
00101         $params = $this->extractRequestParams();
00102 
00103         foreach ( $params['userid'] as $user ) {
00104             $user = User::newFromId( $user );
00105 
00106             try {
00107                 TranslateSandbox::promoteUser( $user );
00108             } catch ( MWException $e ) {
00109                 $this->dieUsage( $e->getMessage(), 'invalidparam' );
00110             }
00111         }
00112     }
00113 
00114     protected function doRemind() {
00115         $params = $this->extractRequestParams();
00116 
00117         // Do validations
00118         foreach ( explode( '|', 'subject|body' ) as $field ) {
00119             if ( !isset( $params[$field] ) ) {
00120                 $this->dieUsage( "Missing parameter $field", 'missingparam' );
00121             }
00122         }
00123 
00124         foreach ( $params['userid'] as $user ) {
00125             $user = User::newFromId( $user );
00126 
00127             try {
00128                 TranslateSandbox::sendReminder( $this->getUser(), $user, $params['subject'], $params['body'] );
00129             } catch ( MWException $e ) {
00130                 $this->dieUsage( $e->getMessage(), 'invalidparam' );
00131             }
00132         }
00133     }
00134 
00135     public function mustBePosted() {
00136         return true;
00137     }
00138 
00139     public function isWriteMode() {
00140         return true;
00141     }
00142 
00143     public function needsToken() {
00144         return true;
00145     }
00146 
00147     public function getTokenSalt() {
00148         return 'sandbox';
00149     }
00150 
00151     public static function getToken() {
00152         // Who designed this?!?!?!
00153         $user = RequestContext::getMain()->getUser();
00154         return $user->getEditToken( 'sandbox' );
00155     }
00156 
00157     public function getAllowedParams() {
00158         return array(
00159             'do' => array(
00160                 ApiBase::PARAM_TYPE => array( 'create', 'delete', 'promote', 'remind' ),
00161                 ApiBase::PARAM_REQUIRED => true,
00162             ),
00163             'userid' => array(
00164                 ApiBase::PARAM_TYPE => 'integer',
00165                 ApiBase::PARAM_DFLT => 0,
00166                 ApiBase::PARAM_ISMULTI => true,
00167             ),
00168             'token' => array(
00169                 ApiBase::PARAM_TYPE => 'string',
00170                 ApiBase::PARAM_REQUIRED => true,
00171             ),
00172             'username' => array( ApiBase::PARAM_TYPE => 'string' ),
00173             'password' => array( ApiBase::PARAM_TYPE => 'string' ),
00174             'email' => array( ApiBase::PARAM_TYPE => 'string' ),
00175             'subject' => array( ApiBase::PARAM_TYPE => 'string' ),
00176             'body' => array( ApiBase::PARAM_TYPE => 'string' ),
00177         );
00178     }
00179 
00180     public function getParamDescription() {
00181         $action = TranslateUtils::getTokenAction( 'translatesandbox' );
00182 
00183         return array(
00184             'do' => 'What to do',
00185             'userid' => 'User ids of the users being managed. Use 0 for creations.',
00186             'token' => "A token previously acquired with $action",
00187             'username' => 'Username when creating user',
00188             'password' => 'Password when creating user',
00189             'email' => 'Email when creating user',
00190             'subject' => 'Subject of the reminder email when reminding',
00191             'body' => 'Body of the reminder email when reminding',
00192         );
00193     }
00194 
00195     public function getDescription() {
00196         return 'Signup and manage sandboxed users';
00197     }
00198 
00199     public static function injectTokenFunction( &$list ) {
00200         $list['translatesandbox'] = array( __CLASS__, 'getToken' );
00201 
00202         return true;
00203     }
00204 
00205     // BC for MW 1.20
00206     public function getVersion() {
00207         return __CLASS__ . ': ' . TRANSLATE_VERSION;
00208     }
00209 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3