MediaWiki  master
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 }
SpecialLinkSearch\getMaxResults
getMaxResults()
enwiki complained about low limits on this special page
Definition: SpecialLinkSearch.php:264
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:744
SpecialLinkSearch
Special:LinkSearch to search the external-links table.
Definition: SpecialLinkSearch.php:34
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
SpecialLinkSearch\__construct
__construct(ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory)
Definition: SpecialLinkSearch.php:54
QueryPage\getDBLoadBalancer
getDBLoadBalancer()
Definition: QueryPage.php:900
SpecialLinkSearch\isSyndicated
isSyndicated()
Disable RSS/Atom feeds.
Definition: SpecialLinkSearch.php:158
LinkFilter\getQueryConditions
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
Definition: LinkFilter.php:256
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
SpecialLinkSearch\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialLinkSearch.php:172
SpecialLinkSearch\linkParameters
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
Definition: SpecialLinkSearch.php:162
SpecialLinkSearch\setParams
setParams( $params)
Definition: SpecialLinkSearch.php:44
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
$res
$res
Definition: testCompression.php:57
QueryPage
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
QueryPage\executeLBFromResultWrapper
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
Definition: QueryPage.php:874
SpecialLinkSearch\formatResult
formatResult( $skin, $result)
Definition: SpecialLinkSearch.php:234
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:54
QueryPage\setLinkBatchFactory
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:135
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:948
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:776
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:878
SpecialLinkSearch\$mQuery
string null $mQuery
Definition: SpecialLinkSearch.php:38
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2307
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:26
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:618
Linker\makeExternalLink
static makeExternalLink( $url, $text, $escape=true, $linktype='', $attribs=[], $title=null)
Make an external link.
Definition: Linker.php:1011
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:764
SpecialLinkSearch\$mProt
string null $mProt
Definition: SpecialLinkSearch.php:42
SpecialLinkSearch\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialLinkSearch.php:253
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:780
QueryPage\setDBLoadBalancer
setDBLoadBalancer(ILoadBalancer $loadBalancer)
Definition: QueryPage.php:892
SpecialLinkSearch\getOrderFields
getOrderFields()
Override to squash the ORDER BY.
Definition: SpecialLinkSearch.php:249
QueryPage\$linkBatchFactory
LinkBatchFactory null $linkBatchFactory
Definition: QueryPage.php:74
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1028
SpecialLinkSearch\isCacheable
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Definition: SpecialLinkSearch.php:63
Parser\normalizeLinkUrl
static normalizeLinkUrl( $url)
Replace unusual escape codes in a URL with their equivalent characters.
Definition: Parser.php:2287
SpecialLinkSearch\preprocessResults
preprocessResults( $db, $res)
Pre-fill the link cache.
Definition: SpecialLinkSearch.php:225
SpecialLinkSearch\$mungedQuery
array bool $mungedQuery
Definition: SpecialLinkSearch.php:36
SpecialLinkSearch\$mNs
int null $mNs
Definition: SpecialLinkSearch.php:40
QueryPage\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: QueryPage.php:71
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:326
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:709
SpecialLinkSearch\execute
execute( $par)
This is the actual workhorse.
Definition: SpecialLinkSearch.php:67
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40