From c5c8b3145015f24bfcb21dca9e581e0c7df2d9bc Mon Sep 17 00:00:00 2001 From: btongminh Date: Wed, 6 Feb 2013 21:15:07 +0100 Subject: [PATCH 1/1] (bug 40326) Check if files exist with a different extension during uploading Added FileRepo::findFilesByPrefix( $prefix ), which does a img_name LIKE '$prefix%' search and use this in UploadBase::getExistsWarning(). Change-Id: I21eddc5d08ca3c23b342ebc1c6947e36c4201a45 --- RELEASE-NOTES-1.21 | 2 ++ includes/filerepo/FileRepo.php | 12 ++++++++++++ includes/filerepo/LocalRepo.php | 28 ++++++++++++++++++++++++++++ includes/upload/UploadBase.php | 11 +++++++++++ 4 files changed, 53 insertions(+) diff --git a/RELEASE-NOTES-1.21 b/RELEASE-NOTES-1.21 index 4f38f3a6b5..80c2c885af 100644 --- a/RELEASE-NOTES-1.21 +++ b/RELEASE-NOTES-1.21 @@ -189,6 +189,8 @@ production. "password mismatch" error. * (bug 44599) On Special:Version, HEADs for submodule checkouts (e.g. for extensions) performed using Git 1.7.8+ should now appear. +* (bug 42184) $wgUploadSizeWarning missing second variable +* (bug 40326) Check if files exist with a different extension during uploading === API changes in 1.21 === * prop=revisions can now report the contentmodel and contentformat. diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 05e71d4ecf..6dbe2458d4 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -490,6 +490,18 @@ class FileRepo { return $result; } + /** + * Return an array of files where the name starts with $prefix. + * + * STUB + * @param string $prefix The prefix to search for + * @param int $limit The maximum amount of files to return + * @return array + */ + public function findFilesByPrefix( $prefix, $limit ) { + return array(); + } + /** * Get the public root URL of the repository * diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index 0fbaeef8a0..229f8bf668 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -281,6 +281,34 @@ class LocalRepo extends FileRepo { return $result; } + /** + * Return an array of files where the name starts with $prefix. + * + * @param string $prefix The prefix to search for + * @param int $limit The maximum amount of files to return + * @return array + */ + public function findFilesByPrefix( $prefix, $limit ) { + $selectOptions = array( 'ORDER BY' => 'img_name', 'LIMIT' => intval( $limit ) ); + + // Query database + $dbr = $this->getSlaveDB(); + $res = $dbr->select( + 'image', + LocalFile::selectFields(), + 'img_name ' . $dbr->buildLike( $prefix, $dbr->anyString() ), + __METHOD__, + $selectOptions + ); + + // Build file objects + $files = array(); + foreach ( $res as $row ) { + $files[] = $this->newFileFromRow( $row ); + } + return $files; + } + /** * Get a connection to the slave DB * @return DatabaseBase diff --git a/includes/upload/UploadBase.php b/includes/upload/UploadBase.php index 49713fc973..9d756f8cb2 100644 --- a/includes/upload/UploadBase.php +++ b/includes/upload/UploadBase.php @@ -1377,6 +1377,17 @@ abstract class UploadBase { } } + // Check for files with the same name but a different extension + $similarFiles = RepoGroup::singleton()->getLocalRepo()->findFilesByPrefix( + "{$partname}.", 1 ); + if ( count( $similarFiles ) ) { + return array( + 'warning' => 'exists-normalized', + 'file' => $file, + 'normalizedFile' => $similarFiles[0], + ); + } + if ( self::isThumbName( $file->getName() ) ) { # Check for filenames like 50px- or 180px-, these are mostly thumbnails $nt_thb = Title::newFromText( substr( $partname, strpos( $partname, '-' ) + 1 ) . '.' . $extension, NS_FILE ); -- 2.20.1