MediaWiki
REL1_21
|
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( 00141 'filearchive', 00142 self::selectFields(), 00143 $conds, 00144 __METHOD__, 00145 array( 'ORDER BY' => 'fa_timestamp DESC' ) 00146 ); 00147 if ( !$row ) { 00148 // this revision does not exist? 00149 return null; 00150 } 00151 00152 // initialize fields for filestore image object 00153 $this->loadFromRow( $row ); 00154 } else { 00155 throw new MWException( 'This title does not correspond to an image page.' ); 00156 } 00157 $this->exists = true; 00158 00159 return true; 00160 } 00161 00169 public static function newFromRow( $row ) { 00170 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) ); 00171 $file->loadFromRow( $row ); 00172 return $file; 00173 } 00174 00179 static function selectFields() { 00180 return array( 00181 'fa_id', 00182 'fa_name', 00183 'fa_archive_name', 00184 'fa_storage_key', 00185 'fa_storage_group', 00186 'fa_size', 00187 'fa_bits', 00188 'fa_width', 00189 'fa_height', 00190 'fa_metadata', 00191 'fa_media_type', 00192 'fa_major_mime', 00193 'fa_minor_mime', 00194 'fa_description', 00195 'fa_user', 00196 'fa_user_text', 00197 'fa_timestamp', 00198 'fa_deleted', 00199 'fa_sha1', 00200 ); 00201 } 00202 00209 public function loadFromRow( $row ) { 00210 $this->id = intval( $row->fa_id ); 00211 $this->name = $row->fa_name; 00212 $this->archive_name = $row->fa_archive_name; 00213 $this->group = $row->fa_storage_group; 00214 $this->key = $row->fa_storage_key; 00215 $this->size = $row->fa_size; 00216 $this->bits = $row->fa_bits; 00217 $this->width = $row->fa_width; 00218 $this->height = $row->fa_height; 00219 $this->metadata = $row->fa_metadata; 00220 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime"; 00221 $this->media_type = $row->fa_media_type; 00222 $this->description = $row->fa_description; 00223 $this->user = $row->fa_user; 00224 $this->user_text = $row->fa_user_text; 00225 $this->timestamp = $row->fa_timestamp; 00226 $this->deleted = $row->fa_deleted; 00227 if( isset( $row->fa_sha1 ) ) { 00228 $this->sha1 = $row->fa_sha1; 00229 } else { 00230 // old row, populate from key 00231 $this->sha1 = LocalRepo::getHashFromKey( $this->key ); 00232 } 00233 } 00234 00240 public function getTitle() { 00241 return $this->title; 00242 } 00243 00249 public function getName() { 00250 return $this->name; 00251 } 00252 00256 public function getID() { 00257 $this->load(); 00258 return $this->id; 00259 } 00260 00264 public function exists() { 00265 $this->load(); 00266 return $this->exists; 00267 } 00268 00273 public function getKey() { 00274 $this->load(); 00275 return $this->key; 00276 } 00277 00282 public function getStorageKey() { 00283 return $this->getKey(); 00284 } 00285 00290 public function getGroup() { 00291 return $this->group; 00292 } 00293 00298 public function getWidth() { 00299 $this->load(); 00300 return $this->width; 00301 } 00302 00307 public function getHeight() { 00308 $this->load(); 00309 return $this->height; 00310 } 00311 00316 public function getMetadata() { 00317 $this->load(); 00318 return $this->metadata; 00319 } 00320 00325 public function getSize() { 00326 $this->load(); 00327 return $this->size; 00328 } 00329 00334 public function getBits() { 00335 $this->load(); 00336 return $this->bits; 00337 } 00338 00343 public function getMimeType() { 00344 $this->load(); 00345 return $this->mime; 00346 } 00347 00352 function getHandler() { 00353 if ( !isset( $this->handler ) ) { 00354 $this->handler = MediaHandler::getHandler( $this->getMimeType() ); 00355 } 00356 return $this->handler; 00357 } 00358 00363 function pageCount() { 00364 if ( !isset( $this->pageCount ) ) { 00365 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) { 00366 $this->pageCount = $this->handler->pageCount( $this ); 00367 } else { 00368 $this->pageCount = false; 00369 } 00370 } 00371 return $this->pageCount; 00372 } 00373 00379 public function getMediaType() { 00380 $this->load(); 00381 return $this->media_type; 00382 } 00383 00389 public function getTimestamp() { 00390 $this->load(); 00391 return wfTimestamp( TS_MW, $this->timestamp ); 00392 } 00393 00400 function getSha1() { 00401 $this->load(); 00402 return $this->sha1; 00403 } 00404 00410 public function getUser() { 00411 $this->load(); 00412 if( $this->isDeleted( File::DELETED_USER ) ) { 00413 return 0; 00414 } else { 00415 return $this->user; 00416 } 00417 } 00418 00424 public function getUserText() { 00425 $this->load(); 00426 if( $this->isDeleted( File::DELETED_USER ) ) { 00427 return 0; 00428 } else { 00429 return $this->user_text; 00430 } 00431 } 00432 00438 public function getDescription() { 00439 $this->load(); 00440 if( $this->isDeleted( File::DELETED_COMMENT ) ) { 00441 return 0; 00442 } else { 00443 return $this->description; 00444 } 00445 } 00446 00452 public function getRawUser() { 00453 $this->load(); 00454 return $this->user; 00455 } 00456 00462 public function getRawUserText() { 00463 $this->load(); 00464 return $this->user_text; 00465 } 00466 00472 public function getRawDescription() { 00473 $this->load(); 00474 return $this->description; 00475 } 00476 00481 public function getVisibility() { 00482 $this->load(); 00483 return $this->deleted; 00484 } 00485 00492 public function isDeleted( $field ) { 00493 $this->load(); 00494 return ($this->deleted & $field) == $field; 00495 } 00496 00504 public function userCan( $field, User $user = null ) { 00505 $this->load(); 00506 return Revision::userCanBitfield( $this->deleted, $field, $user ); 00507 } 00508 }