Add noratelimit right to list of core rights, was missing.
[lhc/web/wiklou.git] / includes / api / ApiQueryLinks.php
1 <?php
2
3 /*
4 * Created on May 12, 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 list all wiki links on a given set of pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryLinks extends ApiQueryGeneratorBase {
37
38 const LINKS = 'links';
39 const TEMPLATES = 'templates';
40
41 private $table, $prefix, $description;
42
43 public function __construct($query, $moduleName) {
44
45 switch ($moduleName) {
46 case self::LINKS :
47 $this->table = 'pagelinks';
48 $this->prefix = 'pl';
49 $this->description = 'link';
50 break;
51 case self::TEMPLATES :
52 $this->table = 'templatelinks';
53 $this->prefix = 'tl';
54 $this->description = 'template';
55 break;
56 default :
57 ApiBase :: dieDebug(__METHOD__, 'Unknown module name');
58 }
59
60 parent :: __construct($query, $moduleName, $this->prefix);
61 }
62
63 public function execute() {
64 $this->run();
65 }
66
67 public function executeGenerator($resultPageSet) {
68 $this->run($resultPageSet);
69 }
70
71 private function run($resultPageSet = null) {
72
73 if ($this->getPageSet()->getGoodTitleCount() == 0)
74 return; // nothing to do
75
76 $params = $this->extractRequestParams();
77
78 $this->addFields(array (
79 $this->prefix . '_from pl_from',
80 $this->prefix . '_namespace pl_namespace',
81 $this->prefix . '_title pl_title'
82 ));
83
84 $this->addTables($this->table);
85 $this->addWhereFld($this->prefix . '_from', array_keys($this->getPageSet()->getGoodTitles()));
86 $this->addWhereFld($this->prefix . '_namespace', $params['namespace']);
87
88 # Here's some MySQL craziness going on: if you use WHERE foo='bar'
89 # and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
90 # but instead goes and filesorts, because the index for foo was used
91 # already. To work around this, we drop constant fields in the WHERE
92 # clause from the ORDER BY clause
93 $order = array();
94 if(count($this->getPageSet()->getGoodTitles()) != 1)
95 $order[] = "{$this->prefix}_from";
96 // pl_namespace is always constant
97 $order[] = "{$this->prefix}_title";
98 $this->addOption('ORDER BY', implode(", ", $order));
99 $this->addOption('USE INDEX', "{$this->prefix}_from");
100
101 $db = $this->getDB();
102 $res = $this->select(__METHOD__);
103
104 if (is_null($resultPageSet)) {
105
106 $data = array();
107 $lastId = 0; // database has no ID 0
108 while ($row = $db->fetchObject($res)) {
109 if ($lastId != $row->pl_from) {
110 if($lastId != 0) {
111 $this->addPageSubItems($lastId, $data);
112 $data = array();
113 }
114 $lastId = $row->pl_from;
115 }
116
117 $vals = array();
118 ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->pl_namespace, $row->pl_title));
119 $data[] = $vals;
120 }
121
122 if($lastId != 0) {
123 $this->addPageSubItems($lastId, $data);
124 }
125
126 } else {
127
128 $titles = array();
129 while ($row = $db->fetchObject($res)) {
130 $titles[] = Title :: makeTitle($row->pl_namespace, $row->pl_title);
131 }
132 $resultPageSet->populateFromTitles($titles);
133 }
134
135 $db->freeResult($res);
136 }
137
138 public function getAllowedParams()
139 {
140 return array(
141 'namespace' => array(
142 ApiBase :: PARAM_TYPE => 'namespace',
143 ApiBase :: PARAM_ISMULTI => true
144 )
145 );
146 }
147
148 public function getParamDescription()
149 {
150 return array(
151 'namespace' => "Show {$this->description}s in this namespace(s) only"
152 );
153 }
154
155 public function getDescription() {
156 return "Returns all {$this->description}s from the given page(s)";
157 }
158
159 protected function getExamples() {
160 return array (
161 "Get {$this->description}s from the [[Main Page]]:",
162 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
163 "Get information about the {$this->description} pages in the [[Main Page]]:",
164 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
165 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
166 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
167 );
168 }
169
170 public function getVersion() {
171 return __CLASS__ . ': $Id$';
172 }
173 }