MediaWiki  REL1_20
ApiEmailUser.php
Go to the documentation of this file.
00001 <?php
00031 class ApiEmailUser extends ApiBase {
00032 
00033         public function __construct( $main, $action ) {
00034                 parent::__construct( $main, $action );
00035         }
00036 
00037         public function execute() {
00038                 $params = $this->extractRequestParams();
00039 
00040                 // Validate target
00041                 $targetUser = SpecialEmailUser::getTarget( $params['target'] );
00042                 if ( !( $targetUser instanceof User ) ) {
00043                         $this->dieUsageMsg( array( $targetUser ) );
00044                 }
00045 
00046                 // Check permissions and errors
00047                 $error = SpecialEmailUser::getPermissionsError( $this->getUser(), $params['token'] );
00048                 if ( $error ) {
00049                         $this->dieUsageMsg( array( $error ) );
00050                 }
00051 
00052                 $data = array(
00053                         'Target' => $targetUser->getName(),
00054                         'Text' => $params['text'],
00055                         'Subject' => $params['subject'],
00056                         'CCMe' => $params['ccme'],
00057                 );
00058                 $retval = SpecialEmailUser::submit( $data, $this->getContext() );
00059 
00060                 if ( $retval instanceof Status ) {
00061                         // SpecialEmailUser sometimes returns a status
00062                         // sometimes it doesn't.
00063                         if ( $retval->isGood() ) {
00064                                 $retval = true;
00065                         } else {
00066                                 $retval = $retval->getErrorsArray();
00067                         }
00068                 }
00069 
00070                 if ( $retval === true ) {
00071                         $result = array( 'result' => 'Success' );
00072                 } else {
00073                         $result = array(
00074                                 'result' => 'Failure',
00075                                 'message' => $retval
00076                         );
00077                 }
00078 
00079                 $this->getResult()->addValue( null, $this->getModuleName(), $result );
00080         }
00081 
00082         public function mustBePosted() {
00083                 return true;
00084         }
00085 
00086         public function isWriteMode() {
00087                 return true;
00088         }
00089 
00090         public function getAllowedParams() {
00091                 return array(
00092                         'target' => array(
00093                                 ApiBase::PARAM_TYPE => 'string',
00094                                 ApiBase::PARAM_REQUIRED => true
00095                         ),
00096                         'subject' => null,
00097                         'text' => array(
00098                                 ApiBase::PARAM_TYPE => 'string',
00099                                 ApiBase::PARAM_REQUIRED => true
00100                         ),
00101                         'token' => array(
00102                                 ApiBase::PARAM_TYPE => 'string',
00103                                 ApiBase::PARAM_REQUIRED => true
00104                         ),
00105                         'ccme' => false,
00106                 );
00107         }
00108 
00109         public function getParamDescription() {
00110                 return array(
00111                         'target' => 'User to send email to',
00112                         'subject' => 'Subject header',
00113                         'text' => 'Mail body',
00114                         'token' => 'A token previously acquired via prop=info',
00115                         'ccme' => 'Send a copy of this mail to me',
00116                 );
00117         }
00118 
00119         public function getResultProperties() {
00120                 return array(
00121                         '' => array(
00122                                 'result' => array(
00123                                         ApiBase::PROP_TYPE => array(
00124                                                 'Success',
00125                                                 'Failure'
00126                                         ),
00127                                 ),
00128                                 'message' => array(
00129                                         ApiBase::PROP_TYPE => 'string',
00130                                         ApiBase::PROP_NULLABLE => true
00131                                 )
00132                         )
00133                 );
00134         }
00135 
00136         public function getDescription() {
00137                 return 'Email a user.';
00138         }
00139 
00140         public function getPossibleErrors() {
00141                 return array_merge( parent::getPossibleErrors(), array(
00142                         array( 'usermaildisabled' ),
00143                 ) );
00144         }
00145 
00146         public function needsToken() {
00147                 return true;
00148         }
00149 
00150         public function getTokenSalt() {
00151                 return '';
00152         }
00153 
00154         public function getExamples() {
00155                 return array(
00156                         'api.php?action=emailuser&target=WikiSysop&text=Content' => 'Send an email to the User "WikiSysop" with the text "Content"',
00157                 );
00158         }
00159 
00160         public function getHelpUrls() {
00161                 return 'https://www.mediawiki.org/wiki/API:E-mail';
00162         }
00163 
00164         public function getVersion() {
00165                 return __CLASS__ . ': $Id$';
00166         }
00167 }