* Split off WikiPage class from Article, WikiFilePage class from ImagePage, and WikiC...
[lhc/web/wiklou.git] / includes / WikiFilePage.php
1 <?php
2 /**
3 * Special handling for file pages
4 *
5 * @ingroup Media
6 */
7 class WikiFilePage extends WikiPage {
8 protected $mFile = false; // !< File object
9 protected $mRepo = null; // !<
10 protected $mFileLoaded = false; // !<
11 protected $mDupes = null; // !<
12
13 function __construct( $title ) {
14 parent::__construct( $title );
15 $this->mDupes = null;
16 $this->mRepo = null;
17 }
18
19 /**
20 * @param $file File:
21 * @return void
22 */
23 public function setFile( $file ) {
24 $this->mFile = $file;
25 $this->mFileLoaded = true;
26 }
27
28 protected function loadFile() {
29 if ( $this->mFileLoaded ) {
30 return true;
31 }
32 $this->mFileLoaded = true;
33
34 $this->mFile = false;
35 if ( !$this->mFile ) {
36 $this->mFile = wfFindFile( $this->mTitle );
37 if ( !$this->mFile ) {
38 $this->mFile = wfLocalFile( $this->mTitle ); // always a File
39 }
40 }
41 $this->mRepo = $this->mFile->getRepo();
42 }
43
44 public function getRedirectTarget() {
45 $this->loadFile();
46 if ( $this->mFile->isLocal() ) {
47 return parent::getRedirectTarget();
48 }
49 // Foreign image page
50 $from = $this->mFile->getRedirected();
51 $to = $this->mFile->getName();
52 if ( $from == $to ) {
53 return null;
54 }
55 return $this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
56 }
57
58 public function followRedirect() {
59 $this->loadFile();
60 if ( $this->mFile->isLocal() ) {
61 return parent::followRedirect();
62 }
63 $from = $this->mFile->getRedirected();
64 $to = $this->mFile->getName();
65 if ( $from == $to ) {
66 return false;
67 }
68 return Title::makeTitle( NS_FILE, $to );
69 }
70
71 public function isRedirect( $text = false ) {
72 $this->loadFile();
73 if ( $this->mFile->isLocal() ) {
74 return parent::isRedirect( $text );
75 }
76
77 return (bool)$this->mFile->getRedirected();
78 }
79
80 public function isLocal() {
81 $this->loadFile();
82 return $this->mFile->isLocal();
83 }
84
85 public function getFile() {
86 $this->loadFile();
87 return $this->mFile;
88 }
89
90 public function getDuplicates() {
91 $this->loadFile();
92 if ( !is_null( $this->mDupes ) ) {
93 return $this->mDupes;
94 }
95 $hash = $this->mFile->getSha1();
96 if ( !( $hash ) ) {
97 return $this->mDupes = array();
98 }
99 $dupes = RepoGroup::singleton()->findBySha1( $hash );
100 // Remove duplicates with self and non matching file sizes
101 $self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
102 $size = $this->mFile->getSize();
103 foreach ( $dupes as $index => $file ) {
104 $key = $file->getRepoName() . ':' . $file->getName();
105 if ( $key == $self ) {
106 unset( $dupes[$index] );
107 }
108 if ( $file->getSize() != $size ) {
109 unset( $dupes[$index] );
110 }
111 }
112 $this->mDupes = $dupes;
113 return $this->mDupes;
114 }
115
116 /**
117 * Override handling of action=purge
118 */
119 public function doPurge() {
120 $this->loadFile();
121 if ( $this->mFile->exists() ) {
122 wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() . "\n" );
123 $update = new HTMLCacheUpdate( $this->mTitle, 'imagelinks' );
124 $update->doUpdate();
125 $this->mFile->upgradeRow();
126 $this->mFile->purgeCache();
127 } else {
128 wfDebug( 'ImagePage::doPurge no image for ' . $this->mFile->getName() . "; limiting purge to cache only\n" );
129 // even if the file supposedly doesn't exist, force any cached information
130 // to be updated (in case the cached information is wrong)
131 $this->mFile->purgeCache();
132 }
133 parent::doPurge();
134 }
135 }