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
25 use Wikimedia\Rdbms\IResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * Special:LinkSearch to search the external-links table.
30 * @ingroup SpecialPage
32 class LinkSearchPage
extends QueryPage
{
33 /** @var array|bool */
34 private $mungedQuery = false;
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.
50 function isCacheable() {
54 public function execute( $par ) {
56 $this->outputHeader();
58 $out = $this->getOutput();
59 $out->allowClickjacking();
61 $request = $this->getRequest();
62 $target = $request->getVal( 'target', $par );
63 $namespace = $request->getIntOrNull( 'namespace' );
66 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
67 if ( $prot !== '//' ) {
68 $protocols_list[] = $prot;
72 $target2 = Parser
::normalizeLinkUrl( $target );
73 // Get protocol, default is http://
74 $protocol = 'http://';
75 $bits = wfParseUrl( $target );
76 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
77 $protocol = $bits['scheme'] . $bits['delimiter'];
78 // Make sure wfParseUrl() didn't make some well-intended correction in the
80 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
81 $target2 = substr( $target, strlen( $protocol ) );
83 // If it did, let LinkFilter::makeLikeArray() handle this
90 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
91 count( $protocols_list )
99 'label-message' => 'linksearch-pat',
100 'default' => $target,
104 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
107 'type' => 'namespaceselect',
108 'name' => 'namespace',
109 'label-message' => 'linksearch-ns',
110 'default' => $namespace,
113 'cssclass' => 'namespaceselector',
118 'title' => $this->getPageTitle()->getPrefixedDBkey(),
120 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
121 $htmlForm->addHiddenFields( $hiddenFields );
122 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
123 $htmlForm->setWrapperLegendMsg( 'linksearch' );
124 $htmlForm->setAction( wfScript() );
125 $htmlForm->setMethod( 'get' );
126 $htmlForm->prepareForm()->displayForm( false );
127 $this->addHelpLink( 'Help:Linksearch' );
129 if ( $target != '' ) {
132 'namespace' => $namespace,
133 'protocol' => $protocol ] );
134 parent
::execute( $par );
135 if ( $this->mungedQuery
=== false ) {
136 $out->addWikiMsg( 'linksearch-error' );
142 * Disable RSS/Atom feeds
145 function isSyndicated() {
149 function linkParameters() {
151 $params['target'] = $this->mProt
. $this->mQuery
;
152 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
153 $params['namespace'] = $this->mNs
;
159 public function getQueryInfo() {
160 $dbr = wfGetDB( DB_REPLICA
);
162 if ( $this->mQuery
=== '*' && $this->mProt
!== '' ) {
163 $this->mungedQuery
= [
164 'el_index_60' . $dbr->buildLike( $this->mProt
, $dbr->anyString() ),
167 $this->mungedQuery
= LinkFilter
::getQueryConditions( $this->mQuery
, [
168 'protocol' => $this->mProt
,
169 'oneWildcard' => true,
173 if ( $this->mungedQuery
=== false ) {
174 // Invalid query; return no results
175 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
179 if ( !isset( $this->mungedQuery
['el_index_60'] ) ) {
180 $orderBy[] = 'el_index_60';
182 $orderBy[] = 'el_id';
185 'tables' => [ 'page', 'externallinks' ],
187 'namespace' => 'page_namespace',
188 'title' => 'page_title',
189 'value' => 'el_index',
192 'conds' => array_merge(
198 'options' => [ 'ORDER BY' => $orderBy ]
201 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
202 $retval['conds']['page_namespace'] = $this->mNs
;
209 * Pre-fill the link cache
211 * @param IDatabase $db
212 * @param IResultWrapper $res
214 function preprocessResults( $db, $res ) {
215 $this->executeLBFromResultWrapper( $res );
220 * @param object $result Result row
223 function formatResult( $skin, $result ) {
224 $title = new TitleValue( (int)$result->namespace, $result->title
);
225 $pageLink = $this->getLinkRenderer()->makeLink( $title );
228 $urlLink = Linker
::makeExternalLink( $url, $url );
230 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
234 * Override to squash the ORDER BY.
235 * Not much point in descending order here.
238 function getOrderFields() {
242 protected function getGroupName() {
247 * enwiki complained about low limits on this special page
250 * @todo FIXME This special page should not use LIMIT for paging
253 protected function getMaxResults() {
254 return max( parent
::getMaxResults(), 60000 );