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