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
;
28 * Special:LinkSearch to search the external-links table.
29 * @ingroup SpecialPage
31 class LinkSearchPage
extends QueryPage
{
32 /** @var array|bool */
33 private $mungedQuery = false;
35 function setParams( $params ) {
36 $this->mQuery
= $params['query'];
37 $this->mNs
= $params['namespace'];
38 $this->mProt
= $params['protocol'];
41 function __construct( $name = 'LinkSearch' ) {
42 parent
::__construct( $name );
44 // Since we don't control the constructor parameters, we can't inject services that way.
45 // Instead, we initialize services in the execute() method, and allow them to be overridden
46 // using the setServices() method.
49 function isCacheable() {
53 public function execute( $par ) {
55 $this->outputHeader();
57 $out = $this->getOutput();
58 $out->allowClickjacking();
60 $request = $this->getRequest();
61 $target = $request->getVal( 'target', $par );
62 $namespace = $request->getIntOrNull( 'namespace' );
65 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
66 if ( $prot !== '//' ) {
67 $protocols_list[] = $prot;
72 // Get protocol, default is http://
73 $protocol = 'http://';
74 $bits = wfParseUrl( $target );
75 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
76 $protocol = $bits['scheme'] . $bits['delimiter'];
77 // Make sure wfParseUrl() didn't make some well-intended correction in the
79 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
80 $target2 = substr( $target, strlen( $protocol ) );
82 // If it did, let LinkFilter::makeLikeArray() handle this
89 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
90 count( $protocols_list )
98 'label-message' => 'linksearch-pat',
103 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
106 'type' => 'namespaceselect',
107 'name' => 'namespace',
108 'label-message' => 'linksearch-ns',
109 'default' => $namespace,
112 'cssclass' => 'namespaceselector',
117 'title' => $this->getPageTitle()->getPrefixedDBkey(),
119 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
120 $htmlForm->addHiddenFields( $hiddenFields );
121 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
122 $htmlForm->setWrapperLegendMsg( 'linksearch' );
123 $htmlForm->setAction( wfScript() );
124 $htmlForm->setMethod( 'get' );
125 $htmlForm->prepareForm()->displayForm( false );
126 $this->addHelpLink( 'Help:Linksearch' );
128 if ( $target != '' ) {
130 'query' => Parser
::normalizeLinkUrl( $target2 ),
131 'namespace' => $namespace,
132 'protocol' => $protocol ] );
133 parent
::execute( $par );
134 if ( $this->mungedQuery
=== false ) {
135 $out->addWikiMsg( 'linksearch-error' );
141 * Disable RSS/Atom feeds
144 function isSyndicated() {
149 * Return an appropriately formatted LIKE query and the clause
151 * @param string $query Search pattern to search for
152 * @param string $prot Protocol, e.g. 'http://'
156 static function mungeQuery( $query, $prot ) {
158 $dbr = wfGetDB( DB_REPLICA
);
160 if ( $query === '*' && $prot !== '' ) {
161 // Allow queries like 'ftp://*' to find all ftp links
162 $rv = [ $prot, $dbr->anyString() ];
164 $rv = LinkFilter
::makeLikeArray( $query, $prot );
167 if ( $rv === false ) {
168 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
169 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
170 if ( preg_match( $pattern, $query ) ) {
171 $rv = [ $prot . rtrim( $query, " \t*" ), $dbr->anyString() ];
176 return [ $rv, $field ];
179 function linkParameters() {
181 $params['target'] = $this->mProt
. $this->mQuery
;
182 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
183 $params['namespace'] = $this->mNs
;
189 public function getQueryInfo() {
190 $dbr = wfGetDB( DB_REPLICA
);
191 // strip everything past first wildcard, so that
192 // index-based-only lookup would be done
193 list( $this->mungedQuery
, $clause ) = self
::mungeQuery( $this->mQuery
, $this->mProt
);
194 if ( $this->mungedQuery
=== false ) {
195 // Invalid query; return no results
196 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
199 $stripped = LinkFilter
::keepOneWildcard( $this->mungedQuery
);
200 $like = $dbr->buildLike( $stripped );
202 'tables' => [ 'page', 'externallinks' ],
204 'namespace' => 'page_namespace',
205 'title' => 'page_title',
206 'value' => 'el_index',
213 'options' => [ 'USE INDEX' => $clause ]
216 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
217 $retval['conds']['page_namespace'] = $this->mNs
;
224 * Pre-fill the link cache
226 * @param IDatabase $db
227 * @param ResultWrapper $res
229 function preprocessResults( $db, $res ) {
230 $this->executeLBFromResultWrapper( $res );
235 * @param object $result Result row
238 function formatResult( $skin, $result ) {
239 $title = new TitleValue( (int)$result->namespace, $result->title
);
240 $pageLink = $this->getLinkRenderer()->makeLink( $title );
243 $urlLink = Linker
::makeExternalLink( $url, $url );
245 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
249 * Override to squash the ORDER BY.
250 * We do a truncated index search, so the optimizer won't trust
251 * it as good enough for optimizing sort. The implicit ordering
252 * from the scan will usually do well enough for our needs.
255 function getOrderFields() {
259 protected function getGroupName() {
264 * enwiki complained about low limits on this special page
267 * @todo FIXME This special page should not use LIMIT for paging
269 protected function getMaxResults() {
270 return max( parent
::getMaxResults(), 60000 );