Move ApiFormatYaml_spyc.php to spyc.php as per r71763
[lhc/web/wiklou.git] / includes / api / ApiQueryExternalLinks.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on May 13, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
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 external URLs found on a given set of pages.
34 *
35 * @ingroup API
36 */
37 class ApiQueryExternalLinks extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'el' );
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 'el_from',
51 'el_to'
52 ) );
53
54 $this->addTables( 'externallinks' );
55 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
56
57 // Don't order by el_from if it's constant in the WHERE clause
58 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
59 $this->addOption( 'ORDER BY', 'el_from' );
60 }
61
62 $this->addOption( 'LIMIT', $params['limit'] + 1 );
63 if ( !is_null( $params['offset'] ) ) {
64 $this->addOption( 'OFFSET', $params['offset'] );
65 }
66
67 $res = $this->select( __METHOD__ );
68
69 $count = 0;
70 foreach ( $res as $row ) {
71 if ( ++$count > $params['limit'] ) {
72 // We've reached the one extra which shows that
73 // there are additional pages to be had. Stop here...
74 $this->setContinueEnumParameter( 'offset', @$params['offset'] + $params['limit'] );
75 break;
76 }
77 $entry = array();
78 ApiResult::setContent( $entry, $row->el_to );
79 $fit = $this->addPageSubItem( $row->el_from, $entry );
80 if ( !$fit ) {
81 $this->setContinueEnumParameter( 'offset', @$params['offset'] + $count - 1 );
82 break;
83 }
84 }
85 }
86
87 public function getCacheMode( $params ) {
88 return 'public';
89 }
90
91 public function getAllowedParams() {
92 return array(
93 'limit' => array(
94 ApiBase::PARAM_DFLT => 10,
95 ApiBase::PARAM_TYPE => 'limit',
96 ApiBase::PARAM_MIN => 1,
97 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
98 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
99 ),
100 'offset' => null,
101 );
102 }
103
104 public function getParamDescription() {
105 return array(
106 'limit' => 'How many links to return',
107 'offset' => 'When more results are available, use this to continue',
108 );
109 }
110
111 public function getDescription() {
112 return 'Returns all external urls (not interwikies) from the given page(s)';
113 }
114
115 protected function getExamples() {
116 return array(
117 'Get a list of external links on the [[Main Page]]:',
118 ' api.php?action=query&prop=extlinks&titles=Main%20Page',
119 );
120 }
121
122 public function getVersion() {
123 return __CLASS__ . ': $Id$';
124 }
125 }