]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryCategoryMembers.php
MediaWiki 1.11.0-scripts
[autoinstallsdev/mediawiki.git] / includes / api / ApiQueryCategoryMembers.php
1 <?php
2
3 /*
4  * Created on June 14, 2007
5  *
6  * API for MediaWiki 1.8+
7  *
8  * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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  * A query module to enumerate pages that belong to a category.
33  * 
34  * @addtogroup API
35  */
36 class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
37
38         public function __construct($query, $moduleName) {
39                 parent :: __construct($query, $moduleName, 'cm');
40         }
41
42         public function execute() {
43                 $this->run();
44         }
45
46         public function executeGenerator($resultPageSet) {
47                 $this->run($resultPageSet);
48         }
49
50         private function run($resultPageSet = null) {
51
52                 $params = $this->extractRequestParams();
53
54                 $category = $params['category'];
55                 if (is_null($category))
56                         $this->dieUsage("Category parameter is required", 'param_category');
57                 $categoryTitle = Title::makeTitleSafe( NS_CATEGORY, $category );
58                 if ( is_null( $categoryTitle ) )
59                         $this->dieUsage("Category name $category is not valid", 'param_category');
60                 
61                 $prop = array_flip($params['prop']);
62                 $fld_ids = isset($prop['ids']);
63                 $fld_title = isset($prop['title']);
64                 $fld_sortkey = isset($prop['sortkey']);
65                 $fld_timestamp = isset($prop['timestamp']);
66
67                 if (is_null($resultPageSet)) {
68                         $this->addFields(array('cl_from', 'cl_sortkey', 'page_namespace', 'page_title'));
69                         $this->addFieldsIf('page_id', $fld_ids);
70                 } else {
71                         $this->addFields($resultPageSet->getPageTableFields()); // will include page_ id, ns, title
72                         $this->addFields(array('cl_from', 'cl_sortkey'));
73                 }
74
75                 $this->addFieldsIf('cl_timestamp', $fld_timestamp);
76                 $this->addTables(array('page','categorylinks'));        // must be in this order for 'USE INDEX' 
77                                                                         // Not needed after bug 10280 is applied to servers
78                 if($params['sort'] == 'timestamp')
79                 {
80                         $this->addOption('USE INDEX', 'cl_timestamp');
81                         $this->addOption('ORDER BY', 'cl_to, cl_timestamp');
82                 }
83                 else
84                 {
85                         $this->addOption('USE INDEX', 'cl_sortkey');
86                         $this->addOption('ORDER BY', 'cl_to, cl_sortkey, cl_from');
87                 }
88
89                 $this->addWhere('cl_from=page_id');
90                 $this->setContinuation($params['continue']);            
91                 $this->addWhereFld('cl_to', $categoryTitle->getDBkey());
92                 $this->addWhereFld('page_namespace', $params['namespace']);
93                 
94                 $limit = $params['limit'];
95                 $this->addOption('LIMIT', $limit +1);
96
97                 $db = $this->getDB();
98
99                 $data = array ();
100                 $count = 0;
101                 $lastSortKey = null;
102                 $res = $this->select(__METHOD__);
103                 while ($row = $db->fetchObject($res)) {
104                         if (++ $count > $limit) {
105                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
106                                 // TODO: Security issue - if the user has no right to view next title, it will still be shown
107                                 $this->setContinueEnumParameter('continue', $this->getContinueStr($row, $lastSortKey));
108                                 break;
109                         }
110
111                         $lastSortKey = $row->cl_sortkey;        // detect duplicate sortkeys 
112                         
113                         if (is_null($resultPageSet)) {
114                                 $vals = array();
115                                 if ($fld_ids)
116                                         $vals['pageid'] = intval($row->page_id); 
117                                 if ($fld_title) {
118                                         $title = Title :: makeTitle($row->page_namespace, $row->page_title);
119                                         $vals['ns'] = intval($title->getNamespace());
120                                         $vals['title'] = $title->getPrefixedText();
121                                 }
122                                 if ($fld_sortkey)
123                                         $vals['sortkey'] = $row->cl_sortkey;
124                                 if ($fld_timestamp)
125                                         $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->cl_timestamp);
126                                 $data[] = $vals;
127                         } else {
128                                 $resultPageSet->processDbRow($row);
129                         }
130                 }
131                 $db->freeResult($res);
132
133                 if (is_null($resultPageSet)) {
134                         $this->getResult()->setIndexedTagName($data, 'cm');
135                         $this->getResult()->addValue('query', $this->getModuleName(), $data);
136                 }
137         }
138         
139         private function getContinueStr($row, $lastSortKey) {
140                 $ret = $row->cl_sortkey . '|';
141                 if ($row->cl_sortkey == $lastSortKey)   // duplicate sort key, add cl_from
142                         $ret .= $row->cl_from;
143                 return $ret;
144         }
145         
146         /**
147          * Add DB WHERE clause to continue previous query based on 'continue' parameter 
148          */
149         private function setContinuation($continue) {
150                 if (is_null($continue))
151                         return; // This is not a continuation request
152                         
153                 $continueList = explode('|', $continue);
154                 $hasError = count($continueList) != 2;
155                 $from = 0;
156                 if (!$hasError && strlen($continueList[1]) > 0) {
157                         $from = intval($continueList[1]);
158                         $hasError = ($from == 0); 
159                 }
160                 
161                 if ($hasError)
162                         $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
163
164                 $encSortKey = $this->getDB()->addQuotes($continueList[0]);
165                 $encFrom = $this->getDB()->addQuotes($from);
166
167                 if ($from != 0) {
168                         // Duplicate sort key continue
169                         $this->addWhere( "cl_sortkey>$encSortKey OR (cl_sortkey=$encSortKey AND cl_from>=$encFrom)" );
170                 } else {
171                         $this->addWhere( "cl_sortkey>=$encSortKey" );
172                 }
173         }
174
175         protected function getAllowedParams() {
176                 return array (
177                         'category' => null,
178                         'prop' => array (
179                                 ApiBase :: PARAM_DFLT => 'ids|title',
180                                 ApiBase :: PARAM_ISMULTI => true,
181                                 ApiBase :: PARAM_TYPE => array (
182                                         'ids',
183                                         'title',
184                                         'sortkey',
185                                         'timestamp',
186                                 )
187                         ),
188                         'namespace' => array (
189                                 ApiBase :: PARAM_ISMULTI => true,
190                                 ApiBase :: PARAM_TYPE => 'namespace',
191                         ),
192                         'continue' => null,
193                         'limit' => array (
194                                 ApiBase :: PARAM_TYPE => 'limit',
195                                 ApiBase :: PARAM_DFLT => 10,
196                                 ApiBase :: PARAM_MIN => 1,
197                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
198                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
199                         ),
200                         'sort' => array(
201                                 ApiBase :: PARAM_DFLT => 'sortkey',
202                                 ApiBase :: PARAM_TYPE => array(
203                                         'sortkey',
204                                         'timestamp'
205                                 )
206                         )
207                 );
208         }
209
210         protected function getParamDescription() {
211                 return array (
212                         'category' => 'Which category to enumerate (required)',
213                         'prop' => 'What pieces of information to include',
214                         'namespace' => 'Only include pages in these namespaces',
215                         'sort' => 'Property to sort by',
216                         'continue' => 'For large categories, give the value retured from previous query',
217                         'limit' => 'The maximum number of pages to return.',
218                 );
219         }
220
221         protected function getDescription() {
222                 return 'List all pages in a given category';
223         }
224
225         protected function getExamples() {
226                 return array (
227                                 "Get first 10 pages in the categories [[Physics]]:",
228                                 "  api.php?action=query&list=categorymembers&cmcategory=Physics",
229                                 "Get page info about first 10 pages in the categories [[Physics]]:",
230                                 "  api.php?action=query&generator=categorymembers&gcmcategory=Physics&prop=info",
231                         );
232         }
233
234         public function getVersion() {
235                 return __CLASS__ . ': $Id: ApiQueryCategoryMembers.php 25474 2007-09-04 14:30:31Z catrope $';
236         }
237 }
238