46c35bd950a0d2b8b74eeeba5746924add4d83ce
[lhc/web/wiklou.git] / includes / filerepo / OldLocalFile.php
1 <?php
2
3 /**
4 * Class to represent a file in the oldimage table
5 *
6 * @ingroup FileRepo
7 */
8 class OldLocalFile extends LocalFile {
9 var $requestedTime, $archive_name;
10
11 const CACHE_VERSION = 1;
12 const MAX_CACHE_ROWS = 20;
13
14 static function newFromTitle( $title, $repo, $time = null ) {
15 # The null default value is only here to avoid an E_STRICT
16 if( $time === null )
17 throw new MWException( __METHOD__.' got null for $time parameter' );
18 return new self( $title, $repo, $time, null );
19 }
20
21 static function newFromArchiveName( $title, $repo, $archiveName ) {
22 return new self( $title, $repo, null, $archiveName );
23 }
24
25 static function newFromRow( $row, $repo ) {
26 $title = Title::makeTitle( NS_FILE, $row->oi_name );
27 $file = new self( $title, $repo, null, $row->oi_archive_name );
28 $file->loadFromRow( $row, 'oi_' );
29 return $file;
30 }
31
32 static function newFromKey( $sha1, $repo, $timestamp = false ) {
33 # Polymorphic function name to distinguish foreign and local fetches
34 $fname = get_class( $this ) . '::' . __FUNCTION__;
35
36 $conds = array( 'oi_sha1' => $sha1 );
37 if( $timestamp ) {
38 $conds['oi_timestamp'] = $timestamp;
39 }
40 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ), $conds, $fname );
41 if( $row ) {
42 return self::newFromRow( $row, $repo );
43 } else {
44 return false;
45 }
46 }
47
48 /**
49 * Fields in the oldimage table
50 */
51 static function selectFields() {
52 return array(
53 'oi_name',
54 'oi_archive_name',
55 'oi_size',
56 'oi_width',
57 'oi_height',
58 'oi_metadata',
59 'oi_bits',
60 'oi_media_type',
61 'oi_major_mime',
62 'oi_minor_mime',
63 'oi_description',
64 'oi_user',
65 'oi_user_text',
66 'oi_timestamp',
67 'oi_deleted',
68 'oi_sha1',
69 );
70 }
71
72 /**
73 * @param Title $title
74 * @param FileRepo $repo
75 * @param string $time Timestamp or null to load by archive name
76 * @param string $archiveName Archive name or null to load by timestamp
77 */
78 function __construct( $title, $repo, $time, $archiveName ) {
79 parent::__construct( $title, $repo );
80 $this->requestedTime = $time;
81 $this->archive_name = $archiveName;
82 if ( is_null( $time ) && is_null( $archiveName ) ) {
83 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
84 }
85 }
86
87 function getCacheKey() {
88 return false;
89 }
90
91 function getArchiveName() {
92 if ( !isset( $this->archive_name ) ) {
93 $this->load();
94 }
95 return $this->archive_name;
96 }
97
98 function isOld() {
99 return true;
100 }
101
102 function isVisible() {
103 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
104 }
105
106 function loadFromDB() {
107 wfProfileIn( __METHOD__ );
108 $this->dataLoaded = true;
109 $dbr = $this->repo->getSlaveDB();
110 $conds = array( 'oi_name' => $this->getName() );
111 if ( is_null( $this->requestedTime ) ) {
112 $conds['oi_archive_name'] = $this->archive_name;
113 } else {
114 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
115 }
116 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
117 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
118 if ( $row ) {
119 $this->loadFromRow( $row, 'oi_' );
120 } else {
121 $this->fileExists = false;
122 }
123 wfProfileOut( __METHOD__ );
124 }
125
126 function getCacheFields( $prefix = 'img_' ) {
127 $fields = parent::getCacheFields( $prefix );
128 $fields[] = $prefix . 'archive_name';
129 $fields[] = $prefix . 'deleted';
130 return $fields;
131 }
132
133 function getRel() {
134 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
135 }
136
137 function getUrlRel() {
138 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
139 }
140
141 function upgradeRow() {
142 wfProfileIn( __METHOD__ );
143 $this->loadFromFile();
144
145 # Don't destroy file info of missing files
146 if ( !$this->fileExists ) {
147 wfDebug( __METHOD__.": file does not exist, aborting\n" );
148 wfProfileOut( __METHOD__ );
149 return;
150 }
151
152 $dbw = $this->repo->getMasterDB();
153 list( $major, $minor ) = self::splitMime( $this->mime );
154
155 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
156 $dbw->update( 'oldimage',
157 array(
158 'oi_width' => $this->width,
159 'oi_height' => $this->height,
160 'oi_bits' => $this->bits,
161 'oi_media_type' => $this->media_type,
162 'oi_major_mime' => $major,
163 'oi_minor_mime' => $minor,
164 'oi_metadata' => $this->metadata,
165 'oi_sha1' => $this->sha1,
166 ), array(
167 'oi_name' => $this->getName(),
168 'oi_archive_name' => $this->archive_name ),
169 __METHOD__
170 );
171 wfProfileOut( __METHOD__ );
172 }
173
174 /**
175 * int $field one of DELETED_* bitfield constants
176 * for file or revision rows
177 * @return bool
178 */
179 function isDeleted( $field ) {
180 return ($this->deleted & $field) == $field;
181 }
182
183 /**
184 * Determine if the current user is allowed to view a particular
185 * field of this FileStore image file, if it's marked as deleted.
186 * @param int $field
187 * @return bool
188 */
189 function userCan( $field ) {
190 if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
191 global $wgUser;
192 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
193 ? 'suppressrevision'
194 : 'deleterevision';
195 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
196 return $wgUser->isAllowed( $permission );
197 } else {
198 return true;
199 }
200 }
201 }