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 isCacheable() {
45 function execute( $par ) {
46 global $wgUrlProtocols, $wgMiserMode;
49 $this->outputHeader();
51 $out = $this->getOutput();
52 $out->allowClickjacking();
54 $request = $this->getRequest();
55 $target = $request->getVal( 'target', $par );
56 $namespace = $request->getIntorNull( 'namespace', null );
58 $protocols_list = array();
59 foreach( $wgUrlProtocols as $prot ) {
60 if ( $prot !== '//' ) {
61 $protocols_list[] = $prot;
67 $pr_sl = strpos($target2, '//' );
68 $pr_cl = strpos($target2, ':' );
70 // For protocols with '//'
71 $protocol = substr( $target2, 0 , $pr_sl+
2 );
72 $target2 = substr( $target2, $pr_sl+
2 );
73 } elseif ( !$pr_sl && $pr_cl ) {
74 // For protocols without '//' like 'mailto:'
75 $protocol = substr( $target2, 0 , $pr_cl+
1 );
76 $target2 = substr( $target2, $pr_cl+
1 );
77 } elseif ( $protocol == '' && $target2 != '' ) {
79 $protocol = 'http://';
81 if ( $protocol != '' && !in_array( $protocol, $protocols_list ) ) {
82 // unsupported protocol, show original search request
87 $out->addWikiMsg( 'linksearch-text', '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>' );
88 $s = Xml
::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
89 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
91 Xml
::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
92 Xml
::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
93 if ( !$wgMiserMode ) {
94 $s .= Xml
::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
95 Xml
::namespaceSelector( $namespace, '' );
97 $s .= Xml
::submitButton( wfMsg( 'linksearch-ok' ) ) .
99 Xml
::closeElement( 'form' );
102 if( $target != '' ) {
103 $this->setParams( array(
105 'namespace' => $namespace,
106 'protocol' => $protocol ) );
107 parent
::execute( $par );
108 if( $this->mMungedQuery
=== false )
109 $out->addWikiMsg( 'linksearch-error' );
114 * Disable RSS/Atom feeds
117 function isSyndicated() {
122 * Return an appropriately formatted LIKE query and the clause
126 static function mungeQuery( $query, $prot ) {
128 $rv = LinkFilter
::makeLikeArray( $query , $prot );
129 if ( $rv === false ) {
130 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
131 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
132 $dbr = wfGetDB( DB_SLAVE
);
133 $rv = array( $prot . rtrim( $query, " \t*" ), $dbr->anyString() );
137 return array( $rv, $field );
140 function linkParameters() {
143 $params['target'] = $this->mProt
. $this->mQuery
;
144 if( isset( $this->mNs
) && !$wgMiserMode ) {
145 $params['namespace'] = $this->mNs
;
150 function getQueryInfo() {
152 $dbr = wfGetDB( DB_SLAVE
);
153 // strip everything past first wildcard, so that
154 // index-based-only lookup would be done
155 list( $this->mMungedQuery
, $clause ) = self
::mungeQuery(
156 $this->mQuery
, $this->mProt
);
157 if( $this->mMungedQuery
=== false )
158 // Invalid query; return no results
159 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
161 $stripped = LinkFilter
::keepOneWildcard( $this->mMungedQuery
);
162 $like = $dbr->buildLike( $stripped );
164 'tables' => array ( 'page', 'externallinks' ),
165 'fields' => array ( 'page_namespace AS namespace',
166 'page_title AS title',
167 'el_index AS value', 'el_to AS url' ),
168 'conds' => array ( 'page_id = el_from',
170 'options' => array( 'USE INDEX' => $clause )
172 if ( isset( $this->mNs
) && !$wgMiserMode ) {
173 $retval['conds']['page_namespace'] = $this->mNs
;
178 function formatResult( $skin, $result ) {
179 $title = Title
::makeTitle( $result->namespace, $result->title
);
181 $pageLink = Linker
::linkKnown( $title );
182 $urlLink = Linker
::makeExternalLink( $url, $url );
184 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
188 * Override to check query validity.
190 function doQuery( $offset = false, $limit = false ) {
191 list( $this->mMungedQuery
, ) = LinkSearchPage
::mungeQuery( $this->mQuery
, $this->mProt
);
192 if( $this->mMungedQuery
=== false ) {
193 $this->getOutput()->addWikiMsg( 'linksearch-error' );
196 // Generates invalid xhtml with patterns that contain --
197 //$this->getOutput()->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
198 parent
::doQuery( $offset, $limit );
203 * Override to squash the ORDER BY.
204 * We do a truncated index search, so the optimizer won't trust
205 * it as good enough for optimizing sort. The implicit ordering
206 * from the scan will usually do well enough for our needs.
209 function getOrderFields() {