4 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\ExternalLinks\LinkFilter
;
25 use MediaWiki\MainConfigNames
;
26 use MediaWiki\Title\Title
;
27 use MediaWiki\Utils\UrlUtils
;
28 use Wikimedia\ParamValidator\ParamValidator
;
29 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
34 class ApiQueryExtLinksUsage
extends ApiQueryGeneratorBase
{
36 private UrlUtils
$urlUtils;
39 * @param ApiQuery $query
40 * @param string $moduleName
41 * @param UrlUtils $urlUtils
43 public function __construct( ApiQuery
$query, $moduleName, UrlUtils
$urlUtils ) {
44 parent
::__construct( $query, $moduleName, 'eu' );
46 $this->urlUtils
= $urlUtils;
49 public function execute() {
53 public function getCacheMode( $params ) {
57 public function executeGenerator( $resultPageSet ) {
58 $this->run( $resultPageSet );
62 * @param ApiPageSet|null $resultPageSet
65 private function run( $resultPageSet = null ) {
66 $params = $this->extractRequestParams();
69 $query = $params['query'];
70 $protocol = LinkFilter
::getProtocolPrefix( $params['protocol'] );
72 $this->addTables( [ 'externallinks', 'page' ] );
73 $this->addJoinConds( [ 'page' => [ 'JOIN', 'page_id=el_from' ] ] );
74 $continueField = 'el_to_domain_index';
75 $fields = [ 'el_to_domain_index', 'el_to_path' ];
78 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
79 $miser_ns = $params['namespace'] ?
: [];
81 $this->addWhereFld( 'page_namespace', $params['namespace'] );
83 if ( $query !== null && $query !== '' ) {
84 // Normalize query to match the normalization applied for the externallinks table
85 $query = Parser
::normalizeLinkUrl( $query );
86 $conds = LinkFilter
::getQueryConditions( $query, [
87 'protocol' => $protocol,
88 'oneWildcard' => true,
92 $this->dieWithError( 'apierror-badquery' );
94 $this->addWhere( $conds );
96 if ( $protocol !== null ) {
97 $this->addWhere( $continueField . $db->buildLike( "$protocol", $db->anyString() ) );
100 $orderBy = [ 'el_id' ];
102 $this->addOption( 'ORDER BY', $orderBy );
103 $this->addFields( $orderBy ); // Make sure
105 $prop = array_fill_keys( $params['prop'], true );
106 $fld_ids = isset( $prop['ids'] );
107 $fld_title = isset( $prop['title'] );
108 $fld_url = isset( $prop['url'] );
110 if ( $resultPageSet === null ) {
116 foreach ( $fields as $field ) {
117 $this->addFieldsIf( $field, $fld_url );
120 $this->addFields( $resultPageSet->getPageTableFields() );
123 $limit = $params['limit'];
124 $this->addOption( 'LIMIT', $limit +
1 );
126 // T244254: Avoid MariaDB deciding to scan all of `page`.
127 $this->addOption( 'STRAIGHT_JOIN' );
129 if ( $params['continue'] !== null ) {
130 $cont = $this->parseContinueParamOrDie( $params['continue'],
131 array_fill( 0, count( $orderBy ), 'string' ) );
132 $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
133 $this->addWhere( $db->buildComparison( '>=', $conds ) );
136 $res = $this->select( __METHOD__
);
138 $result = $this->getResult();
140 if ( $resultPageSet === null ) {
141 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__
);
145 foreach ( $res as $row ) {
146 if ( ++
$count > $limit ) {
147 // We've reached the one extra which shows that there are
148 // additional pages to be had. Stop here...
149 $this->setContinue( $orderBy, $row );
153 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
157 if ( $resultPageSet === null ) {
159 ApiResult
::META_TYPE
=> 'assoc',
162 $vals['pageid'] = (int)$row->page_id
;
165 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
166 ApiQueryBase
::addTitleInfo( $vals, $title );
169 $to = LinkFilter
::reverseIndexes( $row->el_to_domain_index
) . $row->el_to_path
;
170 // expand protocol-relative urls
171 if ( $params['expandurl'] ) {
172 $to = (string)$this->urlUtils
->expand( $to, PROTO_CANONICAL
);
176 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
178 $this->setContinue( $orderBy, $row );
182 $resultPageSet->processDbRow( $row );
186 if ( $resultPageSet === null ) {
187 $result->addIndexedTagName( [ 'query', $this->getModuleName() ],
188 $this->getModulePrefix() );
192 private function setContinue( $orderBy, $row ) {
194 foreach ( $orderBy as $field ) {
195 $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
197 $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
200 public function getAllowedParams() {
203 ParamValidator
::PARAM_ISMULTI
=> true,
204 ParamValidator
::PARAM_DEFAULT
=> 'ids|title|url',
205 ParamValidator
::PARAM_TYPE
=> [
210 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
213 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
216 ParamValidator
::PARAM_TYPE
=> LinkFilter
::prepareProtocols(),
217 ParamValidator
::PARAM_DEFAULT
=> '',
221 ParamValidator
::PARAM_ISMULTI
=> true,
222 ParamValidator
::PARAM_TYPE
=> 'namespace'
225 ParamValidator
::PARAM_DEFAULT
=> 10,
226 ParamValidator
::PARAM_TYPE
=> 'limit',
227 IntegerDef
::PARAM_MIN
=> 1,
228 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
229 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
232 ParamValidator
::PARAM_TYPE
=> 'boolean',
233 ParamValidator
::PARAM_DEFAULT
=> false,
234 ParamValidator
::PARAM_DEPRECATED
=> true,
238 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
239 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
240 'api-help-param-limited-in-miser-mode',
247 protected function getExamplesMessages() {
249 'action=query&list=exturlusage&euquery=www.mediawiki.org'
250 => 'apihelp-query+exturlusage-example-simple',
254 public function getHelpUrls() {
255 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage';