]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryLinks.php
MediaWiki 1.15.5
[autoinstalls/mediawiki.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 getCacheMode( $params ) {
68                 return 'public';
69         }
70
71         public function executeGenerator($resultPageSet) {
72                 $this->run($resultPageSet);
73         }
74
75         private function run($resultPageSet = null) {
76
77                 if ($this->getPageSet()->getGoodTitleCount() == 0)
78                         return; // nothing to do
79
80                 $params = $this->extractRequestParams();
81
82                 $this->addFields(array (
83                         $this->prefix . '_from AS pl_from',
84                         $this->prefix . '_namespace AS pl_namespace',
85                         $this->prefix . '_title AS pl_title'
86                 ));
87
88                 $this->addTables($this->table);
89                 $this->addWhereFld($this->prefix . '_from', array_keys($this->getPageSet()->getGoodTitles()));
90                 $this->addWhereFld($this->prefix . '_namespace', $params['namespace']);
91
92                 if(!is_null($params['continue'])) {
93                         $cont = explode('|', $params['continue']);
94                         if(count($cont) != 3)
95                                 $this->dieUsage("Invalid continue param. You should pass the " .
96                                         "original value returned by the previous query", "_badcontinue");
97                         $plfrom = intval($cont[0]);
98                         $plns = intval($cont[1]);
99                         $pltitle = $this->getDB()->strencode($this->titleToKey($cont[2]));
100                         $this->addWhere("{$this->prefix}_from > $plfrom OR ".
101                                         "({$this->prefix}_from = $plfrom AND ".
102                                         "({$this->prefix}_namespace > $plns OR ".
103                                         "({$this->prefix}_namespace = $plns AND ".
104                                         "{$this->prefix}_title >= '$pltitle')))");
105                 }
106
107                 # Here's some MySQL craziness going on: if you use WHERE foo='bar'
108                 # and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
109                 # but instead goes and filesorts, because the index for foo was used
110                 # already. To work around this, we drop constant fields in the WHERE
111                 # clause from the ORDER BY clause
112                 $order = array();
113                 if(count($this->getPageSet()->getGoodTitles()) != 1)
114                         $order[] = "{$this->prefix}_from";
115                 if(count($params['namespace']) != 1)
116                         $order[] = "{$this->prefix}_namespace";
117                 $order[] = "{$this->prefix}_title";
118                 $this->addOption('ORDER BY', implode(", ", $order));
119                 $this->addOption('USE INDEX', "{$this->prefix}_from");
120                 $this->addOption('LIMIT', $params['limit'] + 1);
121
122                 $db = $this->getDB();
123                 $res = $this->select(__METHOD__);
124
125                 if (is_null($resultPageSet)) {
126                         $count = 0;
127                         while ($row = $db->fetchObject($res)) {
128                                 if(++$count > $params['limit']) {
129                                         // We've reached the one extra which shows that
130                                         // there are additional pages to be had. Stop here...
131                                         $this->setContinueEnumParameter('continue',
132                                                 "{$row->pl_from}|{$row->pl_namespace}|" .
133                                                 $this->keyToTitle($row->pl_title));
134                                         break;
135                                 }
136                                 $vals = array();
137                                 ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->pl_namespace, $row->pl_title));
138                                 $fit = $this->addPageSubItem($row->pl_from, $vals);
139                                 if(!$fit)
140                                 {
141                                         $this->setContinueEnumParameter('continue',
142                                                 "{$row->pl_from}|{$row->pl_namespace}|" .
143                                                 $this->keyToTitle($row->pl_title));
144                                         break;
145                                 }
146                         }
147                 } else {
148
149                         $titles = array();
150                         $count = 0;
151                         while ($row = $db->fetchObject($res)) {
152                                 if(++$count > $params['limit']) {
153                                         // We've reached the one extra which shows that
154                                         // there are additional pages to be had. Stop here...
155                                         $this->setContinueEnumParameter('continue',
156                                                 "{$row->pl_from}|{$row->pl_namespace}|" .
157                                                 $this->keyToTitle($row->pl_title));
158                                         break;
159                                 }
160                                 $titles[] = Title :: makeTitle($row->pl_namespace, $row->pl_title);
161                         }
162                         $resultPageSet->populateFromTitles($titles);
163                 }
164
165                 $db->freeResult($res);
166         }
167
168         public function getAllowedParams()
169         {
170                 return array(
171                                 'namespace' => array(
172                                         ApiBase :: PARAM_TYPE => 'namespace',
173                                         ApiBase :: PARAM_ISMULTI => true
174                                 ),
175                                 'limit' => array(
176                                         ApiBase :: PARAM_DFLT => 10,
177                                         ApiBase :: PARAM_TYPE => 'limit',
178                                         ApiBase :: PARAM_MIN => 1,
179                                         ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
180                                         ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
181                                 ),
182                                 'continue' => null,
183                         );
184         }
185
186         public function getParamDescription()
187         {
188                 return array(
189                                 'namespace' => "Show {$this->description}s in this namespace(s) only",
190                                 'limit' => "How many {$this->description}s to return",
191                                 'continue' => 'When more results are available, use this to continue',
192                 );
193         }
194
195         public function getDescription() {
196                 return "Returns all {$this->description}s from the given page(s)";
197         }
198
199         protected function getExamples() {
200                 return array (
201                                 "Get {$this->description}s from the [[Main Page]]:",
202                                 "  api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
203                                 "Get information about the {$this->description} pages in the [[Main Page]]:",
204                                 "  api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
205                                 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
206                                 "  api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
207                         );
208         }
209
210         public function getVersion() {
211                 return __CLASS__ . ': $Id: ApiQueryLinks.php 69986 2010-07-27 03:57:39Z tstarling $';
212         }
213 }