Fix regression in unwatch links sent out by enotif. {{FULLPAGENAMEE}} happens to...
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlistRaw.php
1 <?php
2
3 /**
4 * Created on Oct 4, 2008
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * This query action allows clients to retrieve a list of pages
33 * on the logged-in user's watchlist.
34 *
35 * @ingroup API
36 */
37 class ApiQueryWatchlistRaw extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'wr' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 private function run( $resultPageSet = null ) {
52 global $wgUser;
53
54 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
55
56 if ( !$wgUser->isLoggedIn() ) {
57 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
58 }
59 $params = $this->extractRequestParams();
60 $prop = array_flip( (array)$params['prop'] );
61 $show = array_flip( (array)$params['show'] );
62 if ( isset( $show['changed'] ) && isset( $show['!changed'] ) ) {
63 $this->dieUsageMsg( array( 'show' ) );
64 }
65
66 $this->addTables( 'watchlist' );
67 $this->addFields( array( 'wl_namespace', 'wl_title' ) );
68 $this->addFieldsIf( 'wl_notificationtimestamp', isset( $prop['changed'] ) );
69 $this->addWhereFld( 'wl_user', $wgUser->getId() );
70 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
71 $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['changed'] ) );
72 $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!changed'] ) );
73
74 if ( isset( $params['continue'] ) ) {
75 $cont = explode( '|', $params['continue'] );
76 if ( count( $cont ) != 2 ) {
77 $this->dieUsage( "Invalid continue param. You should pass the " .
78 "original value returned by the previous query", "_badcontinue" );
79 }
80 $ns = intval( $cont[0] );
81 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
82 $this->addWhere(
83 "wl_namespace > '$ns' OR " .
84 "(wl_namespace = '$ns' AND " .
85 "wl_title >= '$title')"
86 );
87 }
88
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' );
92 } else {
93 $this->addOption( 'ORDER BY', 'wl_namespace, wl_title' );
94 }
95 $this->addOption( 'LIMIT', $params['limit'] + 1 );
96 $res = $this->select( __METHOD__ );
97
98 $db = $this->getDB();
99 $titles = array();
100 $count = 0;
101 while ( $row = $db->fetchObject( $res ) ) {
102 if ( ++$count > $params['limit'] ) {
103 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
104 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
105 $this->keyToTitle( $row->wl_title ) );
106 break;
107 }
108 $t = Title::makeTitle( $row->wl_namespace, $row->wl_title );
109
110 if ( is_null( $resultPageSet ) ) {
111 $vals = array();
112 ApiQueryBase::addTitleInfo( $vals, $t );
113 if ( isset( $prop['changed'] ) && !is_null( $row->wl_notificationtimestamp ) )
114 {
115 $vals['changed'] = wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
116 }
117 $fit = $this->getResult()->addValue( $this->getModuleName(), null, $vals );
118 if ( !$fit ) {
119 $this->setContinueEnumParameter( 'continue', $row->wl_namespace . '|' .
120 $this->keyToTitle( $row->wl_title ) );
121 break;
122 }
123 } else {
124 $titles[] = $t;
125 }
126 }
127 if ( is_null( $resultPageSet ) ) {
128 $this->getResult()->setIndexedTagName_internal( $this->getModuleName(), 'wr' );
129 } else {
130 $resultPageSet->populateFromTitles( $titles );
131 }
132 }
133
134 public function getAllowedParams() {
135 return array(
136 'continue' => null,
137 'namespace' => array(
138 ApiBase::PARAM_ISMULTI => true,
139 ApiBase::PARAM_TYPE => 'namespace'
140 ),
141 'limit' => array(
142 ApiBase::PARAM_DFLT => 10,
143 ApiBase::PARAM_TYPE => 'limit',
144 ApiBase::PARAM_MIN => 1,
145 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
146 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
147 ),
148 'prop' => array(
149 ApiBase::PARAM_ISMULTI => true,
150 ApiBase::PARAM_TYPE => array(
151 'changed',
152 )
153 ),
154 'show' => array(
155 ApiBase::PARAM_ISMULTI => true,
156 ApiBase::PARAM_TYPE => array(
157 'changed',
158 '!changed',
159 )
160 )
161 );
162 }
163
164 public function getParamDescription() {
165 return array(
166 'continue' => 'When more results are available, use this to continue',
167 'namespace' => 'Only list pages in the given namespace(s).',
168 'limit' => 'How many total results to return per request.',
169 'prop' => 'Which additional properties to get (non-generator mode only).',
170 'show' => 'Only list items that meet these criteria.',
171 );
172 }
173
174 public function getDescription() {
175 return "Get all pages on the logged in user's watchlist";
176 }
177
178 public function getPossibleErrors() {
179 return array_merge( parent::getPossibleErrors(), array(
180 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
181 array( 'show' ),
182 ) );
183 }
184
185 protected function getExamples() {
186 return array(
187 'api.php?action=query&list=watchlistraw',
188 'api.php?action=query&generator=watchlistraw&gwrshow=changed&prop=revisions',
189 );
190 }
191
192 public function getVersion() {
193 return __CLASS__ . ': $Id$';
194 }
195 }