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