MediaWiki master
UploadStashPager.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Pager;
23
24use File;
25use HTMLForm;
26use LocalRepo;
31use UnexpectedValueException;
32use UploadStash;
35
40 private UploadStash $stash;
41 private LocalRepo $localRepo;
42
44 protected $mFieldNames = null;
45
47 private array $files = [];
48
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
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
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}
getContext()
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:74
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:49
setContext(IContextSource $context)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
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)