Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / includes / upload / UploadBase.php
index 2ee40a8..ddb4bba 100644 (file)
@@ -247,7 +247,7 @@ abstract class UploadBase {
 
        /**
         * @param string $tempPath File system path to temporary file containing the upload
-        * @param integer $fileSize
+        * @param int $fileSize
         */
        protected function setTempFile( $tempPath, $fileSize = null ) {
                $this->mTempPath = $tempPath;
@@ -420,7 +420,7 @@ abstract class UploadBase {
                        $chunk = fread( $fp, 256 );
                        fclose( $fp );
 
-                       $magic = MimeMagic::singleton();
+                       $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
                        $extMime = $magic->guessTypesForExtension( $this->mFinalExtension );
                        $ieTypes = $magic->getIEMimeTypes( $this->mTempPath, $chunk, $extMime );
                        foreach ( $ieTypes as $ieType ) {
@@ -446,7 +446,7 @@ abstract class UploadBase {
                        return $status;
                }
 
-               $mwProps = new MWFileProps( MimeMagic::singleton() );
+               $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
                $this->mFileProps = $mwProps->getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
                $mime = $this->mFileProps['mime'];
 
@@ -505,7 +505,7 @@ abstract class UploadBase {
                # getTitle() sets some internal parameters like $this->mFinalExtension
                $this->getTitle();
 
-               $mwProps = new MWFileProps( MimeMagic::singleton() );
+               $mwProps = new MWFileProps( MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer() );
                $this->mFileProps = $mwProps->getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
 
                # check MIME type, if desired
@@ -674,7 +674,10 @@ abstract class UploadBase {
                        $warnings['was-deleted'] = $filename;
                }
 
-               $dupes = $this->checkAgainstExistingDupes( $hash );
+               // If a file with the same name exists locally then the local file has already been tested
+               // for duplication of content
+               $ignoreLocalDupes = isset( $warnings[ 'exists '] );
+               $dupes = $this->checkAgainstExistingDupes( $hash, $ignoreLocalDupes );
                if ( $dupes ) {
                        $warnings['duplicate'] = $dupes;
                }
@@ -789,15 +792,19 @@ abstract class UploadBase {
 
        /**
         * @param string $hash sha1 hash of the file to check
+        * @param bool $ignoreLocalDupes True to ignore local duplicates
         *
         * @return File[] Duplicate files, if found.
         */
-       private function checkAgainstExistingDupes( $hash ) {
+       private function checkAgainstExistingDupes( $hash, $ignoreLocalDupes ) {
                $dupes = RepoGroup::singleton()->findBySha1( $hash );
                $title = $this->getTitle();
-               // Remove all matches against self
                foreach ( $dupes as $key => $dupe ) {
-                       if ( $title->equals( $dupe->getTitle() ) ) {
+                       if (
+                               ( $dupe instanceof LocalFile ) &&
+                               $ignoreLocalDupes &&
+                               $title->equals( $dupe->getTitle() )
+                       ) {
                                unset( $dupes[$key] );
                        }
                }
@@ -950,7 +957,7 @@ abstract class UploadBase {
                        $this->mFinalExtension = '';
 
                        # No extension, try guessing one
-                       $magic = MimeMagic::singleton();
+                       $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
                        $mime = $magic->guessMimeType( $this->mTempPath );
                        if ( $mime !== 'unknown/unknown' ) {
                                # Get a space separated list of extensions
@@ -1207,7 +1214,7 @@ abstract class UploadBase {
         * @return bool
         */
        public static function verifyExtension( $mime, $extension ) {
-               $magic = MimeMagic::singleton();
+               $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
 
                if ( !$mime || $mime == 'unknown' || $mime == 'unknown/unknown' ) {
                        if ( !$magic->isRecognizableExtension( $extension ) ) {
@@ -1656,9 +1663,8 @@ abstract class UploadBase {
                        # image/svg, text/xml, application/xml, and text/html, which can contain scripts
                        if ( $stripped == 'href' && strncasecmp( 'data:', $value, 5 ) === 0 ) {
                                // rfc2397 parameters. This is only slightly slower than (;[\w;]+)*.
-                               // @codingStandardsIgnoreStart Generic.Files.LineLength
+                               // phpcs:ignore Generic.Files.LineLength
                                $parameters = '(?>;[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+=(?>[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+|"(?>[\0-\x0c\x0e-\x21\x23-\x5b\x5d-\x7f]+|\\\\[\0-\x7f])*"))*(?:;base64)?';
-                               // @codingStandardsIgnoreEnd
 
                                if ( !preg_match( "!^data:\s*image/(gif|jpeg|jpg|png)$parameters,!i", $value ) ) {
                                        wfDebug( __METHOD__ . ": Found href to unwhitelisted data: uri "