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
27 * Special:LinkSearch to search the external-links table.
28 * @ingroup SpecialPage
30 class LinkSearchPage
extends QueryPage
{
31 function setParams( $params ) {
32 $this->mQuery
= $params['query'];
33 $this->mNs
= $params['namespace'];
34 $this->mProt
= $params['protocol'];
37 function __construct( $name = 'LinkSearch' ) {
38 parent
::__construct( $name );
41 function execute( $par ) {
42 global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode, $wgLang, $wgScript;
43 $target = $wgRequest->getVal( 'target', $par );
44 $namespace = $wgRequest->getIntorNull( 'namespace', null );
46 $protocols_list[] = '';
47 foreach( $wgUrlProtocols as $prot ) {
48 $protocols_list[] = $prot;
53 $pr_sl = strpos($target2, '//' );
54 $pr_cl = strpos($target2, ':' );
56 // For protocols with '//'
57 $protocol = substr( $target2, 0 , $pr_sl+
2 );
58 $target2 = substr( $target2, $pr_sl+
2 );
59 } elseif ( !$pr_sl && $pr_cl ) {
60 // For protocols without '//' like 'mailto:'
61 $protocol = substr( $target2, 0 , $pr_cl+
1 );
62 $target2 = substr( $target2, $pr_cl+
1 );
63 } elseif ( $protocol == '' && $target2 != '' ) {
65 $protocol = 'http://';
67 if ( !in_array( $protocol, $protocols_list ) ) {
68 // unsupported protocol, show original search request
73 $self = $this->getTitle();
75 $wgOut->addWikiMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols ) . '</nowiki>' );
76 $s = Xml
::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
77 Html
::hidden( 'title', $self->getPrefixedDbKey() ) .
79 Xml
::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
80 Xml
::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
81 if ( !$wgMiserMode ) {
82 $s .= Xml
::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
83 Xml
::namespaceSelector( $namespace, '' );
85 $s .= Xml
::submitButton( wfMsg( 'linksearch-ok' ) ) .
87 Xml
::closeElement( 'form' );
88 $wgOut->addHTML( $s );
91 $this->setParams( array(
93 'namespace' => $namespace,
94 'protocol' => $protocol ) );
95 parent
::execute( $par );
96 if( $this->mMungedQuery
=== false )
97 $wgOut->addWikiText( wfMsg( 'linksearch-error' ) );
102 * Disable RSS/Atom feeds
104 function isSyndicated() {
109 * Return an appropriately formatted LIKE query and the clause
111 static function mungeQuery( $query , $prot ) {
113 $rv = LinkFilter
::makeLike( $query , $prot );
114 if ( $rv === false ) {
115 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
116 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
117 $rv = $prot . rtrim($query, " \t*") . '%';
121 return array( $rv, $field );
124 function linkParameters() {
127 $params['target'] = $this->mProt
. $this->mQuery
;
128 if( isset( $this->mNs
) && !$wgMiserMode ) {
129 $params['namespace'] = $this->mNs
;
134 function getQueryInfo() {
136 $dbr = wfGetDB( DB_SLAVE
);
137 // strip everything past first wildcard, so that
138 // index-based-only lookup would be done
139 list( $this->mMungedQuery
, $clause ) = self
::mungeQuery(
140 $this->mQuery
, $this->mProt
);
141 if( $this->mMungedQuery
=== false )
142 // Invalid query; return no results
143 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
145 $stripped = substr( $this->mMungedQuery
, 0, strpos( $this->mMungedQuery
, '%' ) +
1 );
146 $encSearch = $dbr->addQuotes( $stripped );
148 'tables' => array ( 'page', 'externallinks' ),
149 'fields' => array ( 'page_namespace AS namespace',
150 'page_title AS title',
151 'el_index AS value', 'el_to AS url' ),
152 'conds' => array ( 'page_id = el_from',
153 "$clause LIKE $encSearch" ),
154 'options' => array( 'USE INDEX' => $clause )
156 if ( isset( $this->mNs
) && !$wgMiserMode ) {
157 $retval['conds']['page_namespace'] = $this->mNs
;
162 function formatResult( $skin, $result ) {
163 $title = Title
::makeTitle( $result->namespace, $result->title
);
165 $pageLink = $skin->linkKnown( $title );
166 $urlLink = $skin->makeExternalLink( $url, $url );
168 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
172 * Override to check query validity.
174 function doQuery( $offset, $limit, $shownavigation=true ) {
176 list( $this->mMungedQuery
, ) = LinkSearchPage
::mungeQuery( $this->mQuery
, $this->mProt
);
177 if( $this->mMungedQuery
=== false ) {
178 $wgOut->addWikiMsg( 'linksearch-error' );
181 // Generates invalid xhtml with patterns that contain --
182 //$wgOut->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
183 parent
::doQuery( $offset, $limit, $shownavigation );
188 * Override to squash the ORDER BY.
189 * We do a truncated index search, so the optimizer won't trust
190 * it as good enough for optimizing sort. The implicit ordering
191 * from the scan will usually do well enough for our needs.
193 function getOrderFields() {