summaryrefslogtreecommitdiff
path: root/includes/specials/SpecialLinkSearch.php
blob: 70b2257a09428812d47e3abac6486ca472c8940d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
 * @file
 * @ingroup SpecialPage
 *
 * @author Brion Vibber
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */

/**
 * Special:LinkSearch to search the external-links table.
 */
function wfSpecialLinkSearch( $par ) {

	list( $limit, $offset ) = wfCheckLimits();
	global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode, $wgLang;
	$target = $GLOBALS['wgRequest']->getVal( 'target', $par );
	$namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', null );

	$protocols_list[] = '';
	foreach( $wgUrlProtocols as $prot ) {
		$protocols_list[] = $prot;
	}

	$target2 = $target;
	$protocol = '';
	$pr_sl = strpos($target2, '//' );
	$pr_cl = strpos($target2, ':' );
	if ( $pr_sl ) {
		// For protocols with '//'
		$protocol = substr( $target2, 0 , $pr_sl+2 );
		$target2 = substr( $target2, $pr_sl+2 );
	} elseif ( !$pr_sl && $pr_cl ) {
		// For protocols without '//' like 'mailto:'
		$protocol = substr( $target2, 0 , $pr_cl+1 );
		$target2 = substr( $target2, $pr_cl+1 );
	} elseif ( $protocol == '' && $target2 != '' ) {
		// default
		$protocol = 'http://';
	}
	if ( !in_array( $protocol, $protocols_list ) ) {
		// unsupported protocol, show original search request
		$target2 = $target;
		$protocol = '';
	}

	$wgOut->allowClickjacking();

	$self = Title::makeTitle( NS_SPECIAL, 'Linksearch' );
	
	$wgOut->addWikiMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols ) . '</nowiki>' );
	$s =	Xml::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
		Xml::hidden( 'title', $self->getPrefixedDbKey() ) .
		'<fieldset>' .
		Xml::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
		Xml::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
	if ( !$wgMiserMode ) {
		$s .= Xml::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
			XML::namespaceSelector( $namespace, '' );
	}
	$s .=	Xml::submitButton( wfMsg( 'linksearch-ok' ) ) .
		'</fieldset>' .
		Xml::closeElement( 'form' );
	$wgOut->addHTML( $s );

	if( $target != '' ) {
		$searcher = new LinkSearchPage;
		$searcher->setParams( array( 
			'query' => $target2, 
			'namespace' => $namespace, 
			'protocol' => $protocol ) );
		$searcher->doQuery( $offset, $limit );
	}
}

class LinkSearchPage extends QueryPage {
	function setParams( $params ) {
		$this->mQuery = $params['query'];
		$this->mNs = $params['namespace'];
		$this->mProt = $params['protocol'];
	}

	function getName() {
		return 'LinkSearch';
	}

	/**
	 * Disable RSS/Atom feeds
	 */
	function isSyndicated() {
		return false;
	}

	/**
	 * Return an appropriately formatted LIKE query and the clause
	 */
	static function mungeQuery( $query , $prot ) {
		$field = 'el_index';
		$rv = LinkFilter::makeLikeArray( $query , $prot );
		if ($rv === false) {
			//makeLike doesn't handle wildcard in IP, so we'll have to munge here.
			if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
				$rv = array( $prot . rtrim($query, " \t*"), $dbr->anyString() );
				$field = 'el_to';
			}
		}
		return array( $rv, $field );
	}

	function linkParameters() {
		global $wgMiserMode;
		$params = array();
		$params['target'] = $this->mProt . $this->mQuery;
		if( isset( $this->mNs ) && !$wgMiserMode ) {
			$params['namespace'] = $this->mNs;
		}
		return $params;
	}

	function getSQL() {
		global $wgMiserMode;
		$dbr = wfGetDB( DB_SLAVE );
		$page = $dbr->tableName( 'page' );
		$externallinks = $dbr->tableName( 'externallinks' );

		/* strip everything past first wildcard, so that index-based-only lookup would be done */
		list( $munged, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
		$stripped = LinkFilter::keepOneWildcard( $munged );
		$like = $dbr->buildLike( $stripped );

		$encSQL = '';
		if ( isset ($this->mNs) && !$wgMiserMode )
			$encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs );

		$use_index = $dbr->useIndexClause( $clause );
		return
			"SELECT
				page_namespace AS namespace,
				page_title AS title,
				el_index AS value,
				el_to AS url
			FROM
				$page,
				$externallinks $use_index
			WHERE
				page_id=el_from
				AND $clause $like
				$encSQL";
	}

	function formatResult( $skin, $result ) {
		$title = Title::makeTitle( $result->namespace, $result->title );
		$url = $result->url;
		$pageLink = $skin->linkKnown( $title );
		$urlLink = $skin->makeExternalLink( $url, $url );

		return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
	}

	/**
	 * Override to check query validity.
	 */
	function doQuery( $offset, $limit, $shownavigation=true ) {
		global $wgOut;
		list( $this->mMungedQuery, $clause ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
		if( $this->mMungedQuery === false ) {
			$wgOut->addWikiMsg( 'linksearch-error' );
		} else {
			// For debugging
			// Generates invalid xhtml with patterns that contain --
			//$wgOut->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
			parent::doQuery( $offset, $limit, $shownavigation );
		}
	}

	/**
	 * Override to squash the ORDER BY.
	 * We do a truncated index search, so the optimizer won't trust
	 * it as good enough for optimizing sort. The implicit ordering
	 * from the scan will usually do well enough for our needs.
	 */
	function getOrder() {
		return '';
	}
}