MediaWiki master
UploadStashPager.php
Go to the documentation of this file.
1<?php
9
20use UnexpectedValueException;
22
27 private UploadStash $stash;
28 private LocalRepo $localRepo;
29
31 protected $mFieldNames = null;
32
34 private array $files = [];
35
36 public function __construct(
37 IContextSource $context,
38 LinkRenderer $linkRenderer,
39 IConnectionProvider $dbProvider,
40 UploadStash $stash,
41 LocalRepo $localRepo
42 ) {
43 $this->setContext( $context );
44
45 // Set database before parent constructor to avoid setting it there
46 $this->mDb = $dbProvider->getReplicaDatabase();
47
48 parent::__construct( $context, $linkRenderer );
49
50 $this->stash = $stash;
51 $this->localRepo = $localRepo;
52 }
53
55 protected function getFieldNames() {
56 if ( !$this->mFieldNames ) {
57 $this->mFieldNames = [
58 'us_timestamp' => $this->msg( 'uploadstash-header-date' )->text(),
59 'us_key' => $this->msg( 'uploadstash-header-filekey' )->text(),
60 'thumb' => $this->msg( 'uploadstash-header-thumb' )->text(),
61 'us_size' => $this->msg( 'uploadstash-header-dimensions' )->text(),
62 ];
63 }
64
65 return $this->mFieldNames;
66 }
67
69 protected function isFieldSortable( $field ) {
70 return in_array( $field, [ 'us_timestamp', 'us_key' ] );
71 }
72
74 public function getQueryInfo() {
75 return [
76 'tables' => [ 'uploadstash' ],
77 'fields' => [
78 'us_id',
79 'us_timestamp',
80 'us_key',
81 'us_size',
82 'us_path',
83 'us_sha1',
84 'us_mime',
85 'us_status',
86 ],
87 'conds' => [ 'us_user' => $this->getUser()->getId() ],
88 'options' => [],
89 'join_conds' => [],
90 ];
91 }
92
94 public function getIndexField() {
95 return [ [ 'us_timestamp', 'us_id' ] ];
96 }
97
99 public function getDefaultSort() {
100 return 'us_timestamp';
101 }
102
108 public function formatValue( $field, $value ) {
109 $linkRenderer = $this->getLinkRenderer();
110
111 switch ( $field ) {
112 case 'us_timestamp':
113 // We may want to make this a link to the "old" version when displaying old files
114 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
115 case 'us_key':
116 $linkRenderer = $this->getLinkRenderer();
117 $html = $linkRenderer->makeKnownLink(
118 SpecialPage::getTitleFor( 'UploadStash', "file/$value" ),
119 $value
120 );
121 if ( $this->mCurrentRow->us_status === 'finished' ) {
122 $html .= '<br />' . Html::rawElement( 'span', [ 'class' => 'mw-uploadstash-tryagain' ],
123 $this->msg( 'parentheses' )->rawParams( $linkRenderer->makeKnownLink(
124 SpecialPage::getTitleFor( 'Upload' ),
125 $this->msg( 'uploadstash-pager-tryagain' )->text(),
126 [],
127 [ 'wpSourceType' => 'Stash', 'wpSessionKey' => $value ]
128 ) )->escaped()
129 );
130 }
131 return $html;
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(
160 $this->localRepo,
161 $this->mCurrentRow->us_path,
162 $fileKey,
163 $this->mCurrentRow->us_sha1,
164 $this->mCurrentRow->us_mime ?? false
165 );
166 }
167
171 private function getEscapedLimitSelectList(): array {
172 $list = $this->getLimitSelectList();
173 $result = [];
174 foreach ( $list as $key => $value ) {
175 $result[htmlspecialchars( $key )] = $value;
176 }
177 return $result;
178 }
179
181 public function getForm() {
182 $formDescriptor = [];
183 $formDescriptor['limit'] = [
184 'type' => 'radio',
185 'name' => 'limit',
186 'label-message' => 'table_pager_limit_label',
187 'options' => $this->getEscapedLimitSelectList(),
188 'flatlist' => true,
189 'default' => $this->mLimit
190 ];
191
192 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
193 ->setMethod( 'get' )
194 ->setId( 'mw-uploadstash-form' )
195 ->setTitle( $this->getTitle() )
196 ->setSubmitTextMsg( 'uploadstash-pager-submit' )
197 ->setWrapperLegendMsg( 'uploadstash' )
198 ->prepareForm()
199 ->displayForm( '' );
200 }
201
203 protected function getTableClass() {
204 return parent::getTableClass() . ' uploadstash';
205 }
206
208 protected function getNavClass() {
209 return parent::getNavClass() . ' uploadstash_nav';
210 }
211
213 protected function getSortHeaderClass() {
214 return parent::getSortHeaderClass() . ' uploadstash_sort';
215 }
216}
217
219class_alias( UploadStashPager::class, 'MediaWiki\\Pager\\UploadStashPager' );
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:79
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:45
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
Class that generates HTML for internal links.
Table-based display with a user-selectable sort order.
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,...
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
getQueryInfo()
Provides all parameters needed for the main paged query.It returns an associative array with the foll...
__construct(IContextSource $context, LinkRenderer $linkRenderer, IConnectionProvider $dbProvider, UploadStash $stash, LocalRepo $localRepo)
getDefaultSort()
The database field name used as a default sort order.Note that this field will only be sorted on if i...
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.to override string
isFieldSortable( $field)
Return true if the named field should be sortable by the UI, false otherwise.bool
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
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(string|false $domain=false, $group=null)
Get connection to a replica database.
element(SerializerNode $parent, SerializerNode $node, $contents)