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