Call Linker methods statically
[lhc/web/wiklou.git] / includes / api / ApiWatch.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jan 4, 2008
6 *
7 * Copyright © 2008 Yuri Astrakhan <Firstname><Lastname>@gmail.com,
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
30 }
31
32 /**
33 * API module to allow users to watch a page
34 *
35 * @ingroup API
36 */
37 class ApiWatch extends ApiBase {
38
39 public function __construct( $main, $action ) {
40 parent::__construct( $main, $action );
41 }
42
43 public function execute() {
44 global $wgUser;
45 if ( !$wgUser->isLoggedIn() ) {
46 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
47 }
48
49 $params = $this->extractRequestParams();
50 $title = Title::newFromText( $params['title'] );
51
52 if ( !$title ) {
53 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
54 }
55
56 $article = new Article( $title, 0 );
57 $res = array( 'title' => $title->getPrefixedText() );
58
59 if ( $params['unwatch'] ) {
60 $res['unwatched'] = '';
61 $res['message'] = wfMsgExt( 'removedwatchtext', array( 'parse' ), $title->getPrefixedText() );
62 $success = UnwatchAction::doUnwatch( $title, $wgUser );
63 } else {
64 $res['watched'] = '';
65 $res['message'] = wfMsgExt( 'addedwatchtext', array( 'parse' ), $title->getPrefixedText() );
66 $success = WatchAction::doWatch( $title, $wgUser );
67 }
68 if ( !$success ) {
69 $this->dieUsageMsg( 'hookaborted' );
70 }
71 $this->getResult()->addValue( null, $this->getModuleName(), $res );
72 }
73
74 public function mustBePosted() {
75 return true;
76 }
77
78 public function isWriteMode() {
79 return true;
80 }
81
82 public function needsToken() {
83 return true;
84 }
85
86 public function getTokenSalt() {
87 return 'watch';
88 }
89
90 public function getAllowedParams() {
91 return array(
92 'title' => array(
93 ApiBase::PARAM_TYPE => 'string',
94 ApiBase::PARAM_REQUIRED => true
95 ),
96 'unwatch' => false,
97 'token' => null,
98 );
99 }
100
101 public function getParamDescription() {
102 return array(
103 'title' => 'The page to (un)watch',
104 'unwatch' => 'If set the page will be unwatched rather than watched',
105 'token' => 'A token previously acquired via prop=info',
106 );
107 }
108
109 public function getDescription() {
110 return 'Add or remove a page from/to the current user\'s watchlist';
111 }
112
113 public function getPossibleErrors() {
114 return array_merge( parent::getPossibleErrors(), array(
115 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
116 array( 'invalidtitle', 'title' ),
117 array( 'hookaborted' ),
118 ) );
119 }
120
121 public function getExamples() {
122 return array(
123 'api.php?action=watch&title=Main_Page',
124 'api.php?action=watch&title=Main_Page&unwatch=',
125 );
126 }
127
128 public function getHelpUrls() {
129 return 'http://www.mediawiki.org/wiki/API:Watch';
130 }
131
132 public function getVersion() {
133 return __CLASS__ . ': $Id$';
134 }
135 }