MediaWiki 1.44.0
UploadStashPager.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Pager;
23
31use UnexpectedValueException;
32use UploadStash;
35
40 private UploadStash $stash;
41 private LocalRepo $localRepo;
42
44 protected $mFieldNames = null;
45
47 private array $files = [];
48
49 public function __construct(
50 IContextSource $context,
51 LinkRenderer $linkRenderer,
52 IConnectionProvider $dbProvider,
53 UploadStash $stash,
54 LocalRepo $localRepo
55 ) {
56 $this->setContext( $context );
57
58 // Set database before parent constructor to avoid setting it there
59 $this->mDb = $dbProvider->getReplicaDatabase();
60
61 parent::__construct( $context, $linkRenderer );
62
63 $this->stash = $stash;
64 $this->localRepo = $localRepo;
65 }
66
67 protected function getFieldNames() {
68 if ( !$this->mFieldNames ) {
69 $this->mFieldNames = [
70 'us_timestamp' => $this->msg( 'uploadstash-header-date' )->text(),
71 'us_key' => $this->msg( 'uploadstash-header-filekey' )->text(),
72 'thumb' => $this->msg( 'uploadstash-header-thumb' )->text(),
73 'us_size' => $this->msg( 'uploadstash-header-dimensions' )->text(),
74 ];
75 }
76
77 return $this->mFieldNames;
78 }
79
80 protected function isFieldSortable( $field ) {
81 return in_array( $field, [ 'us_timestamp', 'us_key' ] );
82 }
83
84 public function getQueryInfo() {
85 return [
86 'tables' => [ 'uploadstash' ],
87 'fields' => [
88 'us_id',
89 'us_timestamp',
90 'us_key',
91 'us_size',
92 'us_path',
93 'us_sha1',
94 'us_mime',
95 ],
96 'conds' => [ 'us_user' => $this->getUser()->getId() ],
97 'options' => [],
98 'join_conds' => [],
99 ];
100 }
101
102 public function getIndexField() {
103 return [ [ 'us_timestamp', 'us_id' ] ];
104 }
105
106 public function getDefaultSort() {
107 return 'us_timestamp';
108 }
109
115 public function formatValue( $field, $value ) {
116 $linkRenderer = $this->getLinkRenderer();
117
118 switch ( $field ) {
119 case 'us_timestamp':
120 // We may want to make this a link to the "old" version when displaying old files
121 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
122 case 'us_key':
123 return $this->getLinkRenderer()->makeKnownLink(
124 SpecialPage::getTitleFor( 'UploadStash', "file/$value" ),
125 $value
126 );
127 case 'thumb':
128 $file = $this->getCurrentFile();
129 if ( $file->allowInlineDisplay() ) {
130 $thumbnail = $file->transform( [
131 'width' => '120',
132 'height' => '120',
133 ] );
134 if ( $thumbnail ) {
135 return $thumbnail->toHtml( [ 'loading' => 'lazy' ] );
136 }
137 }
138 return $this->msg( 'uploadstash-nothumb' )->escaped();
139 case 'us_size':
140 $file = $this->getCurrentFile();
141 return htmlspecialchars( $file->getDimensionsString() )
142 . $this->msg( 'word-separator' )->escaped()
143 . Html::element( 'span', [ 'style' => 'white-space: nowrap;' ],
144 $this->msg( 'parentheses' )->sizeParams( (int)$value )->text()
145 );
146 default:
147 throw new UnexpectedValueException( "Unknown field '$field'" );
148 }
149 }
150
151 private function getCurrentFile(): File {
152 $fileKey = $this->mCurrentRow->us_key;
153 return $this->files[$fileKey]
154 ?? new UploadStashFile(
155 $this->localRepo,
156 $this->mCurrentRow->us_path,
157 $fileKey,
158 $this->mCurrentRow->us_sha1,
159 $this->mCurrentRow->us_mime ?? false
160 );
161 }
162
166 private function getEscapedLimitSelectList(): array {
167 $list = $this->getLimitSelectList();
168 $result = [];
169 foreach ( $list as $key => $value ) {
170 $result[htmlspecialchars( $key )] = $value;
171 }
172 return $result;
173 }
174
175 public function getForm() {
176 $formDescriptor = [];
177 $formDescriptor['limit'] = [
178 'type' => 'radio',
179 'name' => 'limit',
180 'label-message' => 'table_pager_limit_label',
181 'options' => $this->getEscapedLimitSelectList(),
182 'flatlist' => true,
183 'default' => $this->mLimit
184 ];
185
186 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
187 ->setMethod( 'get' )
188 ->setId( 'mw-uploadstash-form' )
189 ->setTitle( $this->getTitle() )
190 ->setSubmitTextMsg( 'uploadstash-pager-submit' )
191 ->setWrapperLegendMsg( 'uploadstash' )
192 ->prepareForm()
193 ->displayForm( '' );
194 }
195
196 protected function getTableClass() {
197 return parent::getTableClass() . ' uploadstash';
198 }
199
200 protected function getNavClass() {
201 return parent::getNavClass() . ' uploadstash_nav';
202 }
203
204 protected function getSortHeaderClass() {
205 return parent::getSortHeaderClass() . ' uploadstash_sort';
206 }
207}
setContext(IContextSource $context)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:57
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
Class that generates HTML for internal links.
Table-based display with a user-selectable sort order.
getQueryInfo()
Provides all parameters needed for the main paged query.
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
__construct(IContextSource $context, LinkRenderer $linkRenderer, IConnectionProvider $dbProvider, UploadStash $stash, LocalRepo $localRepo)
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
getDefaultSort()
The database field name used as a default sort order.
isFieldSortable( $field)
Return true if the named field should be sortable by the UI, false otherwise.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
UploadStash is intended to accomplish a few things:
Interface for objects which can provide a MediaWiki context on request.
Provide primary and replica IDatabase connections.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.
element(SerializerNode $parent, SerializerNode $node, $contents)