Revert r40627, r40551, r40536, r40535 (mark non-autoconfirmed users in RC and watchli...
[lhc/web/wiklou.git] / includes / specials / SpecialLinkSearch.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 *
6 * @author Brion Vibber
7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 */
9
10 /**
11 * Special:LinkSearch to search the external-links table.
12 * @ingroup SpecialPage
13 */
14
15 function wfSpecialLinkSearch( $par ) {
16
17 list( $limit, $offset ) = wfCheckLimits();
18 global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode;
19 $target = $GLOBALS['wgRequest']->getVal( 'target', $par );
20 $namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', null );
21
22 $protocols_list[] = '';
23 foreach( $wgUrlProtocols as $prot ) {
24 $protocols_list[] = $prot;
25 }
26
27 $target2 = $target;
28 $protocol = '';
29 $pr_sl = strpos($target2, '//' );
30 $pr_cl = strpos($target2, ':' );
31 if ( $pr_sl ) {
32 // For protocols with '//'
33 $protocol = substr( $target2, 0 , $pr_sl+2 );
34 $target2 = substr( $target2, $pr_sl+2 );
35 } elseif ( !$pr_sl && $pr_cl ) {
36 // For protocols without '//' like 'mailto:'
37 $protocol = substr( $target2, 0 , $pr_cl+1 );
38 $target2 = substr( $target2, $pr_cl+1 );
39 } elseif ( $protocol == '' && $target2 != '' ) {
40 // default
41 $protocol = 'http://';
42 }
43 if ( !in_array( $protocol, $protocols_list ) ) {
44 // unsupported protocol, show original search request
45 $target2 = $target;
46 $protocol = '';
47 }
48
49 $self = Title::makeTitle( NS_SPECIAL, 'Linksearch' );
50
51 $wgOut->addWikiText( wfMsg( 'linksearch-text', '<nowiki>' . implode( ', ', $wgUrlProtocols) . '</nowiki>' ) );
52 $s = Xml::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
53 Xml::hidden( 'title', $self->getPrefixedDbKey() ) .
54 '<fieldset>' .
55 Xml::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
56 Xml::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
57 if ( !$wgMiserMode ) {
58 $s .= Xml::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
59 XML::namespaceSelector( $namespace, '' );
60 }
61 $s .= Xml::submitButton( wfMsg( 'linksearch-ok' ) ) .
62 '</fieldset>' .
63 Xml::closeElement( 'form' );
64 $wgOut->addHtml( $s );
65
66 if( $target != '' ) {
67 $searcher = new LinkSearchPage( $target2, $namespace, $protocol );
68 $searcher->doQuery( $offset, $limit );
69 }
70 }
71
72 class LinkSearchPage extends QueryPage {
73
74 function __construct( $query, $ns, $prot ) {
75 $this->mQuery = $query;
76 $this->mNs = $ns;
77 $this->mProt = $prot;
78 }
79
80 function getName() {
81 return 'LinkSearch';
82 }
83
84 /**
85 * Disable RSS/Atom feeds
86 */
87 function isSyndicated() {
88 return false;
89 }
90
91 /**
92 * Return an appropriately formatted LIKE query and the clause
93 */
94 static function mungeQuery( $query , $prot ) {
95 $field = 'el_index';
96 $rv = LinkFilter::makeLike( $query , $prot );
97 if ($rv === false) {
98 //makeLike doesn't handle wildcard in IP, so we'll have to munge here.
99 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
100 $rv = $prot . rtrim($query, " \t*") . '%';
101 $field = 'el_to';
102 }
103 }
104 return array( $rv, $field );
105 }
106
107 function linkParameters() {
108 global $wgMiserMode;
109 $params = array();
110 $params['target'] = $this->mProt . $this->mQuery;
111 if( isset( $this->mNs ) && !$wgMiserMode ) {
112 $params['namespace'] = $this->mNs;
113 }
114 return $params;
115 }
116
117 function getSQL() {
118 global $wgMiserMode;
119 $dbr = wfGetDB( DB_SLAVE );
120 $page = $dbr->tableName( 'page' );
121 $externallinks = $dbr->tableName( 'externallinks' );
122
123 /* strip everything past first wildcard, so that index-based-only lookup would be done */
124 list( $munged, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
125 $stripped = substr($munged,0,strpos($munged,'%')+1);
126 $encSearch = $dbr->addQuotes( $stripped );
127
128 $encSQL = '';
129 if ( isset ($this->mNs) && !$wgMiserMode )
130 $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs );
131
132 $use_index = $dbr->useIndexClause( $clause );
133 return
134 "SELECT
135 page_namespace AS namespace,
136 page_title AS title,
137 el_index AS value,
138 el_to AS url
139 FROM
140 $page,
141 $externallinks $use_index
142 WHERE
143 page_id=el_from
144 AND $clause LIKE $encSearch
145 $encSQL";
146 }
147
148 function formatResult( $skin, $result ) {
149 $title = Title::makeTitle( $result->namespace, $result->title );
150 $url = $result->url;
151 $pageLink = $skin->makeKnownLinkObj( $title );
152 $urlLink = $skin->makeExternalLink( $url, $url );
153
154 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
155 }
156
157 /**
158 * Override to check query validity.
159 */
160 function doQuery( $offset, $limit, $shownavigation=true ) {
161 global $wgOut;
162 list( $this->mMungedQuery, $clause ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
163 if( $this->mMungedQuery === false ) {
164 $wgOut->addWikiText( wfMsg( 'linksearch-error' ) );
165 } else {
166 // For debugging
167 // Generates invalid xhtml with patterns that contain --
168 //$wgOut->addHtml( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
169 parent::doQuery( $offset, $limit, $shownavigation );
170 }
171 }
172
173 /**
174 * Override to squash the ORDER BY.
175 * We do a truncated index search, so the optimizer won't trust
176 * it as good enough for optimizing sort. The implicit ordering
177 * from the scan will usually do well enough for our needs.
178 */
179 function getOrder() {
180 return '';
181 }
182 }