MediaWiki  master
RevDelFileItem.php
Go to the documentation of this file.
1 <?php
24 
28 class RevDelFileItem extends RevDelItem {
30  protected $list;
32  protected $file;
33 
34  public function __construct( RevisionListBase $list, $row ) {
35  parent::__construct( $list, $row );
36  $this->file = static::initFile( $list, $row );
37  }
38 
46  protected static function initFile( $list, $row ) {
47  return MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
48  ->newFileFromRow( $row );
49  }
50 
51  public function getIdField() {
52  return 'oi_archive_name';
53  }
54 
55  public function getTimestampField() {
56  return 'oi_timestamp';
57  }
58 
59  public function getAuthorIdField() {
60  return 'oi_user';
61  }
62 
63  public function getAuthorNameField() {
64  return 'oi_user_text';
65  }
66 
67  public function getAuthorActorField() {
68  return 'oi_actor';
69  }
70 
71  public function getId() {
72  $parts = explode( '!', $this->row->oi_archive_name );
73 
74  return $parts[0];
75  }
76 
77  public function canView() {
78  return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getAuthority() );
79  }
80 
81  public function canViewContent() {
82  return $this->file->userCan( File::DELETED_FILE, $this->list->getAuthority() );
83  }
84 
85  public function getBits() {
86  return $this->file->getVisibility();
87  }
88 
89  public function setBits( $bits ) {
90  # Queue the file op
91  # @todo FIXME: Move to LocalFile.php
92  if ( $this->isDeleted() ) {
93  if ( $bits & File::DELETED_FILE ) {
94  # Still deleted
95  } else {
96  # Newly undeleted
97  $key = $this->file->getStorageKey();
98  $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
99  $this->list->storeBatch[] = [
100  $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
101  'public',
102  $this->file->getRel()
103  ];
104  $this->list->cleanupBatch[] = $key;
105  }
106  } elseif ( $bits & File::DELETED_FILE ) {
107  # Newly deleted
108  $key = $this->file->getStorageKey();
109  $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
110  $this->list->deleteBatch[] = [ $this->file->getRel(), $dstRel ];
111  }
112 
113  # Do the database operations
114  $dbw = wfGetDB( DB_PRIMARY );
115  $dbw->update( 'oldimage',
116  [ 'oi_deleted' => $bits ],
117  [
118  'oi_name' => $this->row->oi_name,
119  'oi_timestamp' => $this->row->oi_timestamp,
120  'oi_deleted' => $this->getBits()
121  ],
122  __METHOD__
123  );
124 
125  return (bool)$dbw->affectedRows();
126  }
127 
128  public function isDeleted() {
129  return $this->file->isDeleted( File::DELETED_FILE );
130  }
131 
137  protected function getLink() {
138  $date = $this->list->getLanguage()->userTimeAndDate(
139  $this->file->getTimestamp(), $this->list->getUser() );
140 
141  if ( !$this->isDeleted() ) {
142  # Regular files...
143  return Html::element( 'a', [ 'href' => $this->file->getUrl() ], $date );
144  }
145 
146  # Hidden files...
147  if ( !$this->canViewContent() ) {
148  $link = htmlspecialchars( $date );
149  } else {
150  $link = $this->getLinkRenderer()->makeLink(
151  SpecialPage::getTitleFor( 'Revisiondelete' ),
152  $date,
153  [],
154  [
155  'target' => $this->list->getPageName(),
156  'file' => $this->file->getArchiveName(),
157  'token' => $this->list->getUser()->getEditToken(
158  $this->file->getArchiveName() )
159  ]
160  );
161  }
162 
163  return '<span class="history-deleted">' . $link . '</span>';
164  }
165 
170  protected function getUserTools() {
171  $uploader = $this->file->getUploader( File::FOR_THIS_USER, $this->list->getAuthority() );
172  if ( $uploader ) {
173  $link = Linker::userLink( $uploader->getId(), $uploader->getName() ) .
174  Linker::userToolLinks( $uploader->getId(), $uploader->getName() );
175  return $link;
176  } else {
177  $link = $this->list->msg( 'rev-deleted-user' )->escaped();
178  }
179  if ( $this->file->isDeleted( File::DELETED_USER ) ) {
180  return '<span class="history-deleted">' . $link . '</span>';
181  }
182  return $link;
183  }
184 
191  protected function getComment() {
192  if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getAuthority() ) ) {
193  $block = Linker::commentBlock( $this->file->getDescription() );
194  } else {
195  $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
196  }
197  if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
198  return "<span class=\"history-deleted\">$block</span>";
199  }
200 
201  return $block;
202  }
203 
204  public function getHTML() {
205  $data =
206  $this->list->msg( 'widthheight' )->numParams(
207  $this->file->getWidth(),
208  $this->file->getHeight() )->escaped() .
209  ' (' . $this->list->msg( 'nbytes' )->numParams(
210  $this->file->getSize() )->escaped() . ')';
211 
212  return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
213  $data . ' ' . $this->getComment() . '</li>';
214  }
215 
216  public function getApiData( ApiResult $result ) {
217  $file = $this->file;
218  $user = $this->list->getUser();
219  $ret = [
220  'title' => $this->list->getPageName(),
221  'archivename' => $file->getArchiveName(),
222  'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
223  'width' => $file->getWidth(),
224  'height' => $file->getHeight(),
225  'size' => $file->getSize(),
226  'userhidden' => (bool)$file->isDeleted( RevisionRecord::DELETED_USER ),
227  'commenthidden' => (bool)$file->isDeleted( RevisionRecord::DELETED_COMMENT ),
228  'contenthidden' => (bool)$this->isDeleted(),
229  ];
230  if ( !$this->isDeleted() ) {
231  $ret += [
232  'url' => $file->getUrl(),
233  ];
234  } elseif ( $this->canViewContent() ) {
235  $ret += [
236  'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
237  [
238  'target' => $this->list->getPageName(),
239  'file' => $file->getArchiveName(),
240  'token' => $user->getEditToken( $file->getArchiveName() )
241  ]
242  ),
243  ];
244  }
245  $uploader = $file->getUploader( File::FOR_THIS_USER, $user );
246  if ( $uploader ) {
247  $ret += [
248  'userid' => $uploader->getId(),
249  'user' => $uploader->getName(),
250  ];
251  }
252  $comment = $file->getDescription( File::FOR_THIS_USER, $user );
253  if ( ( $comment ?? '' ) !== '' ) {
254  $ret += [
255  'comment' => $comment,
256  ];
257  }
258 
259  return $ret;
260  }
261 
262  public function lock() {
263  return $this->file->acquireFileLock();
264  }
265 
266  public function unlock() {
267  return $this->file->releaseFileLock();
268  }
269 }
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:73
const DELETED_RESTRICTED
Definition: File.php:75
const DELETED_FILE
Definition: File.php:72
getUrl()
Return the URL of the file.
Definition: File.php:385
const DELETED_USER
Definition: File.php:74
const FOR_THIS_USER
Definition: File.php:89
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:236
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1069
static commentBlock( $comment, $title=null, $local=false, $wikiId=null, $useParentheses=true)
Wrap a comment in standard punctuation and formatting if it's non-empty, otherwise return empty strin...
Definition: Linker.php:1538
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition: Linker.php:1114
getWidth( $page=1)
Return the width of the image @stable to override.
Definition: LocalFile.php:967
getUploader(int $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: LocalFile.php:2494
getSize()
Returns the size of the image file, in bytes @stable to override.
Definition: LocalFile.php:1256
getDescription( $audience=self::FOR_PUBLIC, Authority $performer=null)
Definition: LocalFile.php:2511
getHeight( $page=1)
Return the height of the image @stable to override.
Definition: LocalFile.php:1000
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