3 * API for MediaWiki 1.8+
5 * Created on July 7, 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' );
35 class ApiQueryExtLinksUsage
extends ApiQueryGeneratorBase
{
37 public function __construct( $query, $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'eu' );
41 public function execute() {
45 public function getCacheMode( $params ) {
49 public function executeGenerator( $resultPageSet ) {
50 $this->run( $resultPageSet );
53 private function run( $resultPageSet = null ) {
54 $params = $this->extractRequestParams();
56 $protocol = $params['protocol'];
57 $query = $params['query'];
59 // Find the right prefix
60 global $wgUrlProtocols;
61 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
62 foreach ( $wgUrlProtocols as $p ) {
63 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
73 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
74 $this->addOption( 'USE INDEX', 'el_index' );
75 $this->addWhere( 'page_id=el_from' );
76 $this->addWhereFld( 'page_namespace', $params['namespace'] );
78 if ( !is_null( $query ) ||
$query != '' ) {
79 if ( is_null( $protocol ) ) {
80 $protocol = 'http://';
83 $likeQuery = LinkFilter
::makeLikeArray( $query, $protocol );
85 $this->dieUsage( 'Invalid query', 'bad_query' );
88 $likeQuery = LinkFilter
::keepOneWildcard( $likeQuery );
89 $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) );
90 } elseif ( !is_null( $protocol ) ) {
91 $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) );
94 $prop = array_flip( $params['prop'] );
95 $fld_ids = isset( $prop['ids'] );
96 $fld_title = isset( $prop['title'] );
97 $fld_url = isset( $prop['url'] );
99 if ( is_null( $resultPageSet ) ) {
100 $this->addFields( array(
105 $this->addFieldsIf( 'el_to', $fld_url );
107 $this->addFields( $resultPageSet->getPageTableFields() );
110 $limit = $params['limit'];
111 $offset = $params['offset'];
112 $this->addOption( 'LIMIT', $limit +
1 );
113 if ( isset( $offset ) ) {
114 $this->addOption( 'OFFSET', $offset );
117 $res = $this->select( __METHOD__
);
119 $result = $this->getResult();
121 foreach ( $res as $row ) {
122 if ( ++
$count > $limit ) {
123 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
124 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
128 if ( is_null( $resultPageSet ) ) {
131 $vals['pageid'] = intval( $row->page_id
);
134 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
135 ApiQueryBase
::addTitleInfo( $vals, $title );
138 $vals['url'] = $row->el_to
;
140 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
142 $this->setContinueEnumParameter( 'offset', $offset +
$count - 1 );
146 $resultPageSet->processDbRow( $row );
150 if ( is_null( $resultPageSet ) ) {
151 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
152 $this->getModulePrefix() );
156 public function getAllowedParams() {
157 global $wgUrlProtocols;
158 $protocols = array( '' );
159 foreach ( $wgUrlProtocols as $p ) {
160 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
165 ApiBase
::PARAM_ISMULTI
=> true,
166 ApiBase
::PARAM_DFLT
=> 'ids|title|url',
167 ApiBase
::PARAM_TYPE
=> array(
174 ApiBase
::PARAM_TYPE
=> 'integer'
177 ApiBase
::PARAM_TYPE
=> $protocols,
178 ApiBase
::PARAM_DFLT
=> '',
181 'namespace' => array(
182 ApiBase
::PARAM_ISMULTI
=> true,
183 ApiBase
::PARAM_TYPE
=> 'namespace'
186 ApiBase
::PARAM_DFLT
=> 10,
187 ApiBase
::PARAM_TYPE
=> 'limit',
188 ApiBase
::PARAM_MIN
=> 1,
189 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
190 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
195 public function getParamDescription() {
196 $p = $this->getModulePrefix();
199 'What pieces of information to include',
200 ' ids - Adds the id of page',
201 ' title - Adds the title and namespace id of the page',
202 ' url - Adds the URL used in the page',
204 'offset' => 'Used for paging. Use the value returned for "continue"',
206 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
207 "Leave both this and {$p}query empty to list all external links"
209 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
210 'namespace' => 'The page namespace(s) to enumerate.',
211 'limit' => 'How many pages to return.'
215 public function getDescription() {
216 return 'Enumerate pages that contain a given URL';
219 public function getPossibleErrors() {
220 return array_merge( parent
::getPossibleErrors(), array(
221 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
225 protected function getExamples() {
227 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
231 public function getVersion() {
232 return __CLASS__
. ': $Id$';