Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryExtLinksUsage.php
blobee28e4012cce8a19e03bf144e8ec8a1227be78b4
1 <?php
3 /**
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
21 * @file
24 namespace MediaWiki\Api;
26 use MediaWiki\ExternalLinks\LinkFilter;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\Parser\Parser;
29 use MediaWiki\Title\Title;
30 use MediaWiki\Utils\UrlUtils;
31 use Wikimedia\ParamValidator\ParamValidator;
32 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
33 use Wikimedia\Rdbms\IExpression;
34 use Wikimedia\Rdbms\LikeValue;
36 /**
37 * @ingroup API
39 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
41 private UrlUtils $urlUtils;
43 public function __construct( ApiQuery $query, string $moduleName, UrlUtils $urlUtils ) {
44 parent::__construct( $query, $moduleName, 'eu' );
46 $this->urlUtils = $urlUtils;
49 public function execute() {
50 $this->run();
53 public function getCacheMode( $params ) {
54 return 'public';
57 public function executeGenerator( $resultPageSet ) {
58 $this->run( $resultPageSet );
61 /**
62 * @param ApiPageSet|null $resultPageSet
63 * @return void
65 private function run( $resultPageSet = null ) {
66 $params = $this->extractRequestParams();
67 $db = $this->getDB();
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 $fields = [ 'el_to_domain_index', 'el_to_path' ];
76 $miser_ns = [];
77 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
78 $miser_ns = $params['namespace'] ?: [];
79 } else {
80 $this->addWhereFld( 'page_namespace', $params['namespace'] );
82 if ( $query !== null && $query !== '' ) {
83 // Normalize query to match the normalization applied for the externallinks table
84 $query = Parser::normalizeLinkUrl( $query );
85 $conds = LinkFilter::getQueryConditions( $query, [
86 'protocol' => $protocol,
87 'oneWildcard' => true,
88 'db' => $db
89 ] );
90 if ( !$conds ) {
91 $this->dieWithError( 'apierror-badquery' );
93 $this->addWhere( $conds );
94 } else {
95 if ( $protocol !== null ) {
96 $this->addWhere(
97 $db->expr( 'el_to_domain_index', IExpression::LIKE, new LikeValue( "$protocol", $db->anyString() ) )
101 $orderBy = [ 'el_id' ];
103 $this->addOption( 'ORDER BY', $orderBy );
104 $this->addFields( $orderBy ); // Make sure
106 $prop = array_fill_keys( $params['prop'], true );
107 $fld_ids = isset( $prop['ids'] );
108 $fld_title = isset( $prop['title'] );
109 $fld_url = isset( $prop['url'] );
111 if ( $resultPageSet === null ) {
112 $this->addFields( [
113 'page_id',
114 'page_namespace',
115 'page_title'
116 ] );
117 foreach ( $fields as $field ) {
118 $this->addFieldsIf( $field, $fld_url );
120 } else {
121 $this->addFields( $resultPageSet->getPageTableFields() );
124 $limit = $params['limit'];
125 $this->addOption( 'LIMIT', $limit + 1 );
127 // T244254: Avoid MariaDB deciding to scan all of `page`.
128 $this->addOption( 'STRAIGHT_JOIN' );
130 if ( $params['continue'] !== null ) {
131 $cont = $this->parseContinueParamOrDie( $params['continue'],
132 array_fill( 0, count( $orderBy ), 'string' ) );
133 $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) );
134 $this->addWhere( $db->buildComparison( '>=', $conds ) );
137 $res = $this->select( __METHOD__ );
139 $result = $this->getResult();
141 if ( $resultPageSet === null ) {
142 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
145 $count = 0;
146 foreach ( $res as $row ) {
147 if ( ++$count > $limit ) {
148 // We've reached the one extra which shows that there are
149 // additional pages to be had. Stop here...
150 $this->setContinue( $orderBy, $row );
151 break;
154 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
155 continue;
158 if ( $resultPageSet === null ) {
159 $vals = [
160 ApiResult::META_TYPE => 'assoc',
162 if ( $fld_ids ) {
163 $vals['pageid'] = (int)$row->page_id;
165 if ( $fld_title ) {
166 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
167 ApiQueryBase::addTitleInfo( $vals, $title );
169 if ( $fld_url ) {
170 $to = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path;
171 // expand protocol-relative urls
172 if ( $params['expandurl'] ) {
173 $to = (string)$this->urlUtils->expand( $to, PROTO_CANONICAL );
175 $vals['url'] = $to;
177 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
178 if ( !$fit ) {
179 $this->setContinue( $orderBy, $row );
180 break;
182 } else {
183 $resultPageSet->processDbRow( $row );
187 if ( $resultPageSet === null ) {
188 $result->addIndexedTagName( [ 'query', $this->getModuleName() ],
189 $this->getModulePrefix() );
193 private function setContinue( $orderBy, $row ) {
194 $fields = [];
195 foreach ( $orderBy as $field ) {
196 $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
198 $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
201 public function getAllowedParams() {
202 $ret = [
203 'prop' => [
204 ParamValidator::PARAM_ISMULTI => true,
205 ParamValidator::PARAM_DEFAULT => 'ids|title|url',
206 ParamValidator::PARAM_TYPE => [
207 'ids',
208 'title',
209 'url'
211 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
213 'continue' => [
214 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
216 'protocol' => [
217 ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(),
218 ParamValidator::PARAM_DEFAULT => '',
220 'query' => null,
221 'namespace' => [
222 ParamValidator::PARAM_ISMULTI => true,
223 ParamValidator::PARAM_TYPE => 'namespace'
225 'limit' => [
226 ParamValidator::PARAM_DEFAULT => 10,
227 ParamValidator::PARAM_TYPE => 'limit',
228 IntegerDef::PARAM_MIN => 1,
229 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
230 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
232 'expandurl' => [
233 ParamValidator::PARAM_TYPE => 'boolean',
234 ParamValidator::PARAM_DEFAULT => false,
235 ParamValidator::PARAM_DEPRECATED => true,
239 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
240 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
241 'api-help-param-limited-in-miser-mode',
245 return $ret;
248 protected function getExamplesMessages() {
249 return [
250 'action=query&list=exturlusage&euquery=www.mediawiki.org'
251 => 'apihelp-query+exturlusage-example-simple',
255 public function getHelpUrls() {
256 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage';
260 /** @deprecated class alias since 1.43 */
261 class_alias( ApiQueryExtLinksUsage::class, 'ApiQueryExtLinksUsage' );