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