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.
29 function wfSpecialLinkSearch( $par ) {
31 list( $limit, $offset ) = wfCheckLimits();
32 global $wgOut, $wgUrlProtocols, $wgMiserMode, $wgLang;
33 $target = $GLOBALS['wgRequest']->getVal( 'target', $par );
34 $namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', null );
36 $protocols_list[] = '';
37 foreach( $wgUrlProtocols as $prot ) {
38 $protocols_list[] = $prot;
43 $pr_sl = strpos($target2, '//' );
44 $pr_cl = strpos($target2, ':' );
46 // For protocols with '//'
47 $protocol = substr( $target2, 0 , $pr_sl+
2 );
48 $target2 = substr( $target2, $pr_sl+
2 );
49 } elseif ( !$pr_sl && $pr_cl ) {
50 // For protocols without '//' like 'mailto:'
51 $protocol = substr( $target2, 0 , $pr_cl+
1 );
52 $target2 = substr( $target2, $pr_cl+
1 );
53 } elseif ( $protocol == '' && $target2 != '' ) {
55 $protocol = 'http://';
57 if ( !in_array( $protocol, $protocols_list ) ) {
58 // unsupported protocol, show original search request
63 $self = Title
::makeTitle( NS_SPECIAL
, 'Linksearch' );
65 $wgOut->addWikiMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols ) . '</nowiki>' );
66 $s = Xml
::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
67 Xml
::hidden( 'title', $self->getPrefixedDbKey() ) .
69 Xml
::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
70 Xml
::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
71 if ( !$wgMiserMode ) {
72 $s .= Xml
::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
73 Xml
::namespaceSelector( $namespace, '' );
75 $s .= Xml
::submitButton( wfMsg( 'linksearch-ok' ) ) .
77 Xml
::closeElement( 'form' );
78 $wgOut->addHTML( $s );
81 $searcher = new LinkSearchPage
;
82 $searcher->setParams( array(
84 'namespace' => $namespace,
85 'protocol' => $protocol ) );
86 $searcher->doQuery( $offset, $limit );
91 * @ingroup SpecialPage
93 class LinkSearchPage
extends QueryPage
{
94 function setParams( $params ) {
95 $this->mQuery
= $params['query'];
96 $this->mNs
= $params['namespace'];
97 $this->mProt
= $params['protocol'];
105 * Disable RSS/Atom feeds
107 function isSyndicated() {
112 * Return an appropriately formatted LIKE query and the clause
114 static function mungeQuery( $query , $prot ) {
116 $rv = LinkFilter
::makeLikeArray( $query , $prot );
118 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
119 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
120 $rv = array( $prot . rtrim($query, " \t*"), $dbr->anyString() );
124 return array( $rv, $field );
127 function linkParameters() {
130 $params['target'] = $this->mProt
. $this->mQuery
;
131 if( isset( $this->mNs
) && !$wgMiserMode ) {
132 $params['namespace'] = $this->mNs
;
139 $dbr = wfGetDB( DB_SLAVE
);
140 $page = $dbr->tableName( 'page' );
141 $externallinks = $dbr->tableName( 'externallinks' );
143 /* strip everything past first wildcard, so that index-based-only lookup would be done */
144 list( $munged, $clause ) = self
::mungeQuery( $this->mQuery
, $this->mProt
);
145 $stripped = LinkFilter
::keepOneWildcard( $munged );
146 $like = $dbr->buildLike( $stripped );
149 if ( isset ($this->mNs
) && !$wgMiserMode )
150 $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs
);
152 $use_index = $dbr->useIndexClause( $clause );
155 page_namespace AS namespace,
161 $externallinks $use_index
168 function formatResult( $skin, $result ) {
169 $title = Title
::makeTitle( $result->namespace, $result->title
);
171 $pageLink = $skin->linkKnown( $title );
172 $urlLink = $skin->makeExternalLink( $url, $url );
174 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
178 * Override to check query validity.
180 function doQuery( $offset, $limit, $shownavigation=true ) {
182 list( $this->mMungedQuery
, $clause ) = LinkSearchPage
::mungeQuery( $this->mQuery
, $this->mProt
);
183 if( $this->mMungedQuery
=== false ) {
184 $wgOut->addWikiMsg( 'linksearch-error' );
187 // Generates invalid xhtml with patterns that contain --
188 //$wgOut->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
189 parent
::doQuery( $offset, $limit, $shownavigation );
194 * Override to squash the ORDER BY.
195 * We do a truncated index search, so the optimizer won't trust
196 * it as good enough for optimizing sort. The implicit ordering
197 * from the scan will usually do well enough for our needs.
199 function getOrder() {