5 * Created on May 13, 2007
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
33 * A query module to list all external URLs found on a given set of pages.
37 class ApiQueryExternalLinks
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'el' );
43 public function execute() {
44 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
48 $params = $this->extractRequestParams();
51 $query = $params['query'];
52 $protocol = ApiQueryExtLinksUsage
::getProtocolPrefix( $params['protocol'] );
54 $this->addFields( array(
59 $this->addTables( 'externallinks' );
60 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
62 //TODO: Refactor out,duplicated from ApiQueryExtLinksUsage
63 if ( !is_null( $query ) ||
$query != '' ) {
64 if ( is_null( $protocol ) ) {
65 $protocol = 'http://';
68 $likeQuery = LinkFilter
::makeLikeArray( $query, $protocol );
70 $this->dieUsage( 'Invalid query', 'bad_query' );
73 $likeQuery = LinkFilter
::keepOneWildcard( $likeQuery );
74 $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) );
75 } elseif ( !is_null( $protocol ) ) {
76 $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) );
79 // Don't order by el_from if it's constant in the WHERE clause
80 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
81 $this->addOption( 'ORDER BY', 'el_from' );
84 $this->addOption( 'LIMIT', $params['limit'] +
1 );
85 if ( !is_null( $params['offset'] ) ) {
86 $this->addOption( 'OFFSET', $params['offset'] );
89 $res = $this->select( __METHOD__
);
92 foreach ( $res as $row ) {
93 if ( ++
$count > $params['limit'] ) {
94 // We've reached the one extra which shows that
95 // there are additional pages to be had. Stop here...
96 $this->setContinueEnumParameter( 'offset', @$params['offset'] +
$params['limit'] );
100 ApiResult
::setContent( $entry, $row->el_to
);
101 $fit = $this->addPageSubItem( $row->el_from
, $entry );
103 $this->setContinueEnumParameter( 'offset', @$params['offset'] +
$count - 1 );
109 public function getCacheMode( $params ) {
113 public function getAllowedParams() {
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
124 ApiBase
::PARAM_TYPE
=> ApiQueryExtLinksUsage
::prepareProtocols(),
125 ApiBase
::PARAM_DFLT
=> '',
131 public function getParamDescription() {
132 $p = $this->getModulePrefix();
134 'limit' => 'How many links to return',
135 'offset' => 'When more results are available, use this to continue',
137 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
138 "Leave both this and {$p}query empty to list all external links"
140 'query' => 'Search string without protocol. Useful for checking whether a certain page contains a certain external url',
144 public function getDescription() {
145 return 'Returns all external urls (not interwikies) from the given page(s)';
148 public function getPossibleErrors() {
149 return array_merge( parent
::getPossibleErrors(), array(
150 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
154 protected function getExamples() {
156 'Get a list of external links on the [[Main Page]]:',
157 ' api.php?action=query&prop=extlinks&titles=Main%20Page',
161 public function getVersion() {
162 return __CLASS__
. ': $Id$';