Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / specials / SpecialFilepath.php
blob8ee783f49e0a9c06a0303483de41ae8b835aa9db
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Specials;
23 use MediaWiki\SpecialPage\RedirectSpecialPage;
24 use MediaWiki\SpecialPage\SpecialPage;
25 use MediaWiki\Title\Title;
26 use SearchEngineFactory;
28 /**
29 * Redirects to the URL of a thumbnail for the given file.
31 * @ingroup SpecialPage
33 class SpecialFilepath extends RedirectSpecialPage {
35 private SearchEngineFactory $searchEngineFactory;
37 /**
38 * @param SearchEngineFactory $searchEngineFactory
40 public function __construct(
41 SearchEngineFactory $searchEngineFactory
42 ) {
43 parent::__construct( 'Filepath' );
44 $this->mAllowedRedirectParams = [ 'width', 'height' ];
45 $this->searchEngineFactory = $searchEngineFactory;
48 /**
49 * Implement by redirecting through Special:Redirect/file.
51 * @param string|null $par
52 * @return Title
54 public function getRedirect( $par ) {
55 $file = $par ?: $this->getRequest()->getText( 'file' );
57 $redirect = null;
58 if ( $file ) {
59 $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" );
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' );
67 // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid
68 return $redirect;
71 /**
72 * Return an array of subpages beginning with $search that this special page will accept.
74 * @param string $search Prefix to search for
75 * @param int $limit Maximum number of results to return (usually 10)
76 * @param int $offset Number of results to skip (usually 0)
77 * @return string[] Matching subpages
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 [];
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 );
91 return array_map( static function ( Title $t ) {
92 // Remove namespace in search suggestion
93 return $t->getText();
94 }, $result );
97 protected function getGroupName() {
98 return 'media';
102 /** @deprecated class alias since 1.41 */
103 class_alias( SpecialFilepath::class, 'SpecialFilepath' );