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