MediaWiki  REL1_19
WikiFilePage.php
Go to the documentation of this file.
00001 <?php
00007 class WikiFilePage extends WikiPage {
00011         protected $mFile = false;                               // !< File object
00012         protected $mRepo = null;                            // !<
00013         protected $mFileLoaded = false;             // !<
00014         protected $mDupes = null;                               // !<
00015 
00016         public function __construct( $title ) {
00017                 parent::__construct( $title );
00018                 $this->mDupes = null;
00019                 $this->mRepo = null;
00020         }
00021 
00022         public function getActionOverrides() {
00023                 return array( 'revert' => 'RevertFileAction' );
00024         }
00025 
00029         public function setFile( $file ) {
00030                 $this->mFile = $file;
00031                 $this->mFileLoaded = true;
00032         }
00033 
00037         protected function loadFile() {
00038                 if ( $this->mFileLoaded ) {
00039                         return true;
00040                 }
00041                 $this->mFileLoaded = true;
00042 
00043                 $this->mFile = false;
00044                 if ( !$this->mFile ) {
00045                         $this->mFile = wfFindFile( $this->mTitle );
00046                         if ( !$this->mFile ) {
00047                                 $this->mFile = wfLocalFile( $this->mTitle ); // always a File
00048                         }
00049                 }
00050                 $this->mRepo = $this->mFile->getRepo();
00051                 return true;
00052         }
00053 
00057         public function getRedirectTarget() {
00058                 $this->loadFile();
00059                 if ( $this->mFile->isLocal() ) {
00060                         return parent::getRedirectTarget();
00061                 }
00062                 // Foreign image page
00063                 $from = $this->mFile->getRedirected();
00064                 $to = $this->mFile->getName();
00065                 if ( $from == $to ) {
00066                         return null;
00067                 }
00068                 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
00069         }
00070 
00074         public function followRedirect() {
00075                 $this->loadFile();
00076                 if ( $this->mFile->isLocal() ) {
00077                         return parent::followRedirect();
00078                 }
00079                 $from = $this->mFile->getRedirected();
00080                 $to = $this->mFile->getName();
00081                 if ( $from == $to ) {
00082                         return false;
00083                 }
00084                 return Title::makeTitle( NS_FILE, $to );
00085         }
00086 
00091         public function isRedirect( $text = false ) {
00092                 $this->loadFile();
00093                 if ( $this->mFile->isLocal() ) {
00094                         return parent::isRedirect( $text );
00095                 }
00096 
00097                 return (bool)$this->mFile->getRedirected();
00098         }
00099 
00103         public function isLocal() {
00104                 $this->loadFile();
00105                 return $this->mFile->isLocal();
00106         }
00107 
00111         public function getFile() {
00112                 $this->loadFile();
00113                 return $this->mFile;
00114         }
00115 
00119         public function getDuplicates() {
00120                 $this->loadFile();
00121                 if ( !is_null( $this->mDupes ) ) {
00122                         return $this->mDupes;
00123                 }
00124                 $hash = $this->mFile->getSha1();
00125                 if ( !( $hash ) ) {
00126                         return $this->mDupes = array();
00127                 }
00128                 $dupes = RepoGroup::singleton()->findBySha1( $hash );
00129                 // Remove duplicates with self and non matching file sizes
00130                 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
00131                 $size = $this->mFile->getSize();
00132 
00136                 foreach ( $dupes as $index => $file ) {
00137                         $key = $file->getRepoName() . ':' . $file->getName();
00138                         if ( $key == $self ) {
00139                                 unset( $dupes[$index] );
00140                         }
00141                         if ( $file->getSize() != $size ) {
00142                                 unset( $dupes[$index] );
00143                         }
00144                 }
00145                 $this->mDupes = $dupes;
00146                 return $this->mDupes;
00147         }
00148 
00152         public function doPurge() {
00153                 $this->loadFile();
00154                 if ( $this->mFile->exists() ) {
00155                         wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
00156                         $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
00157                         $update->doUpdate();
00158                         $this->mFile->upgradeRow();
00159                         $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
00160                 } else {
00161                         wfDebug( 'ImagePage::doPurge no image for ' . $this->mFile->getName() . "; limiting purge to cache only\n" );
00162                         // even if the file supposedly doesn't exist, force any cached information
00163                         // to be updated (in case the cached information is wrong)
00164                         $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
00165                 }
00166                 return parent::doPurge();
00167         }
00168 }