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
{
32 * @var PageLinkRenderer
34 protected $linkRenderer = null;
36 function setParams( $params ) {
37 $this->mQuery
= $params['query'];
38 $this->mNs
= $params['namespace'];
39 $this->mProt
= $params['protocol'];
42 function __construct( $name = 'LinkSearch' ) {
43 parent
::__construct( $name );
45 // Since we don't control the constructor parameters, we can't inject services that way.
46 // Instead, we initialize services in the execute() method, and allow them to be overridden
47 // using the setServices() method.
51 * Initialize or override the PageLinkRenderer LinkSearchPage collaborates with.
52 * Useful mainly for testing.
54 * @todo: query logic and rendering logic should be split and also injected
56 * @param PageLinkRenderer $linkRenderer
58 public function setPageLinkRenderer(
59 PageLinkRenderer
$linkRenderer
61 $this->linkRenderer
= $linkRenderer;
65 * Initialize any services we'll need (unless it has already been provided via a setter).
66 * This allows for dependency injection even though we don't control object creation.
68 private function initServices() {
69 if ( !$this->linkRenderer
) {
70 $lang = $this->getContext()->getLanguage();
71 $titleFormatter = new MediaWikiTitleCodec( $lang, GenderCache
::singleton() );
72 $this->linkRenderer
= new MediaWikiPageLinkRenderer( $titleFormatter );
76 function isCacheable() {
80 function execute( $par ) {
81 global $wgUrlProtocols, $wgMiserMode, $wgScript;
83 $this->initServices();
86 $this->outputHeader();
88 $out = $this->getOutput();
89 $out->allowClickjacking();
91 $request = $this->getRequest();
92 $target = $request->getVal( 'target', $par );
93 $namespace = $request->getIntorNull( 'namespace', null );
95 $protocols_list = array();
96 foreach ( $wgUrlProtocols as $prot ) {
97 if ( $prot !== '//' ) {
98 $protocols_list[] = $prot;
103 // Get protocol, default is http://
104 $protocol = 'http://';
105 $bits = wfParseUrl( $target );
106 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
107 $protocol = $bits['scheme'] . $bits['delimiter'];
108 // Make sure wfParseUrl() didn't make some well-intended correction in the
110 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
111 $target2 = substr( $target, strlen( $protocol ) );
113 // If it did, let LinkFilter::makeLikeArray() handle this
120 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
121 count( $protocols_list )
123 $s = Html
::openElement(
125 array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => $wgScript )
127 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) . "\n" .
128 Html
::openElement( 'fieldset' ) . "\n" .
129 Html
::element( 'legend', array(), $this->msg( 'linksearch' )->text() ) . "\n" .
131 $this->msg( 'linksearch-pat' )->text(),
138 if ( !$wgMiserMode ) {
139 $s .= Html
::namespaceSelector(
141 'selected' => $namespace,
143 'label' => $this->msg( 'linksearch-ns' )->text()
145 'name' => 'namespace',
147 'class' => 'namespaceselector',
152 $s .= Xml
::submitButton( $this->msg( 'linksearch-ok' )->text() ) . "\n" .
153 Html
::closeElement( 'fieldset' ) . "\n" .
154 Html
::closeElement( 'form' ) . "\n";
157 if ( $target != '' ) {
158 $this->setParams( array(
160 'namespace' => $namespace,
161 'protocol' => $protocol ) );
162 parent
::execute( $par );
163 if ( $this->mMungedQuery
=== false ) {
164 $out->addWikiMsg( 'linksearch-error' );
170 * Disable RSS/Atom feeds
173 function isSyndicated() {
178 * Return an appropriately formatted LIKE query and the clause
180 * @param string $query Search pattern to search for
181 * @param string $prot Protocol, e.g. 'http://'
185 static function mungeQuery( $query, $prot ) {
187 $dbr = wfGetDB( DB_SLAVE
);
189 if ( $query === '*' && $prot !== '' ) {
190 // Allow queries like 'ftp://*' to find all ftp links
191 $rv = array( $prot, $dbr->anyString() );
193 $rv = LinkFilter
::makeLikeArray( $query, $prot );
196 if ( $rv === false ) {
197 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
198 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
199 if ( preg_match( $pattern, $query ) ) {
200 $rv = array( $prot . rtrim( $query, " \t*" ), $dbr->anyString() );
205 return array( $rv, $field );
208 function linkParameters() {
211 $params['target'] = $this->mProt
. $this->mQuery
;
212 if ( isset( $this->mNs
) && !$wgMiserMode ) {
213 $params['namespace'] = $this->mNs
;
219 function getQueryInfo() {
221 $dbr = wfGetDB( DB_SLAVE
);
222 // strip everything past first wildcard, so that
223 // index-based-only lookup would be done
224 list( $this->mMungedQuery
, $clause ) = self
::mungeQuery( $this->mQuery
, $this->mProt
);
225 if ( $this->mMungedQuery
=== false ) {
226 // Invalid query; return no results
227 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
230 $stripped = LinkFilter
::keepOneWildcard( $this->mMungedQuery
);
231 $like = $dbr->buildLike( $stripped );
233 'tables' => array( 'page', 'externallinks' ),
235 'namespace' => 'page_namespace',
236 'title' => 'page_title',
237 'value' => 'el_index',
244 'options' => array( 'USE INDEX' => $clause )
247 if ( isset( $this->mNs
) && !$wgMiserMode ) {
248 $retval['conds']['page_namespace'] = $this->mNs
;
256 * @param object $result Result row
259 function formatResult( $skin, $result ) {
260 $title = new TitleValue( (int)$result->namespace, $result->title
);
261 $pageLink = $this->linkRenderer
->renderHtmlLink( $title );
264 $urlLink = Linker
::makeExternalLink( $url, $url );
266 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
270 * Override to check query validity.
272 * @param mixed $offset Numerical offset or false for no offset
273 * @param mixed $limit Numerical limit or false for no limit
275 function doQuery( $offset = false, $limit = false ) {
276 list( $this->mMungedQuery
, ) = LinkSearchPage
::mungeQuery( $this->mQuery
, $this->mProt
);
277 if ( $this->mMungedQuery
=== false ) {
278 $this->getOutput()->addWikiMsg( 'linksearch-error' );
281 // Generates invalid xhtml with patterns that contain --
282 //$this->getOutput()->addHTML( "\n<!-- " . htmlspecialchars( $this->mMungedQuery ) . " -->\n" );
283 parent
::doQuery( $offset, $limit );
288 * Override to squash the ORDER BY.
289 * We do a truncated index search, so the optimizer won't trust
290 * it as good enough for optimizing sort. The implicit ordering
291 * from the scan will usually do well enough for our needs.
294 function getOrderFields() {
298 protected function getGroupName() {