Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 83 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
UploadStashPager | |
0.00% |
0 / 83 |
|
0.00% |
0 / 13 |
506 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getFieldNames | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
isFieldSortable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getQueryInfo | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
getIndexField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDefaultSort | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatValue | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
getCurrentFile | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getEscapedLimitSelectList | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getForm | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
getTableClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNavClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSortHeaderClass | |
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 | * @ingroup Pager |
20 | */ |
21 | |
22 | namespace MediaWiki\Pager; |
23 | |
24 | use File; |
25 | use LocalRepo; |
26 | use MediaWiki\Context\IContextSource; |
27 | use MediaWiki\Html\Html; |
28 | use MediaWiki\HTMLForm\HTMLForm; |
29 | use MediaWiki\Linker\LinkRenderer; |
30 | use MediaWiki\SpecialPage\SpecialPage; |
31 | use UnexpectedValueException; |
32 | use UploadStash; |
33 | use UploadStashFile; |
34 | use Wikimedia\Rdbms\IConnectionProvider; |
35 | |
36 | /** |
37 | * @ingroup Pager |
38 | */ |
39 | class UploadStashPager extends TablePager { |
40 | private UploadStash $stash; |
41 | private LocalRepo $localRepo; |
42 | |
43 | /** @var string[]|null */ |
44 | protected $mFieldNames = null; |
45 | |
46 | /** @var File[] */ |
47 | private array $files = []; |
48 | |
49 | /** |
50 | * @param IContextSource $context |
51 | * @param LinkRenderer $linkRenderer |
52 | * @param IConnectionProvider $dbProvider |
53 | * @param UploadStash $stash |
54 | * @param LocalRepo $localRepo |
55 | */ |
56 | public function __construct( |
57 | IContextSource $context, |
58 | LinkRenderer $linkRenderer, |
59 | IConnectionProvider $dbProvider, |
60 | UploadStash $stash, |
61 | LocalRepo $localRepo |
62 | ) { |
63 | $this->setContext( $context ); |
64 | |
65 | // Set database before parent constructor to avoid setting it there with wfGetDB |
66 | $this->mDb = $dbProvider->getReplicaDatabase(); |
67 | |
68 | parent::__construct( $context, $linkRenderer ); |
69 | |
70 | $this->stash = $stash; |
71 | $this->localRepo = $localRepo; |
72 | } |
73 | |
74 | protected function getFieldNames() { |
75 | if ( !$this->mFieldNames ) { |
76 | $this->mFieldNames = [ |
77 | 'us_timestamp' => $this->msg( 'uploadstash-header-date' )->text(), |
78 | 'us_key' => $this->msg( 'uploadstash-header-filekey' )->text(), |
79 | 'thumb' => $this->msg( 'uploadstash-header-thumb' )->text(), |
80 | 'us_size' => $this->msg( 'uploadstash-header-dimensions' )->text(), |
81 | ]; |
82 | } |
83 | |
84 | return $this->mFieldNames; |
85 | } |
86 | |
87 | protected function isFieldSortable( $field ) { |
88 | return in_array( $field, [ 'us_timestamp', 'us_key' ] ); |
89 | } |
90 | |
91 | public function getQueryInfo() { |
92 | return [ |
93 | 'tables' => [ 'uploadstash' ], |
94 | 'fields' => [ |
95 | 'us_id', |
96 | 'us_timestamp', |
97 | 'us_key', |
98 | 'us_size', |
99 | 'us_path', |
100 | ], |
101 | 'conds' => [ 'us_user' => $this->getUser()->getId() ], |
102 | 'options' => [], |
103 | 'join_conds' => [], |
104 | ]; |
105 | } |
106 | |
107 | public function getIndexField() { |
108 | return [ [ 'us_timestamp', 'us_id' ] ]; |
109 | } |
110 | |
111 | public function getDefaultSort() { |
112 | return 'us_timestamp'; |
113 | } |
114 | |
115 | /** |
116 | * @param string $field |
117 | * @param string|null $value |
118 | * @return string |
119 | */ |
120 | public function formatValue( $field, $value ) { |
121 | $linkRenderer = $this->getLinkRenderer(); |
122 | |
123 | switch ( $field ) { |
124 | case 'us_timestamp': |
125 | // We may want to make this a link to the "old" version when displaying old files |
126 | return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) ); |
127 | case 'us_key': |
128 | return $this->getLinkRenderer()->makeKnownLink( |
129 | SpecialPage::getTitleFor( 'UploadStash', "file/$value" ), |
130 | $value |
131 | ); |
132 | case 'thumb': |
133 | $file = $this->getCurrentFile(); |
134 | if ( $file->allowInlineDisplay() ) { |
135 | $thumbnail = $file->transform( [ |
136 | 'width' => '120', |
137 | 'height' => '120', |
138 | ] ); |
139 | if ( $thumbnail ) { |
140 | return $thumbnail->toHtml( [ 'loading' => 'lazy' ] ); |
141 | } |
142 | } |
143 | return $this->msg( 'uploadstash-nothumb' )->escaped(); |
144 | case 'us_size': |
145 | $file = $this->getCurrentFile(); |
146 | return htmlspecialchars( $file->getDimensionsString() ) |
147 | . $this->msg( 'word-separator' )->escaped() |
148 | . Html::element( 'span', [ 'style' => 'white-space: nowrap;' ], |
149 | $this->msg( 'parentheses' )->sizeParams( (int)$value )->text() |
150 | ); |
151 | default: |
152 | throw new UnexpectedValueException( "Unknown field '$field'" ); |
153 | } |
154 | } |
155 | |
156 | private function getCurrentFile(): File { |
157 | $fileKey = $this->mCurrentRow->us_key; |
158 | return $this->files[$fileKey] |
159 | ?? new UploadStashFile( $this->localRepo, $this->mCurrentRow->us_path, $fileKey ); |
160 | } |
161 | |
162 | /** |
163 | * Escape the options list |
164 | */ |
165 | private function getEscapedLimitSelectList(): array { |
166 | $list = $this->getLimitSelectList(); |
167 | $result = []; |
168 | foreach ( $list as $key => $value ) { |
169 | $result[htmlspecialchars( $key )] = $value; |
170 | } |
171 | return $result; |
172 | } |
173 | |
174 | public function getForm() { |
175 | $formDescriptor = []; |
176 | $formDescriptor['limit'] = [ |
177 | 'type' => 'radio', |
178 | 'name' => 'limit', |
179 | 'label-message' => 'table_pager_limit_label', |
180 | 'options' => $this->getEscapedLimitSelectList(), |
181 | 'flatlist' => true, |
182 | 'default' => $this->mLimit |
183 | ]; |
184 | |
185 | HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) |
186 | ->setMethod( 'get' ) |
187 | ->setId( 'mw-uploadstash-form' ) |
188 | ->setTitle( $this->getTitle() ) |
189 | ->setSubmitTextMsg( 'uploadstash-pager-submit' ) |
190 | ->setWrapperLegendMsg( 'uploadstash' ) |
191 | ->prepareForm() |
192 | ->displayForm( '' ); |
193 | } |
194 | |
195 | protected function getTableClass() { |
196 | return parent::getTableClass() . ' uploadstash'; |
197 | } |
198 | |
199 | protected function getNavClass() { |
200 | return parent::getNavClass() . ' uploadstash_nav'; |
201 | } |
202 | |
203 | protected function getSortHeaderClass() { |
204 | return parent::getSortHeaderClass() . ' uploadstash_sort'; |
205 | } |
206 | } |