More function level documentation
[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 /**
39 * @static
40 * @param $sha1
41 * @param $repo LocalRepo
42 * @param bool $timestamp
43 * @return bool|OldLocalFile
44 */
45 static function newFromKey( $sha1, $repo, $timestamp = false ) {
46 $conds = array( 'oi_sha1' => $sha1 );
47 if( $timestamp ) {
48 $conds['oi_timestamp'] = $timestamp;
49 }
50 $dbr = $repo->getSlaveDB();
51 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
52 if( $row ) {
53 return self::newFromRow( $row, $repo );
54 } else {
55 return false;
56 }
57 }
58
59 /**
60 * Fields in the oldimage table
61 */
62 static function selectFields() {
63 return array(
64 'oi_name',
65 'oi_archive_name',
66 'oi_size',
67 'oi_width',
68 'oi_height',
69 'oi_metadata',
70 'oi_bits',
71 'oi_media_type',
72 'oi_major_mime',
73 'oi_minor_mime',
74 'oi_description',
75 'oi_user',
76 'oi_user_text',
77 'oi_timestamp',
78 'oi_deleted',
79 'oi_sha1',
80 );
81 }
82
83 /**
84 * @param $title Title
85 * @param $repo FileRepo
86 * @param $time String: timestamp or null to load by archive name
87 * @param $archiveName String: archive name or null to load by timestamp
88 */
89 function __construct( $title, $repo, $time, $archiveName ) {
90 parent::__construct( $title, $repo );
91 $this->requestedTime = $time;
92 $this->archive_name = $archiveName;
93 if ( is_null( $time ) && is_null( $archiveName ) ) {
94 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
95 }
96 }
97
98 function getCacheKey() {
99 return false;
100 }
101
102 function getArchiveName() {
103 if ( !isset( $this->archive_name ) ) {
104 $this->load();
105 }
106 return $this->archive_name;
107 }
108
109 function isOld() {
110 return true;
111 }
112
113 function isVisible() {
114 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
115 }
116
117 function loadFromDB() {
118 wfProfileIn( __METHOD__ );
119 $this->dataLoaded = true;
120 $dbr = $this->repo->getSlaveDB();
121 $conds = array( 'oi_name' => $this->getName() );
122 if ( is_null( $this->requestedTime ) ) {
123 $conds['oi_archive_name'] = $this->archive_name;
124 } else {
125 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
126 }
127 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
128 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
129 if ( $row ) {
130 $this->loadFromRow( $row, 'oi_' );
131 } else {
132 $this->fileExists = false;
133 }
134 wfProfileOut( __METHOD__ );
135 }
136
137 function getCacheFields( $prefix = 'img_' ) {
138 $fields = parent::getCacheFields( $prefix );
139 $fields[] = $prefix . 'archive_name';
140 $fields[] = $prefix . 'deleted';
141 return $fields;
142 }
143
144 function getRel() {
145 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
146 }
147
148 function getUrlRel() {
149 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
150 }
151
152 function upgradeRow() {
153 wfProfileIn( __METHOD__ );
154 $this->loadFromFile();
155
156 # Don't destroy file info of missing files
157 if ( !$this->fileExists ) {
158 wfDebug( __METHOD__.": file does not exist, aborting\n" );
159 wfProfileOut( __METHOD__ );
160 return;
161 }
162
163 $dbw = $this->repo->getMasterDB();
164 list( $major, $minor ) = self::splitMime( $this->mime );
165
166 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
167 $dbw->update( 'oldimage',
168 array(
169 'oi_width' => $this->width,
170 'oi_height' => $this->height,
171 'oi_bits' => $this->bits,
172 'oi_media_type' => $this->media_type,
173 'oi_major_mime' => $major,
174 'oi_minor_mime' => $minor,
175 'oi_metadata' => $this->metadata,
176 'oi_sha1' => $this->sha1,
177 ), array(
178 'oi_name' => $this->getName(),
179 'oi_archive_name' => $this->archive_name ),
180 __METHOD__
181 );
182 wfProfileOut( __METHOD__ );
183 }
184
185 /**
186 * @param $field Integer: one of DELETED_* bitfield constants
187 * for file or revision rows
188 * @return bool
189 */
190 function isDeleted( $field ) {
191 $this->load();
192 return ($this->deleted & $field) == $field;
193 }
194
195 /**
196 * Returns bitfield value
197 * @return int
198 */
199 function getVisibility() {
200 $this->load();
201 return (int)$this->deleted;
202 }
203
204 /**
205 * Determine if the current user is allowed to view a particular
206 * field of this image file, if it's marked as deleted.
207 *
208 * @param $field Integer
209 * @return bool
210 */
211 function userCan( $field ) {
212 $this->load();
213 return Revision::userCanBitfield( $this->deleted, $field );
214 }
215 }