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