r21543 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r21542‎ | r21543 | r21544 >
Date:20:04, 24 April 2007
Author:raymond
Status:old
Tags:
Comment:
namespace search (misermode) and other protocols. No UI change.
Modified paths:
  • /trunk/extensions/LinkSearch/LinkSearch.i18n.php (modified) (history)
  • /trunk/extensions/LinkSearch/LinkSearch.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LinkSearch/LinkSearch.i18n.php
@@ -10,7 +10,10 @@
1111
1212 $wgLinkSearchMessages['en'] = array(
1313 'linksearch' => 'Search web links',
14 - 'linksearch-text' => 'Wildcards such as "*.wikipedia.org" may be used.',
 14+ 'linksearch-pat' => 'Search pattern:',
 15+ 'linksearch-ns' => 'Namespace:',
 16+ 'linksearch-ok' => 'Search',
 17+ 'linksearch-text' => 'Wildcards such as "*.wikipedia.org" may be used.<br />Supported protocols: <tt>$1</tt>',
1518 'linksearch-line' => '$1 linked from $2',
1619 'linksearch-error' => 'Wildcards may appear only at the start of the hostname.',
1720 );
@@ -22,7 +25,10 @@
2326 );
2427 $wgLinkSearchMessages['de'] = array(
2528 'linksearch' => 'Weblink-Suche',
26 - 'linksearch-text' => 'Diese Spezialseite ermöglicht die Suche nach Seiten, in denen bestimmte Weblinks enthalten sind. Dabei können Wildcards wie beispielsweise <tt>*.example.com</tt> benutzt werden. ',
 29+ 'linksearch-pat' => 'Suchmuster:',
 30+ 'linksearch-ns' => 'Namensraum:',
 31+ 'linksearch-ok' => 'Suche',
 32+ 'linksearch-text' => 'Diese Spezialseite ermöglicht die Suche nach Seiten, in denen bestimmte Weblinks enthalten sind. Dabei können Wildcards wie beispielsweise <tt>*.example.com</tt> benutzt werden.<br />Unterstützte Protokolle: <tt>$1</tt>',
2733 'linksearch-line' => '$1 ist verlinkt von $2',
2834 'linksearch-error' => 'Wildcards können nur am Anfang der URL verwendet werden.',
2935 );
Index: trunk/extensions/LinkSearch/LinkSearch.php
@@ -27,49 +27,51 @@
2828 $GLOBALS['wgSpecialPages']['Linksearch'] = array( /*class*/ 'SpecialPage',
2929 /*name*/ 'Linksearch', /* permission */'', /*listed*/ true,
3030 /*function*/ false, /*file*/ false );
31 -
 31+
3232 class LinkSearchPage extends QueryPage {
33 - function __construct( $query ) {
 33+ function __construct( $query , $ns , $prot ) {
3434 $this->mQuery = $query;
 35+ $this->mNs = $ns;
 36+ $this->mProt = $prot;
3537 }
36 -
 38+
3739 function getName() {
3840 return 'Linksearch';
3941 }
40 -
 42+
4143 /**
4244 * Disable RSS/Atom feeds
4345 */
4446 function isSyndicated() {
4547 return false;
4648 }
47 -
 49+
4850 /**
4951 * Return an appropriately formatted LIKE query
50 - * @fixme Fix up LinkFilter to work with non-http links as well
5152 */
52 - static function mungeQuery( $query ) {
53 - if( substr( $query, 0, 7 ) == 'http://' ) {
54 - $query = substr( $query, 7 );
55 - }
56 - return LinkFilter::makeLike( $query );
 53+ static function mungeQuery( $query , $prot ) {
 54+ return LinkFilter::makeLike( $query , $prot );
5755 }
58 -
 56+
5957 function linkParameters() {
60 - return array( 'target' => $this->mQuery );
 58+ return array( 'target' => $this->mQuery, 'namespace' => $this->mNs );
6159 }
62 -
 60+
6361 function getSQL() {
 62+ global $wgMiserMode;
6463 $dbr = wfGetDB( DB_SLAVE );
65 -
6664 $page = $dbr->tableName( 'page' );
6765 $externallinks = $dbr->tableName( 'externallinks' );
68 -
 66+
6967 /* strip everything past first wildcard, so that index-based-only lookup would be done */
70 - $munged = self::mungeQuery( $this->mQuery );
 68+ $munged = self::mungeQuery( $this->mQuery, $this->mProt );
7169 $stripped = substr($munged,0,strpos($munged,'%')+1);
7270 $encSearch = $dbr->addQuotes( $stripped );
73 -
 71+
 72+ $encSQL = '';
 73+ if ( isset ($this->mNs) && !$wgMiserMode ) $encSQL = 'AND page_namespace=' . $this->mNs;
 74+
 75+
7476 return
7577 "SELECT
7678 page_namespace AS namespace,
@@ -81,25 +83,25 @@
8284 $externallinks FORCE INDEX (el_index)
8385 WHERE
8486 page_id=el_from
85 - AND el_index LIKE $encSearch";
 87+ AND el_index LIKE $encSearch
 88+ $encSQL";
8689 }
87 -
 90+
8891 function formatResult( $skin, $result ) {
8992 $title = Title::makeTitle( $result->namespace, $result->title );
9093 $url = $result->url;
91 -
9294 $pageLink = $skin->makeKnownLinkObj( $title );
9395 $urlLink = $skin->makeExternalLink( $url, $url );
94 -
 96+
9597 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
9698 }
97 -
 99+
98100 /**
99101 * Override to check query validity.
100102 */
101103 function doQuery( $offset, $limit ) {
102104 global $wgOut;
103 - $this->mMungedQuery = LinkSearchPage::mungeQuery( $this->mQuery );
 105+ $this->mMungedQuery = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
104106 if( $this->mMungedQuery === false ) {
105107 $wgOut->addWikiText( wfMsg( 'linksearch-error' ) );
106108 } else {
@@ -108,7 +110,7 @@
109111 parent::doQuery( $offset, $limit );
110112 }
111113 }
112 -
 114+
113115 /**
114116 * Override to squash the ORDER BY.
115117 * We do a truncated index search, so the optimizer won't trust
@@ -118,27 +120,61 @@
119121 function getOrder() {
120122 return '';
121123 }
122 -
123124 }
124 -
125 - function wfSpecialLinksearch( $par=null ) {
 125+
 126+ function wfSpecialLinksearch( $par=null, $ns=null ) {
126127 list( $limit, $offset ) = wfCheckLimits();
 128+ global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode;
127129 $target = $GLOBALS['wgRequest']->getVal( 'target', $par );
128 -
 130+ $namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', $ns );
 131+
 132+ $protocols_list[] = '';
 133+ foreach( $wgUrlProtocols as $prot ) {
 134+ $protocols_list[] = $prot;
 135+ }
 136+
 137+ $target2 = $target;
 138+ $protocol = '';
 139+ $pr_sl = strpos($target2, '//' );
 140+ $pr_cl = strpos($target2, ':' );
 141+ if ( $pr_sl ) {
 142+ // For protocols with '//'
 143+ $protocol = substr( $target2, 0 , $pr_sl+2 );
 144+ $target2 = substr( $target2, $pr_sl+2 );
 145+ } elseif ( !$pr_sl && $pr_cl ) {
 146+ // For protocols without '//' like 'mailto:'
 147+ $protocol = substr( $target2, 0 , $pr_cl+1 );
 148+ $target2 = substr( $target2, $pr_cl+1 );
 149+ } elseif ( $protocol == '' && $target2 != '' ) {
 150+ // default
 151+ $protocol = 'http://';
 152+ }
 153+ if ( !in_array( $protocol, $protocols_list ) ) {
 154+ // unsupported protocol, show original search request
 155+ $target2 = $target;
 156+ $protocol = '';
 157+ }
 158+
129159 $self = Title::makeTitle( NS_SPECIAL, 'Linksearch' );
130 -
131 - global $wgOut;
132 - $wgOut->addWikiText( wfMsg( 'linksearch-text' ) );
133 - $wgOut->addHtml(
134 - wfOpenElement( 'form',
135 - array( 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
136 - wfHidden( 'title', $self->getPrefixedDbKey() ) .
137 - wfInput( 'target', 50, $target ) .
138 - wfSubmitButton( wfMsg( 'search' ) ) .
139 - wfCloseElement( 'form' ) );
140160
 161+ $wgOut->addWikiText( wfMsg( 'linksearch-text', '<nowiki>' . implode( ', ', $wgUrlProtocols) . '</nowiki>' ) );
 162+ $s = Xml::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
 163+ Xml::hidden( 'title', $self->getPrefixedDbKey() ) .
 164+ '<fieldset>' .
 165+ Xml::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
 166+ Xml::label( wfMsg( 'linksearch-pat' ), 'target' ) . ' ' .
 167+ Xml::input( 'target', 50 , $target ) . ' ';
 168+ if ( !$wgMiserMode ) {
 169+ $s .= Xml::label( wfMsg( 'linksearch-ns' ), 'namespace' ) .
 170+ XML::namespaceSelector( $namespace, '' );
 171+ }
 172+ $s .= Xml::submitButton( wfMsg( 'linksearch-ok' ) ) .
 173+ '</fieldset>' .
 174+ Xml::closeElement( 'form' );
 175+ $wgOut->addHtml( $s );
 176+
141177 if( $target != '' ) {
142 - $searcher = new LinkSearchPage( $target );
 178+ $searcher = new LinkSearchPage( $target2, $namespace, $protocol );
143179 $searcher->doQuery( $offset, $limit );
144180 }
145181 }