Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 124
0.00% covered (danger)
0.00%
0 / 20
CRAP
0.00% covered (danger)
0.00%
0 / 1
RevDelFileItem
0.00% covered (danger)
0.00%
0 / 124
0.00% covered (danger)
0.00%
0 / 20
1122
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 initFile
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getIdField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTimestampField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthorIdField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthorNameField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAuthorActorField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 canView
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 canViewContent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBits
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setBits
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
20
 isDeleted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLink
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
 getUserTools
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 getComment
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getHTML
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getApiData
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
30
 lock
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 unlock
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup RevisionDelete
20 */
21
22use MediaWiki\Html\Html;
23use MediaWiki\Linker\Linker;
24use MediaWiki\MediaWikiServices;
25use MediaWiki\RevisionList\RevisionListBase;
26use MediaWiki\SpecialPage\SpecialPage;
27use Wikimedia\Rdbms\IConnectionProvider;
28
29/**
30 * Item class for an oldimage table row
31 */
32class RevDelFileItem extends RevDelItem {
33    /** @var RevDelFileList */
34    protected $list;
35    /** @var OldLocalFile */
36    protected $file;
37    protected IConnectionProvider $dbProvider;
38
39    public function __construct( RevisionListBase $list, $row ) {
40        parent::__construct( $list, $row );
41        $this->file = static::initFile( $list, $row );
42        $this->dbProvider = MediaWikiServices::getInstance()->getConnectionProvider();
43    }
44
45    /**
46     * Create file object from $row sourced from $list
47     *
48     * @param RevisionListBase $list
49     * @param mixed $row
50     * @return mixed
51     */
52    protected static function initFile( $list, $row ) {
53        return MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
54            ->newFileFromRow( $row );
55    }
56
57    public function getIdField() {
58        return 'oi_archive_name';
59    }
60
61    public function getTimestampField() {
62        return 'oi_timestamp';
63    }
64
65    public function getAuthorIdField() {
66        return 'oi_user';
67    }
68
69    public function getAuthorNameField() {
70        return 'oi_user_text';
71    }
72
73    public function getAuthorActorField() {
74        return 'oi_actor';
75    }
76
77    public function getId() {
78        $parts = explode( '!', $this->row->oi_archive_name );
79
80        return $parts[0];
81    }
82
83    public function canView() {
84        return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getAuthority() );
85    }
86
87    public function canViewContent() {
88        return $this->file->userCan( File::DELETED_FILE, $this->list->getAuthority() );
89    }
90
91    public function getBits() {
92        return $this->file->getVisibility();
93    }
94
95    public function setBits( $bits ) {
96        # Queue the file op
97        # @todo FIXME: Move to LocalFile.php
98        if ( $this->isDeleted() ) {
99            if ( $bits & File::DELETED_FILE ) {
100                # Still deleted
101            } else {
102                # Newly undeleted
103                $key = $this->file->getStorageKey();
104                $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
105                $this->list->storeBatch[] = [
106                    $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
107                    'public',
108                    $this->file->getRel()
109                ];
110                $this->list->cleanupBatch[] = $key;
111            }
112        } elseif ( $bits & File::DELETED_FILE ) {
113            # Newly deleted
114            $key = $this->file->getStorageKey();
115            $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
116            $this->list->deleteBatch[] = [ $this->file->getRel(), $dstRel ];
117        }
118
119        # Do the database operations
120        $dbw = $this->dbProvider->getPrimaryDatabase();
121        $dbw->newUpdateQueryBuilder()
122            ->update( 'oldimage' )
123            ->set( [ 'oi_deleted' => $bits ] )
124            ->where( [
125                'oi_name' => $this->row->oi_name,
126                'oi_timestamp' => $this->row->oi_timestamp,
127                'oi_deleted' => $this->getBits()
128            ] )
129            ->caller( __METHOD__ )->execute();
130
131        return (bool)$dbw->affectedRows();
132    }
133
134    public function isDeleted() {
135        return $this->file->isDeleted( File::DELETED_FILE );
136    }
137
138    /**
139     * Get the link to the file.
140     * Overridden by RevDelArchivedFileItem.
141     * @return string
142     */
143    protected function getLink() {
144        $date = $this->list->getLanguage()->userTimeAndDate(
145            $this->file->getTimestamp(), $this->list->getUser() );
146
147        if ( !$this->isDeleted() ) {
148            # Regular files...
149            return Html::element( 'a', [ 'href' => $this->file->getUrl() ], $date );
150        }
151
152        # Hidden files...
153        if ( !$this->canViewContent() ) {
154            $link = htmlspecialchars( $date );
155        } else {
156            $link = $this->getLinkRenderer()->makeLink(
157                SpecialPage::getTitleFor( 'Revisiondelete' ),
158                $date,
159                [],
160                [
161                    'target' => $this->list->getPageName(),
162                    'file' => $this->file->getArchiveName(),
163                    'token' => $this->list->getUser()->getEditToken(
164                        $this->file->getArchiveName() )
165                ]
166            );
167        }
168
169        return '<span class="history-deleted">' . $link . '</span>';
170    }
171
172    /**
173     * Generate a user tool link cluster if the current user is allowed to view it
174     * @return string HTML
175     */
176    protected function getUserTools() {
177        $uploader = $this->file->getUploader( File::FOR_THIS_USER, $this->list->getAuthority() );
178        if ( $uploader ) {
179            $link = Linker::userLink( $uploader->getId(), $uploader->getName() ) .
180                Linker::userToolLinks( $uploader->getId(), $uploader->getName() );
181            return $link;
182        } else {
183            $link = $this->list->msg( 'rev-deleted-user' )->escaped();
184        }
185        if ( $this->file->isDeleted( File::DELETED_USER ) ) {
186            return '<span class="history-deleted">' . $link . '</span>';
187        }
188        return $link;
189    }
190
191    /**
192     * Wrap and format the file's comment block, if the current
193     * user is allowed to view it.
194     *
195     * @return string HTML
196     */
197    protected function getComment() {
198        if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getAuthority() ) ) {
199            $block = MediaWikiServices::getInstance()->getCommentFormatter()
200                ->formatBlock( $this->file->getDescription() );
201        } else {
202            $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
203        }
204        if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
205            return "<span class=\"history-deleted\">$block</span>";
206        }
207
208        return $block;
209    }
210
211    public function getHTML() {
212        $data =
213            $this->list->msg( 'widthheight' )->numParams(
214                $this->file->getWidth(),
215                $this->file->getHeight() )->escaped() .
216            ' (' . $this->list->msg( 'nbytes' )->numParams(
217                $this->file->getSize() )->escaped() . ')';
218
219        return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
220            $data . ' ' . $this->getComment() . '</li>';
221    }
222
223    public function getApiData( ApiResult $result ) {
224        $file = $this->file;
225        $user = $this->list->getUser();
226        $ret = [
227            'title' => $this->list->getPageName(),
228            'archivename' => $file->getArchiveName(),
229            'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
230            'width' => $file->getWidth(),
231            'height' => $file->getHeight(),
232            'size' => $file->getSize(),
233            'userhidden' => (bool)$file->isDeleted( File::DELETED_USER ),
234            'commenthidden' => (bool)$file->isDeleted( File::DELETED_COMMENT ),
235            'contenthidden' => (bool)$this->isDeleted(),
236        ];
237        if ( !$this->isDeleted() ) {
238            $ret += [
239                'url' => $file->getUrl(),
240            ];
241        } elseif ( $this->canViewContent() ) {
242            $ret += [
243                'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
244                    [
245                        'target' => $this->list->getPageName(),
246                        'file' => $file->getArchiveName(),
247                        'token' => $user->getEditToken( $file->getArchiveName() )
248                    ]
249                ),
250            ];
251        }
252        $uploader = $file->getUploader( File::FOR_THIS_USER, $user );
253        if ( $uploader ) {
254            $ret += [
255                'userid' => $uploader->getId(),
256                'user' => $uploader->getName(),
257            ];
258        }
259        $comment = $file->getDescription( File::FOR_THIS_USER, $user );
260        if ( ( $comment ?? '' ) !== '' ) {
261            $ret += [
262                'comment' => $comment,
263            ];
264        }
265
266        return $ret;
267    }
268
269    public function lock() {
270        return $this->file->acquireFileLock();
271    }
272
273    public function unlock() {
274        return $this->file->releaseFileLock();
275    }
276}