Split out some internal methods in QuorumLockManager for readability
[mediawiki.git] / includes / specials / SpecialLinkSearch.php
blobdae60744dce9ce941af3d126e7ff10a6b3119bf9
1 <?php
2 /**
3 * Implements Special:LinkSearch
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
20 * @file
21 * @ingroup SpecialPage
22 * @author Brion Vibber
25 use Wikimedia\Rdbms\ResultWrapper;
26 use Wikimedia\Rdbms\IDatabase;
28 /**
29 * Special:LinkSearch to search the external-links table.
30 * @ingroup SpecialPage
32 class LinkSearchPage extends QueryPage {
33 /** @var array|bool */
34 private $mungedQuery = false;
36 function setParams( $params ) {
37 $this->mQuery = $params['query'];
38 $this->mNs = $params['namespace'];
39 $this->mProt = $params['protocol'];
42 function __construct( $name = 'LinkSearch' ) {
43 parent::__construct( $name );
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.
50 function isCacheable() {
51 return false;
54 public function execute( $par ) {
55 $this->setHeaders();
56 $this->outputHeader();
58 $out = $this->getOutput();
59 $out->allowClickjacking();
61 $request = $this->getRequest();
62 $target = $request->getVal( 'target', $par );
63 $namespace = $request->getIntOrNull( 'namespace' );
65 $protocols_list = [];
66 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
67 if ( $prot !== '//' ) {
68 $protocols_list[] = $prot;
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 = '';
88 $out->addWikiMsg(
89 'linksearch-text',
90 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
91 count( $protocols_list )
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',
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',
117 $hiddenFields = [
118 'title' => $this->getPageTitle()->getPrefixedDBkey(),
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' );
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' );
142 * Disable RSS/Atom feeds
143 * @return bool
145 function isSyndicated() {
146 return false;
150 * Return an appropriately formatted LIKE query and the clause
152 * @param string $query Search pattern to search for
153 * @param string $prot Protocol, e.g. 'http://'
155 * @return array
157 static function mungeQuery( $query, $prot ) {
158 $field = 'el_index';
159 $dbr = wfGetDB( DB_REPLICA );
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 );
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';
177 return [ $rv, $field ];
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;
187 return $params;
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' ];
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'
210 'conds' => [
211 'page_id = el_from',
212 "$clause $like"
214 'options' => [ 'USE INDEX' => $clause ]
217 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
218 $retval['conds']['page_namespace'] = $this->mNs;
221 return $retval;
225 * Pre-fill the link cache
227 * @param IDatabase $db
228 * @param ResultWrapper $res
230 function preprocessResults( $db, $res ) {
231 $this->executeLBFromResultWrapper( $res );
235 * @param Skin $skin
236 * @param object $result Result row
237 * @return string
239 function formatResult( $skin, $result ) {
240 $title = new TitleValue( (int)$result->namespace, $result->title );
241 $pageLink = $this->getLinkRenderer()->makeLink( $title );
243 $url = $result->url;
244 $urlLink = Linker::makeExternalLink( $url, $url );
246 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
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
256 function getOrderFields() {
257 return [];
260 protected function getGroupName() {
261 return 'redirects';
265 * enwiki complained about low limits on this special page
267 * @see T130058
268 * @todo FIXME This special page should not use LIMIT for paging
270 protected function getMaxResults() {
271 return max( parent::getMaxResults(), 60000 );