MediaWiki
REL1_20
|
00001 <?php 00032 class ApiWatch extends ApiBase { 00033 00034 public function __construct( $main, $action ) { 00035 parent::__construct( $main, $action ); 00036 } 00037 00038 public function execute() { 00039 $user = $this->getUser(); 00040 if ( !$user->isLoggedIn() ) { 00041 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' ); 00042 } 00043 00044 $params = $this->extractRequestParams(); 00045 $title = Title::newFromText( $params['title'] ); 00046 00047 if ( !$title || $title->getNamespace() < 0 ) { 00048 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); 00049 } 00050 00051 $res = array( 'title' => $title->getPrefixedText() ); 00052 00053 if ( $params['unwatch'] ) { 00054 $res['unwatched'] = ''; 00055 $res['message'] = $this->msg( 'removedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock(); 00056 $success = UnwatchAction::doUnwatch( $title, $user ); 00057 } else { 00058 $res['watched'] = ''; 00059 $res['message'] = $this->msg( 'addedwatchtext', $title->getPrefixedText() )->title( $title )->parseAsBlock(); 00060 $success = WatchAction::doWatch( $title, $user ); 00061 } 00062 if ( !$success ) { 00063 $this->dieUsageMsg( 'hookaborted' ); 00064 } 00065 $this->getResult()->addValue( null, $this->getModuleName(), $res ); 00066 } 00067 00068 public function mustBePosted() { 00069 return true; 00070 } 00071 00072 public function isWriteMode() { 00073 return true; 00074 } 00075 00076 public function needsToken() { 00077 return true; 00078 } 00079 00080 public function getTokenSalt() { 00081 return 'watch'; 00082 } 00083 00084 public function getAllowedParams() { 00085 return array( 00086 'title' => array( 00087 ApiBase::PARAM_TYPE => 'string', 00088 ApiBase::PARAM_REQUIRED => true 00089 ), 00090 'unwatch' => false, 00091 'token' => array( 00092 ApiBase::PARAM_TYPE => 'string', 00093 ApiBase::PARAM_REQUIRED => true 00094 ), 00095 ); 00096 } 00097 00098 public function getParamDescription() { 00099 return array( 00100 'title' => 'The page to (un)watch', 00101 'unwatch' => 'If set the page will be unwatched rather than watched', 00102 'token' => 'A token previously acquired via prop=info', 00103 ); 00104 } 00105 00106 public function getResultProperties() { 00107 return array( 00108 '' => array( 00109 'title' => 'string', 00110 'unwatched' => 'boolean', 00111 'watched' => 'boolean', 00112 'message' => 'string' 00113 ) 00114 ); 00115 } 00116 00117 public function getDescription() { 00118 return 'Add or remove a page from/to the current user\'s watchlist'; 00119 } 00120 00121 public function getPossibleErrors() { 00122 return array_merge( parent::getPossibleErrors(), array( 00123 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ), 00124 array( 'invalidtitle', 'title' ), 00125 array( 'hookaborted' ), 00126 ) ); 00127 } 00128 00129 public function getExamples() { 00130 return array( 00131 'api.php?action=watch&title=Main_Page' => 'Watch the page "Main Page"', 00132 'api.php?action=watch&title=Main_Page&unwatch=' => 'Unwatch the page "Main Page"', 00133 ); 00134 } 00135 00136 public function getHelpUrls() { 00137 return 'https://www.mediawiki.org/wiki/API:Watch'; 00138 } 00139 00140 public function getVersion() { 00141 return __CLASS__ . ': $Id$'; 00142 } 00143 }