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
30 class ApiQueryExtLinksUsage
extends ApiQueryGeneratorBase
{
32 public function __construct( $query, $moduleName ) {
33 parent
::__construct( $query, $moduleName, 'eu' );
36 public function execute() {
40 public function getCacheMode( $params ) {
44 public function executeGenerator( $resultPageSet ) {
45 $this->run( $resultPageSet );
49 * @param $resultPageSet ApiPageSet
52 private function run( $resultPageSet = null ) {
53 $params = $this->extractRequestParams();
55 $query = $params['query'];
56 $protocol = self
::getProtocolPrefix( $params['protocol'] );
58 $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX'
59 $this->addOption( 'USE INDEX', 'el_index' );
60 $this->addWhere( 'page_id=el_from' );
65 $miser_ns = $params['namespace'];
67 $this->addWhereFld( 'page_namespace', $params['namespace'] );
70 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
72 if ( $whereQuery !== null ) {
73 $this->addWhere( $whereQuery );
76 $prop = array_flip( $params['prop'] );
77 $fld_ids = isset( $prop['ids'] );
78 $fld_title = isset( $prop['title'] );
79 $fld_url = isset( $prop['url'] );
81 if ( is_null( $resultPageSet ) ) {
82 $this->addFields( array(
87 $this->addFieldsIf( 'el_to', $fld_url );
89 $this->addFields( $resultPageSet->getPageTableFields() );
92 $limit = $params['limit'];
93 $offset = $params['offset'];
94 $this->addOption( 'LIMIT', $limit +
1 );
95 if ( isset( $offset ) ) {
96 $this->addOption( 'OFFSET', $offset );
99 $res = $this->select( __METHOD__
);
101 $result = $this->getResult();
103 foreach ( $res as $row ) {
104 if ( ++
$count > $limit ) {
105 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
106 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
110 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
114 if ( is_null( $resultPageSet ) ) {
117 $vals['pageid'] = intval( $row->page_id
);
120 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
121 ApiQueryBase
::addTitleInfo( $vals, $title );
124 // We *could* run this through wfExpandUrl() but I think it's better to output the link verbatim, even if it's protocol-relative --Roan
125 $vals['url'] = $row->el_to
;
127 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
129 $this->setContinueEnumParameter( 'offset', $offset +
$count - 1 );
133 $resultPageSet->processDbRow( $row );
137 if ( is_null( $resultPageSet ) ) {
138 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ),
139 $this->getModulePrefix() );
143 public function getAllowedParams() {
146 ApiBase
::PARAM_ISMULTI
=> true,
147 ApiBase
::PARAM_DFLT
=> 'ids|title|url',
148 ApiBase
::PARAM_TYPE
=> array(
155 ApiBase
::PARAM_TYPE
=> 'integer'
158 ApiBase
::PARAM_TYPE
=> self
::prepareProtocols(),
159 ApiBase
::PARAM_DFLT
=> '',
162 'namespace' => array(
163 ApiBase
::PARAM_ISMULTI
=> true,
164 ApiBase
::PARAM_TYPE
=> 'namespace'
167 ApiBase
::PARAM_DFLT
=> 10,
168 ApiBase
::PARAM_TYPE
=> 'limit',
169 ApiBase
::PARAM_MIN
=> 1,
170 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
171 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
176 public static function prepareProtocols() {
177 global $wgUrlProtocols;
178 $protocols = array( '' );
179 foreach ( $wgUrlProtocols as $p ) {
181 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
187 public static function getProtocolPrefix( $protocol ) {
188 // Find the right prefix
189 global $wgUrlProtocols;
190 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
191 foreach ( $wgUrlProtocols as $p ) {
192 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
204 public function getParamDescription() {
206 $p = $this->getModulePrefix();
209 'What pieces of information to include',
210 ' ids - Adds the ID of page',
211 ' title - Adds the title and namespace ID of the page',
212 ' url - Adds the URL used in the page',
214 'offset' => 'Used for paging. Use the value returned for "continue"',
216 "Protocol of the url. If empty and {$p}query set, the protocol is http.",
217 "Leave both this and {$p}query empty to list all external links"
219 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links',
220 'namespace' => 'The page namespace(s) to enumerate.',
221 'limit' => 'How many pages to return.'
224 if ( $wgMiserMode ) {
225 $desc['namespace'] = array(
227 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
228 'returned before continuing; in extreme cases, zero results may be returned',
235 public function getDescription() {
236 return 'Enumerate pages that contain a given URL';
239 public function getPossibleErrors() {
240 return array_merge( parent
::getPossibleErrors(), array(
241 array( 'code' => 'bad_query', 'info' => 'Invalid query' ),
245 public function getExamples() {
247 'api.php?action=query&list=exturlusage&euquery=www.mediawiki.org'
251 public function getHelpUrls() {
252 return 'https://www.mediawiki.org/wiki/API:Exturlusage';
255 public function getVersion() {
256 return __CLASS__
. ': $Id$';