r79347 didn't update other callers to mungeQuery(), so let's just take the easy way...
[lhc/web/wiklou.git] / includes / specials / SpecialLinkSearch.php
1 <?php
2 /**
3 * Implements Special:LinkSearch
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Brion Vibber
23 */
24
25
26 /**
27 * Special:LinkSearch to search the external-links table.
28 * @ingroup SpecialPage
29 */
30 class LinkSearchPage extends QueryPage {
31 function setParams( $params ) {
32 $this->mQuery = $params['query'];
33 $this->mNs = $params['namespace'];
34 $this->mProt = $params['protocol'];
35 }
36
37 function __construct( $name = 'LinkSearch' ) {
38 parent::__construct( $name );
39 }
40
41 function isCacheable() {
42 return false;
43 }
44
45 function execute( $par ) {
46 global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode, $wgLang, $wgScript;
47 $this->setHeaders();
48
49 $target = $wgRequest->getVal( 'target', $par );
50 $namespace = $wgRequest->getIntorNull( 'namespace', null );
51
52 $protocols_list[] = '';
53 foreach( $wgUrlProtocols as $prot ) {
54 $protocols_list[] = $prot;
55 }
56
57 $target2 = $target;
58 $protocol = '';
59 $pr_sl = strpos($target2, '//' );
60 $pr_cl = strpos($target2, ':' );
61 if ( $pr_sl ) {
62 // For protocols with '//'
63 $protocol = substr( $target2, 0 , $pr_sl+2 );
64 $target2 = substr( $target2, $pr_sl+2 );
65 } elseif ( !$pr_sl && $pr_cl ) {
66 // For protocols without '//' like 'mailto:'
67 $protocol = substr( $target2, 0 , $pr_cl+1 );
68 $target2 = substr( $target2, $pr_cl+1 );
69 } elseif ( $protocol == '' && $target2 != '' ) {
70 // default
71 $protocol = 'http://';
72 }
73 if ( !in_array( $protocol, $protocols_list ) ) {
74 // unsupported protocol, show original search request
75 $target2 = $target;
76 $protocol = '';
77 }
78
79 $self = $this->getTitle();
80
81 $wgOut->addWikiMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols ) . '</nowiki>' );
82 $s = Xml::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
83 Html::hidden( 'title', $self->getPrefixedDbKey() ) .
84 '<fieldset>' .
85 Xml::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
86 Xml::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
87 if ( !$wgMiserMode ) {
88 $s .= Xml::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
89 Xml::namespaceSelector( $namespace, '' );
90 }
91 $s .= Xml::submitButton( wfMsg( 'linksearch-ok' ) ) .
92 '</fieldset>' .
93 Xml::closeElement( 'form' );
94 $wgOut->addHTML( $s );
95
96 if( $target != '' ) {
97 $this->setParams( array(
98 'query' => $target2,
99 'namespace' => $namespace,
100 'protocol' => $protocol ) );
101 parent::execute( $par );
102 if( $this->mMungedQuery === false )
103 $wgOut->addWikiText( wfMsg( 'linksearch-error' ) );
104 }
105 }
106
107 /**
108 * Disable RSS/Atom feeds
109 */
110 function isSyndicated() {
111 return false;
112 }
113
114 /**
115 * Return an appropriately formatted LIKE query and the clause
116 */
117 static function mungeQuery( $query, $prot ) {
118 $field = 'el_index';
119 $rv = LinkFilter::makeLikeArray( $query , $prot );
120 if ( $rv === false ) {
121 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
122 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
123 $dbr = wfGetDB( DB_SLAVE );
124 $rv = array( $prot . rtrim( $query, " \t*" ), $dbr->anyString() );
125 $field = 'el_to';
126 }
127 }
128 return array( $rv, $field );
129 }
130
131 function linkParameters() {
132 global $wgMiserMode;
133 $params = array();
134 $params['target'] = $this->mProt . $this->mQuery;
135 if( isset( $this->mNs ) && !$wgMiserMode ) {
136 $params['namespace'] = $this->mNs;
137 }
138 return $params;
139 }
140
141 function getQueryInfo() {
142 global $wgMiserMode;
143 $dbr = wfGetDB( DB_SLAVE );
144 // strip everything past first wildcard, so that
145 // index-based-only lookup would be done
146 list( $this->mMungedQuery, $clause ) = self::mungeQuery(
147 $this->mQuery, $this->mProt );
148 if( $this->mMungedQuery === false )
149 // Invalid query; return no results
150 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
151
152 $stripped = LinkFilter::keepOneWildcard( $this->mMungedQuery );
153 $like = $dbr->buildLike( $stripped );
154 $retval = array (
155 'tables' => array ( 'page', 'externallinks' ),
156 'fields' => array ( 'page_namespace AS namespace',
157 'page_title AS title',
158 'el_index AS value', 'el_to AS url' ),
159 'conds' => array ( 'page_id = el_from',
160 "$clause $like" ),
161 'options' => array( 'USE INDEX' => $clause )
162 );
163 if ( isset( $this->mNs ) && !$wgMiserMode ) {
164 $retval['conds']['page_namespace'] = $this->mNs;
165 }
166 return $retval;
167 }
168
169 function formatResult( $skin, $result ) {
170 $title = Title::makeTitle( $result->namespace, $result->title );
171 $url = $result->url;
172 $pageLink = $skin->linkKnown( $title );
173 $urlLink = $skin->makeExternalLink( $url, $url );
174
175 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
176 }
177
178 /**
179 * Override to check query validity.
180 */
181 function doQuery( $limit, $offset = false ) {
182 global $wgOut;
183 list( $this->mMungedQuery, ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
184 if( $this->mMungedQuery === false ) {
185 $wgOut->addWikiMsg( 'linksearch-error' );
186 } else {
187 // For debugging
188 // Generates invalid xhtml with patterns that contain --
189 //$wgOut->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
190 parent::doQuery( $limit, $offset );
191 }
192 }
193
194 /**
195 * Override to squash the ORDER BY.
196 * We do a truncated index search, so the optimizer won't trust
197 * it as good enough for optimizing sort. The implicit ordering
198 * from the scan will usually do well enough for our needs.
199 */
200 function getOrderFields() {
201 return array();
202 }
203 }