00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( 'ApiBase.php' );
00029 }
00030
00036 class ApiWatch extends ApiBase {
00037
00038 public function __construct( $main, $action ) {
00039 parent :: __construct( $main, $action );
00040 }
00041
00042 public function execute() {
00043 global $wgUser;
00044 if ( !$wgUser->isLoggedIn() )
00045 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
00046
00047 $params = $this->extractRequestParams();
00048 $title = Title::newFromText( $params['title'] );
00049
00050 if ( !$title )
00051 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
00052
00053 $article = new Article( $title );
00054 $res = array( 'title' => $title->getPrefixedText() );
00055
00056 if ( $params['unwatch'] )
00057 {
00058 $res['unwatched'] = '';
00059 $success = $article->doUnwatch();
00060 }
00061 else
00062 {
00063 $res['watched'] = '';
00064 $success = $article->doWatch();
00065 }
00066 if ( !$success )
00067 $this->dieUsageMsg( array( 'hookaborted' ) );
00068 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00069 }
00070
00071 public function isWriteMode() {
00072 return true;
00073 }
00074
00075 public function getAllowedParams() {
00076 return array (
00077 'title' => null,
00078 'unwatch' => false,
00079 );
00080 }
00081
00082 public function getParamDescription() {
00083 return array (
00084 'title' => 'The page to (un)watch',
00085 'unwatch' => 'If set the page will be unwatched rather than watched',
00086 );
00087 }
00088
00089 public function getDescription() {
00090 return array (
00091 'Add or remove a page from/to the current user\'s watchlist'
00092 );
00093 }
00094
00095 public function getPossibleErrors() {
00096 return array_merge( parent::getPossibleErrors(), array(
00097 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
00098 array( 'invalidtitle', 'title' ),
00099 array( 'hookaborted' ),
00100 ) );
00101 }
00102
00103 protected function getExamples() {
00104 return array(
00105 'api.php?action=watch&title=Main_Page',
00106 'api.php?action=watch&title=Main_Page&unwatch',
00107 );
00108 }
00109
00110 public function getVersion() {
00111 return __CLASS__ . ': $Id: ApiWatch.php 69578 2010-07-20 02:46:20Z tstarling $';
00112 }
00113 }