da664f3341ad4d6af417730ba99c61deaf6ec2c1
[lhc/web/wiklou.git] / includes / api / ApiQueryIWLinks.php
1 <?php
2
3 /**
4 * Created on May 14, 2010
5 *
6 * API for MediaWiki 1.17+
7 *
8 * Copyright © 2010 Sam Reed
9 * Copyright © 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 * A query module to list all interwiki links on a page
34 *
35 * @ingroup API
36 */
37 class ApiQueryIWLinks extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'iw' );
41 }
42
43 public function execute() {
44 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
45 return;
46 }
47
48 $params = $this->extractRequestParams();
49 $this->addFields( array(
50 'iwl_from',
51 'iwl_prefix',
52 'iwl_title'
53 ) );
54
55 $this->addTables( 'iwlinks' );
56 $this->addWhereFld( 'iwl_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
57
58 $url = !is_null( $params['url'] );
59 if ( $url ) {
60 $this->addTables( 'interwiki' );
61 $this->addJoinConds( array( 'interwiki' => array( 'INNER JOIN', 'iw_prefix=iwl_prefix' ) ) );
62 $this->addFields( 'iw_url' );
63 }
64
65 if ( !is_null( $params['continue'] ) ) {
66 $cont = explode( '|', $params['continue'] );
67 if ( count( $cont ) != 3 ) {
68 $this->dieUsage( 'Invalid continue param. You should pass the ' .
69 'original value returned by the previous query', '_badcontinue' );
70 }
71 $iwlfrom = intval( $cont[0] );
72 $iwlprefix = $this->getDB()->strencode( $cont[1] );
73 $iwltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
74 $this->addWhere(
75 "iwl_from > $iwlfrom OR " .
76 "(iwl_from = $iwlfrom AND " .
77 "(iwl_prefix > '$iwlprefix' OR " .
78 "(iwl_prefix = '$iwlprefix' AND " .
79 "iwl_title >= '$iwltitle')))"
80 );
81 }
82
83 // Don't order by iwl_from if it's constant in the WHERE clause
84 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
85 $this->addOption( 'ORDER BY', 'iwl_prefix' );
86 } else {
87 $this->addOption( 'ORDER BY', 'iwl_from, iwl_prefix' );
88 }
89 $this->addOption( 'LIMIT', $params['limit'] + 1 );
90 $res = $this->select( __METHOD__ );
91
92 $count = 0;
93 $db = $this->getDB();
94 while ( $row = $db->fetchObject( $res ) ) {
95 if ( ++$count > $params['limit'] ) {
96 // We've reached the one extra which shows that
97 // there are additional pages to be had. Stop here...
98 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
99 break;
100 }
101 $entry = array( 'prefix' => $row->iwl_prefix );
102
103 if ( $url ) {
104 $rowUrl = str_replace( '$1', $row->iwl_title, $row->iw_url );
105 $entry = array_merge( $entry, array( 'url' => $rowUrl ) );
106 }
107
108 ApiResult::setContent( $entry, $row->iwl_title );
109 $fit = $this->addPageSubItem( $row->iwl_from, $entry );
110 if ( !$fit ) {
111 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
112 break;
113 }
114 }
115 $db->freeResult( $res );
116 }
117
118 public function getAllowedParams() {
119 return array(
120 'url' => null,
121 'limit' => array(
122 ApiBase::PARAM_DFLT => 10,
123 ApiBase::PARAM_TYPE => 'limit',
124 ApiBase::PARAM_MIN => 1,
125 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
126 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
127 ),
128 'continue' => null,
129 );
130 }
131
132 public function getParamDescription() {
133 return array(
134 'url' => 'Whether to get the full URL',
135 'limit' => 'How many interwiki links to return',
136 'continue' => 'When more results are available, use this to continue',
137 );
138 }
139
140 public function getDescription() {
141 return 'Returns all interwiki links from the given page(s)';
142 }
143
144 public function getPossibleErrors() {
145 return array_merge( parent::getPossibleErrors(), array(
146 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
147 ) );
148 }
149
150 protected function getExamples() {
151 return array(
152 'Get interwiki links from the [[Main Page]]:',
153 ' api.php?action=query&prop=iwlinks&titles=Main%20Page',
154 );
155 }
156
157 public function getVersion() {
158 return __CLASS__ . ': $Id$';
159 }
160 }