7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
11 * Special:LinkSearch to search the external-links table.
13 function wfSpecialLinkSearch( $par ) {
15 list( $limit, $offset ) = wfCheckLimits();
16 global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode, $wgLang;
17 $target = $GLOBALS['wgRequest']->getVal( 'target', $par );
18 $namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', null );
20 $protocols_list[] = '';
21 foreach( $wgUrlProtocols as $prot ) {
22 $protocols_list[] = $prot;
27 $pr_sl = strpos($target2, '//' );
28 $pr_cl = strpos($target2, ':' );
30 // For protocols with '//'
31 $protocol = substr( $target2, 0 , $pr_sl+2 );
32 $target2 = substr( $target2, $pr_sl+2 );
33 } elseif ( !$pr_sl && $pr_cl ) {
34 // For protocols without '//' like 'mailto:'
35 $protocol = substr( $target2, 0 , $pr_cl+1 );
36 $target2 = substr( $target2, $pr_cl+1 );
37 } elseif ( $protocol == '' && $target2 != '' ) {
39 $protocol = 'http://';
41 if ( !in_array( $protocol, $protocols_list ) ) {
42 // unsupported protocol, show original search request
47 $self = Title::makeTitle( NS_SPECIAL, 'Linksearch' );
49 $wgOut->addWikiMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols ) . '</nowiki>' );
50 $s = Xml::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
51 Xml::hidden( 'title', $self->getPrefixedDbKey() ) .
53 Xml::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
54 Xml::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
55 if ( !$wgMiserMode ) {
56 $s .= Xml::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
57 XML::namespaceSelector( $namespace, '' );
59 $s .= Xml::submitButton( wfMsg( 'linksearch-ok' ) ) .
61 Xml::closeElement( 'form' );
62 $wgOut->addHTML( $s );
65 $searcher = new LinkSearchPage;
66 $searcher->setParams( array(
68 'namespace' => $namespace,
69 'protocol' => $protocol ) );
70 $searcher->doQuery( $offset, $limit );
74 class LinkSearchPage extends QueryPage {
75 function setParams( $params ) {
76 $this->mQuery = $params['query'];
77 $this->mNs = $params['namespace'];
78 $this->mProt = $params['protocol'];
86 * Disable RSS/Atom feeds
88 function isSyndicated() {
93 * Return an appropriately formatted LIKE query and the clause
95 static function mungeQuery( $query , $prot ) {
97 $rv = LinkFilter::makeLikeArray( $query , $prot );
99 //makeLike doesn't handle wildcard in IP, so we'll have to munge here.
100 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
101 $rv = array( $prot . rtrim($query, " \t*"), $dbr->anyString() );
105 return array( $rv, $field );
108 function linkParameters() {
111 $params['target'] = $this->mProt . $this->mQuery;
112 if( isset( $this->mNs ) && !$wgMiserMode ) {
113 $params['namespace'] = $this->mNs;
120 $dbr = wfGetDB( DB_SLAVE );
121 $page = $dbr->tableName( 'page' );
122 $externallinks = $dbr->tableName( 'externallinks' );
124 /* strip everything past first wildcard, so that index-based-only lookup would be done */
125 list( $munged, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
126 $stripped = LinkFilter::keepOneWildcard( $munged );
127 $like = $dbr->buildLike( $stripped );
130 if ( isset ($this->mNs) && !$wgMiserMode )
131 $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs );
133 $use_index = $dbr->useIndexClause( $clause );
136 page_namespace AS namespace,
142 $externallinks $use_index
149 function formatResult( $skin, $result ) {
150 $title = Title::makeTitle( $result->namespace, $result->title );
152 $pageLink = $skin->linkKnown( $title );
153 $urlLink = $skin->makeExternalLink( $url, $url );
155 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
159 * Override to check query validity.
161 function doQuery( $offset, $limit, $shownavigation=true ) {
163 list( $this->mMungedQuery, $clause ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
164 if( $this->mMungedQuery === false ) {
165 $wgOut->addWikiMsg( 'linksearch-error' );
168 // Generates invalid xhtml with patterns that contain --
169 //$wgOut->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
170 parent::doQuery( $offset, $limit, $shownavigation );
175 * Override to squash the ORDER BY.
176 * We do a truncated index search, so the optimizer won't trust
177 * it as good enough for optimizing sort. The implicit ordering
178 * from the scan will usually do well enough for our needs.
180 function getOrder() {