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