Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 140 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ApiQueryExtLinksUsage | |
0.00% |
0 / 139 |
|
0.00% |
0 / 9 |
1056 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| run | |
0.00% |
0 / 84 |
|
0.00% |
0 / 1 |
506 | |||
| setContinue | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getAllowedParams | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
6 | |||
| getExamplesMessages | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com" |
| 5 | * |
| 6 | * @license GPL-2.0-or-later |
| 7 | * @file |
| 8 | */ |
| 9 | |
| 10 | namespace MediaWiki\Api; |
| 11 | |
| 12 | use MediaWiki\Deferred\LinksUpdate\ExternalLinksTable; |
| 13 | use MediaWiki\ExternalLinks\LinkFilter; |
| 14 | use MediaWiki\MainConfigNames; |
| 15 | use MediaWiki\Parser\Parser; |
| 16 | use MediaWiki\Title\Title; |
| 17 | use MediaWiki\Utils\UrlUtils; |
| 18 | use Wikimedia\ParamValidator\ParamValidator; |
| 19 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 20 | use Wikimedia\Rdbms\IExpression; |
| 21 | use Wikimedia\Rdbms\LikeValue; |
| 22 | |
| 23 | /** |
| 24 | * @ingroup API |
| 25 | */ |
| 26 | class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { |
| 27 | |
| 28 | private UrlUtils $urlUtils; |
| 29 | |
| 30 | public function __construct( |
| 31 | ApiQuery $query, |
| 32 | string $moduleName, |
| 33 | UrlUtils $urlUtils |
| 34 | ) { |
| 35 | parent::__construct( $query, $moduleName, 'eu' ); |
| 36 | |
| 37 | $this->urlUtils = $urlUtils; |
| 38 | } |
| 39 | |
| 40 | public function execute() { |
| 41 | $this->run(); |
| 42 | } |
| 43 | |
| 44 | /** @inheritDoc */ |
| 45 | public function getCacheMode( $params ) { |
| 46 | return 'public'; |
| 47 | } |
| 48 | |
| 49 | /** @inheritDoc */ |
| 50 | public function executeGenerator( $resultPageSet ) { |
| 51 | $this->run( $resultPageSet ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param ApiPageSet|null $resultPageSet |
| 56 | * @return void |
| 57 | */ |
| 58 | private function run( $resultPageSet = null ) { |
| 59 | $params = $this->extractRequestParams(); |
| 60 | $db = $this->getDB(); |
| 61 | |
| 62 | $query = $params['query']; |
| 63 | $protocol = LinkFilter::getProtocolPrefix( $params['protocol'] ); |
| 64 | |
| 65 | $this->addTables( [ 'externallinks', 'page' ] ); |
| 66 | $this->addJoinConds( [ 'page' => [ 'JOIN', 'page_id=el_from' ] ] ); |
| 67 | $fields = [ 'el_to_domain_index', 'el_to_path' ]; |
| 68 | |
| 69 | $miser_ns = []; |
| 70 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 71 | $miser_ns = $params['namespace'] ?: []; |
| 72 | } else { |
| 73 | $this->addWhereFld( 'page_namespace', $params['namespace'] ); |
| 74 | } |
| 75 | if ( $query !== null && $query !== '' ) { |
| 76 | // Normalize query to match the normalization applied for the externallinks table |
| 77 | $query = Parser::normalizeLinkUrl( $query ); |
| 78 | $conds = LinkFilter::getQueryConditions( $query, [ |
| 79 | 'protocol' => $protocol, |
| 80 | 'oneWildcard' => true, |
| 81 | 'db' => $db |
| 82 | ] ); |
| 83 | if ( !$conds ) { |
| 84 | $this->dieWithError( 'apierror-badquery' ); |
| 85 | } |
| 86 | $this->addWhere( $conds ); |
| 87 | } else { |
| 88 | if ( $protocol !== null ) { |
| 89 | $this->addWhere( |
| 90 | $db->expr( 'el_to_domain_index', IExpression::LIKE, new LikeValue( "$protocol", $db->anyString() ) ) |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 | $orderBy = [ 'el_id' ]; |
| 95 | |
| 96 | $this->addOption( 'ORDER BY', $orderBy ); |
| 97 | $this->addFields( $orderBy ); // Make sure |
| 98 | |
| 99 | $prop = array_fill_keys( $params['prop'], true ); |
| 100 | $fld_ids = isset( $prop['ids'] ); |
| 101 | $fld_title = isset( $prop['title'] ); |
| 102 | $fld_url = isset( $prop['url'] ); |
| 103 | |
| 104 | if ( $resultPageSet === null ) { |
| 105 | $this->addFields( [ |
| 106 | 'page_id', |
| 107 | 'page_namespace', |
| 108 | 'page_title' |
| 109 | ] ); |
| 110 | foreach ( $fields as $field ) { |
| 111 | $this->addFieldsIf( $field, $fld_url ); |
| 112 | } |
| 113 | } else { |
| 114 | $this->addFields( $resultPageSet->getPageTableFields() ); |
| 115 | } |
| 116 | |
| 117 | $limit = $params['limit']; |
| 118 | $this->addOption( 'LIMIT', $limit + 1 ); |
| 119 | |
| 120 | // T244254: Avoid MariaDB deciding to scan all of `page`. |
| 121 | $this->addOption( 'STRAIGHT_JOIN' ); |
| 122 | |
| 123 | if ( $params['continue'] !== null ) { |
| 124 | $cont = $this->parseContinueParamOrDie( $params['continue'], |
| 125 | array_fill( 0, count( $orderBy ), 'string' ) ); |
| 126 | $conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) ); |
| 127 | $this->addWhere( $db->buildComparison( '>=', $conds ) ); |
| 128 | } |
| 129 | |
| 130 | $this->setVirtualDomain( ExternalLinksTable::VIRTUAL_DOMAIN ); |
| 131 | $res = $this->select( __METHOD__ ); |
| 132 | $this->resetVirtualDomain(); |
| 133 | |
| 134 | $result = $this->getResult(); |
| 135 | |
| 136 | if ( $resultPageSet === null ) { |
| 137 | $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ ); |
| 138 | } |
| 139 | |
| 140 | $count = 0; |
| 141 | foreach ( $res as $row ) { |
| 142 | if ( ++$count > $limit ) { |
| 143 | // We've reached the one extra which shows that there are |
| 144 | // additional pages to be had. Stop here... |
| 145 | $this->setContinue( $orderBy, $row ); |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) { |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | if ( $resultPageSet === null ) { |
| 154 | $vals = [ |
| 155 | ApiResult::META_TYPE => 'assoc', |
| 156 | ]; |
| 157 | if ( $fld_ids ) { |
| 158 | $vals['pageid'] = (int)$row->page_id; |
| 159 | } |
| 160 | if ( $fld_title ) { |
| 161 | $title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 162 | ApiQueryBase::addTitleInfo( $vals, $title ); |
| 163 | } |
| 164 | if ( $fld_url ) { |
| 165 | $to = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path; |
| 166 | // expand protocol-relative urls |
| 167 | if ( $params['expandurl'] ) { |
| 168 | $to = (string)$this->urlUtils->expand( $to, PROTO_CANONICAL ); |
| 169 | } |
| 170 | $vals['url'] = $to; |
| 171 | } |
| 172 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
| 173 | if ( !$fit ) { |
| 174 | $this->setContinue( $orderBy, $row ); |
| 175 | break; |
| 176 | } |
| 177 | } else { |
| 178 | $resultPageSet->processDbRow( $row ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if ( $resultPageSet === null ) { |
| 183 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], |
| 184 | $this->getModulePrefix() ); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private function setContinue( array $orderBy, \stdClass $row ) { |
| 189 | $fields = []; |
| 190 | foreach ( $orderBy as $field ) { |
| 191 | $fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] ); |
| 192 | } |
| 193 | $this->setContinueEnumParameter( 'continue', implode( '|', $fields ) ); |
| 194 | } |
| 195 | |
| 196 | /** @inheritDoc */ |
| 197 | public function getAllowedParams() { |
| 198 | $ret = [ |
| 199 | 'prop' => [ |
| 200 | ParamValidator::PARAM_ISMULTI => true, |
| 201 | ParamValidator::PARAM_DEFAULT => 'ids|title|url', |
| 202 | ParamValidator::PARAM_TYPE => [ |
| 203 | 'ids', |
| 204 | 'title', |
| 205 | 'url' |
| 206 | ], |
| 207 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 208 | ], |
| 209 | 'continue' => [ |
| 210 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 211 | ], |
| 212 | 'protocol' => [ |
| 213 | ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(), |
| 214 | ParamValidator::PARAM_DEFAULT => '', |
| 215 | ], |
| 216 | 'query' => null, |
| 217 | 'namespace' => [ |
| 218 | ParamValidator::PARAM_ISMULTI => true, |
| 219 | ParamValidator::PARAM_TYPE => 'namespace' |
| 220 | ], |
| 221 | 'limit' => [ |
| 222 | ParamValidator::PARAM_DEFAULT => 10, |
| 223 | ParamValidator::PARAM_TYPE => 'limit', |
| 224 | IntegerDef::PARAM_MIN => 1, |
| 225 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 226 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 227 | ], |
| 228 | 'expandurl' => [ |
| 229 | ParamValidator::PARAM_TYPE => 'boolean', |
| 230 | ParamValidator::PARAM_DEFAULT => false, |
| 231 | ParamValidator::PARAM_DEPRECATED => true, |
| 232 | ], |
| 233 | ]; |
| 234 | |
| 235 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 236 | $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [ |
| 237 | 'api-help-param-limited-in-miser-mode', |
| 238 | ]; |
| 239 | } |
| 240 | |
| 241 | return $ret; |
| 242 | } |
| 243 | |
| 244 | /** @inheritDoc */ |
| 245 | protected function getExamplesMessages() { |
| 246 | return [ |
| 247 | 'action=query&list=exturlusage&euquery=www.mediawiki.org' |
| 248 | => 'apihelp-query+exturlusage-example-simple', |
| 249 | ]; |
| 250 | } |
| 251 | |
| 252 | /** @inheritDoc */ |
| 253 | public function getHelpUrls() { |
| 254 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage'; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** @deprecated class alias since 1.43 */ |
| 259 | class_alias( ApiQueryExtLinksUsage::class, 'ApiQueryExtLinksUsage' ); |