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