MediaWiki  master
RevDelFileItem.php
Go to the documentation of this file.
1 <?php
25 
29 class RevDelFileItem extends RevDelItem {
31  protected $list;
33  protected $file;
34 
35  public function __construct( RevisionListBase $list, $row ) {
36  parent::__construct( $list, $row );
37  $this->file = static::initFile( $list, $row );
38  }
39 
47  protected static function initFile( $list, $row ) {
48  return MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
49  ->newFileFromRow( $row );
50  }
51 
52  public function getIdField() {
53  return 'oi_archive_name';
54  }
55 
56  public function getTimestampField() {
57  return 'oi_timestamp';
58  }
59 
60  public function getAuthorIdField() {
61  return 'oi_user';
62  }
63 
64  public function getAuthorNameField() {
65  return 'oi_user_text';
66  }
67 
68  public function getAuthorActorField() {
69  return 'oi_actor';
70  }
71 
72  public function getId() {
73  $parts = explode( '!', $this->row->oi_archive_name );
74 
75  return $parts[0];
76  }
77 
78  public function canView() {
79  return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getAuthority() );
80  }
81 
82  public function canViewContent() {
83  return $this->file->userCan( File::DELETED_FILE, $this->list->getAuthority() );
84  }
85 
86  public function getBits() {
87  return $this->file->getVisibility();
88  }
89 
90  public function setBits( $bits ) {
91  # Queue the file op
92  # @todo FIXME: Move to LocalFile.php
93  if ( $this->isDeleted() ) {
94  if ( $bits & File::DELETED_FILE ) {
95  # Still deleted
96  } else {
97  # Newly undeleted
98  $key = $this->file->getStorageKey();
99  $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
100  $this->list->storeBatch[] = [
101  $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
102  'public',
103  $this->file->getRel()
104  ];
105  $this->list->cleanupBatch[] = $key;
106  }
107  } elseif ( $bits & File::DELETED_FILE ) {
108  # Newly deleted
109  $key = $this->file->getStorageKey();
110  $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
111  $this->list->deleteBatch[] = [ $this->file->getRel(), $dstRel ];
112  }
113 
114  # Do the database operations
115  $dbw = wfGetDB( DB_PRIMARY );
116  $dbw->update( 'oldimage',
117  [ 'oi_deleted' => $bits ],
118  [
119  'oi_name' => $this->row->oi_name,
120  'oi_timestamp' => $this->row->oi_timestamp,
121  'oi_deleted' => $this->getBits()
122  ],
123  __METHOD__
124  );
125 
126  return (bool)$dbw->affectedRows();
127  }
128 
129  public function isDeleted() {
130  return $this->file->isDeleted( File::DELETED_FILE );
131  }
132 
138  protected function getLink() {
139  $date = $this->list->getLanguage()->userTimeAndDate(
140  $this->file->getTimestamp(), $this->list->getUser() );
141 
142  if ( !$this->isDeleted() ) {
143  # Regular files...
144  return Html::element( 'a', [ 'href' => $this->file->getUrl() ], $date );
145  }
146 
147  # Hidden files...
148  if ( !$this->canViewContent() ) {
149  $link = htmlspecialchars( $date );
150  } else {
151  $link = $this->getLinkRenderer()->makeLink(
152  SpecialPage::getTitleFor( 'Revisiondelete' ),
153  $date,
154  [],
155  [
156  'target' => $this->list->getPageName(),
157  'file' => $this->file->getArchiveName(),
158  'token' => $this->list->getUser()->getEditToken(
159  $this->file->getArchiveName() )
160  ]
161  );
162  }
163 
164  return '<span class="history-deleted">' . $link . '</span>';
165  }
166 
171  protected function getUserTools() {
172  $uploader = $this->file->getUploader( File::FOR_THIS_USER, $this->list->getAuthority() );
173  if ( $uploader ) {
174  $link = Linker::userLink( $uploader->getId(), $uploader->getName() ) .
175  Linker::userToolLinks( $uploader->getId(), $uploader->getName() );
176  return $link;
177  } else {
178  $link = $this->list->msg( 'rev-deleted-user' )->escaped();
179  }
180  if ( $this->file->isDeleted( File::DELETED_USER ) ) {
181  return '<span class="history-deleted">' . $link . '</span>';
182  }
183  return $link;
184  }
185 
192  protected function getComment() {
193  if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getAuthority() ) ) {
194  $block = MediaWikiServices::getInstance()->getCommentFormatter()
195  ->formatBlock( $this->file->getDescription() );
196  } else {
197  $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
198  }
199  if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
200  return "<span class=\"history-deleted\">$block</span>";
201  }
202 
203  return $block;
204  }
205 
206  public function getHTML() {
207  $data =
208  $this->list->msg( 'widthheight' )->numParams(
209  $this->file->getWidth(),
210  $this->file->getHeight() )->escaped() .
211  ' (' . $this->list->msg( 'nbytes' )->numParams(
212  $this->file->getSize() )->escaped() . ')';
213 
214  return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
215  $data . ' ' . $this->getComment() . '</li>';
216  }
217 
218  public function getApiData( ApiResult $result ) {
219  $file = $this->file;
220  $user = $this->list->getUser();
221  $ret = [
222  'title' => $this->list->getPageName(),
223  'archivename' => $file->getArchiveName(),
224  'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
225  'width' => $file->getWidth(),
226  'height' => $file->getHeight(),
227  'size' => $file->getSize(),
228  'userhidden' => (bool)$file->isDeleted( RevisionRecord::DELETED_USER ),
229  'commenthidden' => (bool)$file->isDeleted( RevisionRecord::DELETED_COMMENT ),
230  'contenthidden' => (bool)$this->isDeleted(),
231  ];
232  if ( !$this->isDeleted() ) {
233  $ret += [
234  'url' => $file->getUrl(),
235  ];
236  } elseif ( $this->canViewContent() ) {
237  $ret += [
238  'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
239  [
240  'target' => $this->list->getPageName(),
241  'file' => $file->getArchiveName(),
242  'token' => $user->getEditToken( $file->getArchiveName() )
243  ]
244  ),
245  ];
246  }
247  $uploader = $file->getUploader( File::FOR_THIS_USER, $user );
248  if ( $uploader ) {
249  $ret += [
250  'userid' => $uploader->getId(),
251  'user' => $uploader->getName(),
252  ];
253  }
254  $comment = $file->getDescription( File::FOR_THIS_USER, $user );
255  if ( ( $comment ?? '' ) !== '' ) {
256  $ret += [
257  'comment' => $comment,
258  ];
259  }
260 
261  return $ret;
262  }
263 
264  public function lock() {
265  return $this->file->acquireFileLock();
266  }
267 
268  public function unlock() {
269  return $this->file->releaseFileLock();
270  }
271 }
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
This class represents the result of the API operations.
Definition: ApiResult.php:35
const DELETED_COMMENT
Definition: File.php:72
const DELETED_RESTRICTED
Definition: File.php:74
const DELETED_FILE
Definition: File.php:71
getUrl()
Return the URL of the file.
Definition: File.php:386
const DELETED_USER
Definition: File.php:73
const FOR_THIS_USER
Definition: File.php:88
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
getWidth( $page=1)
Return the width of the image @stable to override.
Definition: LocalFile.php:959
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: LocalFile.php:2473
getSize()
Returns the size of the image file, in bytes @stable to override.
Definition: LocalFile.php:1248
getDescription( $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: LocalFile.php:2490
getHeight( $page=1)
Return the height of the image @stable to override.
Definition: LocalFile.php:992
Some internal bits split of from Skin.php.
Definition: Linker.php:65
Service locator for MediaWiki core services.
Page revision base class.
isDeleted( $field)
Item class for an oldimage table row.
getTimestampField()
Get the DB field name storing timestamps.
getComment()
Wrap and format the file's comment block, if the current user is allowed to view it.
getLink()
Get the link to the file.
RevDelFileList $list
canViewContent()
Returns true if the current user can view the item text/file.
getId()
Get the ID, as it would appear in the ids URL parameter.
getIdField()
Get the DB field name associated with the ID list.
getBits()
Get the current deletion bitfield value.
getAuthorIdField()
Get the DB field name storing user ids.
static initFile( $list, $row)
Create file object from $row sourced from $list.
getHTML()
Get the HTML of the list item.
getUserTools()
Generate a user tool link cluster if the current user is allowed to view it.
__construct(RevisionListBase $list, $row)
unlock()
Unlock the item against changes outside of the DB.
getAuthorNameField()
Get the DB field name storing user names.
lock()
Lock the item against changes outside of the DB.
canView()
Returns true if the current user can view the item.
getAuthorActorField()
Get the DB field name storing actor ids.
setBits( $bits)
Set the visibility of the item.
OldLocalFile $file
getApiData(ApiResult $result)
Get the return information about the revision for the API.
Abstract base class for deletable items.
Definition: RevDelItem.php:25
stdClass $row
The database result row.
getLinkRenderer()
Returns an instance of LinkRenderer.
List for revision table items for a single page.
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,...
const DB_PRIMARY
Definition: defines.php:28