WARNING: HUGE COMMIT
[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_IMAGE, $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 * @param Title $title
50 * @param FileRepo $repo
51 * @param string $time Timestamp or null to load by archive name
52 * @param string $archiveName Archive name or null to load by timestamp
53 */
54 function __construct( $title, $repo, $time, $archiveName ) {
55 parent::__construct( $title, $repo );
56 $this->requestedTime = $time;
57 $this->archive_name = $archiveName;
58 if ( is_null( $time ) && is_null( $archiveName ) ) {
59 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
60 }
61 }
62
63 function getCacheKey() {
64 return false;
65 }
66
67 function getArchiveName() {
68 if ( !isset( $this->archive_name ) ) {
69 $this->load();
70 }
71 return $this->archive_name;
72 }
73
74 function isOld() {
75 return true;
76 }
77
78 function isVisible() {
79 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
80 }
81
82 function loadFromDB() {
83 wfProfileIn( __METHOD__ );
84 $this->dataLoaded = true;
85 $dbr = $this->repo->getSlaveDB();
86 $conds = array( 'oi_name' => $this->getName() );
87 if ( is_null( $this->requestedTime ) ) {
88 $conds['oi_archive_name'] = $this->archive_name;
89 } else {
90 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
91 }
92 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
93 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
94 if ( $row ) {
95 $this->loadFromRow( $row, 'oi_' );
96 } else {
97 $this->fileExists = false;
98 }
99 wfProfileOut( __METHOD__ );
100 }
101
102 function getCacheFields( $prefix = 'img_' ) {
103 $fields = parent::getCacheFields( $prefix );
104 $fields[] = $prefix . 'archive_name';
105 $fields[] = $prefix . 'deleted';
106 return $fields;
107 }
108
109 function getRel() {
110 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
111 }
112
113 function getUrlRel() {
114 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
115 }
116
117 function upgradeRow() {
118 wfProfileIn( __METHOD__ );
119 $this->loadFromFile();
120
121 # Don't destroy file info of missing files
122 if ( !$this->fileExists ) {
123 wfDebug( __METHOD__.": file does not exist, aborting\n" );
124 wfProfileOut( __METHOD__ );
125 return;
126 }
127
128 $dbw = $this->repo->getMasterDB();
129 list( $major, $minor ) = self::splitMime( $this->mime );
130
131 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
132 $dbw->update( 'oldimage',
133 array(
134 'oi_width' => $this->width,
135 'oi_height' => $this->height,
136 'oi_bits' => $this->bits,
137 'oi_media_type' => $this->media_type,
138 'oi_major_mime' => $major,
139 'oi_minor_mime' => $minor,
140 'oi_metadata' => $this->metadata,
141 'oi_sha1' => $this->sha1,
142 ), array(
143 'oi_name' => $this->getName(),
144 'oi_archive_name' => $this->archive_name ),
145 __METHOD__
146 );
147 wfProfileOut( __METHOD__ );
148 }
149
150 /**
151 * int $field one of DELETED_* bitfield constants
152 * for file or revision rows
153 * @return bool
154 */
155 function isDeleted( $field ) {
156 return ($this->deleted & $field) == $field;
157 }
158
159 /**
160 * Determine if the current user is allowed to view a particular
161 * field of this FileStore image file, if it's marked as deleted.
162 * @param int $field
163 * @return bool
164 */
165 function userCan( $field ) {
166 if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
167 global $wgUser;
168 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
169 ? 'hiderevision'
170 : 'deleterevision';
171 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
172 return $wgUser->isAllowed( $permission );
173 } else {
174 return true;
175 }
176 }
177 }