]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/api/ApiQueryAllLinks.php
MediaWiki 1.14.0
[autoinstallsdev/mediawiki.git] / includes / api / ApiQueryAllLinks.php
1 <?php
2
3 /*
4  * Created on July 7, 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  * Query module to enumerate links from all pages together.
33  *
34  * @ingroup API
35  */
36 class ApiQueryAllLinks extends ApiQueryGeneratorBase {
37
38         public function __construct($query, $moduleName) {
39                 parent :: __construct($query, $moduleName, 'al');
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                 $db = $this->getDB();
53                 $params = $this->extractRequestParams();
54
55                 $prop = array_flip($params['prop']);
56                 $fld_ids = isset($prop['ids']);
57                 $fld_title = isset($prop['title']);
58
59                 if ($params['unique']) {
60                         if (!is_null($resultPageSet))
61                                 $this->dieUsage($this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params');
62                         if ($fld_ids)
63                                 $this->dieUsage($this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params');
64                         $this->addOption('DISTINCT');
65                 }
66
67                 $this->addTables('pagelinks');
68                 $this->addWhereFld('pl_namespace', $params['namespace']);
69                 
70                 if (!is_null($params['from']) && !is_null($params['continue']))
71                         $this->dieUsage('alcontinue and alfrom cannot be used together', 'params');
72                 if (!is_null($params['continue']))
73                 {
74                         $arr = explode('|', $params['continue']);
75                         if(count($arr) != 2)
76                                 $this->dieUsage("Invalid continue parameter", 'badcontinue');
77                         $from = $this->getDB()->strencode($this->titleToKey($arr[0]));
78                         $id = intval($arr[1]);
79                         $this->addWhere("pl_title > '$from' OR " .
80                                         "(pl_title = '$from' AND " .
81                                         "pl_from > $id)");
82                 }               
83
84                 if (!is_null($params['from']))
85                         $this->addWhere('pl_title>=' . $db->addQuotes($this->titlePartToKey($params['from'])));
86                 if (isset ($params['prefix']))
87                         $this->addWhere("pl_title LIKE '" . $db->escapeLike($this->titlePartToKey($params['prefix'])) . "%'");
88
89                 $this->addFields(array (
90                         'pl_title',
91                 ));
92                 $this->addFieldsIf('pl_from', !$params['unique']);
93
94                 $this->addOption('USE INDEX', 'pl_namespace');
95                 $limit = $params['limit'];
96                 $this->addOption('LIMIT', $limit+1);
97                 if($params['unique'])
98                         $this->addOption('ORDER BY', 'pl_title');
99                 else
100                         $this->addOption('ORDER BY', 'pl_title, pl_from');
101
102                 $res = $this->select(__METHOD__);
103
104                 $data = array ();
105                 $count = 0;
106                 while ($row = $db->fetchObject($res)) {
107                         if (++ $count > $limit) {
108                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
109                                 // TODO: Security issue - if the user has no right to view next title, it will still be shown
110                                 if($params['unique'])
111                                         $this->setContinueEnumParameter('from', $this->keyToTitle($row->pl_title));
112                                 else
113                                         $this->setContinueEnumParameter('continue', $this->keyToTitle($row->pl_title) . "|" . $row->pl_from);
114                                 break;
115                         }
116
117                         if (is_null($resultPageSet)) {
118                                 $vals = array();
119                                 if ($fld_ids)
120                                         $vals['fromid'] = intval($row->pl_from);
121                                 if ($fld_title) {
122                                         $title = Title :: makeTitle($params['namespace'], $row->pl_title);
123                                         $vals['ns'] = intval($title->getNamespace());
124                                         $vals['title'] = $title->getPrefixedText();
125                                 }
126                                 $data[] = $vals;
127                         } else {
128                                 $pageids[] = $row->pl_from;
129                         }
130                 }
131                 $db->freeResult($res);
132
133                 if (is_null($resultPageSet)) {
134                         $result = $this->getResult();
135                         $result->setIndexedTagName($data, 'l');
136                         $result->addValue('query', $this->getModuleName(), $data);
137                 } else {
138                         $resultPageSet->populateFromPageIDs($pageids);
139                 }
140         }
141
142         public function getAllowedParams() {
143                 return array (
144                         'continue' => null,
145                         'from' => null,
146                         'prefix' => null,
147                         'unique' => false,
148                         'prop' => array (
149                                 ApiBase :: PARAM_ISMULTI => true,
150                                 ApiBase :: PARAM_DFLT => 'title',
151                                 ApiBase :: PARAM_TYPE => array (
152                                         'ids',
153                                         'title'
154                                 )
155                         ),
156                         'namespace' => array (
157                                 ApiBase :: PARAM_DFLT => 0,
158                                 ApiBase :: PARAM_TYPE => 'namespace'
159                         ),
160                         'limit' => array (
161                                 ApiBase :: PARAM_DFLT => 10,
162                                 ApiBase :: PARAM_TYPE => 'limit',
163                                 ApiBase :: PARAM_MIN => 1,
164                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
165                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
166                         )
167                 );
168         }
169
170         public function getParamDescription() {
171                 return array (
172                         'from' => 'The page title to start enumerating from.',
173                         'prefix' => 'Search for all page titles that begin with this value.',
174                         'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
175                         'prop' => 'What pieces of information to include',
176                         'namespace' => 'The namespace to enumerate.',
177                         'limit' => 'How many total links to return.',
178                         'continue' => 'When more results are available, use this to continue.',
179                 );
180         }
181
182         public function getDescription() {
183                 return 'Enumerate all links that point to a given namespace';
184         }
185
186         protected function getExamples() {
187                 return array (
188                         'api.php?action=query&list=alllinks&alunique&alfrom=B',
189                 );
190         }
191
192         public function getVersion() {
193                 return __CLASS__ . ': $Id: ApiQueryAllLinks.php 45850 2009-01-17 20:03:25Z catrope $';
194         }
195 }