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