ef0e2c28e842fa46ffd8a296fd5a597e0bc16484
[lhc/web/wiklou.git] / includes / specials / SpecialLinkSearch.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * @file
22 * @ingroup SpecialPage
23 *
24 * @author Brion Vibber
25 */
26
27
28 /**
29 * Special:LinkSearch to search the external-links table.
30 */
31 function wfSpecialLinkSearch( $par ) {
32
33 list( $limit, $offset ) = wfCheckLimits();
34 global $wgOut, $wgUrlProtocols, $wgMiserMode, $wgLang;
35 $target = $GLOBALS['wgRequest']->getVal( 'target', $par );
36 $namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', null );
37
38 $protocols_list[] = '';
39 foreach( $wgUrlProtocols as $prot ) {
40 $protocols_list[] = $prot;
41 }
42
43 $target2 = $target;
44 $protocol = '';
45 $pr_sl = strpos($target2, '//' );
46 $pr_cl = strpos($target2, ':' );
47 if ( $pr_sl ) {
48 // For protocols with '//'
49 $protocol = substr( $target2, 0 , $pr_sl+2 );
50 $target2 = substr( $target2, $pr_sl+2 );
51 } elseif ( !$pr_sl && $pr_cl ) {
52 // For protocols without '//' like 'mailto:'
53 $protocol = substr( $target2, 0 , $pr_cl+1 );
54 $target2 = substr( $target2, $pr_cl+1 );
55 } elseif ( $protocol == '' && $target2 != '' ) {
56 // default
57 $protocol = 'http://';
58 }
59 if ( !in_array( $protocol, $protocols_list ) ) {
60 // unsupported protocol, show original search request
61 $target2 = $target;
62 $protocol = '';
63 }
64
65 $self = Title::makeTitle( NS_SPECIAL, 'Linksearch' );
66
67 $wgOut->addWikiMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols ) . '</nowiki>' );
68 $s = Xml::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
69 Xml::hidden( 'title', $self->getPrefixedDbKey() ) .
70 '<fieldset>' .
71 Xml::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
72 Xml::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
73 if ( !$wgMiserMode ) {
74 $s .= Xml::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
75 Xml::namespaceSelector( $namespace, '' );
76 }
77 $s .= Xml::submitButton( wfMsg( 'linksearch-ok' ) ) .
78 '</fieldset>' .
79 Xml::closeElement( 'form' );
80 $wgOut->addHTML( $s );
81
82 if( $target != '' ) {
83 $searcher = new LinkSearchPage;
84 $searcher->setParams( array(
85 'query' => $target2,
86 'namespace' => $namespace,
87 'protocol' => $protocol ) );
88 $searcher->doQuery( $offset, $limit );
89 }
90 }
91
92 class LinkSearchPage extends QueryPage {
93 function setParams( $params ) {
94 $this->mQuery = $params['query'];
95 $this->mNs = $params['namespace'];
96 $this->mProt = $params['protocol'];
97 }
98
99 function getName() {
100 return 'LinkSearch';
101 }
102
103 /**
104 * Disable RSS/Atom feeds
105 */
106 function isSyndicated() {
107 return false;
108 }
109
110 /**
111 * Return an appropriately formatted LIKE query and the clause
112 */
113 static function mungeQuery( $query , $prot ) {
114 $field = 'el_index';
115 $rv = LinkFilter::makeLikeArray( $query , $prot );
116 if ($rv === false) {
117 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
118 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
119 $rv = array( $prot . rtrim($query, " \t*"), $dbr->anyString() );
120 $field = 'el_to';
121 }
122 }
123 return array( $rv, $field );
124 }
125
126 function linkParameters() {
127 global $wgMiserMode;
128 $params = array();
129 $params['target'] = $this->mProt . $this->mQuery;
130 if( isset( $this->mNs ) && !$wgMiserMode ) {
131 $params['namespace'] = $this->mNs;
132 }
133 return $params;
134 }
135
136 function getSQL() {
137 global $wgMiserMode;
138 $dbr = wfGetDB( DB_SLAVE );
139 $page = $dbr->tableName( 'page' );
140 $externallinks = $dbr->tableName( 'externallinks' );
141
142 /* strip everything past first wildcard, so that index-based-only lookup would be done */
143 list( $munged, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
144 $stripped = LinkFilter::keepOneWildcard( $munged );
145 $like = $dbr->buildLike( $stripped );
146
147 $encSQL = '';
148 if ( isset ($this->mNs) && !$wgMiserMode )
149 $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs );
150
151 $use_index = $dbr->useIndexClause( $clause );
152 return
153 "SELECT
154 page_namespace AS namespace,
155 page_title AS title,
156 el_index AS value,
157 el_to AS url
158 FROM
159 $page,
160 $externallinks $use_index
161 WHERE
162 page_id=el_from
163 AND $clause $like
164 $encSQL";
165 }
166
167 function formatResult( $skin, $result ) {
168 $title = Title::makeTitle( $result->namespace, $result->title );
169 $url = $result->url;
170 $pageLink = $skin->linkKnown( $title );
171 $urlLink = $skin->makeExternalLink( $url, $url );
172
173 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
174 }
175
176 /**
177 * Override to check query validity.
178 */
179 function doQuery( $offset, $limit, $shownavigation=true ) {
180 global $wgOut;
181 list( $this->mMungedQuery, $clause ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
182 if( $this->mMungedQuery === false ) {
183 $wgOut->addWikiMsg( 'linksearch-error' );
184 } else {
185 // For debugging
186 // Generates invalid xhtml with patterns that contain --
187 //$wgOut->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
188 parent::doQuery( $offset, $limit, $shownavigation );
189 }
190 }
191
192 /**
193 * Override to squash the ORDER BY.
194 * We do a truncated index search, so the optimizer won't trust
195 * it as good enough for optimizing sort. The implicit ordering
196 * from the scan will usually do well enough for our needs.
197 */
198 function getOrder() {
199 return '';
200 }
201 }