| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Actions\FileDeleteAction; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Title\Title; |
| 24 |
use MediaWiki\Title\TitleArrayFromResult; |
| 25 |
use Wikimedia\Rdbms\FakeResultWrapper; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class WikiFilePage extends WikiPage { |
| 33 |
|
| 34 |
protected $mFile = false; |
| 35 |
|
| 36 |
protected $mRepo = null; |
| 37 |
|
| 38 |
protected $mFileLoaded = false; |
| 39 |
|
| 40 |
protected $mDupes = null; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function __construct( $title ) { |
| 46 |
parent::__construct( $title ); |
| 47 |
$this->mDupes = null; |
| 48 |
$this->mRepo = null; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function setFile( File $file ) { |
| 55 |
$this->mFile = $file; |
| 56 |
$this->mFileLoaded = true; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected function loadFile() { |
| 63 |
$services = MediaWikiServices::getInstance(); |
| 64 |
if ( $this->mFileLoaded ) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
$this->mFile = $services->getRepoGroup()->findFile( $this->mTitle ); |
| 69 |
if ( !$this->mFile ) { |
| 70 |
$this->mFile = $services->getRepoGroup()->getLocalRepo() |
| 71 |
->newFile( $this->mTitle ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( !$this->mFile instanceof File ) { |
| 75 |
throw new RuntimeException( 'Expected to find file. See T250767' ); |
| 76 |
} |
| 77 |
|
| 78 |
$this->mRepo = $this->mFile->getRepo(); |
| 79 |
$this->mFileLoaded = true; |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function followRedirect() { |
| 87 |
$this->loadFile(); |
| 88 |
if ( $this->mFile->isLocal() ) { |
| 89 |
return parent::followRedirect(); |
| 90 |
} |
| 91 |
$from = $this->mFile->getRedirected(); |
| 92 |
$to = $this->mFile->getName(); |
| 93 |
if ( $from === null || $from === $to ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
return Title::makeTitle( NS_FILE, $to ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function isRedirect() { |
| 103 |
$this->loadFile(); |
| 104 |
if ( $this->mFile->isLocal() ) { |
| 105 |
return parent::isRedirect(); |
| 106 |
} |
| 107 |
|
| 108 |
return $this->mFile->getRedirected() !== null; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function isLocal() { |
| 115 |
$this->loadFile(); |
| 116 |
return $this->mFile->isLocal(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function getFile(): File { |
| 123 |
$this->loadFile(); |
| 124 |
return $this->mFile; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function getDuplicates() { |
| 131 |
$this->loadFile(); |
| 132 |
if ( $this->mDupes !== null ) { |
| 133 |
return $this->mDupes; |
| 134 |
} |
| 135 |
$hash = $this->mFile->getSha1(); |
| 136 |
if ( !( $hash ) ) { |
| 137 |
$this->mDupes = []; |
| 138 |
return $this->mDupes; |
| 139 |
} |
| 140 |
$dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash ); |
| 141 |
|
| 142 |
$self = $this->mFile->getRepoName() . ':' . $this->mFile->getName(); |
| 143 |
$size = $this->mFile->getSize(); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
foreach ( $dupes as $index => $file ) { |
| 149 |
$key = $file->getRepoName() . ':' . $file->getName(); |
| 150 |
if ( $key === $self || $file->getSize() != $size ) { |
| 151 |
unset( $dupes[$index] ); |
| 152 |
} |
| 153 |
} |
| 154 |
$this->mDupes = $dupes; |
| 155 |
return $this->mDupes; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function doPurge() { |
| 163 |
$this->loadFile(); |
| 164 |
|
| 165 |
if ( $this->mFile->exists() ) { |
| 166 |
wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() ); |
| 167 |
$job = HTMLCacheUpdateJob::newForBacklinks( |
| 168 |
$this->mTitle, |
| 169 |
'imagelinks', |
| 170 |
[ 'causeAction' => 'file-purge' ] |
| 171 |
); |
| 172 |
MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $job ); |
| 173 |
} else { |
| 174 |
wfDebug( 'ImagePage::doPurge no image for ' |
| 175 |
. $this->mFile->getName() . "; limiting purge to cache only" ); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
$this->mFile->purgeCache( [ 'forThumbRefresh' => true ] ); |
| 183 |
|
| 184 |
|
| 185 |
foreach ( $this->mFile->getHistory() as $oldFile ) { |
| 186 |
$oldFile->purgeCache( [ 'forThumbRefresh' => true ] ); |
| 187 |
} |
| 188 |
|
| 189 |
if ( $this->mRepo ) { |
| 190 |
|
| 191 |
$this->mRepo->invalidateImageRedirect( $this->mTitle ); |
| 192 |
} |
| 193 |
|
| 194 |
return parent::doPurge(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function getForeignCategories() { |
| 207 |
$this->loadFile(); |
| 208 |
$title = $this->mTitle; |
| 209 |
$file = $this->mFile; |
| 210 |
$titleFactory = MediaWikiServices::getInstance()->getTitleFactory(); |
| 211 |
|
| 212 |
if ( !$file instanceof LocalFile ) { |
| 213 |
wfDebug( __METHOD__ . " is not supported for this file" ); |
| 214 |
return $titleFactory->newTitleArrayFromResult( new FakeResultWrapper( [] ) ); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
$repo = $file->getRepo(); |
| 219 |
$dbr = $repo->getReplicaDB(); |
| 220 |
|
| 221 |
$res = $dbr->newSelectQueryBuilder() |
| 222 |
->select( [ 'page_title' => 'cl_to', 'page_namespace' => (string)NS_CATEGORY ] ) |
| 223 |
->from( 'page' ) |
| 224 |
->join( 'categorylinks', null, 'page_id = cl_from' ) |
| 225 |
->where( [ 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey(), ] ) |
| 226 |
->caller( __METHOD__ )->fetchResultSet(); |
| 227 |
|
| 228 |
return $titleFactory->newTitleArrayFromResult( $res ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public function getWikiDisplayName() { |
| 236 |
return $this->getFile()->getRepo()->getDisplayName(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function getSourceURL() { |
| 244 |
return $this->getFile()->getDescriptionUrl(); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function getActionOverrides() { |
| 251 |
$file = $this->getFile(); |
| 252 |
if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) { |
| 253 |
|
| 254 |
return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides(); |
| 255 |
} |
| 256 |
|
| 257 |
return parent::getActionOverrides(); |
| 258 |
} |
| 259 |
} |