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