MediaWiki  1.34.0
ApiWatch.php
Go to the documentation of this file.
1 <?php
28 class 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'] ) {
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 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
ApiWatch\getPageSet
getPageSet()
Get a cached instance of an ApiPageSet object.
Definition: ApiWatch.php:132
ApiWatch\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiWatch.php:181
ApiWatch
API module to allow users to watch a page.
Definition: ApiWatch.php:28
ApiContinuationManager
This manages continuation state.
Definition: ApiContinuationManager.php:26
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:131
WatchAction\doUnwatch
static doUnwatch(Title $title, User $user)
Unwatch a page.
Definition: WatchAction.php:145
true
return true
Definition: router.php:92
ApiWatch\$mPageSet
$mPageSet
Definition: ApiWatch.php:29
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiBase\checkUserRightsAny
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
Definition: ApiBase.php:2130
ApiBase\setContinuationManager
setContinuationManager(ApiContinuationManager $manager=null)
Set the continuation manager.
Definition: ApiBase.php:694
$res
$res
Definition: testCompression.php:52
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiWatch\getAllowedParams
getAllowedParams( $flags=0)
Definition: ApiWatch.php:152
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:40
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition: ApiBase.php:112
WatchAction\doWatch
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
Definition: WatchAction.php:116
ApiWatch\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiWatch.php:140
ApiWatch\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiWatch.php:31
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
ApiWatch\watchTitle
watchTitle(Title $title, User $user, array $params, $compatibilityMode=false)
Definition: ApiWatch.php:97
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:616
ApiBase\encodeParamName
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:739
Title
Represents a title within MediaWiki.
Definition: Title.php:42
$status
return $status
Definition: SyntaxHighlight.php:347
ApiWatch\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiWatch.php:144
ApiWatch\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiWatch.php:148
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2086
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
ApiWatch\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiWatch.php:170
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:654