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
21 * @ingroup SpecialPage
22 * @author Brion Vibber
25 use Wikimedia\Rdbms\ResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
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() {
54 public function execute( $par ) {
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' );
66 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
67 if ( $prot !== '//' ) {
68 $protocols_list[] = $prot;
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
80 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
81 $target2 = substr( $target, strlen( $protocol ) );
83 // If it did, let LinkFilter::makeLikeArray() handle this
90 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
91 count( $protocols_list )
99 'label-message' => 'linksearch-pat',
100 'default' => $target,
104 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
107 'type' => 'namespaceselect',
108 'name' => 'namespace',
109 'label-message' => 'linksearch-ns',
110 'default' => $namespace,
113 'cssclass' => 'namespaceselector',
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 != '' ) {
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
145 function isSyndicated() {
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://'
157 static function mungeQuery( $query, $prot ) {
159 $dbr = wfGetDB( DB_REPLICA
);
161 if ( $query === '*' && $prot !== '' ) {
162 // Allow queries like 'ftp://*' to find all ftp links
163 $rv = [ $prot, $dbr->anyString() ];
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() ];
177 return [ $rv, $field ];
180 function linkParameters() {
182 $params['target'] = $this->mProt
. $this->mQuery
;
183 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
184 $params['namespace'] = $this->mNs
;
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 );
203 'tables' => [ 'page', 'externallinks' ],
205 'namespace' => 'page_namespace',
206 'title' => 'page_title',
207 'value' => 'el_index',
214 'options' => [ 'USE INDEX' => $clause ]
217 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
218 $retval['conds']['page_namespace'] = $this->mNs
;
225 * Pre-fill the link cache
227 * @param IDatabase $db
228 * @param ResultWrapper $res
230 function preprocessResults( $db, $res ) {
231 $this->executeLBFromResultWrapper( $res );
236 * @param object $result Result row
239 function formatResult( $skin, $result ) {
240 $title = new TitleValue( (int)$result->namespace, $result->title
);
241 $pageLink = $this->getLinkRenderer()->makeLink( $title );
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.
256 function getOrderFields() {
260 protected function getGroupName() {
265 * enwiki complained about low limits on this special page
268 * @todo FIXME This special page should not use LIMIT for paging
270 protected function getMaxResults() {
271 return max( parent
::getMaxResults(), 60000 );