Nuke $db->freeResult() from Api stuffs
[lhc/web/wiklou.git] / includes / api / ApiQueryIWBacklinks.php
1 <?php
2
3 /*
4 * Created on May 14, 2010
5 *
6 * API for MediaWiki 1.17+
7 *
8 * Copyright (C) 2010 Sam Reed
9 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
30 }
31
32 /**
33 * This gives links pointing to the given interwiki
34 * @ingroup API
35 */
36 class ApiQueryIWBacklinks extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'iwbl' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44
45 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
46 $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
47 }
48
49 if ( !is_null( $params['continue'] ) ) {
50 $cont = explode( '|', $params['continue'] );
51 if ( count( $cont ) != 3 ) {
52 $this->dieUsage( 'Invalid continue param. You should pass the ' .
53 'original value returned by the previous query', '_badcontinue' );
54 }
55
56 $from = intval( $cont[0] );
57 $prefix = $this->getDB()->strencode( $cont[1] );
58 $title = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
59 $this->addWhere(
60 "iwl_from > $from OR " .
61 "(iwl_from = $from AND " .
62 "(iwl_prefix > '$prefix' OR " .
63 "(iwl_prefix = '$prefix' AND " .
64 "iwl_title >= '$title')))"
65 );
66 }
67
68 $this->addTables( array( 'iwlinks', 'page' ) );
69 $this->addWhere( 'iwl_from = page_id' );
70
71 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
72 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
73
74 if ( isset( $params['prefix'] ) ) {
75 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
76 }
77
78 if ( isset( $params['title'] ) ) {
79 $this->addWhereFld( 'iwl_title', $params['title'] );
80 }
81
82 $this->addOption( 'LIMIT', $params['limit'] + 1 );
83 $this->addOption( 'ORDER BY', 'iwl_from' );
84
85 $db = $this->getDB();
86 $res = $this->select( __METHOD__ );
87
88 $count = 0;
89 $result = $this->getResult();
90 while ( $row = $db->fetchObject( $res ) ) {
91 if ( ++ $count > $params['limit'] ) {
92 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
93 // Continue string preserved in case the redirect query doesn't pass the limit
94 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
95 break;
96 }
97
98 $entry = array();
99
100 $entry['pageid'] = intval( $row->page_id );
101 $entry['ns'] = $row->page_namespace;
102 $entry['title'] = $row->page_title;
103
104 if ( $row->page_is_redirect ) {
105 $entry['redirect'] = '';
106 }
107
108 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
109 if ( !$fit ) {
110 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
111 break;
112 }
113 }
114
115 $this->getResult()->setIndexedTagName_internal(
116 array( 'query', $this->getModuleName() ),
117 'iw'
118 );
119 }
120
121 public function getAllowedParams() {
122 return array(
123 'prefix' => null,
124 'title' => null,
125 'continue' => null,
126 'limit' => array(
127 ApiBase::PARAM_DFLT => 10,
128 ApiBase::PARAM_TYPE => 'limit',
129 ApiBase::PARAM_MIN => 1,
130 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
131 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
132 )
133 );
134 }
135
136 public function getParamDescription() {
137 return array(
138 'prefix' => 'Prefix for the interwiki',
139 'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
140 'continue' => 'When more results are available, use this to continue',
141 'limit' => 'How many total pages to return',
142 );
143 }
144
145 public function getDescription() {
146 return array('Find all pages that link to the given interwiki link.',
147 'Can be used to find all links with a prefix, or',
148 'all links to a title (any prefix).',
149 'Using neither parameter is effectively "All IW Links"',
150 );
151 }
152
153 public function getPossibleErrors() {
154 return array_merge( parent::getPossibleErrors(), array(
155 array( 'missingparam', 'prefix' ),
156 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
157 ) );
158 }
159
160 protected function getExamples() {
161 return array(
162 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
163 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
164 );
165 }
166
167 public function getVersion() {
168 return __CLASS__ . ': $Id$';
169 }
170 }