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 SpecialLinkSearch
extends QueryPage
{
33 /** @var array|bool */
34 private $mungedQuery = false;
35 /** @var string|null */
39 /** @var string|null */
42 function setParams( $params ) {
43 $this->mQuery
= $params['query'];
44 $this->mNs
= $params['namespace'];
45 $this->mProt
= $params['protocol'];
48 function __construct( $name = 'LinkSearch' ) {
49 parent
::__construct( $name );
51 // Since we don't control the constructor parameters, we can't inject services that way.
52 // Instead, we initialize services in the execute() method, and allow them to be overridden
53 // using the setServices() method.
56 function isCacheable() {
60 public function execute( $par ) {
62 $this->outputHeader();
64 $out = $this->getOutput();
65 $out->allowClickjacking();
67 $request = $this->getRequest();
68 $target = $request->getVal( 'target', $par );
69 $namespace = $request->getIntOrNull( 'namespace' );
72 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
73 if ( $prot !== '//' ) {
74 $protocols_list[] = $prot;
78 $target2 = Parser
::normalizeLinkUrl( $target );
79 // Get protocol, default is http://
80 $protocol = 'http://';
81 $bits = wfParseUrl( $target );
82 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
83 $protocol = $bits['scheme'] . $bits['delimiter'];
84 // Make sure wfParseUrl() didn't make some well-intended correction in the
86 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
87 $target2 = substr( $target, strlen( $protocol ) );
89 // If it did, let LinkFilter::makeLikeArray() handle this
96 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
97 count( $protocols_list )
105 'label-message' => 'linksearch-pat',
106 'default' => $target,
110 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
113 'type' => 'namespaceselect',
114 'name' => 'namespace',
115 'label-message' => 'linksearch-ns',
116 'default' => $namespace,
119 'cssclass' => 'namespaceselector',
124 'title' => $this->getPageTitle()->getPrefixedDBkey(),
126 $htmlForm = HTMLForm
::factory( 'ooui', $fields, $this->getContext() );
127 $htmlForm->addHiddenFields( $hiddenFields );
128 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
129 $htmlForm->setWrapperLegendMsg( 'linksearch' );
130 $htmlForm->setAction( wfScript() );
131 $htmlForm->setMethod( 'get' );
132 $htmlForm->prepareForm()->displayForm( false );
133 $this->addHelpLink( 'Help:Linksearch' );
135 if ( $target != '' ) {
138 'namespace' => $namespace,
139 'protocol' => $protocol ] );
140 parent
::execute( $par );
141 if ( $this->mungedQuery
=== false ) {
142 $out->addWikiMsg( 'linksearch-error' );
148 * Disable RSS/Atom feeds
151 function isSyndicated() {
155 function linkParameters() {
157 $params['target'] = $this->mProt
. $this->mQuery
;
158 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
159 $params['namespace'] = $this->mNs
;
165 public function getQueryInfo() {
166 $dbr = wfGetDB( DB_REPLICA
);
168 if ( $this->mQuery
=== '*' && $this->mProt
!== '' ) {
169 $this->mungedQuery
= [
170 'el_index_60' . $dbr->buildLike( $this->mProt
, $dbr->anyString() ),
173 $this->mungedQuery
= LinkFilter
::getQueryConditions( $this->mQuery
, [
174 'protocol' => $this->mProt
,
175 'oneWildcard' => true,
179 if ( $this->mungedQuery
=== false ) {
180 // Invalid query; return no results
181 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
185 if ( !isset( $this->mungedQuery
['el_index_60'] ) ) {
186 $orderBy[] = 'el_index_60';
188 $orderBy[] = 'el_id';
191 'tables' => [ 'page', 'externallinks' ],
193 'namespace' => 'page_namespace',
194 'title' => 'page_title',
195 'value' => 'el_index',
198 'conds' => array_merge(
204 'options' => [ 'ORDER BY' => $orderBy ]
207 if ( $this->mNs
!== null && !$this->getConfig()->get( 'MiserMode' ) ) {
208 $retval['conds']['page_namespace'] = $this->mNs
;
215 * Pre-fill the link cache
217 * @param IDatabase $db
218 * @param IResultWrapper $res
220 function preprocessResults( $db, $res ) {
221 $this->executeLBFromResultWrapper( $res );
226 * @param object $result Result row
229 function formatResult( $skin, $result ) {
230 $title = new TitleValue( (int)$result->namespace, $result->title
);
231 $pageLink = $this->getLinkRenderer()->makeLink( $title );
234 $urlLink = Linker
::makeExternalLink( $url, $url );
236 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
240 * Override to squash the ORDER BY.
241 * Not much point in descending order here.
244 function getOrderFields() {
248 protected function getGroupName() {
253 * enwiki complained about low limits on this special page
256 * @todo FIXME This special page should not use LIMIT for paging
259 protected function getMaxResults() {
260 return max( parent
::getMaxResults(), 60000 );