fc5d8a042598552bd871bd1f2ecc221264986daa
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class ApiQueryExtLinksUsage
extends ApiQueryGeneratorBase
{
28 public function __construct( ApiQuery
$query, $moduleName ) {
29 parent
::__construct( $query, $moduleName, 'eu' );
32 public function execute() {
36 public function getCacheMode( $params ) {
40 public function executeGenerator( $resultPageSet ) {
41 $this->run( $resultPageSet );
45 * @param ApiPageSet $resultPageSet
48 private function run( $resultPageSet = null ) {
49 $params = $this->extractRequestParams();
51 $query = $params['query'];
52 $protocol = self
::getProtocolPrefix( $params['protocol'] );
54 $this->addTables( [ 'page', 'externallinks' ] ); // must be in this order for 'USE INDEX'
55 $this->addOption( 'USE INDEX', 'el_index' );
56 $this->addWhere( 'page_id=el_from' );
59 if ( $this->getConfig()->get( 'MiserMode' ) ) {
60 $miser_ns = $params['namespace'] ?
: [];
62 $this->addWhereFld( 'page_namespace', $params['namespace'] );
65 // Normalize query to match the normalization applied for the externallinks table
66 $query = Parser
::normalizeLinkUrl( $query );
68 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
70 if ( $whereQuery !== null ) {
71 $this->addWhere( $whereQuery );
74 $prop = array_flip( $params['prop'] );
75 $fld_ids = isset( $prop['ids'] );
76 $fld_title = isset( $prop['title'] );
77 $fld_url = isset( $prop['url'] );
79 if ( is_null( $resultPageSet ) ) {
85 $this->addFieldsIf( 'el_to', $fld_url );
87 $this->addFields( $resultPageSet->getPageTableFields() );
90 $limit = $params['limit'];
91 $offset = $params['offset'];
92 $this->addOption( 'LIMIT', $limit +
1 );
93 if ( isset( $offset ) ) {
94 $this->addOption( 'OFFSET', $offset );
97 $res = $this->select( __METHOD__
);
99 $result = $this->getResult();
101 foreach ( $res as $row ) {
102 if ( ++
$count > $limit ) {
103 // We've reached the one extra which shows that there are
104 // additional pages to be had. Stop here...
105 $this->setContinueEnumParameter( 'offset', $offset +
$limit );
109 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
113 if ( is_null( $resultPageSet ) ) {
115 ApiResult
::META_TYPE
=> 'assoc',
118 $vals['pageid'] = intval( $row->page_id
);
121 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
122 ApiQueryBase
::addTitleInfo( $vals, $title );
126 // expand protocol-relative urls
127 if ( $params['expandurl'] ) {
128 $to = wfExpandUrl( $to, PROTO_CANONICAL
);
132 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
134 $this->setContinueEnumParameter( 'offset', $offset +
$count - 1 );
138 $resultPageSet->processDbRow( $row );
142 if ( is_null( $resultPageSet ) ) {
143 $result->addIndexedTagName( [ 'query', $this->getModuleName() ],
144 $this->getModulePrefix() );
148 public function getAllowedParams() {
151 ApiBase
::PARAM_ISMULTI
=> true,
152 ApiBase
::PARAM_DFLT
=> 'ids|title|url',
153 ApiBase
::PARAM_TYPE
=> [
158 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
161 ApiBase
::PARAM_TYPE
=> 'integer',
162 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
165 ApiBase
::PARAM_TYPE
=> self
::prepareProtocols(),
166 ApiBase
::PARAM_DFLT
=> '',
170 ApiBase
::PARAM_ISMULTI
=> true,
171 ApiBase
::PARAM_TYPE
=> 'namespace'
174 ApiBase
::PARAM_DFLT
=> 10,
175 ApiBase
::PARAM_TYPE
=> 'limit',
176 ApiBase
::PARAM_MIN
=> 1,
177 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
178 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
180 'expandurl' => false,
183 if ( $this->getConfig()->get( 'MiserMode' ) ) {
184 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
185 'api-help-param-limited-in-miser-mode',
192 public static function prepareProtocols() {
193 global $wgUrlProtocols;
195 foreach ( $wgUrlProtocols as $p ) {
197 $protocols[] = substr( $p, 0, strpos( $p, ':' ) );
204 public static function getProtocolPrefix( $protocol ) {
205 // Find the right prefix
206 global $wgUrlProtocols;
207 if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) {
208 foreach ( $wgUrlProtocols as $p ) {
209 if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) {
221 protected function getExamplesMessages() {
223 'action=query&list=exturlusage&euquery=www.mediawiki.org'
224 => 'apihelp-query+exturlusage-example-simple',
228 public function getHelpUrls() {
229 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Exturlusage';