Correct the address of the FSF in some of the GPL headers
[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 * 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 * 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 if ( !is_null( $params['continue'] ) ) {
59 $cont = explode( '|', $params['continue'] );
60 if ( count( $cont ) != 3 ) {
61 $this->dieUsage( 'Invalid continue param. You should pass the ' .
62 'original value returned by the previous query', '_badcontinue' );
63 }
64 $iwlfrom = intval( $cont[0] );
65 $iwlprefix = $this->getDB()->strencode( $cont[1] );
66 $iwltitle = $this->getDB()->strencode( $this->titleToKey( $cont[2] ) );
67 $this->addWhere(
68 "iwl_from > $iwlfrom OR " .
69 "(iwl_from = $iwlfrom AND " .
70 "(iwl_prefix > '$iwlprefix' OR " .
71 "(iwl_prefix = '$iwlprefix' AND " .
72 "iwl_title >= '$iwltitle')))"
73 );
74 }
75
76 // Don't order by iwl_from if it's constant in the WHERE clause
77 if ( count( $this->getPageSet()->getGoodTitles() ) == 1 ) {
78 $this->addOption( 'ORDER BY', 'iwl_prefix' );
79 } else {
80 $this->addOption( 'ORDER BY', 'iwl_from, iwl_prefix' );
81 }
82 $this->addOption( 'LIMIT', $params['limit'] + 1 );
83 $res = $this->select( __METHOD__ );
84
85 $count = 0;
86 $db = $this->getDB();
87 foreach ( $res as $row ) {
88 if ( ++$count > $params['limit'] ) {
89 // We've reached the one extra which shows that
90 // there are additional pages to be had. Stop here...
91 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
92 break;
93 }
94 $entry = array( 'prefix' => $row->iwl_prefix );
95
96 if ( !is_null( $params['url'] ) ) {
97 $title = Title::newFromText( "{$row->iwl_prefix}:{$row->iwl_title}" );
98 if ( $title ) {
99 $entry = array_merge( $entry, array( 'url' => $title->getFullURL() ) );
100 }
101 }
102
103 ApiResult::setContent( $entry, $row->iwl_title );
104 $fit = $this->addPageSubItem( $row->iwl_from, $entry );
105 if ( !$fit ) {
106 $this->setContinueEnumParameter( 'continue', "{$row->iwl_from}|{$row->iwl_prefix}|{$row->iwl_title}" );
107 break;
108 }
109 }
110 }
111
112 public function getAllowedParams() {
113 return array(
114 'url' => null,
115 'limit' => array(
116 ApiBase::PARAM_DFLT => 10,
117 ApiBase::PARAM_TYPE => 'limit',
118 ApiBase::PARAM_MIN => 1,
119 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
120 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
121 ),
122 'continue' => null,
123 );
124 }
125
126 public function getParamDescription() {
127 return array(
128 'url' => 'Whether to get the full URL',
129 'limit' => 'How many interwiki links to return',
130 'continue' => 'When more results are available, use this to continue',
131 );
132 }
133
134 public function getDescription() {
135 return 'Returns all interwiki links from the given page(s)';
136 }
137
138 public function getPossibleErrors() {
139 return array_merge( parent::getPossibleErrors(), array(
140 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
141 ) );
142 }
143
144 protected function getExamples() {
145 return array(
146 'Get interwiki links from the [[Main Page]]:',
147 ' api.php?action=query&prop=iwlinks&titles=Main%20Page',
148 );
149 }
150
151 public function getVersion() {
152 return __CLASS__ . ': $Id$';
153 }
154 }