Update git submodules
[mediawiki.git] / includes / api / ApiQueryExtLinksUsage.php
blobfdb707d78990309439af9940de5bb4631ef469a1
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 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;
31 /**
32 * @ingroup API
34 class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase {
36 private UrlUtils $urlUtils;
38 /**
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() {
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 $continueField = 'el_to_domain_index';
75 $fields = [ 'el_to_domain_index', 'el_to_path' ];
77 $miser_ns = [];
78 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
79 $miser_ns = $params['namespace'] ?: [];
80 } else {
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,
89 'db' => $db
90 ] );
91 if ( !$conds ) {
92 $this->dieWithError( 'apierror-badquery' );
94 $this->addWhere( $conds );
95 } else {
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 ) {
111 $this->addFields( [
112 'page_id',
113 'page_namespace',
114 'page_title'
115 ] );
116 foreach ( $fields as $field ) {
117 $this->addFieldsIf( $field, $fld_url );
119 } else {
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__ );
144 $count = 0;
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 );
150 break;
153 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
154 continue;
157 if ( $resultPageSet === null ) {
158 $vals = [
159 ApiResult::META_TYPE => 'assoc',
161 if ( $fld_ids ) {
162 $vals['pageid'] = (int)$row->page_id;
164 if ( $fld_title ) {
165 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
166 ApiQueryBase::addTitleInfo( $vals, $title );
168 if ( $fld_url ) {
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 );
174 $vals['url'] = $to;
176 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
177 if ( !$fit ) {
178 $this->setContinue( $orderBy, $row );
179 break;
181 } else {
182 $resultPageSet->processDbRow( $row );
186 if ( $resultPageSet === null ) {
187 $result->addIndexedTagName( [ 'query', $this->getModuleName() ],
188 $this->getModulePrefix() );
192 private function setContinue( $orderBy, $row ) {
193 $fields = [];
194 foreach ( $orderBy as $field ) {
195 $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] );
197 $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) );
200 public function getAllowedParams() {
201 $ret = [
202 'prop' => [
203 ParamValidator::PARAM_ISMULTI => true,
204 ParamValidator::PARAM_DEFAULT => 'ids|title|url',
205 ParamValidator::PARAM_TYPE => [
206 'ids',
207 'title',
208 'url'
210 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
212 'continue' => [
213 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
215 'protocol' => [
216 ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(),
217 ParamValidator::PARAM_DEFAULT => '',
219 'query' => null,
220 'namespace' => [
221 ParamValidator::PARAM_ISMULTI => true,
222 ParamValidator::PARAM_TYPE => 'namespace'
224 'limit' => [
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
231 'expandurl' => [
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',
244 return $ret;
247 protected function getExamplesMessages() {
248 return [
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';