MediaWiki  master
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] )
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] ) ) {
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 = [];
134  ApiQueryBase::addTitleInfo( $vals, $t );
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' => [
160  ApiBase::PARAM_ISMULTI => true,
161  ApiBase::PARAM_TYPE => 'namespace'
162  ],
163  'limit' => [
164  ApiBase::PARAM_DFLT => 10,
165  ApiBase::PARAM_TYPE => 'limit',
166  ApiBase::PARAM_MIN => 1,
169  ],
170  'prop' => [
171  ApiBase::PARAM_ISMULTI => true,
173  'changed',
174  ],
176  ],
177  'show' => [
178  ApiBase::PARAM_ISMULTI => true,
182  ]
183  ],
184  'owner' => [
185  ApiBase::PARAM_TYPE => 'user',
186  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name' ],
187  ],
188  'token' => [
189  ApiBase::PARAM_TYPE => 'string',
190  ApiBase::PARAM_SENSITIVE => true,
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 }
ApiQueryWatchlistRaw\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryWatchlistRaw.php:154
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
ApiQueryWatchlistRaw\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryWatchlistRaw.php:34
ApiQueryWatchlistRaw\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryWatchlistRaw.php:42
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:107
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1808
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
ApiQueryWatchlistRaw\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryWatchlistRaw.php:38
ApiQueryWatchlistRaw
This query action allows clients to retrieve a list of pages on the logged-in user's watchlist.
Definition: ApiQueryWatchlistRaw.php:32
ApiBase\PARAM_SENSITIVE
const PARAM_SENSITIVE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:82
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:23
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiBase\PARAM_MIN
const PARAM_MIN
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:74
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:93
WatchedItemQueryService\FILTER_NOT_CHANGED
const FILTER_NOT_CHANGED
Definition: WatchedItemQueryService.php:54
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:165
WatchedItemStoreInterface\SORT_DESC
const SORT_DESC
Definition: WatchedItemStoreInterface.php:40
ApiBase\PARAM_MAX
const PARAM_MAX
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:72
ApiQueryWatchlistRaw\run
run( $resultPageSet=null)
Definition: ApiQueryWatchlistRaw.php:50
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
ApiBase\getWatchlistUser
getWatchlistUser( $params)
Gets the user for whom to get the watchlist.
Definition: ApiBase.php:1133
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1562
ApiQueryWatchlistRaw\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryWatchlistRaw.php:217
WatchedItemStoreInterface\SORT_ASC
const SORT_ASC
Definition: WatchedItemStoreInterface.php:35
ApiQueryWatchlistRaw\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryWatchlistRaw.php:208
ApiQueryGeneratorBase
Stable to extend.
Definition: ApiQueryGeneratorBase.php:28
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:167
WatchedItemQueryService\FILTER_CHANGED
const FILTER_CHANGED
Definition: WatchedItemQueryService.php:53
ApiQueryBase\parsePrefixedTitlePart
parsePrefixedTitlePart( $titlePart, $defaultNamespace=NS_MAIN)
Convert an input title or title prefix into a TitleValue.
Definition: ApiQueryBase.php:559
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:69
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
ApiBase\PARAM_MAX2
const PARAM_MAX2
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:73
$t
$t
Definition: testCompression.php:74
ApiBase\PARAM_HELP_MSG_PER_VALUE
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:140
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:470