MediaWiki master
ApiQueryWatchlistRaw.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
21use Wikimedia\Timestamp\TimestampFormat as TS;
22
30
31 public function __construct(
32 ApiQuery $query,
33 string $moduleName,
34 private readonly WatchedItemQueryService $watchedItemQueryService,
35 private readonly Language $contentLanguage,
36 private readonly NamespaceInfo $namespaceInfo,
37 private readonly GenderCache $genderCache,
38 ) {
39 parent::__construct( $query, $moduleName, 'wr' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
55 private function run( $resultPageSet = null ) {
56 $params = $this->extractRequestParams();
57
58 $user = $this->getWatchlistUser( $params );
59
60 $prop = array_fill_keys( (array)$params['prop'], true );
61 $show = array_fill_keys( (array)$params['show'], true );
62 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] )
63 && isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] )
64 ) {
65 $this->dieWithError( 'apierror-show' );
66 }
67
68 $options = [];
69 if ( $params['namespace'] ) {
70 $options['namespaceIds'] = $params['namespace'];
71 }
72 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
73 $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
74 }
75 if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
76 $options['filter'] = WatchedItemQueryService::FILTER_NOT_CHANGED;
77 }
78
79 if ( isset( $params['continue'] ) ) {
80 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
81 $options['startFrom'] = TitleValue::tryNew( $cont[0], $cont[1] );
82 $this->dieContinueUsageIf( !$options['startFrom'] );
83 }
84
85 if ( isset( $params['fromtitle'] ) ) {
86 $options['from'] = $this->parsePrefixedTitlePart( $params['fromtitle'] );
87 }
88
89 if ( isset( $params['totitle'] ) ) {
90 $options['until'] = $this->parsePrefixedTitlePart( $params['totitle'] );
91 }
92
93 $options['sort'] = WatchedItemStore::SORT_ASC;
94 if ( $params['dir'] === 'descending' ) {
95 $options['sort'] = WatchedItemStore::SORT_DESC;
96 }
97 $options['limit'] = $params['limit'] + 1;
98
99 $titles = [];
100 $count = 0;
101 $items = $this->watchedItemQueryService->getWatchedItemsForUser( $user, $options );
102
103 // Get gender information
104 if ( $items !== [] && $resultPageSet === null &&
105 $this->contentLanguage->needsGenderDistinction()
106 ) {
107 $usernames = [];
108 foreach ( $items as $item ) {
109 $target = $item->getTarget();
110 if ( $this->namespaceInfo->hasGenderDistinction( $target->getNamespace() ) ) {
111 // GenderCache is able to deal with underscores in usernames
112 $usernames[] = $target->getDBkey();
113 }
114 }
115 if ( $usernames !== [] ) {
116 $this->genderCache->doQuery( $usernames, __METHOD__ );
117 }
118 }
119
120 foreach ( $items as $item ) {
121 $ns = $item->getTarget()->getNamespace();
122 $dbKey = $item->getTarget()->getDBkey();
123 if ( ++$count > $params['limit'] ) {
124 // We've reached the one extra which shows that there are
125 // additional pages to be had. Stop here...
126 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
127 break;
128 }
129 $t = Title::makeTitle( $ns, $dbKey );
130
131 if ( $resultPageSet === null ) {
132 $vals = [];
133 ApiQueryBase::addTitleInfo( $vals, $t );
134 if ( isset( $prop['changed'] ) && $item->getNotificationTimestamp() !== null ) {
135 $vals['changed'] = wfTimestamp( TS::ISO_8601, $item->getNotificationTimestamp() );
136 }
137 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
138 if ( !$fit ) {
139 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
140 break;
141 }
142 } else {
143 $titles[] = $t;
144 }
145 }
146 if ( $resultPageSet === null ) {
147 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
148 } else {
149 $resultPageSet->populateFromTitles( $titles );
150 }
151 }
152
154 public function getAllowedParams() {
155 return [
156 'continue' => [
157 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
158 ],
159 'namespace' => [
160 ParamValidator::PARAM_ISMULTI => true,
161 ParamValidator::PARAM_TYPE => 'namespace'
162 ],
163 'limit' => [
164 ParamValidator::PARAM_DEFAULT => 10,
165 ParamValidator::PARAM_TYPE => 'limit',
166 IntegerDef::PARAM_MIN => 1,
167 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
168 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
169 ],
170 'prop' => [
171 ParamValidator::PARAM_ISMULTI => true,
172 ParamValidator::PARAM_TYPE => [
173 'changed',
174 ],
176 ],
177 'show' => [
178 ParamValidator::PARAM_ISMULTI => true,
179 ParamValidator::PARAM_TYPE => [
180 WatchedItemQueryService::FILTER_CHANGED,
181 WatchedItemQueryService::FILTER_NOT_CHANGED
182 ]
183 ],
184 'owner' => [
185 ParamValidator::PARAM_TYPE => 'user',
186 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
187 ],
188 'token' => [
189 ParamValidator::PARAM_TYPE => 'string',
190 ParamValidator::PARAM_SENSITIVE => true,
191 ],
192 'dir' => [
193 ParamValidator::PARAM_DEFAULT => 'ascending',
194 ParamValidator::PARAM_TYPE => [
195 'ascending',
196 'descending'
197 ],
198 ],
199 'fromtitle' => [
200 ParamValidator::PARAM_TYPE => 'string'
201 ],
202 'totitle' => [
203 ParamValidator::PARAM_TYPE => 'string'
204 ],
205 ];
206 }
207
209 protected function getExamplesMessages() {
210 return [
211 'action=query&list=watchlistraw'
212 => 'apihelp-query+watchlistraw-example-simple',
213 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
214 => 'apihelp-query+watchlistraw-example-generator',
215 ];
216 }
217
219 public function getHelpUrls() {
220 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlistraw';
221 }
222}
223
225class_alias( ApiQueryWatchlistRaw::class, 'ApiQueryWatchlistRaw' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1746
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1707
getWatchlistUser( $params)
Gets the user for whom to get the watchlist.
Definition ApiBase.php:1307
getResult()
Get the result object.
Definition ApiBase.php:696
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:206
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:233
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:231
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
parsePrefixedTitlePart( $titlePart, $defaultNamespace=NS_MAIN)
Convert an input title or title prefix into a TitleValue.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
This query action allows clients to retrieve a list of pages on the logged-in user's watchlist.
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
__construct(ApiQuery $query, string $moduleName, private readonly WatchedItemQueryService $watchedItemQueryService, private readonly Language $contentLanguage, private readonly NamespaceInfo $namespaceInfo, private readonly GenderCache $genderCache,)
executeGenerator( $resultPageSet)
Execute this module as a generator.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
This is the main query class.
Definition ApiQuery.php:36
Look up "gender" user preference.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Base class for language-specific code.
Definition Language.php:65
Type definition for user types.
Definition UserDef.php:27
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents the target of a wiki link.
Represents a title within MediaWiki.
Definition Title.php:69
Class performing complex database queries related to WatchedItems.
Storage layer class for WatchedItems.
Service for formatting and validating API parameters.
Type definition for integer types.
array $params
The job parameters.