MediaWiki REL1_34
ApiWatch.php
Go to the documentation of this file.
1<?php
28class ApiWatch extends ApiBase {
29 private $mPageSet = null;
30
31 public function execute() {
32 $user = $this->getUser();
33 if ( !$user->isLoggedIn() ) {
34 $this->dieWithError( 'watchlistanontext', 'notloggedin' );
35 }
36
37 $this->checkUserRightsAny( 'editmywatchlist' );
38
39 $params = $this->extractRequestParams();
40
41 $continuationManager = new ApiContinuationManager( $this, [], [] );
42 $this->setContinuationManager( $continuationManager );
43
44 $pageSet = $this->getPageSet();
45 // by default we use pageset to extract the page to work on.
46 // title is still supported for backward compatibility
47 if ( !isset( $params['title'] ) ) {
48 $pageSet->execute();
49 $res = $pageSet->getInvalidTitlesAndRevisions( [
50 'invalidTitles',
51 'special',
52 'missingIds',
53 'missingRevIds',
54 'interwikiTitles'
55 ] );
56
57 foreach ( $pageSet->getMissingTitles() as $title ) {
58 $r = $this->watchTitle( $title, $user, $params );
59 $r['missing'] = true;
60 $res[] = $r;
61 }
62
63 foreach ( $pageSet->getGoodTitles() as $title ) {
64 $r = $this->watchTitle( $title, $user, $params );
65 $res[] = $r;
66 }
68 } else {
69 // dont allow use of old title parameter with new pageset parameters.
70 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(), function ( $x ) {
71 return $x !== null && $x !== false;
72 } ) );
73
74 if ( $extraParams ) {
75 $this->dieWithError(
76 [
77 'apierror-invalidparammix-cannotusewith',
78 $this->encodeParamName( 'title' ),
79 $pageSet->encodeParamName( $extraParams[0] )
80 ],
81 'invalidparammix'
82 );
83 }
84
85 $title = Title::newFromText( $params['title'] );
86 if ( !$title || !$title->isWatchable() ) {
87 $this->dieWithError( [ 'invalidtitle', $params['title'] ] );
88 }
89 $res = $this->watchTitle( $title, $user, $params, true );
90 }
91 $this->getResult()->addValue( null, $this->getModuleName(), $res );
92
93 $this->setContinuationManager( null );
94 $continuationManager->setContinuationIntoResult( $this->getResult() );
95 }
96
97 private function watchTitle( Title $title, User $user, array $params,
98 $compatibilityMode = false
99 ) {
100 if ( !$title->isWatchable() ) {
101 return [ 'title' => $title->getPrefixedText(), 'watchable' => 0 ];
102 }
103
104 $res = [ 'title' => $title->getPrefixedText() ];
105
106 if ( $params['unwatch'] ) {
107 $status = UnwatchAction::doUnwatch( $title, $user );
108 $res['unwatched'] = $status->isOK();
109 } else {
110 $status = WatchAction::doWatch( $title, $user );
111 $res['watched'] = $status->isOK();
112 }
113
114 if ( !$status->isOK() ) {
115 if ( $compatibilityMode ) {
116 $this->dieStatus( $status );
117 }
118 $res['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'error' );
119 $res['warnings'] = $this->getErrorFormatter()->arrayFromStatus( $status, 'warning' );
120 if ( !$res['warnings'] ) {
121 unset( $res['warnings'] );
122 }
123 }
124
125 return $res;
126 }
127
132 private function getPageSet() {
133 if ( $this->mPageSet === null ) {
134 $this->mPageSet = new ApiPageSet( $this );
135 }
136
137 return $this->mPageSet;
138 }
139
140 public function mustBePosted() {
141 return true;
142 }
143
144 public function isWriteMode() {
145 return true;
146 }
147
148 public function needsToken() {
149 return 'watch';
150 }
151
152 public function getAllowedParams( $flags = 0 ) {
153 $result = [
154 'title' => [
155 ApiBase::PARAM_TYPE => 'string',
157 ],
158 'unwatch' => false,
159 'continue' => [
160 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
161 ],
162 ];
163 if ( $flags ) {
164 $result += $this->getPageSet()->getFinalParams( $flags );
165 }
166
167 return $result;
168 }
169
170 protected function getExamplesMessages() {
171 return [
172 'action=watch&titles=Main_Page&token=123ABC'
173 => 'apihelp-watch-example-watch',
174 'action=watch&titles=Main_Page&unwatch=&token=123ABC'
175 => 'apihelp-watch-example-unwatch',
176 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
177 => 'apihelp-watch-example-generator',
178 ];
179 }
180
181 public function getHelpUrls() {
182 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';
183 }
184}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition ApiBase.php:112
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition ApiBase.php:2130
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition ApiBase.php:739
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getErrorFormatter()
Get the error formatter.
Definition ApiBase.php:654
setContinuationManager(ApiContinuationManager $manager=null)
Set the continuation manager.
Definition ApiBase.php:694
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:131
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:2086
This manages continuation state.
This class contains a list of pages that the client has requested.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
API module to allow users to watch a page.
Definition ApiWatch.php:28
getAllowedParams( $flags=0)
Definition ApiWatch.php:152
watchTitle(Title $title, User $user, array $params, $compatibilityMode=false)
Definition ApiWatch.php:97
isWriteMode()
Indicates whether this module requires write mode.
Definition ApiWatch.php:144
getPageSet()
Get a cached instance of an ApiPageSet object.
Definition ApiWatch.php:132
needsToken()
Returns the token type this module requires in order to execute.
Definition ApiWatch.php:148
getHelpUrls()
Return links to more detailed help pages about the module.
Definition ApiWatch.php:181
getExamplesMessages()
Returns usage examples for this module.
Definition ApiWatch.php:170
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition ApiWatch.php:140
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition ApiWatch.php:31
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
static doUnwatch(Title $title, User $user)
Unwatch a page.
return true
Definition router.php:94