MediaWiki  1.34.0
SpecialLinkSearch.php
Go to the documentation of this file.
1 <?php
27 
34  private $mungedQuery = false;
36  private $mQuery;
38  private $mNs;
40  private $mProt;
41 
42  function setParams( $params ) {
43  $this->mQuery = $params['query'];
44  $this->mNs = $params['namespace'];
45  $this->mProt = $params['protocol'];
46  }
47 
48  function __construct( $name = 'LinkSearch' ) {
49  parent::__construct( $name );
50 
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.
54  }
55 
56  function isCacheable() {
57  return false;
58  }
59 
60  public function execute( $par ) {
61  $this->setHeaders();
62  $this->outputHeader();
63 
64  $out = $this->getOutput();
65  $out->allowClickjacking();
66 
67  $request = $this->getRequest();
68  $target = $request->getVal( 'target', $par );
69  $namespace = $request->getIntOrNull( 'namespace' );
70 
71  $protocols_list = [];
72  foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
73  if ( $prot !== '//' ) {
74  $protocols_list[] = $prot;
75  }
76  }
77 
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
85  // protocol
86  if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
87  $target2 = substr( $target, strlen( $protocol ) );
88  } else {
89  // If it did, let LinkFilter::makeLikeArray() handle this
90  $protocol = '';
91  }
92  }
93 
94  $out->addWikiMsg(
95  'linksearch-text',
96  '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
97  count( $protocols_list )
98  );
99  $fields = [
100  'target' => [
101  'type' => 'text',
102  'name' => 'target',
103  'id' => 'target',
104  'size' => 50,
105  'label-message' => 'linksearch-pat',
106  'default' => $target,
107  'dir' => 'ltr',
108  ]
109  ];
110  if ( !$this->getConfig()->get( 'MiserMode' ) ) {
111  $fields += [
112  'namespace' => [
113  'type' => 'namespaceselect',
114  'name' => 'namespace',
115  'label-message' => 'linksearch-ns',
116  'default' => $namespace,
117  'id' => 'namespace',
118  'all' => '',
119  'cssclass' => 'namespaceselector',
120  ],
121  ];
122  }
123  $hiddenFields = [
124  'title' => $this->getPageTitle()->getPrefixedDBkey(),
125  ];
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' );
134 
135  if ( $target != '' ) {
136  $this->setParams( [
137  'query' => $target2,
138  'namespace' => $namespace,
139  'protocol' => $protocol ] );
140  parent::execute( $par );
141  if ( $this->mungedQuery === false ) {
142  $out->addWikiMsg( 'linksearch-error' );
143  }
144  }
145  }
146 
151  function isSyndicated() {
152  return false;
153  }
154 
155  function linkParameters() {
156  $params = [];
157  $params['target'] = $this->mProt . $this->mQuery;
158  if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
159  $params['namespace'] = $this->mNs;
160  }
161 
162  return $params;
163  }
164 
165  public function getQueryInfo() {
166  $dbr = wfGetDB( DB_REPLICA );
167 
168  if ( $this->mQuery === '*' && $this->mProt !== '' ) {
169  $this->mungedQuery = [
170  'el_index_60' . $dbr->buildLike( $this->mProt, $dbr->anyString() ),
171  ];
172  } else {
173  $this->mungedQuery = LinkFilter::getQueryConditions( $this->mQuery, [
174  'protocol' => $this->mProt,
175  'oneWildcard' => true,
176  'db' => $dbr
177  ] );
178  }
179  if ( $this->mungedQuery === false ) {
180  // Invalid query; return no results
181  return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
182  }
183 
184  $orderBy = [];
185  if ( !isset( $this->mungedQuery['el_index_60'] ) ) {
186  $orderBy[] = 'el_index_60';
187  }
188  $orderBy[] = 'el_id';
189 
190  $retval = [
191  'tables' => [ 'page', 'externallinks' ],
192  'fields' => [
193  'namespace' => 'page_namespace',
194  'title' => 'page_title',
195  'value' => 'el_index',
196  'url' => 'el_to'
197  ],
198  'conds' => array_merge(
199  [
200  'page_id = el_from',
201  ],
202  $this->mungedQuery
203  ),
204  'options' => [ 'ORDER BY' => $orderBy ]
205  ];
206 
207  if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
208  $retval['conds']['page_namespace'] = $this->mNs;
209  }
210 
211  return $retval;
212  }
213 
220  function preprocessResults( $db, $res ) {
222  }
223 
229  function formatResult( $skin, $result ) {
230  $title = new TitleValue( (int)$result->namespace, $result->title );
231  $pageLink = $this->getLinkRenderer()->makeLink( $title );
232 
233  $url = $result->url;
234  $urlLink = Linker::makeExternalLink( $url, $url );
235 
236  return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
237  }
238 
244  function getOrderFields() {
245  return [];
246  }
247 
248  protected function getGroupName() {
249  return 'redirects';
250  }
251 
259  protected function getMaxResults() {
260  return max( parent::getMaxResults(), 60000 );
261  }
262 }
SpecialLinkSearch\getMaxResults
getMaxResults()
enwiki complained about low limits on this special page
Definition: SpecialLinkSearch.php:259
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:672
SpecialLinkSearch
Special:LinkSearch to search the external-links table.
Definition: SpecialLinkSearch.php:32
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
SpecialLinkSearch\isSyndicated
isSyndicated()
Disable RSS/Atom feeds.
Definition: SpecialLinkSearch.php:151
LinkFilter\getQueryConditions
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
Definition: LinkFilter.php:254
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialLinkSearch\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialLinkSearch.php:165
SpecialLinkSearch\linkParameters
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
Definition: SpecialLinkSearch.php:155
SpecialLinkSearch\setParams
setParams( $params)
Definition: SpecialLinkSearch.php:42
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:749
$res
$res
Definition: testCompression.php:52
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:36
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:800
SpecialLinkSearch\formatResult
formatResult( $skin, $result)
Definition: SpecialLinkSearch.php:229
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:50
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:828
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:793
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:758
SpecialLinkSearch\$mQuery
string null $mQuery
Definition: SpecialLinkSearch.php:36
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2642
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
$title
$title
Definition: testCompression.php:34
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
Linker\makeExternalLink
static makeExternalLink( $url, $text, $escape=true, $linktype='', $attribs=[], $title=null)
Make an external link.
Definition: Linker.php:848
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
SpecialLinkSearch\$mProt
string null $mProt
Definition: SpecialLinkSearch.php:40
SpecialLinkSearch\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialLinkSearch.php:248
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
SpecialLinkSearch\getOrderFields
getOrderFields()
Override to squash the ORDER BY.
Definition: SpecialLinkSearch.php:244
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:904
SpecialLinkSearch\isCacheable
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
Definition: SpecialLinkSearch.php:56
SpecialLinkSearch\preprocessResults
preprocessResults( $db, $res)
Pre-fill the link cache.
Definition: SpecialLinkSearch.php:220
SpecialLinkSearch\$mungedQuery
array bool $mungedQuery
Definition: SpecialLinkSearch.php:34
SpecialLinkSearch\$mNs
int null $mNs
Definition: SpecialLinkSearch.php:38
SpecialLinkSearch\__construct
__construct( $name='LinkSearch')
Definition: SpecialLinkSearch.php:48
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:303
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:639
SpecialLinkSearch\execute
execute( $par)
This is the actual workhorse.
Definition: SpecialLinkSearch.php:60
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36