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
28 * This query action allows clients to retrieve a list of pages
29 * on the logged-in user's watchlist.
33 class ApiQueryWatchlistRaw
extends ApiQueryGeneratorBase
{
35 public function __construct( $query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'wr' );
39 public function execute() {
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
48 * @param $resultPageSet ApiPageSet
51 private function run( $resultPageSet = null ) {
52 $this->selectNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
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['changed'] ) && isset( $show['!changed'] ) ) {
61 $this->dieUsageMsg( 'show' );
64 $this->addTables( 'watchlist' );
65 $this->addFields( array( 'wl_namespace', 'wl_title' ) );
66 $this->addFieldsIf( 'wl_notificationtimestamp', isset( $prop['changed'] ) );
67 $this->addWhereFld( 'wl_user', $user->getId() );
68 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
69 $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['changed'] ) );
70 $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!changed'] ) );
72 if ( isset( $params['continue'] ) ) {
73 $cont = explode( '|', $params['continue'] );
74 if ( count( $cont ) != 2 ) {
75 $this->dieUsage( "Invalid continue param. You should pass the " .
76 "original value returned by the previous query", "_badcontinue" );
78 $ns = intval( $cont[0] );
79 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
80 $op = $params['dir'] == 'ascending' ?
'>' : '<';
82 "wl_namespace $op '$ns' OR " .
83 "(wl_namespace = '$ns' AND " .
84 "wl_title $op= '$title')"
88 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
89 // Don't ORDER BY wl_namespace if it's constant in the WHERE clause
90 if ( count( $params['namespace'] ) == 1 ) {
91 $this->addOption( 'ORDER BY', 'wl_title' . $sort );
93 $this->addOption( 'ORDER BY', 'wl_namespace' . $sort . ', wl_title' . $sort );
95 $this->addOption( 'LIMIT', $params['limit'] +
1 );
96 $res = $this->select( __METHOD__
);
100 foreach ( $res as $row ) {
101 if ( ++
$count > $params['limit'] ) {
102 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
103 $this->setContinueEnumParameter( 'continue', $row->wl_namespace
. '|' .
104 $this->keyToTitle( $row->wl_title
) );
107 $t = Title
::makeTitle( $row->wl_namespace
, $row->wl_title
);
109 if ( is_null( $resultPageSet ) ) {
111 ApiQueryBase
::addTitleInfo( $vals, $t );
112 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp
) )
114 $vals['changed'] = wfTimestamp( TS_ISO_8601
, $row->wl_notificationtimestamp
);
116 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
118 $this->setContinueEnumParameter( 'continue', $row->wl_namespace
. '|' .
119 $this->keyToTitle( $row->wl_title
) );
126 if ( is_null( $resultPageSet ) ) {
127 $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
129 $resultPageSet->populateFromTitles( $titles );
133 public function getAllowedParams() {
136 'namespace' => array(
137 ApiBase
::PARAM_ISMULTI
=> true,
138 ApiBase
::PARAM_TYPE
=> 'namespace'
141 ApiBase
::PARAM_DFLT
=> 10,
142 ApiBase
::PARAM_TYPE
=> 'limit',
143 ApiBase
::PARAM_MIN
=> 1,
144 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
145 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
148 ApiBase
::PARAM_ISMULTI
=> true,
149 ApiBase
::PARAM_TYPE
=> array(
154 ApiBase
::PARAM_ISMULTI
=> true,
155 ApiBase
::PARAM_TYPE
=> array(
161 ApiBase
::PARAM_TYPE
=> 'user'
164 ApiBase
::PARAM_TYPE
=> 'string'
167 ApiBase
::PARAM_DFLT
=> 'ascending',
168 ApiBase
::PARAM_TYPE
=> array(
176 public function getParamDescription() {
178 'continue' => 'When more results are available, use this to continue',
179 'namespace' => 'Only list pages in the given namespace(s)',
180 'limit' => 'How many total results to return per request',
182 'Which additional properties to get (non-generator mode only)',
183 ' changed - Adds timestamp of when the user was last notified about the edit',
185 'show' => 'Only list items that meet these criteria',
186 'owner' => 'The name of the user whose watchlist you\'d like to access',
187 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist',
188 'dir' => 'Direction to sort the titles and namespaces in',
192 public function getDescription() {
193 return "Get all pages on the logged in user's watchlist";
196 public function getPossibleErrors() {
197 return array_merge( parent
::getPossibleErrors(), array(
198 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
200 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
201 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
205 public function getExamples() {
207 'api.php?action=query&list=watchlistraw',
208 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
212 public function getVersion() {
213 return __CLASS__
. ': $Id$';