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
26 * Special:LinkSearch to search the external-links table.
27 * @ingroup SpecialPage
29 class LinkSearchPage
extends QueryPage
{
30 /** @var array|bool */
31 private $mungedQuery = false;
33 function setParams( $params ) {
34 $this->mQuery
= $params['query'];
35 $this->mNs
= $params['namespace'];
36 $this->mProt
= $params['protocol'];
39 function __construct( $name = 'LinkSearch' ) {
40 parent
::__construct( $name );
42 // Since we don't control the constructor parameters, we can't inject services that way.
43 // Instead, we initialize services in the execute() method, and allow them to be overridden
44 // using the setServices() method.
47 function isCacheable() {
51 public function execute( $par ) {
53 $this->outputHeader();
55 $out = $this->getOutput();
56 $out->allowClickjacking();
58 $request = $this->getRequest();
59 $target = $request->getVal( 'target', $par );
60 $namespace = $request->getIntOrNull( 'namespace' );
63 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
64 if ( $prot !== '//' ) {
65 $protocols_list[] = $prot;
70 // Get protocol, default is http://
71 $protocol = 'http://';
72 $bits = wfParseUrl( $target );
73 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
74 $protocol = $bits['scheme'] . $bits['delimiter'];
75 // Make sure wfParseUrl() didn't make some well-intended correction in the
77 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
78 $target2 = substr( $target, strlen( $protocol ) );
80 // If it did, let LinkFilter::makeLikeArray() handle this
87 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
88 count( $protocols_list )
96 'label-message' => 'linksearch-pat',
101 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
104 'type' => 'namespaceselect',
105 'name' => 'namespace',
106 'label-message' => 'linksearch-ns',
107 'default' => $namespace,
110 'cssclass' => 'namespaceselector',
115 'title' => $this->getPageTitle()->getPrefixedDBkey(),
117 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
118 $htmlForm->addHiddenFields( $hiddenFields );
119 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
120 $htmlForm->setWrapperLegendMsg( 'linksearch' );
121 $htmlForm->setAction( wfScript() );
122 $htmlForm->setMethod( 'get' );
123 $htmlForm->prepareForm()->displayForm( false );
124 $this->addHelpLink( 'Help:Linksearch' );
126 if ( $target != '' ) {
128 'query' => Parser
::normalizeLinkUrl( $target2 ),
129 'namespace' => $namespace,
130 'protocol' => $protocol ] );
131 parent
::execute( $par );
132 if ( $this->mungedQuery
=== false ) {
133 $out->addWikiMsg( 'linksearch-error' );
139 * Disable RSS/Atom feeds
142 function isSyndicated() {
147 * Return an appropriately formatted LIKE query and the clause
149 * @param string $query Search pattern to search for
150 * @param string $prot Protocol, e.g. 'http://'
154 static function mungeQuery( $query, $prot ) {
156 $dbr = wfGetDB( DB_REPLICA
);
158 if ( $query === '*' && $prot !== '' ) {
159 // Allow queries like 'ftp://*' to find all ftp links
160 $rv = [ $prot, $dbr->anyString() ];
162 $rv = LinkFilter
::makeLikeArray( $query, $prot );
165 if ( $rv === false ) {
166 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
167 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
168 if ( preg_match( $pattern, $query ) ) {
169 $rv = [ $prot . rtrim( $query, " \t*" ), $dbr->anyString() ];
174 return [ $rv, $field ];
177 function linkParameters() {
179 $params['target'] = $this->mProt
. $this->mQuery
;
180 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
181 $params['namespace'] = $this->mNs
;
187 public function getQueryInfo() {
188 $dbr = wfGetDB( DB_REPLICA
);
189 // strip everything past first wildcard, so that
190 // index-based-only lookup would be done
191 list( $this->mungedQuery
, $clause ) = self
::mungeQuery( $this->mQuery
, $this->mProt
);
192 if ( $this->mungedQuery
=== false ) {
193 // Invalid query; return no results
194 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
197 $stripped = LinkFilter
::keepOneWildcard( $this->mungedQuery
);
198 $like = $dbr->buildLike( $stripped );
200 'tables' => [ 'page', 'externallinks' ],
202 'namespace' => 'page_namespace',
203 'title' => 'page_title',
204 'value' => 'el_index',
211 'options' => [ 'USE INDEX' => $clause ]
214 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
215 $retval['conds']['page_namespace'] = $this->mNs
;
222 * Pre-fill the link cache
224 * @param IDatabase $db
225 * @param ResultWrapper $res
227 function preprocessResults( $db, $res ) {
228 $this->executeLBFromResultWrapper( $res );
233 * @param object $result Result row
236 function formatResult( $skin, $result ) {
237 $title = new TitleValue( (int)$result->namespace, $result->title
);
238 $pageLink = $this->getLinkRenderer()->makeLink( $title );
241 $urlLink = Linker
::makeExternalLink( $url, $url );
243 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
247 * Override to squash the ORDER BY.
248 * We do a truncated index search, so the optimizer won't trust
249 * it as good enough for optimizing sort. The implicit ordering
250 * from the scan will usually do well enough for our needs.
253 function getOrderFields() {
257 protected function getGroupName() {
262 * enwiki complained about low limits on this special page
265 * @todo FIXME This special page should not use LIMIT for paging
267 protected function getMaxResults() {
268 return max( parent
::getMaxResults(), 60000 );