API bug 10096 InterWiki table retrieval through meta=siteinfo
[lhc/web/wiklou.git] / includes / api / ApiQuerySiteinfo.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
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 action to return meta information about the wiki site.
33 *
34 * @addtogroup API
35 */
36 class ApiQuerySiteinfo extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'si');
40 }
41
42 public function execute() {
43
44 $params = $this->extractRequestParams();
45
46 foreach ($params['prop'] as $p) {
47 switch ($p) {
48 default :
49 ApiBase :: dieDebug(__METHOD__, "Unknown prop=$p");
50 case 'general' :
51 $this->appendGeneralInfo($p);
52 break;
53 case 'namespaces' :
54 $this->appendNamespaces($p);
55 break;
56 case 'interwikimap' :
57 $filteriw = isset($params['filteriw']) ? $params['filteriw'] : false;
58 $this->appendInterwikiMap($p, $filteriw);
59 break;
60 }
61 }
62 }
63
64 protected function appendGeneralInfo($property) {
65 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText;
66
67 $data = array ();
68 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
69 $data['mainpage'] = $mainPage->getText();
70 $data['base'] = $mainPage->getFullUrl();
71 $data['sitename'] = $wgSitename;
72 $data['generator'] = "MediaWiki $wgVersion";
73 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // 'case-insensitive' option is reserved for future
74 if (isset($wgRightsCode))
75 $data['rightscode'] = $wgRightsCode;
76 $data['rights'] = $wgRightsText;
77
78 $this->getResult()->addValue('query', $property, $data);
79 }
80
81 protected function appendNamespaces($property) {
82 global $wgContLang;
83
84 $data = array ();
85 foreach ($wgContLang->getFormattedNamespaces() as $ns => $title) {
86 $data[$ns] = array (
87 'id' => $ns
88 );
89 ApiResult :: setContent($data[$ns], $title);
90 }
91
92 $this->getResult()->setIndexedTagName($data, 'ns');
93 $this->getResult()->addValue('query', $property, $data);
94 }
95
96 protected function appendInterwikiMap($property, $filter) {
97
98 $this->addTables('interwiki');
99 $this->addFields(array('iw_prefix', 'iw_local', 'iw_url'));
100
101 if($filter === 'local')
102 $this->addWhere('iw_local = 1');
103 else if($filter === '!local')
104 $this->addWhere('iw_local = 0');
105 else if($filter !== false)
106 ApiBase :: dieDebug(__METHOD__, "Unknown filter=$filter");
107
108 $this->addOption('ORDER BY', 'iw_prefix');
109
110 $db = $this->getDB();
111 $res = $this->select(__METHOD__);
112
113 $data = array();
114 while($row = $db->fetchObject($res))
115 {
116 $val['prefix'] = $row->iw_prefix;
117 if ($row->iw_local == '1')
118 $val['local'] = '';
119 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
120 $val['url'] = $row->iw_url;
121
122 $data[] = $val;
123 }
124 $db->freeResult($res);
125
126 $this->getResult()->setIndexedTagName($data, 'iw');
127 $this->getResult()->addValue('query', $property, $data);
128 }
129
130 protected function getAllowedParams() {
131 return array (
132
133 'prop' => array (
134 ApiBase :: PARAM_DFLT => 'general',
135 ApiBase :: PARAM_ISMULTI => true,
136 ApiBase :: PARAM_TYPE => array (
137 'general',
138 'namespaces',
139 'interwikimap'
140 )),
141
142 'filteriw' => array (
143 ApiBase :: PARAM_TYPE => array (
144 'local',
145 '!local',
146 )),
147 );
148 }
149
150 protected function getParamDescription() {
151 return array (
152 'prop' => array (
153 'Which sysinfo properties to get:',
154 ' "general" - Overall system information',
155 ' "namespaces" - List of registered namespaces (localized)',
156 ' "interwikimap" - Return interwiki map (optionally filtered)'
157 ),
158 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
159 );
160 }
161
162 protected function getDescription() {
163 return 'Return general information about the site.';
164 }
165
166 protected function getExamples() {
167 return array(
168 'api.php?action=query&meta=siteinfo&siprop=general|namespaces',
169 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
170 );
171 }
172
173 public function getVersion() {
174 return __CLASS__ . ': $Id$';
175 }
176 }
177 ?>