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