MediaWiki master
SpecialFilepath.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
13
20
21 private SearchEngineFactory $searchEngineFactory;
22
23 public function __construct(
24 SearchEngineFactory $searchEngineFactory
25 ) {
26 parent::__construct( 'Filepath' );
27 $this->mAllowedRedirectParams = [ 'width', 'height' ];
28 $this->searchEngineFactory = $searchEngineFactory;
29 }
30
37 public function getRedirect( $par ) {
38 $file = $par ?: $this->getRequest()->getText( 'file' );
39
40 $redirect = null;
41 if ( $file ) {
42 $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" );
43 }
44 if ( $redirect === null ) {
45 // The user input is empty or an invalid title,
46 // redirect to form of Special:Redirect with the invalid value prefilled
47 $this->mAddedRedirectParams['wpvalue'] = $file;
48 $redirect = SpecialPage::getSafeTitleFor( 'Redirect', 'file' );
49 }
50 // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid
51 return $redirect;
52 }
53
62 public function prefixSearchSubpages( $search, $limit, $offset ) {
63 $title = Title::newFromText( $search, NS_FILE );
64 if ( !$title || $title->getNamespace() !== NS_FILE ) {
65 // No prefix suggestion outside of file namespace
66 return [];
67 }
68 $searchEngine = $this->searchEngineFactory->create();
69 $searchEngine->setLimitOffset( $limit, $offset );
70 // Autocomplete subpage the same as a normal search, but just for files
71 $searchEngine->setNamespaces( [ NS_FILE ] );
72 $result = $searchEngine->defaultPrefixSearch( $search );
73
74 return array_map( static function ( Title $t ) {
75 // Remove namespace in search suggestion
76 return $t->getText();
77 }, $result );
78 }
79
81 protected function getGroupName() {
82 return 'media';
83 }
84}
85
87class_alias( SpecialFilepath::class, 'SpecialFilepath' );
const NS_FILE
Definition Defines.php:57
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:69
Factory class for SearchEngine.