MediaWiki master
SpecialFilepath.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
27
34
35 private SearchEngineFactory $searchEngineFactory;
36
40 public function __construct(
41 SearchEngineFactory $searchEngineFactory
42 ) {
43 parent::__construct( 'Filepath' );
44 $this->mAllowedRedirectParams = [ 'width', 'height' ];
45 $this->searchEngineFactory = $searchEngineFactory;
46 }
47
54 public function getRedirect( $par ) {
55 $file = $par ?: $this->getRequest()->getText( 'file' );
56
57 $redirect = null;
58 if ( $file ) {
59 $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" );
60 }
61 if ( $redirect === null ) {
62 // The user input is empty or an invalid title,
63 // redirect to form of Special:Redirect with the invalid value prefilled
64 $this->mAddedRedirectParams['wpvalue'] = $file;
65 $redirect = SpecialPage::getSafeTitleFor( 'Redirect', 'file' );
66 }
67 // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid
68 return $redirect;
69 }
70
79 public function prefixSearchSubpages( $search, $limit, $offset ) {
80 $title = Title::newFromText( $search, NS_FILE );
81 if ( !$title || $title->getNamespace() !== NS_FILE ) {
82 // No prefix suggestion outside of file namespace
83 return [];
84 }
85 $searchEngine = $this->searchEngineFactory->create();
86 $searchEngine->setLimitOffset( $limit, $offset );
87 // Autocomplete subpage the same as a normal search, but just for files
88 $searchEngine->setNamespaces( [ NS_FILE ] );
89 $result = $searchEngine->defaultPrefixSearch( $search );
90
91 return array_map( static function ( Title $t ) {
92 // Remove namespace in search suggestion
93 return $t->getText();
94 }, $result );
95 }
96
97 protected function getGroupName() {
98 return 'media';
99 }
100}
101
103class_alias( SpecialFilepath::class, 'SpecialFilepath' );
const NS_FILE
Definition Defines.php:71
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.
Redirects to the URL of a thumbnail for the 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:79
Factory class for SearchEngine.