Merge "registration: Only allow one extension to set a specific config setting"
[lhc/web/wiklou.git] / includes / specials / SpecialLinkSearch.php
1 <?php
2 /**
3 * Implements Special:LinkSearch
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 * @ingroup SpecialPage
22 * @author Brion Vibber
23 */
24
25 use Wikimedia\Rdbms\ResultWrapper;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * Special:LinkSearch to search the external-links table.
30 * @ingroup SpecialPage
31 */
32 class LinkSearchPage extends QueryPage {
33 /** @var array|bool */
34 private $mungedQuery = false;
35
36 function setParams( $params ) {
37 $this->mQuery = $params['query'];
38 $this->mNs = $params['namespace'];
39 $this->mProt = $params['protocol'];
40 }
41
42 function __construct( $name = 'LinkSearch' ) {
43 parent::__construct( $name );
44
45 // Since we don't control the constructor parameters, we can't inject services that way.
46 // Instead, we initialize services in the execute() method, and allow them to be overridden
47 // using the setServices() method.
48 }
49
50 function isCacheable() {
51 return false;
52 }
53
54 public function execute( $par ) {
55 $this->setHeaders();
56 $this->outputHeader();
57
58 $out = $this->getOutput();
59 $out->allowClickjacking();
60
61 $request = $this->getRequest();
62 $target = $request->getVal( 'target', $par );
63 $namespace = $request->getIntOrNull( 'namespace' );
64
65 $protocols_list = [];
66 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
67 if ( $prot !== '//' ) {
68 $protocols_list[] = $prot;
69 }
70 }
71
72 $target2 = $target;
73 // Get protocol, default is http://
74 $protocol = 'http://';
75 $bits = wfParseUrl( $target );
76 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
77 $protocol = $bits['scheme'] . $bits['delimiter'];
78 // Make sure wfParseUrl() didn't make some well-intended correction in the
79 // protocol
80 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
81 $target2 = substr( $target, strlen( $protocol ) );
82 } else {
83 // If it did, let LinkFilter::makeLikeArray() handle this
84 $protocol = '';
85 }
86 }
87
88 $out->addWikiMsg(
89 'linksearch-text',
90 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
91 count( $protocols_list )
92 );
93 $fields = [
94 'target' => [
95 'type' => 'text',
96 'name' => 'target',
97 'id' => 'target',
98 'size' => 50,
99 'label-message' => 'linksearch-pat',
100 'default' => $target,
101 'dir' => 'ltr',
102 ]
103 ];
104 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
105 $fields += [
106 'namespace' => [
107 'type' => 'namespaceselect',
108 'name' => 'namespace',
109 'label-message' => 'linksearch-ns',
110 'default' => $namespace,
111 'id' => 'namespace',
112 'all' => '',
113 'cssclass' => 'namespaceselector',
114 ],
115 ];
116 }
117 $hiddenFields = [
118 'title' => $this->getPageTitle()->getPrefixedDBkey(),
119 ];
120 $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
121 $htmlForm->addHiddenFields( $hiddenFields );
122 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
123 $htmlForm->setWrapperLegendMsg( 'linksearch' );
124 $htmlForm->setAction( wfScript() );
125 $htmlForm->setMethod( 'get' );
126 $htmlForm->prepareForm()->displayForm( false );
127 $this->addHelpLink( 'Help:Linksearch' );
128
129 if ( $target != '' ) {
130 $this->setParams( [
131 'query' => Parser::normalizeLinkUrl( $target2 ),
132 'namespace' => $namespace,
133 'protocol' => $protocol ] );
134 parent::execute( $par );
135 if ( $this->mungedQuery === false ) {
136 $out->addWikiMsg( 'linksearch-error' );
137 }
138 }
139 }
140
141 /**
142 * Disable RSS/Atom feeds
143 * @return bool
144 */
145 function isSyndicated() {
146 return false;
147 }
148
149 /**
150 * Return an appropriately formatted LIKE query and the clause
151 *
152 * @param string $query Search pattern to search for
153 * @param string $prot Protocol, e.g. 'http://'
154 *
155 * @return array
156 */
157 static function mungeQuery( $query, $prot ) {
158 $field = 'el_index';
159 $dbr = wfGetDB( DB_REPLICA );
160
161 if ( $query === '*' && $prot !== '' ) {
162 // Allow queries like 'ftp://*' to find all ftp links
163 $rv = [ $prot, $dbr->anyString() ];
164 } else {
165 $rv = LinkFilter::makeLikeArray( $query, $prot );
166 }
167
168 if ( $rv === false ) {
169 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
170 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
171 if ( preg_match( $pattern, $query ) ) {
172 $rv = [ $prot . rtrim( $query, " \t*" ), $dbr->anyString() ];
173 $field = 'el_to';
174 }
175 }
176
177 return [ $rv, $field ];
178 }
179
180 function linkParameters() {
181 $params = [];
182 $params['target'] = $this->mProt . $this->mQuery;
183 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
184 $params['namespace'] = $this->mNs;
185 }
186
187 return $params;
188 }
189
190 public function getQueryInfo() {
191 $dbr = wfGetDB( DB_REPLICA );
192 // strip everything past first wildcard, so that
193 // index-based-only lookup would be done
194 list( $this->mungedQuery, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
195 if ( $this->mungedQuery === false ) {
196 // Invalid query; return no results
197 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
198 }
199
200 $stripped = LinkFilter::keepOneWildcard( $this->mungedQuery );
201 $like = $dbr->buildLike( $stripped );
202 $retval = [
203 'tables' => [ 'page', 'externallinks' ],
204 'fields' => [
205 'namespace' => 'page_namespace',
206 'title' => 'page_title',
207 'value' => 'el_index',
208 'url' => 'el_to'
209 ],
210 'conds' => [
211 'page_id = el_from',
212 "$clause $like"
213 ],
214 'options' => [ 'USE INDEX' => $clause ]
215 ];
216
217 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
218 $retval['conds']['page_namespace'] = $this->mNs;
219 }
220
221 return $retval;
222 }
223
224 /**
225 * Pre-fill the link cache
226 *
227 * @param IDatabase $db
228 * @param ResultWrapper $res
229 */
230 function preprocessResults( $db, $res ) {
231 $this->executeLBFromResultWrapper( $res );
232 }
233
234 /**
235 * @param Skin $skin
236 * @param object $result Result row
237 * @return string
238 */
239 function formatResult( $skin, $result ) {
240 $title = new TitleValue( (int)$result->namespace, $result->title );
241 $pageLink = $this->getLinkRenderer()->makeLink( $title );
242
243 $url = $result->url;
244 $urlLink = Linker::makeExternalLink( $url, $url );
245
246 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
247 }
248
249 /**
250 * Override to squash the ORDER BY.
251 * We do a truncated index search, so the optimizer won't trust
252 * it as good enough for optimizing sort. The implicit ordering
253 * from the scan will usually do well enough for our needs.
254 * @return array
255 */
256 function getOrderFields() {
257 return [];
258 }
259
260 protected function getGroupName() {
261 return 'redirects';
262 }
263
264 /**
265 * enwiki complained about low limits on this special page
266 *
267 * @see T130058
268 * @todo FIXME This special page should not use LIMIT for paging
269 * @return int
270 */
271 protected function getMaxResults() {
272 return max( parent::getMaxResults(), 60000 );
273 }
274 }