MediaWiki  REL1_23
WikiFilePage.php
Go to the documentation of this file.
00001 <?php
00028 class WikiFilePage extends WikiPage {
00032     protected $mFile = false;               // !< File object
00033     protected $mRepo = null;                // !<
00034     protected $mFileLoaded = false;         // !<
00035     protected $mDupes = null;               // !<
00036 
00037     public function __construct( $title ) {
00038         parent::__construct( $title );
00039         $this->mDupes = null;
00040         $this->mRepo = null;
00041     }
00042 
00043     public function getActionOverrides() {
00044         $overrides = parent::getActionOverrides();
00045         $overrides['revert'] = 'RevertFileAction';
00046         return $overrides;
00047     }
00048 
00052     public function setFile( $file ) {
00053         $this->mFile = $file;
00054         $this->mFileLoaded = true;
00055     }
00056 
00060     protected function loadFile() {
00061         if ( $this->mFileLoaded ) {
00062             return true;
00063         }
00064         $this->mFileLoaded = true;
00065 
00066         $this->mFile = wfFindFile( $this->mTitle );
00067         if ( !$this->mFile ) {
00068             $this->mFile = wfLocalFile( $this->mTitle ); // always a File
00069         }
00070         $this->mRepo = $this->mFile->getRepo();
00071         return true;
00072     }
00073 
00077     public function getRedirectTarget() {
00078         $this->loadFile();
00079         if ( $this->mFile->isLocal() ) {
00080             return parent::getRedirectTarget();
00081         }
00082         // Foreign image page
00083         $from = $this->mFile->getRedirected();
00084         $to = $this->mFile->getName();
00085         if ( $from == $to ) {
00086             return null;
00087         }
00088         $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
00089         return $this->mRedirectTarget;
00090     }
00091 
00095     public function followRedirect() {
00096         $this->loadFile();
00097         if ( $this->mFile->isLocal() ) {
00098             return parent::followRedirect();
00099         }
00100         $from = $this->mFile->getRedirected();
00101         $to = $this->mFile->getName();
00102         if ( $from == $to ) {
00103             return false;
00104         }
00105         return Title::makeTitle( NS_FILE, $to );
00106     }
00107 
00111     public function isRedirect() {
00112         $this->loadFile();
00113         if ( $this->mFile->isLocal() ) {
00114             return parent::isRedirect();
00115         }
00116 
00117         return (bool)$this->mFile->getRedirected();
00118     }
00119 
00123     public function isLocal() {
00124         $this->loadFile();
00125         return $this->mFile->isLocal();
00126     }
00127 
00131     public function getFile() {
00132         $this->loadFile();
00133         return $this->mFile;
00134     }
00135 
00139     public function getDuplicates() {
00140         $this->loadFile();
00141         if ( !is_null( $this->mDupes ) ) {
00142             return $this->mDupes;
00143         }
00144         $hash = $this->mFile->getSha1();
00145         if ( !( $hash ) ) {
00146             $this->mDupes = array();
00147             return $this->mDupes;
00148         }
00149         $dupes = RepoGroup::singleton()->findBySha1( $hash );
00150         // Remove duplicates with self and non matching file sizes
00151         $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
00152         $size = $this->mFile->getSize();
00153 
00157         foreach ( $dupes as $index => $file ) {
00158             $key = $file->getRepoName() . ':' . $file->getName();
00159             if ( $key == $self ) {
00160                 unset( $dupes[$index] );
00161             }
00162             if ( $file->getSize() != $size ) {
00163                 unset( $dupes[$index] );
00164             }
00165         }
00166         $this->mDupes = $dupes;
00167         return $this->mDupes;
00168     }
00169 
00174     public function doPurge() {
00175         $this->loadFile();
00176         if ( $this->mFile->exists() ) {
00177             wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
00178             $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
00179             $update->doUpdate();
00180             $this->mFile->upgradeRow();
00181             $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
00182         } else {
00183             wfDebug( 'ImagePage::doPurge no image for ' . $this->mFile->getName() . "; limiting purge to cache only\n" );
00184             // even if the file supposedly doesn't exist, force any cached information
00185             // to be updated (in case the cached information is wrong)
00186             $this->mFile->purgeCache( array( 'forThumbRefresh' => true ) );
00187         }
00188         if ( $this->mRepo ) {
00189             // Purge redirect cache
00190             $this->mRepo->invalidateImageRedirect( $this->mTitle );
00191         }
00192         return parent::doPurge();
00193     }
00194 
00204     public function getForeignCategories() {
00205         $this->loadFile();
00206         $title = $this->mTitle;
00207         $file = $this->mFile;
00208 
00209         if ( ! $file instanceof LocalFile ) {
00210             wfDebug( __CLASS__ . '::' . __METHOD__ . " is not supported for this file\n" );
00211             return TitleArray::newFromResult( new FakeResultWrapper( array() ) );
00212         }
00213 
00215         $repo = $file->getRepo();
00216         $dbr = $repo->getSlaveDB();
00217 
00218         $res = $dbr->select(
00219             array( 'page', 'categorylinks' ),
00220             array(
00221                 'page_title' => 'cl_to',
00222                 'page_namespace' => NS_CATEGORY,
00223             ),
00224             array(
00225                 'page_namespace' => $title->getNamespace(),
00226                 'page_title' => $title->getDBkey(),
00227             ),
00228             __METHOD__,
00229             array(),
00230             array( 'categorylinks' => array( 'INNER JOIN', 'page_id = cl_from' ) )
00231         );
00232 
00233         return TitleArray::newFromResult( $res );
00234     }
00235 }