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