* (bug 12321) API list=blocks reveals private data
[lhc/web/wiklou.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 * @addtogroup 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']))
71 $this->addWhere('pl_title>=' . $db->addQuotes(ApiQueryBase :: titleToKey($params['from'])));
72 if (isset ($params['prefix']))
73 $this->addWhere("pl_title LIKE '" . $db->escapeLike(ApiQueryBase :: titleToKey($params['prefix'])) . "%'");
74
75 if (is_null($resultPageSet)) {
76 $this->addFields(array (
77 'pl_namespace',
78 'pl_title'
79 ));
80 $this->addFieldsIf('pl_from', $fld_ids);
81 } else {
82 $this->addFields('pl_from');
83 $pageids = array();
84 }
85
86 $this->addOption('USE INDEX', 'pl_namespace');
87 $limit = $params['limit'];
88 $this->addOption('LIMIT', $limit+1);
89 $this->addOption('ORDER BY', 'pl_namespace, pl_title');
90
91 $res = $this->select(__METHOD__);
92
93 $data = array ();
94 $count = 0;
95 while ($row = $db->fetchObject($res)) {
96 if (++ $count > $limit) {
97 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
98 // TODO: Security issue - if the user has no right to view next title, it will still be shown
99 $this->setContinueEnumParameter('from', ApiQueryBase :: keyToTitle($row->pl_title));
100 break;
101 }
102
103 if (is_null($resultPageSet)) {
104 $vals = array();
105 if ($fld_ids)
106 $vals['fromid'] = intval($row->pl_from);
107 if ($fld_title) {
108 $title = Title :: makeTitle($row->pl_namespace, $row->pl_title);
109 $vals['ns'] = intval($title->getNamespace());
110 $vals['title'] = $title->getPrefixedText();
111 }
112 $data[] = $vals;
113 } else {
114 $pageids[] = $row->pl_from;
115 }
116 }
117 $db->freeResult($res);
118
119 if (is_null($resultPageSet)) {
120 $result = $this->getResult();
121 $result->setIndexedTagName($data, 'l');
122 $result->addValue('query', $this->getModuleName(), $data);
123 } else {
124 $resultPageSet->populateFromPageIDs($pageids);
125 }
126 }
127
128 protected function getAllowedParams() {
129 return array (
130 'from' => null,
131 'prefix' => null,
132 'unique' => false,
133 'prop' => array (
134 ApiBase :: PARAM_ISMULTI => true,
135 ApiBase :: PARAM_DFLT => 'title',
136 ApiBase :: PARAM_TYPE => array (
137 'ids',
138 'title'
139 )
140 ),
141 'namespace' => array (
142 ApiBase :: PARAM_DFLT => 0,
143 ApiBase :: PARAM_TYPE => 'namespace'
144 ),
145 'limit' => array (
146 ApiBase :: PARAM_DFLT => 10,
147 ApiBase :: PARAM_TYPE => 'limit',
148 ApiBase :: PARAM_MIN => 1,
149 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
150 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
151 )
152 );
153 }
154
155 protected function getParamDescription() {
156 return array (
157 'from' => 'The page title to start enumerating from.',
158 'prefix' => 'Search for all page titles that begin with this value.',
159 'unique' => 'Only show unique links. Cannot be used with generator or prop=ids',
160 'prop' => 'What pieces of information to include',
161 'namespace' => 'The namespace to enumerate.',
162 'limit' => 'How many total links to return.'
163 );
164 }
165
166 protected function getDescription() {
167 return 'Enumerate all links that point to a given namespace';
168 }
169
170 protected function getExamples() {
171 return array (
172 'api.php?action=query&list=alllinks&alunique&alfrom=B',
173 );
174 }
175
176 public function getVersion() {
177 return __CLASS__ . ': $Id$';
178 }
179 }