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() {
159 ApiBase
::PARAM_ISMULTI
=> true,
160 ApiBase
::PARAM_DFLT
=> 'ids|title|url',
161 ApiBase
::PARAM_TYPE
=> array(
168 ApiBase
::PARAM_TYPE
=> 'integer'
171 ApiBase
::PARAM_TYPE
=> self
::prepareProtocols(),
172 ApiBase
::PARAM_DFLT
=> '',
175 'namespace' => array(
176 ApiBase
::PARAM_ISMULTI
=> true,
177 ApiBase
::PARAM_TYPE
=> 'namespace'
180 ApiBase
::PARAM_DFLT
=> 10,
181 ApiBase
::PARAM_TYPE
=> 'limit',
182 ApiBase
::PARAM_MIN
=> 1,
183 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
184 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
189 public static function prepareProtocols() {
190 global $wgUrlProtocols;
191 $protocols = array( '' );
192 foreach ( $wgUrlProtocols as $p ) {
193 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
198 public function getParamDescription() {
199 $p = $this->getModulePrefix();
202 'What pieces of information to include',
203 ' ids - Adds the ID of page',
204 ' title - Adds the title and namespace ID of the page',
205 ' url - Adds the URL used in the page',
207 'offset' => 'Used for paging. Use the value returned for "continue"',
209 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
210 "Leave both this and {$p}query empty to list all external links"
212 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
213 'namespace' => 'The page namespace(s) to enumerate.',
214 'limit' => 'How many pages to return.'
218 public function getDescription() {
219 return 'Enumerate pages that contain a given URL';
222 public function getPossibleErrors() {
223 return array_merge( parent
::getPossibleErrors(), array(
224 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
228 protected function getExamples() {
230 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
234 public function getVersion() {
235 return __CLASS__
. ': $Id$';