MediaWiki master
SpecialFilepath.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
30
37
38 private SearchEngineFactory $searchEngineFactory;
39
43 public function __construct(
44 SearchEngineFactory $searchEngineFactory
45 ) {
46 parent::__construct( 'Filepath' );
47 $this->mAllowedRedirectParams = [ 'width', 'height' ];
48 $this->searchEngineFactory = $searchEngineFactory;
49 }
50
57 public function getRedirect( $par ) {
58 $file = $par ?: $this->getRequest()->getText( 'file' );
59
60 $redirect = null;
61 if ( $file ) {
62 $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" );
63 }
64 if ( $redirect === null ) {
65 // The user input is empty or an invalid title,
66 // redirect to form of Special:Redirect with the invalid value prefilled
67 $this->mAddedRedirectParams['wpvalue'] = $file;
68 $redirect = SpecialPage::getSafeTitleFor( 'Redirect', 'file' );
69 }
70 // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid
71 return $redirect;
72 }
73
82 public function prefixSearchSubpages( $search, $limit, $offset ) {
83 $title = Title::newFromText( $search, NS_FILE );
84 if ( !$title || $title->getNamespace() !== NS_FILE ) {
85 // No prefix suggestion outside of file namespace
86 return [];
87 }
88 $searchEngine = $this->searchEngineFactory->create();
89 $searchEngine->setLimitOffset( $limit, $offset );
90 // Autocomplete subpage the same as a normal search, but just for files
91 $searchEngine->setNamespaces( [ NS_FILE ] );
92 $result = $searchEngine->defaultPrefixSearch( $search );
93
94 return array_map( static function ( Title $t ) {
95 // Remove namespace in search suggestion
96 return $t->getText();
97 }, $result );
98 }
99
100 protected function getGroupName() {
101 return 'media';
102 }
103}
104
106class_alias( SpecialFilepath::class, 'SpecialFilepath' );
const NS_FILE
Definition Defines.php:70
Shortcut to construct a special page alias.
Parent class for all special pages.
static getSafeTitleFor( $name, $subpage=false)
Get a localised Title object for a page name with a possibly unvalidated subpage.
getRequest()
Get the WebRequest being used for this instance.
A special page that redirects to the URL of a given file.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(SearchEngineFactory $searchEngineFactory)
getRedirect( $par)
Implement by redirecting through Special:Redirect/file.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Represents a title within MediaWiki.
Definition Title.php:78
Factory class for SearchEngine.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...