Merge "Consistently handle anonymous users on logged-in-only special pages"
[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 ( $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 // Since links with unsupported protocols don't end up in
84 // externallinks, assume $protocol is actually part of a link
85 // containing ':' or '//' and default to http as above.
86 $protocol = 'http://';
87 }
88
89 $out->addWikiMsg(
90 'linksearch-text',
91 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
92 count( $protocols_list )
93 );
94 $s = Html::openElement(
95 'form',
96 array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $wgScript )
97 ) . "\n" .
98 Html::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n" .
99 Html::openElement( 'fieldset' ) . "\n" .
100 Html::element( 'legend', array(), $this->msg( 'linksearch' )->text() ) . "\n" .
101 Xml::inputLabel(
102 $this->msg( 'linksearch-pat' )->text(),
103 'target',
104 'target',
105 50,
106 $target
107 ) . "\n";
108
109 if ( !$wgMiserMode ) {
110 $s .= Html::namespaceSelector(
111 array(
112 'selected' => $namespace,
113 'all' => '',
114 'label' => $this->msg( 'linksearch-ns' )->text()
115 ), array(
116 'name' => 'namespace',
117 'id' => 'namespace',
118 'class' => 'namespaceselector',
119 )
120 );
121 }
122
123 $s .= Xml::submitButton( $this->msg( 'linksearch-ok' )->text() ) . "\n" .
124 Html::closeElement( 'fieldset' ) . "\n" .
125 Html::closeElement( 'form' ) . "\n";
126 $out->addHTML( $s );
127
128 if ( $target != '' ) {
129 $this->setParams( array(
130 'query' => $target2,
131 'namespace' => $namespace,
132 'protocol' => $protocol ) );
133 parent::execute( $par );
134 if ( $this->mMungedQuery === false ) {
135 $out->addWikiMsg( 'linksearch-error' );
136 }
137 }
138 }
139
140 /**
141 * Disable RSS/Atom feeds
142 * @return bool
143 */
144 function isSyndicated() {
145 return false;
146 }
147
148 /**
149 * Return an appropriately formatted LIKE query and the clause
150 *
151 * @param string $query
152 * @param string $prot
153 * @return array
154 */
155 static function mungeQuery( $query, $prot ) {
156 $field = 'el_index';
157 $rv = LinkFilter::makeLikeArray( $query, $prot );
158 if ( $rv === false ) {
159 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
160 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
161 if ( preg_match( $pattern, $query ) ) {
162 $dbr = wfGetDB( DB_SLAVE );
163 $rv = array( $prot . rtrim( $query, " \t*" ), $dbr->anyString() );
164 $field = 'el_to';
165 }
166 }
167
168 return array( $rv, $field );
169 }
170
171 function linkParameters() {
172 global $wgMiserMode;
173 $params = array();
174 $params['target'] = $this->mProt . $this->mQuery;
175 if ( isset( $this->mNs ) && !$wgMiserMode ) {
176 $params['namespace'] = $this->mNs;
177 }
178
179 return $params;
180 }
181
182 function getQueryInfo() {
183 global $wgMiserMode;
184 $dbr = wfGetDB( DB_SLAVE );
185 // strip everything past first wildcard, so that
186 // index-based-only lookup would be done
187 list( $this->mMungedQuery, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
188 if ( $this->mMungedQuery === false ) {
189 // Invalid query; return no results
190 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
191 }
192
193 $stripped = LinkFilter::keepOneWildcard( $this->mMungedQuery );
194 $like = $dbr->buildLike( $stripped );
195 $retval = array(
196 'tables' => array( 'page', 'externallinks' ),
197 'fields' => array(
198 'namespace' => 'page_namespace',
199 'title' => 'page_title',
200 'value' => 'el_index',
201 'url' => 'el_to'
202 ),
203 'conds' => array(
204 'page_id = el_from',
205 "$clause $like"
206 ),
207 'options' => array( 'USE INDEX' => $clause )
208 );
209
210 if ( isset( $this->mNs ) && !$wgMiserMode ) {
211 $retval['conds']['page_namespace'] = $this->mNs;
212 }
213
214 return $retval;
215 }
216
217 /**
218 * @param Skin $skin
219 * @param object $result Result row
220 * @return string
221 */
222 function formatResult( $skin, $result ) {
223 $title = Title::makeTitle( $result->namespace, $result->title );
224 $url = $result->url;
225 $pageLink = Linker::linkKnown( $title );
226 $urlLink = Linker::makeExternalLink( $url, $url );
227
228 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
229 }
230
231 /**
232 * Override to check query validity.
233 */
234 function doQuery( $offset = false, $limit = false ) {
235 list( $this->mMungedQuery, ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
236 if ( $this->mMungedQuery === false ) {
237 $this->getOutput()->addWikiMsg( 'linksearch-error' );
238 } else {
239 // For debugging
240 // Generates invalid xhtml with patterns that contain --
241 //$this->getOutput()->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
242 parent::doQuery( $offset, $limit );
243 }
244 }
245
246 /**
247 * Override to squash the ORDER BY.
248 * We do a truncated index search, so the optimizer won't trust
249 * it as good enough for optimizing sort. The implicit ordering
250 * from the scan will usually do well enough for our needs.
251 * @return array
252 */
253 function getOrderFields() {
254 return array();
255 }
256
257 protected function getGroupName() {
258 return 'redirects';
259 }
260 }