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