| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\ExternalLinks\LinkFilter; |
| 26 |
use MediaWiki\Parser\Parser; |
| 27 |
use MediaWiki\Title\Title; |
| 28 |
use MediaWiki\Utils\UrlUtils; |
| 29 |
use Wikimedia\ParamValidator\ParamValidator; |
| 30 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 31 |
use Wikimedia\Rdbms\IExpression; |
| 32 |
use Wikimedia\Rdbms\LikeValue; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ApiQueryExternalLinks extends ApiQueryBase { |
| 40 |
|
| 41 |
private UrlUtils $urlUtils; |
| 42 |
|
| 43 |
public function __construct( ApiQuery $query, string $moduleName, UrlUtils $urlUtils ) { |
| 44 |
parent::__construct( $query, $moduleName, 'el' ); |
| 45 |
|
| 46 |
$this->urlUtils = $urlUtils; |
| 47 |
} |
| 48 |
|
| 49 |
public function execute() { |
| 50 |
$pages = $this->getPageSet()->getGoodPages(); |
| 51 |
if ( $pages === [] ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$params = $this->extractRequestParams(); |
| 56 |
$db = $this->getDB(); |
| 57 |
|
| 58 |
$query = $params['query']; |
| 59 |
$protocol = LinkFilter::getProtocolPrefix( $params['protocol'] ); |
| 60 |
|
| 61 |
$fields = [ 'el_from' ]; |
| 62 |
$fields[] = 'el_to_domain_index'; |
| 63 |
$fields[] = 'el_to_path'; |
| 64 |
$continueField = 'el_to_domain_index'; |
| 65 |
$this->addFields( $fields ); |
| 66 |
|
| 67 |
$this->addTables( 'externallinks' ); |
| 68 |
$this->addWhereFld( 'el_from', array_keys( $pages ) ); |
| 69 |
|
| 70 |
if ( $query !== null && $query !== '' ) { |
| 71 |
|
| 72 |
$query = Parser::normalizeLinkUrl( $query ); |
| 73 |
|
| 74 |
$conds = LinkFilter::getQueryConditions( $query, [ |
| 75 |
'protocol' => $protocol, |
| 76 |
'oneWildcard' => true, |
| 77 |
'db' => $db |
| 78 |
] ); |
| 79 |
if ( !$conds ) { |
| 80 |
$this->dieWithError( 'apierror-badquery' ); |
| 81 |
} |
| 82 |
$this->addWhere( $conds ); |
| 83 |
} else { |
| 84 |
if ( $protocol !== null ) { |
| 85 |
$this->addWhere( |
| 86 |
$db->expr( $continueField, IExpression::LIKE, new LikeValue( "$protocol", $db->anyString() ) ) |
| 87 |
); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$orderBy = [ 'el_id' ]; |
| 92 |
|
| 93 |
$this->addOption( 'ORDER BY', $orderBy ); |
| 94 |
$this->addFields( $orderBy ); |
| 95 |
|
| 96 |
$this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 97 |
|
| 98 |
if ( $params['continue'] !== null ) { |
| 99 |
$cont = $this->parseContinueParamOrDie( $params['continue'], |
| 100 |
array_fill( 0, count( $orderBy ), 'string' ) ); |
| 101 |
$conds = array_combine( $orderBy, array_map( 'rawurldecode', $cont ) ); |
| 102 |
$this->addWhere( $db->buildComparison( '>=', $conds ) ); |
| 103 |
} |
| 104 |
|
| 105 |
$res = $this->select( __METHOD__ ); |
| 106 |
|
| 107 |
$count = 0; |
| 108 |
foreach ( $res as $row ) { |
| 109 |
if ( ++$count > $params['limit'] ) { |
| 110 |
|
| 111 |
|
| 112 |
$this->setContinue( $orderBy, $row ); |
| 113 |
break; |
| 114 |
} |
| 115 |
$entry = []; |
| 116 |
$to = LinkFilter::reverseIndexes( $row->el_to_domain_index ) . $row->el_to_path; |
| 117 |
|
| 118 |
if ( $params['expandurl'] ) { |
| 119 |
$to = (string)$this->urlUtils->expand( $to, PROTO_CANONICAL ); |
| 120 |
} |
| 121 |
ApiResult::setContentValue( $entry, 'url', $to ); |
| 122 |
$fit = $this->addPageSubItem( $row->el_from, $entry ); |
| 123 |
if ( !$fit ) { |
| 124 |
$this->setContinue( $orderBy, $row ); |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
private function setContinue( $orderBy, $row ) { |
| 131 |
$fields = []; |
| 132 |
foreach ( $orderBy as $field ) { |
| 133 |
$fields[] = strtr( $row->$field, [ '%' => '%25', '|' => '%7C' ] ); |
| 134 |
} |
| 135 |
$this->setContinueEnumParameter( 'continue', implode( '|', $fields ) ); |
| 136 |
} |
| 137 |
|
| 138 |
public function getCacheMode( $params ) { |
| 139 |
return 'public'; |
| 140 |
} |
| 141 |
|
| 142 |
public function getAllowedParams() { |
| 143 |
return [ |
| 144 |
'limit' => [ |
| 145 |
ParamValidator::PARAM_DEFAULT => 10, |
| 146 |
ParamValidator::PARAM_TYPE => 'limit', |
| 147 |
IntegerDef::PARAM_MIN => 1, |
| 148 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 149 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 150 |
], |
| 151 |
'continue' => [ |
| 152 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 153 |
], |
| 154 |
'protocol' => [ |
| 155 |
ParamValidator::PARAM_TYPE => LinkFilter::prepareProtocols(), |
| 156 |
ParamValidator::PARAM_DEFAULT => '', |
| 157 |
], |
| 158 |
'query' => null, |
| 159 |
'expandurl' => [ |
| 160 |
ParamValidator::PARAM_TYPE => 'boolean', |
| 161 |
ParamValidator::PARAM_DEFAULT => false, |
| 162 |
ParamValidator::PARAM_DEPRECATED => true, |
| 163 |
], |
| 164 |
]; |
| 165 |
} |
| 166 |
|
| 167 |
protected function getExamplesMessages() { |
| 168 |
$title = Title::newMainPage()->getPrefixedText(); |
| 169 |
$mp = rawurlencode( $title ); |
| 170 |
|
| 171 |
return [ |
| 172 |
"action=query&prop=extlinks&titles={$mp}" |
| 173 |
=> 'apihelp-query+extlinks-example-simple', |
| 174 |
]; |
| 175 |
} |
| 176 |
|
| 177 |
public function getHelpUrls() { |
| 178 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks'; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
class_alias( ApiQueryExternalLinks::class, 'ApiQueryExternalLinks' ); |