MediaWiki master
ApiQueryWatchlistRaw.php
Go to the documentation of this file.
1<?php
32
40
41 private WatchedItemQueryService $watchedItemQueryService;
42 private Language $contentLanguage;
43 private NamespaceInfo $namespaceInfo;
44 private GenderCache $genderCache;
45
54 public function __construct(
55 ApiQuery $query,
56 $moduleName,
57 WatchedItemQueryService $watchedItemQueryService,
58 Language $contentLanguage,
59 NamespaceInfo $namespaceInfo,
60 GenderCache $genderCache
61 ) {
62 parent::__construct( $query, $moduleName, 'wr' );
63 $this->watchedItemQueryService = $watchedItemQueryService;
64 $this->contentLanguage = $contentLanguage;
65 $this->namespaceInfo = $namespaceInfo;
66 $this->genderCache = $genderCache;
67 }
68
69 public function execute() {
70 $this->run();
71 }
72
73 public function executeGenerator( $resultPageSet ) {
74 $this->run( $resultPageSet );
75 }
76
81 private function run( $resultPageSet = null ) {
83
84 $user = $this->getWatchlistUser( $params );
85
86 $prop = array_fill_keys( (array)$params['prop'], true );
87 $show = array_fill_keys( (array)$params['show'], true );
88 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] )
89 && isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] )
90 ) {
91 $this->dieWithError( 'apierror-show' );
92 }
93
94 $options = [];
95 if ( $params['namespace'] ) {
96 $options['namespaceIds'] = $params['namespace'];
97 }
98 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
99 $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
100 }
101 if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
102 $options['filter'] = WatchedItemQueryService::FILTER_NOT_CHANGED;
103 }
104
105 if ( isset( $params['continue'] ) ) {
106 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
107 $options['startFrom'] = TitleValue::tryNew( $cont[0], $cont[1] );
108 $this->dieContinueUsageIf( !$options['startFrom'] );
109 }
110
111 if ( isset( $params['fromtitle'] ) ) {
112 $options['from'] = $this->parsePrefixedTitlePart( $params['fromtitle'] );
113 }
114
115 if ( isset( $params['totitle'] ) ) {
116 $options['until'] = $this->parsePrefixedTitlePart( $params['totitle'] );
117 }
118
119 $options['sort'] = WatchedItemStore::SORT_ASC;
120 if ( $params['dir'] === 'descending' ) {
121 $options['sort'] = WatchedItemStore::SORT_DESC;
122 }
123 $options['limit'] = $params['limit'] + 1;
124
125 $titles = [];
126 $count = 0;
127 $items = $this->watchedItemQueryService->getWatchedItemsForUser( $user, $options );
128
129 // Get gender information
130 if ( $items !== [] && $resultPageSet === null &&
131 $this->contentLanguage->needsGenderDistinction()
132 ) {
133 $usernames = [];
134 foreach ( $items as $item ) {
135 $linkTarget = $item->getTarget();
136 if ( $this->namespaceInfo->hasGenderDistinction( $linkTarget->getNamespace() ) ) {
137 $usernames[] = $linkTarget->getText();
138 }
139 }
140 if ( $usernames !== [] ) {
141 $this->genderCache->doQuery( $usernames, __METHOD__ );
142 }
143 }
144
145 foreach ( $items as $item ) {
146 $ns = $item->getTarget()->getNamespace();
147 $dbKey = $item->getTarget()->getDBkey();
148 if ( ++$count > $params['limit'] ) {
149 // We've reached the one extra which shows that there are
150 // additional pages to be had. Stop here...
151 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
152 break;
153 }
154 $t = Title::makeTitle( $ns, $dbKey );
155
156 if ( $resultPageSet === null ) {
157 $vals = [];
158 ApiQueryBase::addTitleInfo( $vals, $t );
159 if ( isset( $prop['changed'] ) && $item->getNotificationTimestamp() !== null ) {
160 $vals['changed'] = wfTimestamp( TS_ISO_8601, $item->getNotificationTimestamp() );
161 }
162 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
163 if ( !$fit ) {
164 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
165 break;
166 }
167 } else {
168 $titles[] = $t;
169 }
170 }
171 if ( $resultPageSet === null ) {
172 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
173 } else {
174 $resultPageSet->populateFromTitles( $titles );
175 }
176 }
177
178 public function getAllowedParams() {
179 return [
180 'continue' => [
181 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
182 ],
183 'namespace' => [
184 ParamValidator::PARAM_ISMULTI => true,
185 ParamValidator::PARAM_TYPE => 'namespace'
186 ],
187 'limit' => [
188 ParamValidator::PARAM_DEFAULT => 10,
189 ParamValidator::PARAM_TYPE => 'limit',
190 IntegerDef::PARAM_MIN => 1,
191 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
192 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
193 ],
194 'prop' => [
195 ParamValidator::PARAM_ISMULTI => true,
196 ParamValidator::PARAM_TYPE => [
197 'changed',
198 ],
200 ],
201 'show' => [
202 ParamValidator::PARAM_ISMULTI => true,
203 ParamValidator::PARAM_TYPE => [
204 WatchedItemQueryService::FILTER_CHANGED,
205 WatchedItemQueryService::FILTER_NOT_CHANGED
206 ]
207 ],
208 'owner' => [
209 ParamValidator::PARAM_TYPE => 'user',
210 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
211 ],
212 'token' => [
213 ParamValidator::PARAM_TYPE => 'string',
214 ParamValidator::PARAM_SENSITIVE => true,
215 ],
216 'dir' => [
217 ParamValidator::PARAM_DEFAULT => 'ascending',
218 ParamValidator::PARAM_TYPE => [
219 'ascending',
220 'descending'
221 ],
222 ],
223 'fromtitle' => [
224 ParamValidator::PARAM_TYPE => 'string'
225 ],
226 'totitle' => [
227 ParamValidator::PARAM_TYPE => 'string'
228 ],
229 ];
230 }
231
232 protected function getExamplesMessages() {
233 return [
234 'action=query&list=watchlistraw'
235 => 'apihelp-query+watchlistraw-example-simple',
236 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
237 => 'apihelp-query+watchlistraw-example-generator',
238 ];
239 }
240
241 public function getHelpUrls() {
242 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlistraw';
243 }
244}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
run()
Run the job.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1540
getWatchlistUser( $params)
Gets the user for whom to get the watchlist.
Definition ApiBase.php:1256
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1770
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1731
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:212
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:237
getResult()
Get the result object.
Definition ApiBase.php:681
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:821
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:172
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:239
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:542
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.
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, $moduleName, WatchedItemQueryService $watchedItemQueryService, Language $contentLanguage, NamespaceInfo $namespaceInfo, GenderCache $genderCache)
executeGenerator( $resultPageSet)
Execute this module as a generator.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
This is the main query class.
Definition ApiQuery.php:43
Base class for language-specific code.
Definition Language.php:66
Look up "gender" user preference.
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:79
Storage layer class for WatchedItems.
Service for formatting and validating API parameters.
Type definition for integer types.