MediaWiki master
SpecialLinkSearch.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
32use stdClass;
38
47 private $mungedQuery = false;
49 private $mQuery;
51 private $mNs;
53 private $mProt;
54
55 private UrlUtils $urlUtils;
56
57 private function setParams( array $params ) {
58 $this->mQuery = $params['query'];
59 $this->mNs = $params['namespace'];
60 $this->mProt = $params['protocol'];
61 }
62
63 public function __construct(
64 IConnectionProvider $dbProvider,
65 LinkBatchFactory $linkBatchFactory,
66 UrlUtils $urlUtils
67 ) {
68 parent::__construct( 'LinkSearch' );
69 $this->setDatabaseProvider( $dbProvider );
70 $this->setLinkBatchFactory( $linkBatchFactory );
71 $this->urlUtils = $urlUtils;
72 }
73
74 public function isCacheable() {
75 return false;
76 }
77
78 public function execute( $par ) {
79 $this->setHeaders();
80 $this->outputHeader();
81
82 $out = $this->getOutput();
83 $out->getMetadata()->setPreventClickjacking( false );
84
85 $request = $this->getRequest();
86 $target = $request->getVal( 'target', $par ?? '' );
87 $namespace = $request->getIntOrNull( 'namespace' );
88
89 $protocols_list = [];
90 foreach ( $this->getConfig()->get( MainConfigNames::UrlProtocols ) as $prot ) {
91 if ( $prot !== '//' ) {
92 $protocols_list[] = $prot;
93 }
94 }
95
96 $target2 = Parser::normalizeLinkUrl( $target );
97 $protocol = null;
98 $bits = $this->urlUtils->parse( $target );
99 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
100 $protocol = $bits['scheme'] . $bits['delimiter'];
101 // Make sure UrlUtils::parse() didn't make some well-intended correction in the protocol
102 if ( str_starts_with( strtolower( $target ), strtolower( $protocol ) ) ) {
103 $target2 = substr( $target, strlen( $protocol ) );
104 } else {
105 // If it did, let LinkFilter::makeLikeArray() handle this
106 $protocol = '';
107 }
108 }
109
110 $out->addWikiMsg(
111 'linksearch-text',
112 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
113 count( $protocols_list )
114 );
115 $fields = [
116 'target' => [
117 'type' => 'text',
118 'name' => 'target',
119 'id' => 'target',
120 'size' => 50,
121 'label-message' => 'linksearch-pat',
122 'default' => $target,
123 'dir' => 'ltr',
124 ]
125 ];
126 if ( !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
127 $fields += [
128 'namespace' => [
129 'type' => 'namespaceselect',
130 'name' => 'namespace',
131 'label-message' => 'linksearch-ns',
132 'default' => $namespace,
133 'id' => 'namespace',
134 'all' => '',
135 'cssclass' => 'namespaceselector',
136 ],
137 ];
138 }
139 $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
140 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
141 $htmlForm->setWrapperLegendMsg( 'linksearch' );
142 $htmlForm->setTitle( $this->getPageTitle() );
143 $htmlForm->setMethod( 'get' );
144 $htmlForm->prepareForm()->displayForm( false );
145 $this->addHelpLink( 'Help:Linksearch' );
146
147 if ( $target != '' ) {
148 $this->setParams( [
149 'query' => $target2,
150 'namespace' => $namespace,
151 'protocol' => $protocol ] );
152 parent::execute( $par );
153 if ( $this->mungedQuery === false ) {
154 $out->addWikiMsg( 'linksearch-error' );
155 }
156 }
157 }
158
163 public function isSyndicated() {
164 return false;
165 }
166
167 protected function linkParameters() {
168 $params = [];
169 $params['target'] = $this->mProt . $this->mQuery;
170 if ( $this->mNs !== null && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
171 $params['namespace'] = $this->mNs;
172 }
173
174 return $params;
175 }
176
177 public function getQueryInfo() {
178 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
179
180 $field = 'el_to_domain_index';
181 $extraFields = [
182 'urldomain' => 'el_to_domain_index',
183 'urlpath' => 'el_to_path'
184 ];
185 if ( $this->mQuery === '*' && $this->mProt !== '' ) {
186 if ( $this->mProt !== null ) {
187 $this->mungedQuery = [
188 $dbr->expr( $field, IExpression::LIKE, new LikeValue( $this->mProt, $dbr->anyString() ) ),
189 ];
190 } else {
191 $this->mungedQuery = [
192 $dbr->expr( $field, IExpression::LIKE, new LikeValue( 'http://', $dbr->anyString() ) )
193 ->or( $field, IExpression::LIKE, new LikeValue( 'https://', $dbr->anyString() ) ),
194 ];
195 }
196 } else {
197 $this->mungedQuery = LinkFilter::getQueryConditions( $this->mQuery, [
198 'protocol' => $this->mProt,
199 'oneWildcard' => true,
200 'db' => $dbr
201 ] );
202 if ( $this->mungedQuery === false ) {
203 // Invalid query; return no results
204 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
205 }
206 }
207 $orderBy = [ 'el_id' ];
208
209 $retval = [
210 'tables' => [ 'page', 'externallinks' ],
211 'fields' => array_merge( [
212 'namespace' => 'page_namespace',
213 'title' => 'page_title',
214 ], $extraFields ),
215 'conds' => array_merge(
216 [
217 'page_id = el_from',
218 ],
219 $this->mungedQuery
220 ),
221 'options' => [ 'ORDER BY' => $orderBy ]
222 ];
223
224 if ( $this->mNs !== null && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
225 $retval['conds']['page_namespace'] = $this->mNs;
226 }
227
228 return $retval;
229 }
230
237 public function preprocessResults( $db, $res ) {
238 $this->executeLBFromResultWrapper( $res );
239 }
240
246 public function formatResult( $skin, $result ) {
247 $title = new TitleValue( (int)$result->namespace, $result->title );
248 $pageLink = $this->getLinkRenderer()->makeLink( $title );
249 $url = LinkFilter::reverseIndexes( $result->urldomain ) . $result->urlpath;
250
251 $urlLink = $this->getLinkRenderer()->makeExternalLink( $url, $url, $this->getFullTitle() );
252
253 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
254 }
255
261 protected function getOrderFields() {
262 return [];
263 }
264
265 protected function getGroupName() {
266 return 'pages';
267 }
268
276 protected function getMaxResults() {
277 return max( parent::getMaxResults(), 60000 );
278 }
279}
280
282class_alias( SpecialLinkSearch::class, 'SpecialLinkSearch' );
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
A class containing constants representing the names of configuration variables.
const UrlProtocols
Name constant for the UrlProtocols setting, for use with Config::get()
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:147
The base class for all skins.
Definition Skin.php:58
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:87
setDatabaseProvider(IConnectionProvider $databaseProvider)
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getPageTitle( $subpage=false)
Get a self-referential title object.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getLanguage()
Shortcut to get user's language.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
getFullTitle()
Return the full title, including $par.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Special:LinkSearch to search the external-links table.
getOrderFields()
Override to squash the ORDER BY.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
execute( $par)
This is the actual workhorse.
__construct(IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory, UrlUtils $urlUtils)
getMaxResults()
enwiki complained about low limits on this special page
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
preprocessResults( $db, $res)
Pre-fill the link cache.
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Represents the target of a wiki link.
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16
Content of like value.
Definition LikeValue.php:14
Provide primary and replica IDatabase connections.
Interface to a relational database.
Definition IDatabase.php:45
Result wrapper for grabbing data queried from an IDatabase object.