00001 <?php
00015 function wfSpecialLinkSearch( $par ) {
00016
00017 list( $limit, $offset ) = wfCheckLimits();
00018 global $wgOut, $wgRequest, $wgUrlProtocols, $wgMiserMode, $wgLang;
00019 $target = $GLOBALS['wgRequest']->getVal( 'target', $par );
00020 $namespace = $GLOBALS['wgRequest']->getIntorNull( 'namespace', null );
00021
00022 $protocols_list[] = '';
00023 foreach( $wgUrlProtocols as $prot ) {
00024 $protocols_list[] = $prot;
00025 }
00026
00027 $target2 = $target;
00028 $protocol = '';
00029 $pr_sl = strpos($target2, '//' );
00030 $pr_cl = strpos($target2, ':' );
00031 if ( $pr_sl ) {
00032
00033 $protocol = substr( $target2, 0 , $pr_sl+2 );
00034 $target2 = substr( $target2, $pr_sl+2 );
00035 } elseif ( !$pr_sl && $pr_cl ) {
00036
00037 $protocol = substr( $target2, 0 , $pr_cl+1 );
00038 $target2 = substr( $target2, $pr_cl+1 );
00039 } elseif ( $protocol == '' && $target2 != '' ) {
00040
00041 $protocol = 'http://';
00042 }
00043 if ( !in_array( $protocol, $protocols_list ) ) {
00044
00045 $target2 = $target;
00046 $protocol = '';
00047 }
00048
00049 $self = Title::makeTitle( NS_SPECIAL, 'Linksearch' );
00050
00051 $wgOut->addWikiText( wfMsg( 'linksearch-text', '<nowiki>' . $wgLang->commaList( $wgUrlProtocols) . '</nowiki>' ) );
00052 $s = Xml::openElement( 'form', array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $GLOBALS['wgScript'] ) ) .
00053 Xml::hidden( 'title', $self->getPrefixedDbKey() ) .
00054 '<fieldset>' .
00055 Xml::element( 'legend', array(), wfMsg( 'linksearch' ) ) .
00056 Xml::inputLabel( wfMsg( 'linksearch-pat' ), 'target', 'target', 50, $target ) . ' ';
00057 if ( !$wgMiserMode ) {
00058 $s .= Xml::label( wfMsg( 'linksearch-ns' ), 'namespace' ) . ' ' .
00059 XML::namespaceSelector( $namespace, '' );
00060 }
00061 $s .= Xml::submitButton( wfMsg( 'linksearch-ok' ) ) .
00062 '</fieldset>' .
00063 Xml::closeElement( 'form' );
00064 $wgOut->addHTML( $s );
00065
00066 if( $target != '' ) {
00067 $searcher = new LinkSearchPage;
00068 $searcher->setParams( array(
00069 'query' => $target2,
00070 'namespace' => $namespace,
00071 'protocol' => $protocol ) );
00072 $searcher->doQuery( $offset, $limit );
00073 }
00074 }
00075
00076 class LinkSearchPage extends QueryPage {
00077 function setParams( $params ) {
00078 $this->mQuery = $params['query'];
00079 $this->mNs = $params['namespace'];
00080 $this->mProt = $params['protocol'];
00081 }
00082
00083 function getName() {
00084 return 'LinkSearch';
00085 }
00086
00090 function isSyndicated() {
00091 return false;
00092 }
00093
00097 static function mungeQuery( $query , $prot ) {
00098 $field = 'el_index';
00099 $rv = LinkFilter::makeLike( $query , $prot );
00100 if ($rv === false) {
00101
00102 if (preg_match('/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/', $query)) {
00103 $rv = $prot . rtrim($query, " \t*") . '%';
00104 $field = 'el_to';
00105 }
00106 }
00107 return array( $rv, $field );
00108 }
00109
00110 function linkParameters() {
00111 global $wgMiserMode;
00112 $params = array();
00113 $params['target'] = $this->mProt . $this->mQuery;
00114 if( isset( $this->mNs ) && !$wgMiserMode ) {
00115 $params['namespace'] = $this->mNs;
00116 }
00117 return $params;
00118 }
00119
00120 function getSQL() {
00121 global $wgMiserMode;
00122 $dbr = wfGetDB( DB_SLAVE );
00123 $page = $dbr->tableName( 'page' );
00124 $externallinks = $dbr->tableName( 'externallinks' );
00125
00126
00127 list( $munged, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
00128 $stripped = substr($munged,0,strpos($munged,'%')+1);
00129 $encSearch = $dbr->addQuotes( $stripped );
00130
00131 $encSQL = '';
00132 if ( isset ($this->mNs) && !$wgMiserMode )
00133 $encSQL = 'AND page_namespace=' . $dbr->addQuotes( $this->mNs );
00134
00135 $use_index = $dbr->useIndexClause( $clause );
00136 return
00137 "SELECT
00138 page_namespace AS namespace,
00139 page_title AS title,
00140 el_index AS value,
00141 el_to AS url
00142 FROM
00143 $page,
00144 $externallinks $use_index
00145 WHERE
00146 page_id=el_from
00147 AND $clause LIKE $encSearch
00148 $encSQL";
00149 }
00150
00151 function formatResult( $skin, $result ) {
00152 $title = Title::makeTitle( $result->namespace, $result->title );
00153 $url = $result->url;
00154 $pageLink = $skin->makeKnownLinkObj( $title );
00155 $urlLink = $skin->makeExternalLink( $url, $url );
00156
00157 return wfMsgHtml( 'linksearch-line', $urlLink, $pageLink );
00158 }
00159
00163 function doQuery( $offset, $limit, $shownavigation=true ) {
00164 global $wgOut;
00165 list( $this->mMungedQuery, $clause ) = LinkSearchPage::mungeQuery( $this->mQuery, $this->mProt );
00166 if( $this->mMungedQuery === false ) {
00167 $wgOut->addWikiText( wfMsg( 'linksearch-error' ) );
00168 } else {
00169
00170
00171
00172 parent::doQuery( $offset, $limit, $shownavigation );
00173 }
00174 }
00175
00182 function getOrder() {
00183 return '';
00184 }
00185 }