34 private $mPageSet =
null;
37 private $expiryEnabled;
46 parent::__construct( $mainModule, $moduleName );
49 $this->expiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
50 $this->maxDuration = $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
56 $this->
dieWithError(
'watchlistanontext',
'notloggedin' );
66 $pageSet = $this->getPageSet();
69 if ( !isset( $params[
'title'] ) ) {
71 $res = $pageSet->getInvalidTitlesAndRevisions( [
79 foreach ( $pageSet->getMissingTitles() as
$title ) {
80 $r = $this->watchTitle(
$title, $user, $params );
85 foreach ( $pageSet->getGoodTitles() as
$title ) {
86 $r = $this->watchTitle(
$title, $user, $params );
89 ApiResult::setIndexedTagName(
$res,
'w' );
92 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(),
static function ( $x ) {
93 return $x !==
null && $x !==
false;
99 'apierror-invalidparammix-cannotusewith',
101 $pageSet->encodeParamName( $extraParams[0] )
107 $title = Title::newFromText( $params[
'title'] );
108 if ( !
$title || !$this->watchlistManager->isWatchable(
$title ) ) {
109 $this->
dieWithError( [
'invalidtitle', $params[
'title'] ] );
111 $res = $this->watchTitle(
$title, $user, $params,
true );
116 $continuationManager->setContinuationIntoResult( $this->
getResult() );
120 $compatibilityMode =
false
122 $res = [
'title' =>
$title->getPrefixedText(),
'ns' =>
$title->getNamespace() ];
124 if ( !$this->watchlistManager->isWatchable(
$title ) ) {
125 $res[
'watchable'] = 0;
129 if ( $params[
'unwatch'] ) {
130 $status = $this->watchlistManager->removeWatch( $user,
$title );
131 $res[
'unwatched'] = $status->isOK();
136 if ( $this->expiryEnabled && isset( $params[
'expiry'] ) ) {
137 $expiry = $params[
'expiry'];
141 $status = $this->watchlistManager->addWatch( $user,
$title, $expiry );
142 $res[
'watched'] = $status->isOK();
145 if ( !$status->isOK() ) {
146 if ( $compatibilityMode ) {
151 if ( !
$res[
'warnings'] ) {
152 unset(
$res[
'warnings'] );
163 private function getPageSet() {
164 if ( $this->mPageSet ===
null ) {
168 return $this->mPageSet;
186 ParamValidator::PARAM_TYPE =>
'string',
187 ParamValidator::PARAM_DEPRECATED =>
true,
190 ParamValidator::PARAM_TYPE =>
'expiry',
191 ExpiryDef::PARAM_MAX => $this->maxDuration,
192 ExpiryDef::PARAM_USE_MAX =>
true,
201 if ( !$this->expiryEnabled ) {
202 unset( $result[
'expiry'] );
206 $result += $this->getPageSet()->getFinalParams( $flags );
213 $title = Title::newMainPage()->getPrefixedText();
214 $mp = rawurlencode(
$title );
218 "action=watch&titles={$mp}&token=123ABC"
219 =>
'apihelp-watch-example-watch',
221 if ( $this->expiryEnabled ) {
222 $examples[
"action=watch&titles={$mp}|Foo|Bar&expiry=1%20month&token=123ABC"]
223 =
'apihelp-watch-example-watch-expiry';
226 return array_merge( $examples, [
227 "action=watch&titles={$mp}&unwatch=&token=123ABC"
228 =>
'apihelp-watch-example-unwatch',
229 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
230 =>
'apihelp-watch-example-generator',
235 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watch';
This abstract class implements many basic API functions, and is the base of all API classes.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
encodeParamName( $paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
setContinuationManager(ApiContinuationManager $manager=null)
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
getModuleName()
Get the name of the module being executed by this instance.
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
This manages continuation state.
This is the main API class, used for both external and internal processing.
This class contains a list of pages that the client has requested.
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
API module to allow users to watch a page.
getAllowedParams( $flags=0)
isWriteMode()
Indicates whether this module requires write mode.
__construct(ApiMain $mainModule, $moduleName, WatchlistManager $watchlistManager)
needsToken()
Returns the token type this module requires in order to execute.
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
mustBePosted()
Indicates whether this module must be called with a POST request.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
WatchlistManager $watchlistManager
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
isRegistered()
Get whether the user is registered.