2011-06-29 22:09:51 +00:00
|
|
|
<?php
|
2012-05-09 17:55:56 +00:00
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2021-08-11 12:46:34 +00:00
|
|
|
use MediaWiki\Actions\FileDeleteAction;
|
2019-05-14 17:00:34 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-19 05:03:13 +00:00
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
|
|
|
|
|
2011-06-29 22:09:51 +00:00
|
|
|
/**
|
2022-04-08 15:13:15 +00:00
|
|
|
* Special handling for representing file pages.
|
2011-06-29 22:09:51 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Media
|
|
|
|
|
*/
|
|
|
|
|
class WikiFilePage extends WikiPage {
|
2019-07-01 20:30:14 +00:00
|
|
|
/** @var File|false */
|
2016-01-24 20:27:52 +00:00
|
|
|
protected $mFile = false;
|
2019-07-01 20:30:14 +00:00
|
|
|
/** @var LocalRepo|null */
|
2016-01-24 20:27:52 +00:00
|
|
|
protected $mRepo = null;
|
2016-05-04 17:56:23 +00:00
|
|
|
/** @var bool */
|
2016-01-24 20:27:52 +00:00
|
|
|
protected $mFileLoaded = false;
|
2019-07-01 20:30:14 +00:00
|
|
|
/** @var array|null */
|
2016-01-24 20:27:52 +00:00
|
|
|
protected $mDupes = null;
|
2011-06-29 22:09:51 +00:00
|
|
|
|
2021-10-20 15:51:22 +00:00
|
|
|
/**
|
|
|
|
|
* @param Title $title
|
|
|
|
|
*/
|
2011-09-14 17:51:37 +00:00
|
|
|
public function __construct( $title ) {
|
2011-06-29 22:09:51 +00:00
|
|
|
parent::__construct( $title );
|
|
|
|
|
$this->mDupes = null;
|
|
|
|
|
$this->mRepo = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-10 18:50:10 +00:00
|
|
|
* @param File $file
|
2011-06-29 22:09:51 +00:00
|
|
|
*/
|
2020-05-11 10:46:53 +00:00
|
|
|
public function setFile( File $file ) {
|
2011-06-29 22:09:51 +00:00
|
|
|
$this->mFile = $file;
|
|
|
|
|
$this->mFileLoaded = true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-06-29 22:09:51 +00:00
|
|
|
protected function loadFile() {
|
2019-05-14 17:00:34 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2011-06-29 22:09:51 +00:00
|
|
|
if ( $this->mFileLoaded ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 17:00:34 +00:00
|
|
|
$this->mFile = $services->getRepoGroup()->findFile( $this->mTitle );
|
2011-06-29 22:09:51 +00:00
|
|
|
if ( !$this->mFile ) {
|
2019-05-14 17:00:34 +00:00
|
|
|
$this->mFile = $services->getRepoGroup()->getLocalRepo()
|
2021-03-08 14:12:57 +00:00
|
|
|
->newFile( $this->mTitle );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$this->mFile instanceof File ) {
|
|
|
|
|
throw new RuntimeException( 'Expected to find file. See T250767' );
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|
2021-03-08 14:12:57 +00:00
|
|
|
|
2011-06-29 22:09:51 +00:00
|
|
|
$this->mRepo = $this->mFile->getRepo();
|
2021-03-08 14:12:57 +00:00
|
|
|
$this->mFileLoaded = true;
|
2011-10-14 21:18:38 +00:00
|
|
|
return true;
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
|
|
|
|
* @return mixed|null|Title
|
|
|
|
|
*/
|
2011-06-29 22:09:51 +00:00
|
|
|
public function getRedirectTarget() {
|
|
|
|
|
$this->loadFile();
|
|
|
|
|
if ( $this->mFile->isLocal() ) {
|
|
|
|
|
return parent::getRedirectTarget();
|
|
|
|
|
}
|
|
|
|
|
// Foreign image page
|
|
|
|
|
$from = $this->mFile->getRedirected();
|
|
|
|
|
$to = $this->mFile->getName();
|
|
|
|
|
if ( $from == $to ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-01-01 18:56:08 +00:00
|
|
|
$this->mRedirectTarget = Title::makeTitle( NS_FILE, $to );
|
|
|
|
|
return $this->mRedirectTarget;
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool|mixed|Title
|
|
|
|
|
*/
|
2011-06-29 22:09:51 +00:00
|
|
|
public function followRedirect() {
|
|
|
|
|
$this->loadFile();
|
|
|
|
|
if ( $this->mFile->isLocal() ) {
|
|
|
|
|
return parent::followRedirect();
|
|
|
|
|
}
|
|
|
|
|
$from = $this->mFile->getRedirected();
|
|
|
|
|
$to = $this->mFile->getName();
|
|
|
|
|
if ( $from == $to ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return Title::makeTitle( NS_FILE, $to );
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-03-17 15:13:22 +00:00
|
|
|
public function isRedirect() {
|
2011-06-29 22:09:51 +00:00
|
|
|
$this->loadFile();
|
|
|
|
|
if ( $this->mFile->isLocal() ) {
|
2012-06-08 10:10:40 +00:00
|
|
|
return parent::isRedirect();
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|
2011-10-14 21:18:38 +00:00
|
|
|
|
2011-06-29 22:09:51 +00:00
|
|
|
return (bool)$this->mFile->getRedirected();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-06-29 22:09:51 +00:00
|
|
|
public function isLocal() {
|
|
|
|
|
$this->loadFile();
|
|
|
|
|
return $this->mFile->isLocal();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
2021-08-11 12:46:34 +00:00
|
|
|
* @return File
|
2011-10-26 03:45:13 +00:00
|
|
|
*/
|
2021-08-11 12:46:34 +00:00
|
|
|
public function getFile(): File {
|
2011-06-29 22:09:51 +00:00
|
|
|
$this->loadFile();
|
|
|
|
|
return $this->mFile;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
2020-10-28 10:01:33 +00:00
|
|
|
* @return File[]|null
|
2011-10-26 03:45:13 +00:00
|
|
|
*/
|
2011-06-29 22:09:51 +00:00
|
|
|
public function getDuplicates() {
|
|
|
|
|
$this->loadFile();
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $this->mDupes !== null ) {
|
2011-06-29 22:09:51 +00:00
|
|
|
return $this->mDupes;
|
|
|
|
|
}
|
|
|
|
|
$hash = $this->mFile->getSha1();
|
|
|
|
|
if ( !( $hash ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->mDupes = [];
|
2014-01-01 18:56:08 +00:00
|
|
|
return $this->mDupes;
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|
2020-03-08 21:38:47 +00:00
|
|
|
$dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash );
|
2011-06-29 22:09:51 +00:00
|
|
|
// Remove duplicates with self and non matching file sizes
|
|
|
|
|
$self = $this->mFile->getRepoName() . ':' . $this->mFile->getName();
|
|
|
|
|
$size = $this->mFile->getSize();
|
2011-10-26 03:45:13 +00:00
|
|
|
|
|
|
|
|
/**
|
2019-06-12 19:10:25 +00:00
|
|
|
* @var File $file
|
2011-10-26 03:45:13 +00:00
|
|
|
*/
|
2011-06-29 22:09:51 +00:00
|
|
|
foreach ( $dupes as $index => $file ) {
|
|
|
|
|
$key = $file->getRepoName() . ':' . $file->getName();
|
|
|
|
|
if ( $key == $self ) {
|
|
|
|
|
unset( $dupes[$index] );
|
|
|
|
|
}
|
|
|
|
|
if ( $file->getSize() != $size ) {
|
|
|
|
|
unset( $dupes[$index] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->mDupes = $dupes;
|
|
|
|
|
return $this->mDupes;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 15:12:14 +00:00
|
|
|
/**
|
|
|
|
|
* Override handling of action=purge
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function doPurge() {
|
2011-06-29 22:09:51 +00:00
|
|
|
$this->loadFile();
|
2017-06-29 13:21:38 +00:00
|
|
|
|
2011-06-29 22:09:51 +00:00
|
|
|
if ( $this->mFile->exists() ) {
|
2020-06-01 05:00:39 +00:00
|
|
|
wfDebug( 'ImagePage::doPurge purging ' . $this->mFile->getName() );
|
2019-09-11 22:15:17 +00:00
|
|
|
$job = HTMLCacheUpdateJob::newForBacklinks(
|
|
|
|
|
$this->mTitle,
|
|
|
|
|
'imagelinks',
|
|
|
|
|
[ 'causeAction' => 'file-purge' ]
|
2017-10-30 17:47:30 +00:00
|
|
|
);
|
2022-01-27 20:19:18 +00:00
|
|
|
MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( $job );
|
2011-06-29 22:09:51 +00:00
|
|
|
} else {
|
2014-05-11 15:34:55 +00:00
|
|
|
wfDebug( 'ImagePage::doPurge no image for '
|
2020-06-01 05:00:39 +00:00
|
|
|
. $this->mFile->getName() . "; limiting purge to cache only" );
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|
2017-06-29 13:21:38 +00:00
|
|
|
|
|
|
|
|
// even if the file supposedly doesn't exist, force any cached information
|
|
|
|
|
// to be updated (in case the cached information is wrong)
|
|
|
|
|
|
|
|
|
|
// Purge current version and its thumbnails
|
|
|
|
|
$this->mFile->purgeCache( [ 'forThumbRefresh' => true ] );
|
|
|
|
|
|
|
|
|
|
// Purge the old versions and their thumbnails
|
|
|
|
|
foreach ( $this->mFile->getHistory() as $oldFile ) {
|
|
|
|
|
$oldFile->purgeCache( [ 'forThumbRefresh' => true ] );
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-04 03:36:38 +00:00
|
|
|
if ( $this->mRepo ) {
|
|
|
|
|
// Purge redirect cache
|
|
|
|
|
$this->mRepo->invalidateImageRedirect( $this->mTitle );
|
|
|
|
|
}
|
2017-06-29 13:21:38 +00:00
|
|
|
|
2017-02-01 15:12:14 +00:00
|
|
|
return parent::doPurge();
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|
2013-11-13 12:35:25 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the categories this file is a member of on the wiki where it was uploaded.
|
|
|
|
|
* For local files, this is the same as getCategories().
|
|
|
|
|
* For foreign API files (InstantCommons), this is not supported currently.
|
|
|
|
|
* Results will include hidden categories.
|
|
|
|
|
*
|
|
|
|
|
* @return TitleArray|Title[]
|
|
|
|
|
* @since 1.23
|
|
|
|
|
*/
|
|
|
|
|
public function getForeignCategories() {
|
|
|
|
|
$this->loadFile();
|
|
|
|
|
$title = $this->mTitle;
|
|
|
|
|
$file = $this->mFile;
|
|
|
|
|
|
2014-07-21 12:47:42 +00:00
|
|
|
if ( !$file instanceof LocalFile ) {
|
2021-08-30 18:27:28 +00:00
|
|
|
wfDebug( __METHOD__ . " is not supported for this file" );
|
2016-02-17 09:09:32 +00:00
|
|
|
return TitleArray::newFromResult( new FakeResultWrapper( [] ) );
|
2013-11-13 12:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var LocalRepo $repo */
|
|
|
|
|
$repo = $file->getRepo();
|
2016-11-18 15:42:39 +00:00
|
|
|
$dbr = $repo->getReplicaDB();
|
2013-11-13 12:35:25 +00:00
|
|
|
|
|
|
|
|
$res = $dbr->select(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page', 'categorylinks' ],
|
|
|
|
|
[
|
2013-11-13 12:35:25 +00:00
|
|
|
'page_title' => 'cl_to',
|
|
|
|
|
'page_namespace' => NS_CATEGORY,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2013-11-13 12:35:25 +00:00
|
|
|
'page_namespace' => $title->getNamespace(),
|
|
|
|
|
'page_title' => $title->getDBkey(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2013-11-13 12:35:25 +00:00
|
|
|
__METHOD__,
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2019-03-06 17:17:27 +00:00
|
|
|
[ 'categorylinks' => [ 'JOIN', 'page_id = cl_from' ] ]
|
2013-11-13 12:35:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return TitleArray::newFromResult( $res );
|
|
|
|
|
}
|
2016-09-16 01:22:36 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.28
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getWikiDisplayName() {
|
|
|
|
|
return $this->getFile()->getRepo()->getDisplayName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 1.28
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getSourceURL() {
|
|
|
|
|
return $this->getFile()->getDescriptionUrl();
|
|
|
|
|
}
|
2021-08-11 12:46:34 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function getActionOverrides() {
|
2021-11-10 13:37:36 +00:00
|
|
|
$file = $this->getFile();
|
|
|
|
|
if ( $file->exists() && $file->isLocal() && !$file->getRedirected() ) {
|
|
|
|
|
// Would be an actual file deletion
|
|
|
|
|
return [ 'delete' => FileDeleteAction::class ] + parent::getActionOverrides();
|
|
|
|
|
}
|
|
|
|
|
// It should use the normal article deletion interface
|
|
|
|
|
return parent::getActionOverrides();
|
2021-08-11 12:46:34 +00:00
|
|
|
}
|
2011-06-29 22:09:51 +00:00
|
|
|
}
|