35 private $mPageSet =
null;
38 private $expiryEnabled;
47 parent::__construct( $mainModule, $moduleName );
50 $this->expiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
51 $this->maxDuration = $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
57 $this->
dieWithError(
'watchlistanontext',
'notloggedin' );
67 $pageSet = $this->getPageSet();
70 if ( !isset( $params[
'title'] ) ) {
72 $res = $pageSet->getInvalidTitlesAndRevisions( [
80 foreach ( $pageSet->getMissingTitles() as
$title ) {
81 $r = $this->watchTitle(
$title, $user, $params );
86 foreach ( $pageSet->getGoodTitles() as
$title ) {
87 $r = $this->watchTitle(
$title, $user, $params );
90 ApiResult::setIndexedTagName(
$res,
'w' );
93 $extraParams = array_keys( array_filter( $pageSet->extractRequestParams(),
static function ( $x ) {
94 return $x !==
null && $x !==
false;
100 'apierror-invalidparammix-cannotusewith',
102 $pageSet->encodeParamName( $extraParams[0] )
108 $title = Title::newFromText( $params[
'title'] );
109 if ( !
$title || !$this->watchlistManager->isWatchable(
$title ) ) {
110 $this->
dieWithError( [
'invalidtitle', $params[
'title'] ] );
112 $res = $this->watchTitle(
$title, $user, $params,
true );
117 $continuationManager->setContinuationIntoResult( $this->
getResult() );
121 $compatibilityMode =
false
123 $res = [
'title' =>
$title->getPrefixedText(),
'ns' =>
$title->getNamespace() ];
125 if ( !$this->watchlistManager->isWatchable(
$title ) ) {
126 $res[
'watchable'] = 0;
130 if ( $params[
'unwatch'] ) {
131 $status = $this->watchlistManager->removeWatch( $user,
$title );
132 $res[
'unwatched'] = $status->isOK();
137 if ( $this->expiryEnabled && isset( $params[
'expiry'] ) ) {
138 $expiry = $params[
'expiry'];
142 $status = $this->watchlistManager->addWatch( $user,
$title, $expiry );
143 $res[
'watched'] = $status->isOK();
146 if ( !$status->isOK() ) {
147 if ( $compatibilityMode ) {
152 if ( !
$res[
'warnings'] ) {
153 unset(
$res[
'warnings'] );
164 private function getPageSet() {
167 return $this->mPageSet;
185 ParamValidator::PARAM_TYPE =>
'string',
186 ParamValidator::PARAM_DEPRECATED =>
true,
189 ParamValidator::PARAM_TYPE =>
'expiry',
190 ExpiryDef::PARAM_MAX => $this->maxDuration,
191 ExpiryDef::PARAM_USE_MAX =>
true,
200 if ( !$this->expiryEnabled ) {
201 unset( $result[
'expiry'] );
205 $result += $this->getPageSet()->getFinalParams( $flags );
212 $title = Title::newMainPage()->getPrefixedText();
213 $mp = rawurlencode(
$title );
217 "action=watch&titles={$mp}&token=123ABC"
218 =>
'apihelp-watch-example-watch',
220 if ( $this->expiryEnabled ) {
221 $examples[
"action=watch&titles={$mp}|Foo|Bar&expiry=1%20month&token=123ABC"]
222 =
'apihelp-watch-example-watch-expiry';
225 return array_merge( $examples, [
226 "action=watch&titles={$mp}&unwatch=&token=123ABC"
227 =>
'apihelp-watch-example-unwatch',
228 'action=watch&generator=allpages&gapnamespace=0&token=123ABC'
229 =>
'apihelp-watch-example-generator',
234 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.
isRegistered()
Get whether the user is registered.