(bug 27862; follow-up r77714) Make emailuser api module not freak out when SpecialEma...
[lhc/web/wiklou.git] / includes / api / ApiQueryExternalLinks.php
1 <?php
2 /**
3 *
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 $db = $this->getDB();
50
51 $query = $params['query'];
52 $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
53
54 $this->addFields( array(
55 'el_from',
56 'el_to'
57 ) );
58
59 $this->addTables( 'externallinks' );
60 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
61
62 $whereQuery = $this->prepareUrlQuerySearchString( $db, $query, $protocol );
63
64 if ( $whereQuery !== null ) {
65 $this->addWhere( $whereQuery );
66 }
67
68 // Don't order by el_from if it's constant in the WHERE clause
69 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
70 $this->addOption( 'ORDER BY', 'el_from' );
71 }
72
73 $this->addOption( 'LIMIT', $params['limit'] + 1 );
74 if ( !is_null( $params['offset'] ) ) {
75 $this->addOption( 'OFFSET', $params['offset'] );
76 }
77
78 $res = $this->select( __METHOD__ );
79
80 $count = 0;
81 foreach ( $res as $row ) {
82 if ( ++$count > $params['limit'] ) {
83 // We've reached the one extra which shows that
84 // there are additional pages to be had. Stop here...
85 $this->setContinueEnumParameter( 'offset', @$params['offset'] + $params['limit'] );
86 break;
87 }
88 $entry = array();
89 ApiResult::setContent( $entry, $row->el_to );
90 $fit = $this->addPageSubItem( $row->el_from, $entry );
91 if ( !$fit ) {
92 $this->setContinueEnumParameter( 'offset', @$params['offset'] + $count - 1 );
93 break;
94 }
95 }
96 }
97
98 public function getCacheMode( $params ) {
99 return 'public';
100 }
101
102 public function getAllowedParams() {
103 return array(
104 'limit' => array(
105 ApiBase::PARAM_DFLT => 10,
106 ApiBase::PARAM_TYPE => 'limit',
107 ApiBase::PARAM_MIN => 1,
108 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
109 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
110 ),
111 'offset' => null,
112 'protocol' => array(
113 ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
114 ApiBase::PARAM_DFLT => '',
115 ),
116 'query' => null,
117 );
118 }
119
120 public function getParamDescription() {
121 $p = $this->getModulePrefix();
122 return array(
123 'limit' => 'How many links to return',
124 'offset' => 'When more results are available, use this to continue',
125 'protocol' => array(
126 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
127 "Leave both this and {$p}query empty to list all external links"
128 ),
129 'query' => 'Search string without protocol. Useful for checking whether a certain page contains a certain external url',
130 );
131 }
132
133 public function getDescription() {
134 return 'Returns all external urls (not interwikies) from the given page(s)';
135 }
136
137 public function getPossibleErrors() {
138 return array_merge( parent::getPossibleErrors(), array(
139 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
140 ) );
141 }
142
143 protected function getExamples() {
144 return array(
145 'Get a list of external links on the [[Main Page]]:',
146 ' api.php?action=query&prop=extlinks&titles=Main%20Page',
147 );
148 }
149
150 public function getVersion() {
151 return __CLASS__ . ': $Id$';
152 }
153 }