Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
SpecialFilepath | |
0.00% |
0 / 22 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getRedirect | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
prefixSearchSubpages | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getGroupName | |
0.00% |
0 / 1 |
|
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 | |
21 | namespace MediaWiki\Specials; |
22 | |
23 | use MediaWiki\SpecialPage\RedirectSpecialPage; |
24 | use MediaWiki\SpecialPage\SpecialPage; |
25 | use MediaWiki\Title\Title; |
26 | use SearchEngineFactory; |
27 | |
28 | /** |
29 | * Redirects to the URL of a thumbnail for the given file. |
30 | * |
31 | * @ingroup SpecialPage |
32 | */ |
33 | class SpecialFilepath extends RedirectSpecialPage { |
34 | |
35 | private SearchEngineFactory $searchEngineFactory; |
36 | |
37 | /** |
38 | * @param SearchEngineFactory $searchEngineFactory |
39 | */ |
40 | public function __construct( |
41 | SearchEngineFactory $searchEngineFactory |
42 | ) { |
43 | parent::__construct( 'Filepath' ); |
44 | $this->mAllowedRedirectParams = [ 'width', 'height' ]; |
45 | $this->searchEngineFactory = $searchEngineFactory; |
46 | } |
47 | |
48 | /** |
49 | * Implement by redirecting through Special:Redirect/file. |
50 | * |
51 | * @param string|null $par |
52 | * @return Title |
53 | */ |
54 | public function getRedirect( $par ) { |
55 | $file = $par ?: $this->getRequest()->getText( 'file' ); |
56 | |
57 | $redirect = null; |
58 | if ( $file ) { |
59 | $redirect = SpecialPage::getSafeTitleFor( 'Redirect', "file/$file" ); |
60 | } |
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' ); |
66 | } |
67 | // @phan-suppress-next-line PhanTypeMismatchReturnNullable Known to be valid |
68 | return $redirect; |
69 | } |
70 | |
71 | /** |
72 | * Return an array of subpages beginning with $search that this special page will accept. |
73 | * |
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 |
78 | */ |
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 []; |
84 | } |
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 ); |
90 | |
91 | return array_map( static function ( Title $t ) { |
92 | // Remove namespace in search suggestion |
93 | return $t->getText(); |
94 | }, $result ); |
95 | } |
96 | |
97 | protected function getGroupName() { |
98 | return 'media'; |
99 | } |
100 | } |
101 | |
102 | /** @deprecated class alias since 1.41 */ |
103 | class_alias( SpecialFilepath::class, 'SpecialFilepath' ); |