3 * Implements Special:LinkSearch
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.
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.
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
21 * @ingroup SpecialPage
22 * @author Brion Vibber
26 * Special:LinkSearch to search the external-links table.
27 * @ingroup SpecialPage
29 class LinkSearchPage
extends QueryPage
{
30 function setParams( $params ) {
31 $this->mQuery
= $params['query'];
32 $this->mNs
= $params['namespace'];
33 $this->mProt
= $params['protocol'];
36 function __construct( $name = 'LinkSearch' ) {
37 parent
::__construct( $name );
40 function isCacheable() {
44 function execute( $par ) {
45 global $wgUrlProtocols, $wgMiserMode, $wgScript;
48 $this->outputHeader();
50 $out = $this->getOutput();
51 $out->allowClickjacking();
53 $request = $this->getRequest();
54 $target = $request->getVal( 'target', $par );
55 $namespace = $request->getIntorNull( 'namespace', null );
57 $protocols_list = array();
58 foreach ( $wgUrlProtocols as $prot ) {
59 if ( $prot !== '//' ) {
60 $protocols_list[] = $prot;
66 $pr_sl = strpos( $target2, '//' );
67 $pr_cl = strpos( $target2, ':' );
69 // For protocols with '//'
70 $protocol = substr( $target2, 0, $pr_sl +
2 );
71 $target2 = substr( $target2, $pr_sl +
2 );
72 } elseif ( !$pr_sl && $pr_cl ) {
73 // For protocols without '//' like 'mailto:'
74 $protocol = substr( $target2, 0, $pr_cl +
1 );
75 $target2 = substr( $target2, $pr_cl +
1 );
76 } elseif ( $protocol == '' && $target2 != '' ) {
78 $protocol = 'http://';
80 if ( $protocol != '' && !in_array( $protocol, $protocols_list ) ) {
81 // unsupported protocol, show original search request
88 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
89 count( $protocols_list )
91 $s = Html
::openElement(
93 array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $wgScript )
95 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) . "\n" .
96 Html
::openElement( 'fieldset' ) . "\n" .
97 Html
::element( 'legend', array(), $this->msg( 'linksearch' )->text() ) . "\n" .
99 $this->msg( 'linksearch-pat' )->text(),
106 if ( !$wgMiserMode ) {
107 $s .= Html
::namespaceSelector(
109 'selected' => $namespace,
111 'label' => $this->msg( 'linksearch-ns' )->text()
113 'name' => 'namespace',
115 'class' => 'namespaceselector',
120 $s .= Xml
::submitButton( $this->msg( 'linksearch-ok' )->text() ) . "\n" .
121 Html
::closeElement( 'fieldset' ) . "\n" .
122 Html
::closeElement( 'form' ) . "\n";
125 if ( $target != '' ) {
126 $this->setParams( array(
128 'namespace' => $namespace,
129 'protocol' => $protocol ) );
130 parent
::execute( $par );
131 if ( $this->mMungedQuery
=== false ) {
132 $out->addWikiMsg( 'linksearch-error' );
138 * Disable RSS/Atom feeds
141 function isSyndicated() {
146 * Return an appropriately formatted LIKE query and the clause
148 * @param string $query
149 * @param string $prot
152 static function mungeQuery( $query, $prot ) {
154 $rv = LinkFilter
::makeLikeArray( $query, $prot );
155 if ( $rv === false ) {
156 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
157 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
158 if ( preg_match( $pattern, $query ) ) {
159 $dbr = wfGetDB( DB_SLAVE
);
160 $rv = array( $prot . rtrim( $query, " \t*" ), $dbr->anyString() );
165 return array( $rv, $field );
168 function linkParameters() {
171 $params['target'] = $this->mProt
. $this->mQuery
;
172 if ( isset( $this->mNs
) && !$wgMiserMode ) {
173 $params['namespace'] = $this->mNs
;
179 function getQueryInfo() {
181 $dbr = wfGetDB( DB_SLAVE
);
182 // strip everything past first wildcard, so that
183 // index-based-only lookup would be done
184 list( $this->mMungedQuery
, $clause ) = self
::mungeQuery( $this->mQuery
, $this->mProt
);
185 if ( $this->mMungedQuery
=== false ) {
186 // Invalid query; return no results
187 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
190 $stripped = LinkFilter
::keepOneWildcard( $this->mMungedQuery
);
191 $like = $dbr->buildLike( $stripped );
193 'tables' => array( 'page', 'externallinks' ),
195 'namespace' => 'page_namespace',
196 'title' => 'page_title',
197 'value' => 'el_index',
204 'options' => array( 'USE INDEX' => $clause )
207 if ( isset( $this->mNs
) && !$wgMiserMode ) {
208 $retval['conds']['page_namespace'] = $this->mNs
;
216 * @param object $result Result row
219 function formatResult( $skin, $result ) {
220 $title = Title
::makeTitle( $result->namespace, $result->title
);
222 $pageLink = Linker
::linkKnown( $title );
223 $urlLink = Linker
::makeExternalLink( $url, $url );
225 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
229 * Override to check query validity.
231 function doQuery( $offset = false, $limit = false ) {
232 list( $this->mMungedQuery
, ) = LinkSearchPage
::mungeQuery( $this->mQuery
, $this->mProt
);
233 if ( $this->mMungedQuery
=== false ) {
234 $this->getOutput()->addWikiMsg( 'linksearch-error' );
237 // Generates invalid xhtml with patterns that contain --
238 //$this->getOutput()->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
239 parent
::doQuery( $offset, $limit );
244 * Override to squash the ORDER BY.
245 * We do a truncated index search, so the optimizer won't trust
246 * it as good enough for optimizing sort. The implicit ordering
247 * from the scan will usually do well enough for our needs.
250 function getOrderFields() {
254 protected function getGroupName() {