TranslateSandbox.php

Go to the documentation of this file.
00001 <?php
00013 class TranslateSandbox {
00022     public static function addUser( $name, $email, $password ) {
00023         $user = User::newFromName( $name, 'creatable' );
00024         if ( !$user instanceof User ) {
00025             throw new MWException( "Invalid user name" );
00026         }
00027 
00028         $user->setEmail( $email );
00029         $user->setPassword( $password );
00030         $status = $user->addToDatabase();
00031 
00032         if ( !$status->isOK() ) {
00033             throw new MWException( $status->getWikiText() );
00034         }
00035 
00036         // Need to have an id first
00037         $user->addGroup( 'translate-sandboxed' );
00038         $user->clearInstanceCache( 'name' );
00039 
00040         return $user;
00041     }
00042 
00048     public static function deleteUser( User $user ) {
00049         $uid = $user->getId();
00050 
00051         if ( !self::isSandboxed( $user ) ) {
00052             throw new MWException( "Not a sandboxed user" );
00053         }
00054 
00055         $dbw = wfGetDB( DB_MASTER );
00056         $dbw->delete( 'user', array( 'user_id' => $uid ), __METHOD__ );
00057         $dbw->delete( 'user_groups', array( 'ug_user' => $uid ), __METHOD__ );
00058 
00059         $user->clearInstanceCache( 'defaults' );
00060         // @todo why the bunny is this private?!
00061         // $user->clearSharedCache();
00062         global $wgMemc;
00063         $wgMemc->delete( wfMemcKey( 'user', 'id', $uid ) );
00064     }
00065 
00070     public static function getUsers() {
00071         $dbw = wfGetDB( DB_MASTER );
00072         $tables = array( 'user', 'user_groups' );
00073         $fields = User::selectFields();
00074         $conds = array(
00075             'ug_group' => 'translate-sandboxed',
00076             'ug_user = user_id',
00077         );
00078 
00079         $res = $dbw->select( $tables, $fields, $conds, __METHOD__ );
00080 
00081         return UserArray::newFromResult( $res );
00082     }
00083 
00089     public static function promoteUser( User $user ) {
00090         global $wgTranslateSandboxPromotedGroup;
00091 
00092         if ( !self::isSandboxed( $user ) ) {
00093             throw new MWException( "Not a sandboxed user" );
00094         }
00095 
00096         $user->removeGroup( 'translate-sandboxed' );
00097         if ( $wgTranslateSandboxPromotedGroup ) {
00098             $user->addGroup( $wgTranslateSandboxPromotedGroup );
00099         }
00100     }
00101 
00110     public static function sendReminder( User $sender, User $target, $subject, $body ) {
00111         global $wgNoReplyAddress;
00112 
00113         if ( !self::isSandboxed( $user ) ) {
00114             throw new MWException( "Not a sandboxed user" );
00115         }
00116 
00117         $params = array(
00118             'user' => $target->getId(),
00119             'to' => $target->getEmail(),
00120             'from' => $sender->getEmail(),
00121             'replyto' => $wgNoReplyAddress,
00122             'subj' => $subject,
00123             'body' => $body,
00124         );
00125 
00126         TranslateSandboxReminderJob::newJob( $params )->insert();
00127     }
00128 
00135     public static function isSandboxed( User $user ) {
00136         if ( in_array( 'translate-sandboxed', $user->getGroups(), true ) ) {
00137             return true;
00138         }
00139 
00140         return false;
00141     }
00142 
00144     public static function enforcePermissions( User $user, array &$rights ) {
00145         global $wgTranslateUseSandbox;
00146 
00147         if ( !$wgTranslateUseSandbox ) {
00148             return true;
00149         }
00150 
00151         if ( !self::isSandboxed( $user ) ) {
00152             return true;
00153         }
00154 
00155         $rights = array( 'read', 'translate-sandboxaction', 'readapi', 'writeapi', 'editmyoptions' );
00156 
00157         // Do not let other hooks add more actions
00158         return false;
00159     }
00160 
00162     public static function onGetPreferences( $user, &$preferences ) {
00163         $preferences['translate-sandbox'] = $preferences['translate-sandbox-reminders'] =
00164             array( 'type' => 'api' );
00165 
00166         return true;
00167     }
00168 
00173     public static function onApiCheckCanExecute( ApiBase $module, User $user, &$message ) {
00174         $whitelist = array(
00175             // Obviously this is needed to get out of the sandbox
00176             'ApiTranslationStash',
00177             // Used by UniversalLanguageSelector for example
00178             'ApiOptions'
00179         );
00180 
00181         if ( TranslateSandbox::isSandboxed( $user ) ) {
00182             $class = get_class( $module );
00183             if ( $module->isWriteMode() && !in_array( $class, $whitelist, true ) ) {
00184                 $message = 'writerequired';
00185                 return false;
00186             }
00187         }
00188 
00189         return true;
00190     }
00191 }
Generated on Tue Oct 29 00:00:26 2013 for MediaWiki Translate Extension by  doxygen 1.6.3