MediaWiki REL1_37
SpecialLinkSearch.php
Go to the documentation of this file.
1<?php
29
36 private $mungedQuery = false;
38 private $mQuery;
40 private $mNs;
42 private $mProt;
43
44 private function setParams( $params ) {
45 $this->mQuery = $params['query'];
46 $this->mNs = $params['namespace'];
47 $this->mProt = $params['protocol'];
48 }
49
54 public function __construct(
57 ) {
58 parent::__construct( 'LinkSearch' );
59 $this->setDBLoadBalancer( $loadBalancer );
60 $this->setLinkBatchFactory( $linkBatchFactory );
61 }
62
63 public function isCacheable() {
64 return false;
65 }
66
67 public function execute( $par ) {
68 $this->setHeaders();
69 $this->outputHeader();
70
71 $out = $this->getOutput();
72 $out->allowClickjacking();
73
74 $request = $this->getRequest();
75 $target = $request->getVal( 'target', $par ?? '' );
76 $namespace = $request->getIntOrNull( 'namespace' );
77
78 $protocols_list = [];
79 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
80 if ( $prot !== '//' ) {
81 $protocols_list[] = $prot;
82 }
83 }
84
85 $target2 = Parser::normalizeLinkUrl( $target );
86 // Get protocol, default is http://
87 $protocol = 'http://';
88 $bits = wfParseUrl( $target );
89 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
90 $protocol = $bits['scheme'] . $bits['delimiter'];
91 // Make sure wfParseUrl() didn't make some well-intended correction in the
92 // protocol
93 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
94 $target2 = substr( $target, strlen( $protocol ) );
95 } else {
96 // If it did, let LinkFilter::makeLikeArray() handle this
97 $protocol = '';
98 }
99 }
100
101 $out->addWikiMsg(
102 'linksearch-text',
103 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
104 count( $protocols_list )
105 );
106 $fields = [
107 'target' => [
108 'type' => 'text',
109 'name' => 'target',
110 'id' => 'target',
111 'size' => 50,
112 'label-message' => 'linksearch-pat',
113 'default' => $target,
114 'dir' => 'ltr',
115 ]
116 ];
117 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
118 $fields += [
119 'namespace' => [
120 'type' => 'namespaceselect',
121 'name' => 'namespace',
122 'label-message' => 'linksearch-ns',
123 'default' => $namespace,
124 'id' => 'namespace',
125 'all' => '',
126 'cssclass' => 'namespaceselector',
127 ],
128 ];
129 }
130 $hiddenFields = [
131 'title' => $this->getPageTitle()->getPrefixedDBkey(),
132 ];
133 $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
134 $htmlForm->addHiddenFields( $hiddenFields );
135 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
136 $htmlForm->setWrapperLegendMsg( 'linksearch' );
137 $htmlForm->setAction( wfScript() );
138 $htmlForm->setMethod( 'get' );
139 $htmlForm->prepareForm()->displayForm( false );
140 $this->addHelpLink( 'Help:Linksearch' );
141
142 if ( $target != '' ) {
143 $this->setParams( [
144 'query' => $target2,
145 'namespace' => $namespace,
146 'protocol' => $protocol ] );
147 parent::execute( $par );
148 if ( $this->mungedQuery === false ) {
149 $out->addWikiMsg( 'linksearch-error' );
150 }
151 }
152 }
153
158 public function isSyndicated() {
159 return false;
160 }
161
162 protected function linkParameters() {
163 $params = [];
164 $params['target'] = $this->mProt . $this->mQuery;
165 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
166 $params['namespace'] = $this->mNs;
167 }
168
169 return $params;
170 }
171
172 public function getQueryInfo() {
173 $dbr = $this->getDBLoadBalancer()->getConnectionRef( ILoadBalancer::DB_REPLICA );
174
175 $orderBy = [];
176 if ( $this->mQuery === '*' && $this->mProt !== '' ) {
177 $this->mungedQuery = [
178 'el_index_60' . $dbr->buildLike( $this->mProt, $dbr->anyString() ),
179 ];
180 } else {
181 $this->mungedQuery = LinkFilter::getQueryConditions( $this->mQuery, [
182 'protocol' => $this->mProt,
183 'oneWildcard' => true,
184 'db' => $dbr
185 ] );
186 if ( $this->mungedQuery === false ) {
187 // Invalid query; return no results
188 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
189 }
190 $orderBy[] = 'el_index_60';
191 }
192
193 $orderBy[] = 'el_id';
194
195 $retval = [
196 'tables' => [ 'page', 'externallinks' ],
197 'fields' => [
198 'namespace' => 'page_namespace',
199 'title' => 'page_title',
200 'value' => 'el_index',
201 'url' => 'el_to'
202 ],
203 'conds' => array_merge(
204 [
205 'page_id = el_from',
206 ],
207 $this->mungedQuery
208 ),
209 'options' => [ 'ORDER BY' => $orderBy ]
210 ];
211
212 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
213 $retval['conds']['page_namespace'] = $this->mNs;
214 }
215
216 return $retval;
217 }
218
225 public function preprocessResults( $db, $res ) {
227 }
228
234 public function formatResult( $skin, $result ) {
235 $title = new TitleValue( (int)$result->namespace, $result->title );
236 $pageLink = $this->getLinkRenderer()->makeLink( $title );
237
238 $url = $result->url;
239 $urlLink = Linker::makeExternalLink( $url, $url );
240
241 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
242 }
243
249 protected function getOrderFields() {
250 return [];
251 }
252
253 protected function getGroupName() {
254 return 'pages';
255 }
256
264 protected function getMaxResults() {
265 return max( parent::getMaxResults(), 60000 );
266 }
267}
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
static makeExternalLink( $url, $text, $escape=true, $linktype='', $attribs=[], $title=null)
Make an external link.
Definition Linker.php:1011
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition Parser.php:2287
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:41
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)
LinkBatchFactory null $linkBatchFactory
Definition QueryPage.php:74
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
getDBLoadBalancer()
ILoadBalancer null $loadBalancer
Definition QueryPage.php:71
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:38
Database cluster connection, tracking, load balancing, and transaction manager interface.
Result wrapper for grabbing data queried from an IDatabase object.