4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup SpecialPage
24 * @author Brion Vibber
29 * Special:LinkSearch to search the external-links table.
31 function wfSpecialLinkSearch( $par ) {
33 list( $limit, $offset ) = wfCheckLimits();
34 global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode, $wgLang;
35 $target = $GLOBALS['wgRequest']->getVal( 'target', $par );
36 $namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', null );
38 $protocols_list[] = '';
39 foreach( $wgUrlProtocols as $prot ) {
40 $protocols_list[] = $prot;
45 $pr_sl = strpos($target2, '//' );
46 $pr_cl = strpos($target2, ':' );
48 // For protocols with '//'
49 $protocol = substr( $target2, 0 , $pr_sl+
2 );
50 $target2 = substr( $target2, $pr_sl+
2 );
51 } elseif ( !$pr_sl && $pr_cl ) {
52 // For protocols without '//' like 'mailto:'
53 $protocol = substr( $target2, 0 , $pr_cl+
1 );
54 $target2 = substr( $target2, $pr_cl+
1 );
55 } elseif ( $protocol == '' && $target2 != '' ) {
57 $protocol = 'http://';
59 if ( !in_array( $protocol, $protocols_list ) ) {
60 // unsupported protocol, show original search request
65 $self = Title
::makeTitle( NS_SPECIAL
, 'Linksearch' );
67 $wgOut->addWikiMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols ) . '</nowiki>' );
68 $s = Xml
::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
69 Xml
::hidden( 'title', $self->getPrefixedDbKey() ) .
71 Xml
::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
72 Xml
::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
73 if ( !$wgMiserMode ) {
74 $s .= Xml
::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
75 Xml
::namespaceSelector( $namespace, '' );
77 $s .= Xml
::submitButton( wfMsg( 'linksearch-ok' ) ) .
79 Xml
::closeElement( 'form' );
80 $wgOut->addHTML( $s );
83 $searcher = new LinkSearchPage
;
84 $searcher->setParams( array(
86 'namespace' => $namespace,
87 'protocol' => $protocol ) );
88 $searcher->doQuery( $offset, $limit );
92 class LinkSearchPage
extends QueryPage
{
93 function setParams( $params ) {
94 $this->mQuery
= $params['query'];
95 $this->mNs
= $params['namespace'];
96 $this->mProt
= $params['protocol'];
104 * Disable RSS/Atom feeds
106 function isSyndicated() {
111 * Return an appropriately formatted LIKE query and the clause
113 static function mungeQuery( $query , $prot ) {
115 $rv = LinkFilter
::makeLikeArray( $query , $prot );
117 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
118 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
119 $rv = array( $prot . rtrim($query, " \t*"), $dbr->anyString() );
123 return array( $rv, $field );
126 function linkParameters() {
129 $params['target'] = $this->mProt
. $this->mQuery
;
130 if( isset( $this->mNs
) && !$wgMiserMode ) {
131 $params['namespace'] = $this->mNs
;
138 $dbr = wfGetDB( DB_SLAVE
);
139 $page = $dbr->tableName( 'page' );
140 $externallinks = $dbr->tableName( 'externallinks' );
142 /* strip everything past first wildcard, so that index-based-only lookup would be done */
143 list( $munged, $clause ) = self
::mungeQuery( $this->mQuery
, $this->mProt
);
144 $stripped = LinkFilter
::keepOneWildcard( $munged );
145 $like = $dbr->buildLike( $stripped );
148 if ( isset ($this->mNs
) && !$wgMiserMode )
149 $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs
);
151 $use_index = $dbr->useIndexClause( $clause );
154 page_namespace AS namespace,
160 $externallinks $use_index
167 function formatResult( $skin, $result ) {
168 $title = Title
::makeTitle( $result->namespace, $result->title
);
170 $pageLink = $skin->linkKnown( $title );
171 $urlLink = $skin->makeExternalLink( $url, $url );
173 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
177 * Override to check query validity.
179 function doQuery( $offset, $limit, $shownavigation=true ) {
181 list( $this->mMungedQuery
, $clause ) = LinkSearchPage
::mungeQuery( $this->mQuery
, $this->mProt
);
182 if( $this->mMungedQuery
=== false ) {
183 $wgOut->addWikiMsg( 'linksearch-error' );
186 // Generates invalid xhtml with patterns that contain --
187 //$wgOut->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
188 parent
::doQuery( $offset, $limit, $shownavigation );
193 * Override to squash the ORDER BY.
194 * We do a truncated index search, so the optimizer won't trust
195 * it as good enough for optimizing sort. The implicit ordering
196 * from the scan will usually do well enough for our needs.
198 function getOrder() {