MediaWiki
REL1_23
|
00001 <?php 00029 class ArchivedFile { 00031 private $id; 00032 00034 private $name; 00035 00037 private $group; 00038 00040 private $key; 00041 00043 private $size; 00044 00046 private $bits; 00047 00049 private $width; 00050 00052 private $height; 00053 00055 private $metadata; 00056 00058 private $mime; 00059 00061 private $media_type; 00062 00064 private $description; 00065 00067 private $user; 00068 00070 private $user_text; 00071 00073 private $timestamp; 00074 00076 private $dataLoaded; 00077 00079 private $deleted; 00080 00082 private $sha1; 00083 00087 private $pageCount; 00088 00090 private $archive_name; 00091 00093 protected $handler; 00094 00096 protected $title; # image title 00097 00104 function __construct( $title, $id = 0, $key = '' ) { 00105 $this->id = -1; 00106 $this->title = false; 00107 $this->name = false; 00108 $this->group = 'deleted'; // needed for direct use of constructor 00109 $this->key = ''; 00110 $this->size = 0; 00111 $this->bits = 0; 00112 $this->width = 0; 00113 $this->height = 0; 00114 $this->metadata = ''; 00115 $this->mime = "unknown/unknown"; 00116 $this->media_type = ''; 00117 $this->description = ''; 00118 $this->user = 0; 00119 $this->user_text = ''; 00120 $this->timestamp = null; 00121 $this->deleted = 0; 00122 $this->dataLoaded = false; 00123 $this->exists = false; 00124 $this->sha1 = ''; 00125 00126 if ( $title instanceof Title ) { 00127 $this->title = File::normalizeTitle( $title, 'exception' ); 00128 $this->name = $title->getDBkey(); 00129 } 00130 00131 if ( $id ) { 00132 $this->id = $id; 00133 } 00134 00135 if ( $key ) { 00136 $this->key = $key; 00137 } 00138 00139 if ( !$id && !$key && !( $title instanceof Title ) ) { 00140 throw new MWException( "No specifications provided to ArchivedFile constructor." ); 00141 } 00142 } 00143 00149 public function load() { 00150 if ( $this->dataLoaded ) { 00151 return true; 00152 } 00153 $conds = array(); 00154 00155 if ( $this->id > 0 ) { 00156 $conds['fa_id'] = $this->id; 00157 } 00158 if ( $this->key ) { 00159 $conds['fa_storage_group'] = $this->group; 00160 $conds['fa_storage_key'] = $this->key; 00161 } 00162 if ( $this->title ) { 00163 $conds['fa_name'] = $this->title->getDBkey(); 00164 } 00165 00166 if ( !count( $conds ) ) { 00167 throw new MWException( "No specific information for retrieving archived file" ); 00168 } 00169 00170 if ( !$this->title || $this->title->getNamespace() == NS_FILE ) { 00171 $this->dataLoaded = true; // set it here, to have also true on miss 00172 $dbr = wfGetDB( DB_SLAVE ); 00173 $row = $dbr->selectRow( 00174 'filearchive', 00175 self::selectFields(), 00176 $conds, 00177 __METHOD__, 00178 array( 'ORDER BY' => 'fa_timestamp DESC' ) 00179 ); 00180 if ( !$row ) { 00181 // this revision does not exist? 00182 return null; 00183 } 00184 00185 // initialize fields for filestore image object 00186 $this->loadFromRow( $row ); 00187 } else { 00188 throw new MWException( 'This title does not correspond to an image page.' ); 00189 } 00190 $this->exists = true; 00191 00192 return true; 00193 } 00194 00201 public static function newFromRow( $row ) { 00202 $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) ); 00203 $file->loadFromRow( $row ); 00204 00205 return $file; 00206 } 00207 00212 static function selectFields() { 00213 return array( 00214 'fa_id', 00215 'fa_name', 00216 'fa_archive_name', 00217 'fa_storage_key', 00218 'fa_storage_group', 00219 'fa_size', 00220 'fa_bits', 00221 'fa_width', 00222 'fa_height', 00223 'fa_metadata', 00224 'fa_media_type', 00225 'fa_major_mime', 00226 'fa_minor_mime', 00227 'fa_description', 00228 'fa_user', 00229 'fa_user_text', 00230 'fa_timestamp', 00231 'fa_deleted', 00232 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */ 00233 'fa_sha1', 00234 ); 00235 } 00236 00243 public function loadFromRow( $row ) { 00244 $this->id = intval( $row->fa_id ); 00245 $this->name = $row->fa_name; 00246 $this->archive_name = $row->fa_archive_name; 00247 $this->group = $row->fa_storage_group; 00248 $this->key = $row->fa_storage_key; 00249 $this->size = $row->fa_size; 00250 $this->bits = $row->fa_bits; 00251 $this->width = $row->fa_width; 00252 $this->height = $row->fa_height; 00253 $this->metadata = $row->fa_metadata; 00254 $this->mime = "$row->fa_major_mime/$row->fa_minor_mime"; 00255 $this->media_type = $row->fa_media_type; 00256 $this->description = $row->fa_description; 00257 $this->user = $row->fa_user; 00258 $this->user_text = $row->fa_user_text; 00259 $this->timestamp = $row->fa_timestamp; 00260 $this->deleted = $row->fa_deleted; 00261 if ( isset( $row->fa_sha1 ) ) { 00262 $this->sha1 = $row->fa_sha1; 00263 } else { 00264 // old row, populate from key 00265 $this->sha1 = LocalRepo::getHashFromKey( $this->key ); 00266 } 00267 } 00268 00274 public function getTitle() { 00275 return $this->title; 00276 } 00277 00283 public function getName() { 00284 return $this->name; 00285 } 00286 00290 public function getID() { 00291 $this->load(); 00292 00293 return $this->id; 00294 } 00295 00299 public function exists() { 00300 $this->load(); 00301 00302 return $this->exists; 00303 } 00304 00309 public function getKey() { 00310 $this->load(); 00311 00312 return $this->key; 00313 } 00314 00319 public function getStorageKey() { 00320 return $this->getKey(); 00321 } 00322 00327 public function getGroup() { 00328 return $this->group; 00329 } 00330 00335 public function getWidth() { 00336 $this->load(); 00337 00338 return $this->width; 00339 } 00340 00345 public function getHeight() { 00346 $this->load(); 00347 00348 return $this->height; 00349 } 00350 00355 public function getMetadata() { 00356 $this->load(); 00357 00358 return $this->metadata; 00359 } 00360 00365 public function getSize() { 00366 $this->load(); 00367 00368 return $this->size; 00369 } 00370 00375 public function getBits() { 00376 $this->load(); 00377 00378 return $this->bits; 00379 } 00380 00385 public function getMimeType() { 00386 $this->load(); 00387 00388 return $this->mime; 00389 } 00390 00395 function getHandler() { 00396 if ( !isset( $this->handler ) ) { 00397 $this->handler = MediaHandler::getHandler( $this->getMimeType() ); 00398 } 00399 00400 return $this->handler; 00401 } 00402 00407 function pageCount() { 00408 if ( !isset( $this->pageCount ) ) { 00409 if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) { 00410 $this->pageCount = $this->handler->pageCount( $this ); 00411 } else { 00412 $this->pageCount = false; 00413 } 00414 } 00415 00416 return $this->pageCount; 00417 } 00418 00424 public function getMediaType() { 00425 $this->load(); 00426 00427 return $this->media_type; 00428 } 00429 00435 public function getTimestamp() { 00436 $this->load(); 00437 00438 return wfTimestamp( TS_MW, $this->timestamp ); 00439 } 00440 00447 function getSha1() { 00448 $this->load(); 00449 00450 return $this->sha1; 00451 } 00452 00463 public function getUser( $type = 'text' ) { 00464 $this->load(); 00465 00466 if ( $type == 'text' ) { 00467 return $this->user_text; 00468 } elseif ( $type == 'id' ) { 00469 return $this->user; 00470 } 00471 00472 throw new MWException( "Unknown type '$type'." ); 00473 } 00474 00481 public function getUserText() { 00482 wfDeprecated( __METHOD__, '1.23' ); 00483 $this->load(); 00484 if ( $this->isDeleted( File::DELETED_USER ) ) { 00485 return 0; 00486 } else { 00487 return $this->user_text; 00488 } 00489 } 00490 00496 public function getDescription() { 00497 $this->load(); 00498 if ( $this->isDeleted( File::DELETED_COMMENT ) ) { 00499 return 0; 00500 } else { 00501 return $this->description; 00502 } 00503 } 00504 00510 public function getRawUser() { 00511 $this->load(); 00512 00513 return $this->user; 00514 } 00515 00521 public function getRawUserText() { 00522 $this->load(); 00523 00524 return $this->user_text; 00525 } 00526 00532 public function getRawDescription() { 00533 $this->load(); 00534 00535 return $this->description; 00536 } 00537 00542 public function getVisibility() { 00543 $this->load(); 00544 00545 return $this->deleted; 00546 } 00547 00554 public function isDeleted( $field ) { 00555 $this->load(); 00556 00557 return ( $this->deleted & $field ) == $field; 00558 } 00559 00567 public function userCan( $field, User $user = null ) { 00568 $this->load(); 00569 00570 return Revision::userCanBitfield( $this->deleted, $field, $user ); 00571 } 00572 }