Mixture of things.
[mediawiki.git] / includes / api / ApiQueryLinks.php
blob0dafc347b76107704c577f9f55b421f0fdb14ca3
1 <?php
3 /**
4 * Created on May 12, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiQueryBase.php" );
31 /**
32 * A query module to list all wiki links on a given set of pages.
34 * @ingroup API
36 class ApiQueryLinks extends ApiQueryGeneratorBase {
38 const LINKS = 'links';
39 const TEMPLATES = 'templates';
41 private $table, $prefix, $description;
43 public function __construct( $query, $moduleName ) {
44 switch ( $moduleName ) {
45 case self::LINKS:
46 $this->table = 'pagelinks';
47 $this->prefix = 'pl';
48 $this->description = 'link';
49 break;
50 case self::TEMPLATES:
51 $this->table = 'templatelinks';
52 $this->prefix = 'tl';
53 $this->description = 'template';
54 break;
55 default:
56 ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
59 parent::__construct( $query, $moduleName, $this->prefix );
62 public function execute() {
63 $this->run();
66 public function executeGenerator( $resultPageSet ) {
67 $this->run( $resultPageSet );
70 private function run( $resultPageSet = null ) {
71 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
72 return; // nothing to do
75 $params = $this->extractRequestParams();
77 $this->addFields( array(
78 $this->prefix . '_from AS pl_from',
79 $this->prefix . '_namespace AS pl_namespace',
80 $this->prefix . '_title AS pl_title'
81 ) );
83 $this->addTables( $this->table );
84 $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
85 $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
87 if ( !is_null( $params['continue'] ) ) {
88 $cont = explode( '|', $params['continue'] );
89 if ( count( $cont ) != 3 ) {
90 $this->dieUsage( 'Invalid continue param. You should pass the ' .
91 'original value returned by the previous query', '_badcontinue' );
93 $plfrom = intval( $cont[0] );
94 $plns = intval( $cont[1] );
95 $pltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
96 $this->addWhere(
97 "{$this->prefix}_from > $plfrom OR " .
98 "({$this->prefix}_from = $plfrom AND " .
99 "({$this->prefix}_namespace > $plns OR " .
100 "({$this->prefix}_namespace = $plns AND " .
101 "{$this->prefix}_title >= '$pltitle')))"
105 // Here's some MySQL craziness going on: if you use WHERE foo='bar'
106 // and later ORDER BY foo MySQL doesn't notice the ORDER BY is pointless
107 // but instead goes and filesorts, because the index for foo was used
108 // already. To work around this, we drop constant fields in the WHERE
109 // clause from the ORDER BY clause
110 $order = array();
111 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
112 $order[] = "{$this->prefix}_from";
114 if ( count( $params['namespace'] ) != 1 ) {
115 $order[] = "{$this->prefix}_namespace";
118 $order[] = "{$this->prefix}_title";
119 $this->addOption( 'ORDER BY', implode( ', ', $order ) );
120 $this->addOption( 'USE INDEX', "{$this->prefix}_from" );
121 $this->addOption( 'LIMIT', $params['limit'] + 1 );
123 $db = $this->getDB();
124 $res = $this->select( __METHOD__ );
126 if ( is_null( $resultPageSet ) ) {
127 $count = 0;
128 while ( $row = $db->fetchObject( $res ) ) {
129 if ( ++$count > $params['limit'] ) {
130 // We've reached the one extra which shows that
131 // there are additional pages to be had. Stop here...
132 $this->setContinueEnumParameter( 'continue',
133 "{$row->pl_from}|{$row->pl_namespace}|" .
134 $this->keyToTitle( $row->pl_title ) );
135 break;
137 $vals = array();
138 ApiQueryBase::addTitleInfo( $vals, Title::makeTitle( $row->pl_namespace, $row->pl_title ) );
139 $fit = $this->addPageSubItem( $row->pl_from, $vals );
140 if ( !$fit ) {
141 $this->setContinueEnumParameter( 'continue',
142 "{$row->pl_from}|{$row->pl_namespace}|" .
143 $this->keyToTitle( $row->pl_title ) );
144 break;
147 } else {
148 $titles = array();
149 $count = 0;
150 while ( $row = $db->fetchObject( $res ) ) {
151 if ( ++$count > $params['limit'] ) {
152 // We've reached the one extra which shows that
153 // there are additional pages to be had. Stop here...
154 $this->setContinueEnumParameter( 'continue',
155 "{$row->pl_from}|{$row->pl_namespace}|" .
156 $this->keyToTitle( $row->pl_title ) );
157 break;
159 $titles[] = Title::makeTitle( $row->pl_namespace, $row->pl_title );
161 $resultPageSet->populateFromTitles( $titles );
164 $db->freeResult( $res );
167 public function getAllowedParams() {
168 return array(
169 'namespace' => array(
170 ApiBase::PARAM_TYPE => 'namespace',
171 ApiBase::PARAM_ISMULTI => true
173 'limit' => array(
174 ApiBase::PARAM_DFLT => 10,
175 ApiBase::PARAM_TYPE => 'limit',
176 ApiBase::PARAM_MIN => 1,
177 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
178 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
180 'continue' => null,
184 public function getParamDescription() {
185 $desc = $this->description;
186 return array(
187 'namespace' => "Show {$desc}s in this namespace(s) only",
188 'limit' => "How many {$desc}s to return",
189 'continue' => 'When more results are available, use this to continue',
193 public function getDescription() {
194 return "Returns all {$this->description}s from the given page(s)";
197 protected function getExamples() {
198 return array(
199 "Get {$this->description}s from the [[Main Page]]:",
200 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page",
201 "Get information about the {$this->description} pages in the [[Main Page]]:",
202 " api.php?action=query&generator={$this->getModuleName()}&titles=Main%20Page&prop=info",
203 "Get {$this->description}s from the Main Page in the User and Template namespaces:",
204 " api.php?action=query&prop={$this->getModuleName()}&titles=Main%20Page&{$this->prefix}namespace=2|10"
208 public function getVersion() {
209 return __CLASS__ . ': $Id$';