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