Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / includes / api / ApiQueryExternalLinks.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * A query module to list all external URLs found on a given set of pages.
25 *
26 * @ingroup API
27 */
28 class ApiQueryExternalLinks extends ApiQueryBase {
29
30 public function __construct( ApiQuery $query, $moduleName ) {
31 parent::__construct( $query, $moduleName, 'el' );
32 }
33
34 public function execute() {
35 if ( $this->getPageSet()->getGoodTitleCount() == 0 ) {
36 return;
37 }
38
39 $params = $this->extractRequestParams();
40
41 $query = $params['query'];
42 $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
43
44 $this->addFields( [
45 'el_from',
46 'el_to'
47 ] );
48
49 $this->addTables( 'externallinks' );
50 $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
51
52 $whereQuery = $this->prepareUrlQuerySearchString( $query, $protocol );
53
54 if ( $whereQuery !== null ) {
55 $this->addWhere( $whereQuery );
56 }
57
58 // Don't order by el_from if it's constant in the WHERE clause
59 if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
60 $this->addOption( 'ORDER BY', 'el_from' );
61 }
62
63 // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
64 if ( $protocol === null ) {
65 $this->addOption( 'DISTINCT' );
66 }
67
68 $this->addOption( 'LIMIT', $params['limit'] + 1 );
69 $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
70 if ( $offset ) {
71 $this->addOption( 'OFFSET', $params['offset'] );
72 }
73
74 $res = $this->select( __METHOD__ );
75
76 $count = 0;
77 foreach ( $res as $row ) {
78 if ( ++$count > $params['limit'] ) {
79 // We've reached the one extra which shows that
80 // there are additional pages to be had. Stop here...
81 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
82 break;
83 }
84 $entry = [];
85 $to = $row->el_to;
86 // expand protocol-relative urls
87 if ( $params['expandurl'] ) {
88 $to = wfExpandUrl( $to, PROTO_CANONICAL );
89 }
90 ApiResult::setContentValue( $entry, 'url', $to );
91 $fit = $this->addPageSubItem( $row->el_from, $entry );
92 if ( !$fit ) {
93 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
94 break;
95 }
96 }
97 }
98
99 public function getCacheMode( $params ) {
100 return 'public';
101 }
102
103 public function getAllowedParams() {
104 return [
105 'limit' => [
106 ApiBase::PARAM_DFLT => 10,
107 ApiBase::PARAM_TYPE => 'limit',
108 ApiBase::PARAM_MIN => 1,
109 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
110 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
111 ],
112 'offset' => [
113 ApiBase::PARAM_TYPE => 'integer',
114 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
115 ],
116 'protocol' => [
117 ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
118 ApiBase::PARAM_DFLT => '',
119 ],
120 'query' => null,
121 'expandurl' => false,
122 ];
123 }
124
125 protected function getExamplesMessages() {
126 return [
127 'action=query&prop=extlinks&titles=Main%20Page'
128 => 'apihelp-query+extlinks-example-simple',
129 ];
130 }
131
132 public function getHelpUrls() {
133 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Extlinks';
134 }
135 }