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