MediaWiki  master
SpecialFilepath.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Specials;
25 
29 use SpecialPage;
30 
37 
39  private $searchEngineFactory;
40 
44  public function __construct(
45  SearchEngineFactory $searchEngineFactory
46  ) {
47  parent::__construct( 'Filepath' );
48  $this->mAllowedRedirectParams = [ 'width', 'height' ];
49  $this->searchEngineFactory = $searchEngineFactory;
50  }
51 
58  public function getRedirect( $par ) {
59  $file = $par ?: $this->getRequest()->getText( 'file' );
60 
61  $redirect = null;
62  if ( $file ) {
63  $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" );
64  }
65  if ( $redirect === null ) {
66  // The user input is empty or an invalid title,
67  // redirect to form of Special:Redirect with the invalid value prefilled
68  $this->mAddedRedirectParams['wpvalue'] = $file;
69  $redirect = SpecialPage::getSafeTitleFor( 'Redirect', 'file' );
70  }
71  // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid
72  return $redirect;
73  }
74 
83  public function prefixSearchSubpages( $search, $limit, $offset ) {
84  $title = Title::newFromText( $search, NS_FILE );
85  if ( !$title || $title->getNamespace() !== NS_FILE ) {
86  // No prefix suggestion outside of file namespace
87  return [];
88  }
89  $searchEngine = $this->searchEngineFactory->create();
90  $searchEngine->setLimitOffset( $limit, $offset );
91  // Autocomplete subpage the same as a normal search, but just for files
92  $searchEngine->setNamespaces( [ NS_FILE ] );
93  $result = $searchEngine->defaultPrefixSearch( $search );
94 
95  return array_map( static function ( Title $t ) {
96  // Remove namespace in search suggestion
97  return $t->getText();
98  }, $result );
99  }
100 
101  protected function getGroupName() {
102  return 'media';
103  }
104 }
105 
109 class_alias( SpecialFilepath::class, 'SpecialFilepath' );
const NS_FILE
Definition: Defines.php:70
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:82
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:425
Shortcut to construct a special page alias.
Factory class for SearchEngine.
Parent class for all special pages.
Definition: SpecialPage.php:45
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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42