MediaWiki  REL1_22
ApiWatch.php
Go to the documentation of this file.
00001 <?php
00032 class ApiWatch extends ApiBase {
00033 
00034     public function execute() {
00035         $user = $this->getUser();
00036         if ( !$user->isLoggedIn() ) {
00037             $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
00038         }
00039         if ( !$user->isAllowed( 'editmywatchlist' ) ) {
00040             $this->dieUsage( 'You don\'t have permission to edit your watchlist', 'permissiondenied' );
00041         }
00042 
00043         $params = $this->extractRequestParams();
00044         $title = Title::newFromText( $params['title'] );
00045 
00046         if ( !$title || $title->isExternal() || !$title->canExist() ) {
00047             $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00048         }
00049 
00050         $res = array( 'title' => $title->getPrefixedText() );
00051 
00052         // Currently unnecessary, code to act as a safeguard against any change in current behavior of uselang
00053         // Copy from ApiParse
00054         $oldLang = null;
00055         if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
00056             $oldLang = $this->getContext()->getLanguage(); // Backup language
00057             $this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
00058         }
00059 
00060         if ( $params['unwatch'] ) {
00061             $res['unwatched'] = '';
00062             $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
00063             $status = UnwatchAction::doUnwatch( $title, $user );
00064         } else {
00065             $res['watched'] = '';
00066             $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock();
00067             $status = WatchAction::doWatch( $title, $user );
00068         }
00069 
00070         if ( !is_null( $oldLang ) ) {
00071             $this->getContext()->setLanguage( $oldLang ); // Reset language to $oldLang
00072         }
00073 
00074         if ( !$status->isOK() ) {
00075             $this->dieStatus( $status );
00076         }
00077         $this->getResult()->addValue( null, $this->getModuleName(), $res );
00078     }
00079 
00080     public function mustBePosted() {
00081         return true;
00082     }
00083 
00084     public function isWriteMode() {
00085         return true;
00086     }
00087 
00088     public function needsToken() {
00089         return true;
00090     }
00091 
00092     public function getTokenSalt() {
00093         return 'watch';
00094     }
00095 
00096     public function getAllowedParams() {
00097         return array(
00098             'title' => array(
00099                 ApiBase::PARAM_TYPE => 'string',
00100                 ApiBase::PARAM_REQUIRED => true
00101             ),
00102             'unwatch' => false,
00103             'uselang' => null,
00104             'token' => array(
00105                 ApiBase::PARAM_TYPE => 'string',
00106                 ApiBase::PARAM_REQUIRED => true
00107             ),
00108         );
00109     }
00110 
00111     public function getParamDescription() {
00112         return array(
00113             'title' => 'The page to (un)watch',
00114             'unwatch' => 'If set the page will be unwatched rather than watched',
00115             'uselang' => 'Language to show the message in',
00116             'token' => 'A token previously acquired via prop=info',
00117         );
00118     }
00119 
00120     public function getResultProperties() {
00121         return array(
00122             '' => array(
00123                 'title' => 'string',
00124                 'unwatched' => 'boolean',
00125                 'watched' => 'boolean',
00126                 'message' => 'string'
00127             )
00128         );
00129     }
00130 
00131     public function getDescription() {
00132         return 'Add or remove a page from/to the current user\'s watchlist';
00133     }
00134 
00135     public function getPossibleErrors() {
00136         return array_merge( parent::getPossibleErrors(), array(
00137             array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
00138             array( 'invalidtitle', 'title' ),
00139             array( 'hookaborted' ),
00140         ) );
00141     }
00142 
00143     public function getExamples() {
00144         return array(
00145             'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"',
00146             'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"',
00147         );
00148     }
00149 
00150     public function getHelpUrls() {
00151         return 'https://www.mediawiki.org/wiki/API:Watch';
00152     }
00153 }