MediaWiki master
UploadStashPager.php
Go to the documentation of this file.
1<?php
9
21use UnexpectedValueException;
23
28
30 protected $mFieldNames = null;
31
33 private array $files = [];
34
35 public function __construct(
36 IContextSource $context,
37 LinkRenderer $linkRenderer,
38 IConnectionProvider $dbProvider,
39 private readonly UploadStash $stash,
40 private readonly LocalRepo $localRepo,
41 ) {
42 $this->setContext( $context );
43
44 // Set database before parent constructor to avoid setting it there
45 $this->mDb = $dbProvider->getReplicaDatabase();
46 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
47
48 parent::__construct( $context, $linkRenderer );
49 }
50
52 protected function getFieldNames() {
53 if ( !$this->mFieldNames ) {
54 $this->mFieldNames = [
55 'us_timestamp' => $this->msg( 'uploadstash-header-date' )->text(),
56 'us_key' => $this->msg( 'uploadstash-header-filekey' )->text(),
57 'thumb' => $this->msg( 'uploadstash-header-thumb' )->text(),
58 'us_size' => $this->msg( 'uploadstash-header-dimensions' )->text(),
59 ];
60 }
61
62 return $this->mFieldNames;
63 }
64
66 protected function isFieldSortable( $field ) {
67 return in_array( $field, [ 'us_timestamp', 'us_key' ] );
68 }
69
71 public function getQueryInfo() {
72 return [
73 'tables' => [ 'uploadstash' ],
74 'fields' => [
75 'us_id',
76 'us_timestamp',
77 'us_key',
78 'us_size',
79 'us_path',
80 'us_sha1',
81 'us_mime',
82 'us_status',
83 ],
84 'conds' => [ 'us_user' => $this->getUser()->getId() ],
85 'options' => [],
86 'join_conds' => [],
87 ];
88 }
89
91 public function getIndexField() {
92 return [ [ 'us_timestamp', 'us_id' ] ];
93 }
94
96 public function getDefaultSort() {
97 return 'us_timestamp';
98 }
99
105 public function formatValue( $field, $value ) {
106 $linkRenderer = $this->getLinkRenderer();
107
108 switch ( $field ) {
109 case 'us_timestamp':
110 // We may want to make this a link to the "old" version when displaying old files
111 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
112 case 'us_key':
113 $linkRenderer = $this->getLinkRenderer();
114 $html = $linkRenderer->makeKnownLink(
115 SpecialPage::getTitleFor( 'UploadStash', "file/$value" ),
116 $value
117 );
118 if ( $this->mCurrentRow->us_status === 'finished' ) {
119 $html .= '<br />' . Html::rawElement( 'span', [ 'class' => 'mw-uploadstash-tryagain' ],
120 $this->msg( 'parentheses' )->rawParams( $linkRenderer->makeKnownLink(
121 SpecialPage::getTitleFor( 'Upload' ),
122 $this->msg( 'uploadstash-pager-tryagain' )->text(),
123 [],
124 [ 'wpSourceType' => 'Stash', 'wpSessionKey' => $value ]
125 ) )->escaped()
126 );
127 }
128 return $html;
129 case 'thumb':
130 $file = $this->getCurrentFile();
131 if ( $file->allowInlineDisplay() ) {
132 $thumbnail = $file->transform( [
133 'width' => '120',
134 'height' => '120',
135 ] );
136 if ( $thumbnail ) {
137 return $thumbnail->toHtml( [ 'loading' => 'lazy' ] );
138 }
139 }
140 return $this->msg( 'uploadstash-nothumb' )->escaped();
141 case 'us_size':
142 $file = $this->getCurrentFile();
143 return htmlspecialchars( $file->getDimensionsString() )
144 . $this->msg( 'word-separator' )->escaped()
145 . Html::element( 'span', [ 'style' => 'white-space: nowrap;' ],
146 $this->msg( 'parentheses' )->sizeParams( (int)$value )->text()
147 );
148 default:
149 throw new UnexpectedValueException( "Unknown field '$field'" );
150 }
151 }
152
153 private function getCurrentFile(): File {
154 $fileKey = $this->mCurrentRow->us_key;
155 return $this->files[$fileKey]
156 ?? new UploadStashFile(
157 $this->localRepo,
158 $this->mCurrentRow->us_path,
159 $fileKey,
160 $this->mCurrentRow->us_sha1,
161 $this->mCurrentRow->us_mime ?? false
162 );
163 }
164
168 private function getEscapedLimitSelectList(): array {
169 $list = $this->getLimitSelectList();
170 $result = [];
171 foreach ( $list as $key => $value ) {
172 $result[htmlspecialchars( $key )] = $value;
173 }
174 return $result;
175 }
176
178 public function getForm() {
179 $formDescriptor = [];
180 $formDescriptor['limit'] = [
181 'type' => 'radio',
182 'name' => 'limit',
183 'label-message' => 'table_pager_limit_label',
184 'options' => $this->getEscapedLimitSelectList(),
185 'flatlist' => true,
186 'default' => $this->mLimit
187 ];
188
189 HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
190 ->setMethod( 'get' )
191 ->setId( 'mw-uploadstash-form' )
192 ->setTitle( $this->getTitle() )
193 ->setSubmitTextMsg( 'uploadstash-pager-submit' )
194 ->setWrapperLegendMsg( 'uploadstash' )
195 ->prepareForm()
196 ->displayForm( '' );
197 }
198
200 protected function getTableClass() {
201 return parent::getTableClass() . ' uploadstash';
202 }
203
205 protected function getNavClass() {
206 return parent::getNavClass() . ' uploadstash_nav';
207 }
208
210 protected function getSortHeaderClass() {
211 return parent::getSortHeaderClass() . ' uploadstash_sort';
212 }
213}
214
216class_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:80
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:207
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Class that generates HTML for internal links.
Efficient paging for SQL queries that use a (roughly unique) index.
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, private readonly UploadStash $stash, private readonly 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)