MediaWiki
master
|
00001 <?php 00029 class ArchivedFile { 00033 var $id, # filearchive row ID 00034 $name, # image name 00035 $group, # FileStore storage group 00036 $key, # FileStore sha1 key 00037 $size, # file dimensions 00038 $bits, # size in bytes 00039 $width, # width 00040 $height, # height 00041 $metadata, # metadata string 00042 $mime, # mime type 00043 $media_type, # media type 00044 $description, # upload description 00045 $user, # user ID of uploader 00046 $user_text, # user name of uploader 00047 $timestamp, # time of upload 00048 $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx) 00049 $deleted, # Bitfield akin to rev_deleted 00050 $sha1, # sha1 hash of file content 00051 $pageCount, 00052 $archive_name; 00053 00057 var $handler; 00061 var $title; # image title 00062 00071 function __construct( $title, $id=0, $key='' ) { 00072 $this->id = -1; 00073 $this->title = false; 00074 $this->name = false; 00075 $this->group = 'deleted'; // needed for direct use of constructor 00076 $this->key = ''; 00077 $this->size = 0; 00078 $this->bits = 0; 00079 $this->width = 0; 00080 $this->height = 0; 00081 $this->metadata = ''; 00082 $this->mime = "unknown/unknown"; 00083 $this->media_type = ''; 00084 $this->description = ''; 00085 $this->user = 0; 00086 $this->user_text = ''; 00087 $this->timestamp = null; 00088 $this->deleted = 0; 00089 $this->dataLoaded = false; 00090 $this->exists = false; 00091 $this->sha1 = ''; 00092 00093 if( $title instanceof Title ) { 00094 $this->title = File::normalizeTitle( $title, 'exception' ); 00095 $this->name = $title->getDBkey(); 00096 } 00097 00098 if ($id) { 00099 $this->id = $id; 00100 } 00101 00102 if ($key) { 00103 $this->key = $key; 00104 } 00105 00106 if ( !$id && !$key && !( $title instanceof Title ) ) { 00107 throw new MWException( "No specifications provided to ArchivedFile constructor." ); 00108 } 00109 } 00110 00116 public function load() { 00117 if ( $this->dataLoaded ) { 00118 return true; 00119 } 00120 $conds = array(); 00121 00122 if( $this->id > 0 ) { 00123 $conds['fa_id'] = $this->id; 00124 } 00125 if( $this->key ) { 00126 $conds['fa_storage_group'] = $this->group; 00127 $conds['fa_storage_key'] = $this->key; 00128 } 00129 if( $this->title ) { 00130 $conds['fa_name'] = $this->title->getDBkey(); 00131 } 00132 00133 if( !count($conds)) { 00134 throw new MWException( "No specific information for retrieving archived file" ); 00135 } 00136 00137 if( !$this->title || $this->title->getNamespace() == NS_FILE ) { 00138 $this->dataLoaded = true; // set it here, to have also true on miss 00139 $dbr = wfGetDB( DB_SLAVE ); 00140 $row = $dbr->selectRow( 'filearchive', 00141 array( 00142 'fa_id', 00143 'fa_name', 00144 'fa_archive_name', 00145 'fa_storage_key', 00146 'fa_storage_group', 00147 'fa_size', 00148 'fa_bits', 00149 'fa_width', 00150 'fa_height', 00151 'fa_metadata', 00152 'fa_media_type', 00153 'fa_major_mime', 00154 'fa_minor_mime', 00155 'fa_description', 00156 'fa_user', 00157 'fa_user_text', 00158 'fa_timestamp', 00159 'fa_deleted', 00160 'fa_sha1' ), 00161 $conds, 00162 __METHOD__, 00163 array( 'ORDER BY' => 'fa_timestamp DESC') 00164 ); 00165 if ( !$row ) { 00166 // this revision does not exist? 00167 return null; 00168 } 00169 00170 // initialize fields for filestore image object 00171 $this->loadFromRow( $row ); 00172 } else { 00173 throw new MWException( 'This title does not correspond to an image page.' ); 00174 } 00175 $this->exists = true; 00176 00177 return true; 00178 } 00179 00187 public static function newFromRow( $row ) { 00188 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) ); 00189 $file->loadFromRow( $row ); 00190 return $file; 00191 } 00192 00199 public function loadFromRow( $row ) { 00200 $this->id = intval($row->fa_id); 00201 $this->name = $row->fa_name; 00202 $this->archive_name = $row->fa_archive_name; 00203 $this->group = $row->fa_storage_group; 00204 $this->key = $row->fa_storage_key; 00205 $this->size = $row->fa_size; 00206 $this->bits = $row->fa_bits; 00207 $this->width = $row->fa_width; 00208 $this->height = $row->fa_height; 00209 $this->metadata = $row->fa_metadata; 00210 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime"; 00211 $this->media_type = $row->fa_media_type; 00212 $this->description = $row->fa_description; 00213 $this->user = $row->fa_user; 00214 $this->user_text = $row->fa_user_text; 00215 $this->timestamp = $row->fa_timestamp; 00216 $this->deleted = $row->fa_deleted; 00217 if( isset( $row->fa_sha1 ) ) { 00218 $this->sha1 = $row->fa_sha1; 00219 } else { 00220 // old row, populate from key 00221 $this->sha1 = LocalRepo::getHashFromKey( $this->key ); 00222 } 00223 } 00224 00230 public function getTitle() { 00231 return $this->title; 00232 } 00233 00239 public function getName() { 00240 return $this->name; 00241 } 00242 00246 public function getID() { 00247 $this->load(); 00248 return $this->id; 00249 } 00250 00254 public function exists() { 00255 $this->load(); 00256 return $this->exists; 00257 } 00258 00263 public function getKey() { 00264 $this->load(); 00265 return $this->key; 00266 } 00267 00272 public function getStorageKey() { 00273 return $this->getKey(); 00274 } 00275 00280 public function getGroup() { 00281 return $this->group; 00282 } 00283 00288 public function getWidth() { 00289 $this->load(); 00290 return $this->width; 00291 } 00292 00297 public function getHeight() { 00298 $this->load(); 00299 return $this->height; 00300 } 00301 00306 public function getMetadata() { 00307 $this->load(); 00308 return $this->metadata; 00309 } 00310 00315 public function getSize() { 00316 $this->load(); 00317 return $this->size; 00318 } 00319 00324 public function getBits() { 00325 $this->load(); 00326 return $this->bits; 00327 } 00328 00333 public function getMimeType() { 00334 $this->load(); 00335 return $this->mime; 00336 } 00337 00342 function getHandler() { 00343 if ( !isset( $this->handler ) ) { 00344 $this->handler = MediaHandler::getHandler( $this->getMimeType() ); 00345 } 00346 return $this->handler; 00347 } 00348 00353 function pageCount() { 00354 if ( !isset( $this->pageCount ) ) { 00355 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) { 00356 $this->pageCount = $this->handler->pageCount( $this ); 00357 } else { 00358 $this->pageCount = false; 00359 } 00360 } 00361 return $this->pageCount; 00362 } 00363 00369 public function getMediaType() { 00370 $this->load(); 00371 return $this->media_type; 00372 } 00373 00379 public function getTimestamp() { 00380 $this->load(); 00381 return wfTimestamp( TS_MW, $this->timestamp ); 00382 } 00383 00390 function getSha1() { 00391 $this->load(); 00392 return $this->sha1; 00393 } 00394 00400 public function getUser() { 00401 $this->load(); 00402 if( $this->isDeleted( File::DELETED_USER ) ) { 00403 return 0; 00404 } else { 00405 return $this->user; 00406 } 00407 } 00408 00414 public function getUserText() { 00415 $this->load(); 00416 if( $this->isDeleted( File::DELETED_USER ) ) { 00417 return 0; 00418 } else { 00419 return $this->user_text; 00420 } 00421 } 00422 00428 public function getDescription() { 00429 $this->load(); 00430 if( $this->isDeleted( File::DELETED_COMMENT ) ) { 00431 return 0; 00432 } else { 00433 return $this->description; 00434 } 00435 } 00436 00442 public function getRawUser() { 00443 $this->load(); 00444 return $this->user; 00445 } 00446 00452 public function getRawUserText() { 00453 $this->load(); 00454 return $this->user_text; 00455 } 00456 00462 public function getRawDescription() { 00463 $this->load(); 00464 return $this->description; 00465 } 00466 00471 public function getVisibility() { 00472 $this->load(); 00473 return $this->deleted; 00474 } 00475 00482 public function isDeleted( $field ) { 00483 $this->load(); 00484 return ($this->deleted & $field) == $field; 00485 } 00486 00494 public function userCan( $field, User $user = null ) { 00495 $this->load(); 00496 return Revision::userCanBitfield( $this->deleted, $field, $user ); 00497 } 00498 }