* (bug 24485) Make iwbacklinks a generator, display iwprefix and iwtitle optional
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'iwbl' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 public function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
52
53 if ( isset( $params['title'] ) && !isset( $params['prefix'] ) ) {
54 $this->dieUsageMsg( array( 'missingparam', 'prefix' ) );
55 }
56
57 if ( !is_null( $params['continue'] ) ) {
58 $cont = explode( '|', $params['continue'] );
59 if ( count( $cont ) != 3 ) {
60 $this->dieUsage( 'Invalid continue param. You should pass the ' .
61 'original value returned by the previous query', '_badcontinue' );
62 }
63
64 $prefix = $this->getDB()->strencode( $cont[0] );
65 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
66 $from = intval( $cont[2] );
67 $this->addWhere(
68 "iwl_prefix > '$prefix' OR " .
69 "(iwl_prefix = '$prefix' AND " .
70 "(iwl_title > '$title' OR " .
71 "(iwl_title = '$title' AND " .
72 "iwl_from >= $from)))"
73 );
74 }
75
76 $prop = array_flip( $params['prop'] );
77 $iwprefix = isset( $prop['iwprefix'] );
78 $iwtitle = isset( $prop['iwtitle'] );
79
80 $this->addTables( array( 'iwlinks', 'page' ) );
81 $this->addWhere( 'iwl_from = page_id' );
82
83 $this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect',
84 'iwl_from', 'iwl_prefix', 'iwl_title' ) );
85
86 if ( isset( $params['prefix'] ) ) {
87 $this->addWhereFld( 'iwl_prefix', $params['prefix'] );
88 if ( isset( $params['title'] ) ) {
89 $this->addWhereFld( 'iwl_title', $params['title'] );
90 $this->addOption( 'ORDER BY', 'iwl_from' );
91 } else {
92 $this->addOption( 'ORDER BY', 'iwl_title, iwl_from' );
93 }
94 } else {
95 $this->addOption( 'ORDER BY', 'iwl_prefix, iwl_title, iwl_from' );
96 }
97
98 $this->addOption( 'LIMIT', $params['limit'] + 1 );
99
100 $db = $this->getDB();
101 $res = $this->select( __METHOD__ );
102
103 $pages = array();
104
105 $count = 0;
106 $result = $this->getResult();
107 foreach ( $res as $row ) {
108 if ( ++ $count > $params['limit'] ) {
109 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
110 // Continue string preserved in case the redirect query doesn't pass the limit
111 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
112 break;
113 }
114
115 if ( !is_null( $resultPageSet ) ) {
116 $pages[] = Title::makeTitle( $row->page_namespace, $row->page_title )->getPrefixedText();
117 } else {
118 $entry = array();
119
120 $entry['pageid'] = intval( $row->page_id );
121 $entry['ns'] = $row->page_namespace;
122 $entry['title'] = $row->page_title;
123
124 if ( $row->page_is_redirect ) {
125 $entry['redirect'] = '';
126 }
127
128 if ( $iwprefix ) {
129 $entry['iwprefix'] = $row->iwl_prefix;
130 }
131
132 if ( $iwtitle ) {
133 $entry['iwtitle'] = $row->iwl_title;
134 }
135
136 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $entry );
137 if ( !$fit ) {
138 $this->setContinueEnumParameter( 'continue', "{$row->iwl_prefix}|{$row->iwl_title}|{$row->iwl_from}" );
139 break;
140 }
141 }
142 }
143
144 if ( is_null( $resultPageSet ) ) {
145 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'iw' );
146 } else {
147 $resultPageSet->populateFromTitles( $pages );
148 }
149 }
150
151 public function getAllowedParams() {
152 return array(
153 'prefix' => null,
154 'title' => null,
155 'continue' => null,
156 'limit' => array(
157 ApiBase::PARAM_DFLT => 10,
158 ApiBase::PARAM_TYPE => 'limit',
159 ApiBase::PARAM_MIN => 1,
160 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
161 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
162 ),
163 'prop' => array(
164 ApiBase::PARAM_ISMULTI => true,
165 ApiBase::PARAM_DFLT => '',
166 ApiBase::PARAM_TYPE => array(
167 'iwprefix',
168 'iwtitle',
169 ),
170 ),
171 );
172 }
173
174 public function getParamDescription() {
175 return array(
176 'prefix' => 'Prefix for the interwiki',
177 'title' => "Interwiki link to search for. Must be used with {$this->getModulePrefix()}prefix",
178 'continue' => 'When more results are available, use this to continue',
179 'prop' => array(
180 'Which properties to get',
181 ' iwprefix - Adds the prefix of the interwiki',
182 ' iwtitle - Adds the title of the interwiki',
183 ),
184 'limit' => 'How many total pages to return',
185 );
186 }
187
188 public function getDescription() {
189 return array( 'Find all pages that link to the given interwiki link.',
190 'Can be used to find all links with a prefix, or',
191 'all links to a title (with a given prefix).',
192 'Using neither parameter is effectively "All IW Links"',
193 );
194 }
195
196 public function getPossibleErrors() {
197 return array_merge( parent::getPossibleErrors(), array(
198 array( 'missingparam', 'prefix' ),
199 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
200 ) );
201 }
202
203 protected function getExamples() {
204 return array(
205 'api.php?action=query&list=iwbacklinks&iwbltitle=Test&iwblprefix=wikibooks',
206 'api.php?action=query&generator=iwbacklinks&giwbltitle=Test&iwblprefix=wikibooks&prop=info'
207 );
208 }
209
210 public function getVersion() {
211 return __CLASS__ . ': $Id$';
212 }
213 }