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