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