Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / api / ApiQueryWatchlistRaw.php
bloba1078a5d48241db7ad26e418c94d39792fa7c6f9
1 <?php
2 /**
5 * Created on Oct 4, 2008
7 * Copyright © 2008 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 use MediaWiki\MediaWikiServices;
29 /**
30 * This query action allows clients to retrieve a list of pages
31 * on the logged-in user's watchlist.
33 * @ingroup API
35 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'wr' );
41 public function execute() {
42 $this->run();
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
49 /**
50 * @param ApiPageSet $resultPageSet
51 * @return void
53 private function run( $resultPageSet = null ) {
54 $params = $this->extractRequestParams();
56 $user = $this->getWatchlistUser( $params );
58 $prop = array_flip( (array)$params['prop'] );
59 $show = array_flip( (array)$params['show'] );
60 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] )
61 && isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] )
62 ) {
63 $this->dieWithError( 'apierror-show' );
66 $options = [];
67 if ( $params['namespace'] ) {
68 $options['namespaceIds'] = $params['namespace'];
70 if ( isset( $show[WatchedItemQueryService::FILTER_CHANGED] ) ) {
71 $options['filter'] = WatchedItemQueryService::FILTER_CHANGED;
73 if ( isset( $show[WatchedItemQueryService::FILTER_NOT_CHANGED] ) ) {
74 $options['filter'] = WatchedItemQueryService::FILTER_NOT_CHANGED;
77 if ( isset( $params['continue'] ) ) {
78 $cont = explode( '|', $params['continue'] );
79 $this->dieContinueUsageIf( count( $cont ) != 2 );
80 $ns = intval( $cont[0] );
81 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
82 $title = $cont[1];
83 $options['startFrom'] = new TitleValue( $ns, $title );
86 if ( isset( $params['fromtitle'] ) ) {
87 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['fromtitle'] );
88 $options['from'] = new TitleValue( $ns, $title );
91 if ( isset( $params['totitle'] ) ) {
92 list( $ns, $title ) = $this->prefixedTitlePartToKey( $params['totitle'] );
93 $options['until'] = new TitleValue( $ns, $title );
96 $options['sort'] = WatchedItemStore::SORT_ASC;
97 if ( $params['dir'] === 'descending' ) {
98 $options['sort'] = WatchedItemStore::SORT_DESC;
100 $options['limit'] = $params['limit'] + 1;
102 $titles = [];
103 $count = 0;
104 $items = MediaWikiServices::getInstance()->getWatchedItemQueryService()
105 ->getWatchedItemsForUser( $user, $options );
106 foreach ( $items as $item ) {
107 $ns = $item->getLinkTarget()->getNamespace();
108 $dbKey = $item->getLinkTarget()->getDBkey();
109 if ( ++$count > $params['limit'] ) {
110 // We've reached the one extra which shows that there are
111 // additional pages to be had. Stop here...
112 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
113 break;
115 $t = Title::makeTitle( $ns, $dbKey );
117 if ( is_null( $resultPageSet ) ) {
118 $vals = [];
119 ApiQueryBase::addTitleInfo( $vals, $t );
120 if ( isset( $prop['changed'] ) && !is_null( $item->getNotificationTimestamp() ) ) {
121 $vals['changed'] = wfTimestamp( TS_ISO_8601, $item->getNotificationTimestamp() );
123 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
124 if ( !$fit ) {
125 $this->setContinueEnumParameter( 'continue', $ns . '|' . $dbKey );
126 break;
128 } else {
129 $titles[] = $t;
132 if ( is_null( $resultPageSet ) ) {
133 $this->getResult()->addIndexedTagName( $this->getModuleName(), 'wr' );
134 } else {
135 $resultPageSet->populateFromTitles( $titles );
139 public function getAllowedParams() {
140 return [
141 'continue' => [
142 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
144 'namespace' => [
145 ApiBase::PARAM_ISMULTI => true,
146 ApiBase::PARAM_TYPE => 'namespace'
148 'limit' => [
149 ApiBase::PARAM_DFLT => 10,
150 ApiBase::PARAM_TYPE => 'limit',
151 ApiBase::PARAM_MIN => 1,
152 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
153 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
155 'prop' => [
156 ApiBase::PARAM_ISMULTI => true,
157 ApiBase::PARAM_TYPE => [
158 'changed',
160 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
162 'show' => [
163 ApiBase::PARAM_ISMULTI => true,
164 ApiBase::PARAM_TYPE => [
165 WatchedItemQueryService::FILTER_CHANGED,
166 WatchedItemQueryService::FILTER_NOT_CHANGED
169 'owner' => [
170 ApiBase::PARAM_TYPE => 'user'
172 'token' => [
173 ApiBase::PARAM_TYPE => 'string'
175 'dir' => [
176 ApiBase::PARAM_DFLT => 'ascending',
177 ApiBase::PARAM_TYPE => [
178 'ascending',
179 'descending'
182 'fromtitle' => [
183 ApiBase::PARAM_TYPE => 'string'
185 'totitle' => [
186 ApiBase::PARAM_TYPE => 'string'
191 protected function getExamplesMessages() {
192 return [
193 'action=query&list=watchlistraw'
194 => 'apihelp-query+watchlistraw-example-simple',
195 'action=query&generator=watchlistraw&gwrshow=changed&prop=info'
196 => 'apihelp-query+watchlistraw-example-generator',
200 public function getHelpUrls() {
201 return 'https://www.mediawiki.org/wiki/API:Watchlistraw';