MediaWiki REL1_35
ApiQueryWatchlistRaw.php
Go to the documentation of this file.
1<?php
25
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'wr' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
44 }
45
50 private function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 $user = $this->getWatchlistUser( $params );
54
55 $prop = array_flip( (array)$params['prop'] );
56 $show = array_flip( (array)$params['show'] );
57 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] )
58 && isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] )
59 ) {
60 $this->dieWithError( 'apierror-show' );
61 }
62
63 $options = [];
64 if ( $params['namespace'] ) {
65 $options['namespaceIds'] = $params['namespace'];
66 }
67 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
68 $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
69 }
70 if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
71 $options['filter'] = WatchedItemQueryService::FILTER_NOT_CHANGED;
72 }
73
74 if ( isset( $params['continue'] ) ) {
75 $cont = explode( '|', $params['continue'] );
76 $this->dieContinueUsageIf( count( $cont ) != 2 );
77 $ns = (int)$cont[0];
78 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
79 $title = $cont[1];
80 $options['startFrom'] = TitleValue::tryNew( $ns, $title );
81 $this->dieContinueUsageIf( !$options['startFrom'] );
82 }
83
84 if ( isset( $params['fromtitle'] ) ) {
85 $options['from'] = $this->parsePrefixedTitlePart( $params['fromtitle'] );
86 }
87
88 if ( isset( $params['totitle'] ) ) {
89 $options['until'] = $this->parsePrefixedTitlePart( $params['totitle'] );
90 }
91
92 $options['sort'] = WatchedItemStore::SORT_ASC;
93 if ( $params['dir'] === 'descending' ) {
94 $options['sort'] = WatchedItemStore::SORT_DESC;
95 }
96 $options['limit'] = $params['limit'] + 1;
97
98 $titles = [];
99 $count = 0;
100 $services = MediaWikiServices::getInstance();
101 $items = $services->getWatchedItemQueryService()
102 ->getWatchedItemsForUser( $user, $options );
103
104 // Get gender information
105 if ( $items !== [] && $resultPageSet === null &&
106 $services->getContentLanguage()->needsGenderDistinction()
107 ) {
108 $nsInfo = $services->getNamespaceInfo();
109 $usernames = [];
110 foreach ( $items as $item ) {
111 $linkTarget = $item->getLinkTarget();
112 if ( $nsInfo->hasGenderDistinction( $linkTarget->getNamespace() ) ) {
113 $usernames[] = $linkTarget->getText();
114 }
115 }
116 if ( $usernames !== [] ) {
117 $services->getGenderCache()->doQuery( $usernames, __METHOD__ );
118 }
119 }
120
121 foreach ( $items as $item ) {
122 $ns = $item->getLinkTarget()->getNamespace();
123 $dbKey = $item->getLinkTarget()->getDBkey();
124 if ( ++$count > $params['limit'] ) {
125 // We've reached the one extra which shows that there are
126 // additional pages to be had. Stop here...
127 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
128 break;
129 }
130 $t = Title::makeTitle( $ns, $dbKey );
131
132 if ( $resultPageSet === null ) {
133 $vals = [];
135 if ( isset( $prop['changed'] ) && $item->getNotificationTimestamp() !== null ) {
136 $vals['changed'] = wfTimestamp( TS_ISO_8601, $item->getNotificationTimestamp() );
137 }
138 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
139 if ( !$fit ) {
140 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
141 break;
142 }
143 } else {
144 $titles[] = $t;
145 }
146 }
147 if ( $resultPageSet === null ) {
148 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
149 } else {
150 $resultPageSet->populateFromTitles( $titles );
151 }
152 }
153
154 public function getAllowedParams() {
155 return [
156 'continue' => [
157 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
158 ],
159 'namespace' => [
161 ApiBase::PARAM_TYPE => 'namespace'
162 ],
163 'limit' => [
165 ApiBase::PARAM_TYPE => 'limit',
169 ],
170 'prop' => [
173 'changed',
174 ],
176 ],
177 'show' => [
180 WatchedItemQueryService::FILTER_CHANGED,
181 WatchedItemQueryService::FILTER_NOT_CHANGED
182 ]
183 ],
184 'owner' => [
185 ApiBase::PARAM_TYPE => 'user',
186 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
187 ],
188 'token' => [
189 ApiBase::PARAM_TYPE => 'string',
191 ],
192 'dir' => [
193 ApiBase::PARAM_DFLT => 'ascending',
195 'ascending',
196 'descending'
197 ],
198 ],
199 'fromtitle' => [
200 ApiBase::PARAM_TYPE => 'string'
201 ],
202 'totitle' => [
203 ApiBase::PARAM_TYPE => 'string'
204 ],
205 ];
206 }
207
208 protected function getExamplesMessages() {
209 return [
210 'action=query&list=watchlistraw'
211 => 'apihelp-query+watchlistraw-example-simple',
212 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
213 => 'apihelp-query+watchlistraw-example-generator',
214 ];
215 }
216
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlistraw';
219 }
220}
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:1437
const PARAM_MAX2
Definition ApiBase.php:86
const PARAM_MAX
Definition ApiBase.php:82
getWatchlistUser( $params)
Gets the user for whom to get the watchlist.
Definition ApiBase.php:1188
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1617
const PARAM_TYPE
Definition ApiBase.php:78
const PARAM_SENSITIVE
Definition ApiBase.php:122
const PARAM_DFLT
Definition ApiBase.php:70
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition ApiBase.php:195
const PARAM_MIN
Definition ApiBase.php:90
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:220
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:162
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:222
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
const PARAM_ISMULTI
Definition ApiBase.php:74
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.
run( $resultPageSet=null)
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
executeGenerator( $resultPageSet)
Execute this module as a generator.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
This is the main query class.
Definition ApiQuery.php:37
MediaWikiServices is the service locator for the application scope of MediaWiki.
Type definition for user types.
Definition UserDef.php:23
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.