MediaWiki REL1_39
SpecialLinkSearch.php
Go to the documentation of this file.
1<?php
30
37 private $mungedQuery = false;
39 private $mQuery;
41 private $mNs;
43 private $mProt;
44
45 private function setParams( $params ) {
46 $this->mQuery = $params['query'];
47 $this->mNs = $params['namespace'];
48 $this->mProt = $params['protocol'];
49 }
50
55 public function __construct(
56 ILoadBalancer $loadBalancer,
57 LinkBatchFactory $linkBatchFactory
58 ) {
59 parent::__construct( 'LinkSearch' );
60 $this->setDBLoadBalancer( $loadBalancer );
61 $this->setLinkBatchFactory( $linkBatchFactory );
62 }
63
64 public function isCacheable() {
65 return false;
66 }
67
68 public function execute( $par ) {
69 $this->setHeaders();
70 $this->outputHeader();
71
72 $out = $this->getOutput();
73 $out->setPreventClickjacking( false );
74
75 $request = $this->getRequest();
76 $target = $request->getVal( 'target', $par ?? '' );
77 $namespace = $request->getIntOrNull( 'namespace' );
78
79 $protocols_list = [];
80 foreach ( $this->getConfig()->get( MainConfigNames::UrlProtocols ) as $prot ) {
81 if ( $prot !== '//' ) {
82 $protocols_list[] = $prot;
83 }
84 }
85
86 $target2 = Parser::normalizeLinkUrl( $target );
87 // Get protocol, default is http://
88 $protocol = 'http://';
89 $bits = wfParseUrl( $target );
90 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
91 $protocol = $bits['scheme'] . $bits['delimiter'];
92 // Make sure wfParseUrl() didn't make some well-intended correction in the
93 // protocol
94 if ( str_starts_with( strtolower( $target ), strtolower( $protocol ) ) ) {
95 $target2 = substr( $target, strlen( $protocol ) );
96 } else {
97 // If it did, let LinkFilter::makeLikeArray() handle this
98 $protocol = '';
99 }
100 }
101
102 $out->addWikiMsg(
103 'linksearch-text',
104 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
105 count( $protocols_list )
106 );
107 $fields = [
108 'target' => [
109 'type' => 'text',
110 'name' => 'target',
111 'id' => 'target',
112 'size' => 50,
113 'label-message' => 'linksearch-pat',
114 'default' => $target,
115 'dir' => 'ltr',
116 ]
117 ];
118 if ( !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
119 $fields += [
120 'namespace' => [
121 'type' => 'namespaceselect',
122 'name' => 'namespace',
123 'label-message' => 'linksearch-ns',
124 'default' => $namespace,
125 'id' => 'namespace',
126 'all' => '',
127 'cssclass' => 'namespaceselector',
128 ],
129 ];
130 }
131 $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
132 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
133 $htmlForm->setWrapperLegendMsg( 'linksearch' );
134 $htmlForm->setTitle( $this->getPageTitle() );
135 $htmlForm->setMethod( 'get' );
136 $htmlForm->prepareForm()->displayForm( false );
137 $this->addHelpLink( 'Help:Linksearch' );
138
139 if ( $target != '' ) {
140 $this->setParams( [
141 'query' => $target2,
142 'namespace' => $namespace,
143 'protocol' => $protocol ] );
144 parent::execute( $par );
145 if ( $this->mungedQuery === false ) {
146 $out->addWikiMsg( 'linksearch-error' );
147 }
148 }
149 }
150
155 public function isSyndicated() {
156 return false;
157 }
158
159 protected function linkParameters() {
160 $params = [];
161 $params['target'] = $this->mProt . $this->mQuery;
162 if ( $this->mNs !== null && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
163 $params['namespace'] = $this->mNs;
164 }
165
166 return $params;
167 }
168
169 public function getQueryInfo() {
170 $dbr = $this->getDBLoadBalancer()->getConnectionRef( ILoadBalancer::DB_REPLICA );
171
172 $orderBy = [];
173 if ( $this->mQuery === '*' && $this->mProt !== '' ) {
174 $this->mungedQuery = [
175 'el_index_60' . $dbr->buildLike( $this->mProt, $dbr->anyString() ),
176 ];
177 } else {
178 $this->mungedQuery = LinkFilter::getQueryConditions( $this->mQuery, [
179 'protocol' => $this->mProt,
180 'oneWildcard' => true,
181 'db' => $dbr
182 ] );
183 if ( $this->mungedQuery === false ) {
184 // Invalid query; return no results
185 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
186 }
187 $orderBy[] = 'el_index_60';
188 }
189
190 $orderBy[] = 'el_id';
191
192 $retval = [
193 'tables' => [ 'page', 'externallinks' ],
194 'fields' => [
195 'namespace' => 'page_namespace',
196 'title' => 'page_title',
197 'value' => 'el_index',
198 'url' => 'el_to'
199 ],
200 'conds' => array_merge(
201 [
202 'page_id = el_from',
203 ],
204 $this->mungedQuery
205 ),
206 'options' => [ 'ORDER BY' => $orderBy ]
207 ];
208
209 if ( $this->mNs !== null && !$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
210 $retval['conds']['page_namespace'] = $this->mNs;
211 }
212
213 return $retval;
214 }
215
222 public function preprocessResults( $db, $res ) {
224 }
225
231 public function formatResult( $skin, $result ) {
232 $title = new TitleValue( (int)$result->namespace, $result->title );
233 $pageLink = $this->getLinkRenderer()->makeLink( $title );
234
235 $url = $result->url;
236 $urlLink = Linker::makeExternalLink( $url, $url );
237
238 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
239 }
240
246 protected function getOrderFields() {
247 return [];
248 }
249
250 protected function getGroupName() {
251 return 'pages';
252 }
253
261 protected function getMaxResults() {
262 return max( parent::getMaxResults(), 60000 );
263 }
264}
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
static makeExternalLink( $url, $text, $escape=true, $linktype='', $attribs=[], $title=null)
Make an external link.
Definition Linker.php:1061
A class containing constants representing the names of configuration variables.
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition Parser.php:2317
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:42
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
setDBLoadBalancer(ILoadBalancer $loadBalancer)
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
getDBLoadBalancer()
Special:LinkSearch to search the external-links table.
execute( $par)
This is the actual workhorse.
isSyndicated()
Disable RSS/Atom feeds.
getOrderFields()
Override to squash the ORDER BY.
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
formatResult( $skin, $result)
getMaxResults()
enwiki complained about low limits on this special page
__construct(ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
preprocessResults( $db, $res)
Pre-fill the link cache.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Represents a page (or page fragment) title within MediaWiki.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
Create and track the database connections and transactions for a given database cluster.
Result wrapper for grabbing data queried from an IDatabase object.