Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialFilepath
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRedirect
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 prefixSearchSubpages
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21namespace MediaWiki\Specials;
22
23use MediaWiki\SpecialPage\RedirectSpecialPage;
24use MediaWiki\SpecialPage\SpecialPage;
25use MediaWiki\Title\Title;
26use SearchEngineFactory;
27
28/**
29 * Redirects to the URL of a thumbnail for the given file.
30 *
31 * @ingroup SpecialPage
32 */
33class SpecialFilepath extends RedirectSpecialPage {
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
45    /**
46     * Implement by redirecting through Special:Redirect/file.
47     *
48     * @param string|null $par
49     * @return Title
50     */
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
68    /**
69     * Return an array of subpages beginning with $search that this special page will accept.
70     *
71     * @param string $search Prefix to search for
72     * @param int $limit Maximum number of results to return (usually 10)
73     * @param int $offset Number of results to skip (usually 0)
74     * @return string[] Matching subpages
75     */
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
99/** @deprecated class alias since 1.41 */
100class_alias( SpecialFilepath::class, 'SpecialFilepath' );