Remove unused private "filterPage" function from specials/SpecialExport.php
[lhc/web/wiklou.git] / includes / upload / UploadBase.php
index 3f8b1d2..105a421 100644 (file)
@@ -1,18 +1,18 @@
 <?php
 /**
- * @file
+ * @file 
  * @ingroup upload
- *
+ * 
  * UploadBase and subclasses are the backend of MediaWiki's file uploads.
  * The frontends are formed by ApiUpload and SpecialUpload.
- *
+ * 
  * See also includes/docs/upload.txt
- *
+ * 
  * @author Brion Vibber
  * @author Bryan Tong Minh
  * @author Michael Dale
  */
-
 abstract class UploadBase {
        protected $mTempPath;
        protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
@@ -22,20 +22,15 @@ abstract class UploadBase {
 
        const SUCCESS = 0;
        const OK = 0;
-       const BEFORE_PROCESSING = 1;
-       const LARGE_FILE_SERVER = 2;
        const EMPTY_FILE = 3;
        const MIN_LENGTH_PARTNAME = 4;
        const ILLEGAL_FILENAME = 5;
-       const PROTECTED_PAGE = 6;
        const OVERWRITE_EXISTING_FILE = 7;
        const FILETYPE_MISSING = 8;
        const FILETYPE_BADTYPE = 9;
        const VERIFICATION_ERROR = 10;
        const UPLOAD_VERIFICATION_ERROR = 11;
-       const UPLOAD_WARNING = 12;
-       const INTERNAL_ERROR = 13;
-       const MIN_LENGHT_PARTNAME = 14;
+       const HOOK_ABORTED = 11;
 
        const SESSION_VERSION = 2;
 
@@ -80,10 +75,16 @@ abstract class UploadBase {
 
                // Get the upload class
                $type = ucfirst( $type );
-               $className = 'UploadFrom' . $type;
-               wfDebug( __METHOD__ . ": class name: $className\n" );
-               if( !in_array( $type, self::$uploadHandlers ) )
-                       return null;
+               
+               // Give hooks the chance to handle this request
+               $className = null;
+               wfRunHooks( 'UploadCreateFromRequest', array( $type, &$className ) );
+               if ( is_null( $className ) ) {
+                       $className = 'UploadFrom' . $type;
+                       wfDebug( __METHOD__ . ": class name: $className\n" );
+                       if( !in_array( $type, self::$uploadHandlers ) )
+                               return null;
+               }
 
                // Check whether this upload class is enabled
                if( !call_user_func( array( $className, 'isEnabled' ) ) )
@@ -117,7 +118,7 @@ abstract class UploadBase {
                $this->mFileSize = $fileSize;
                $this->mRemoveTempFile = $removeTempFile;
        }
-
+       
        /**
         * Initialize from a WebRequest. Override this in a subclass.
         */
@@ -136,12 +137,12 @@ abstract class UploadBase {
        public function isEmptyFile(){
                return empty( $this->mFileSize );
        }
-
-       /*
-        * getRealPath
-        * @param string $srcPath the source path
-        * @returns the real path if it was a virtual url
-        */
+       
+       /**
+     * getRealPath
+     * @param string $srcPath the source path
+     * @returns the real path if it was a virtual url
+     */
        function getRealPath( $srcPath ){
                $repo = RepoGroup::singleton()->getLocalRepo();
                if ( $repo->isVirtualUrl( $srcPath ) ) {
@@ -160,7 +161,21 @@ abstract class UploadBase {
                 */
                if( $this->isEmptyFile() )
                        return array( 'status' => self::EMPTY_FILE );
+               
+               /**
+                * Look at the contents of the file; if we can recognize the
+                * type but it's corrupt or data of the wrong type, we should
+                * probably not accept it.
+                */
+               $verification = $this->verifyFile();
+               if( $verification !== true ) {
+                       if( !is_array( $verification ) )
+                               $verification = array( $verification );
+                       return array( 'status' => self::VERIFICATION_ERROR,
+                                       'details' => $verification );
 
+               }
+               
                $nt = $this->getTitle();
                if( is_null( $nt ) ) {
                        $result = array( 'status' => $this->mTitleError );
@@ -179,25 +194,11 @@ abstract class UploadBase {
                if( $overwrite !== true )
                        return array( 'status' => self::OVERWRITE_EXISTING_FILE, 'overwrite' => $overwrite );
 
-               /**
-                * Look at the contents of the file; if we can recognize the
-                * type but it's corrupt or data of the wrong type, we should
-                * probably not accept it.
-                */
-               $verification = $this->verifyFile();
-
-               if( $verification !== true ) {
-                       if( !is_array( $verification ) )
-                               $verification = array( $verification );
-                       return array( 'status' => self::VERIFICATION_ERROR,
-                                       'details' => $verification );
-               }
-
                $error = '';
                if( !wfRunHooks( 'UploadVerification',
                                array( $this->mDestName, $this->mTempPath, &$error ) ) ) {
                        // This status needs another name...
-                       return array( 'status' => self::UPLOAD_VERIFICATION_ERROR, 'error' => $error );
+                       return array( 'status' => self::HOOK_ABORTED, 'error' => $error );
                }
 
                return array( 'status' => self::OK );
@@ -221,7 +222,7 @@ abstract class UploadBase {
 
                #check mime type, if desired
                global $wgVerifyMimeType;
-               if ( $wgVerifyMimeType ) {
+               if ( $wgVerifyMimeType ) {              
                        global $wgMimeTypeBlacklist;
                        if ( $this->checkFileExtension( $mime, $wgMimeTypeBlacklist ) )
                                return array( 'filetype-badmime', $mime );
@@ -262,7 +263,7 @@ abstract class UploadBase {
 
        /**
         * Check whether the user can edit, upload and create the image.
-        *
+        * 
         * @param User $user the user to verify the permissions against
         * @return mixed An array as returned by getUserPermissionsErrors or true
         *               in case the user has proper permissions.
@@ -288,7 +289,7 @@ abstract class UploadBase {
 
        /**
         * Check for non fatal problems with the file
-        *
+        * 
         * @return array Array of warnings
         */
        public function checkWarnings() {
@@ -305,6 +306,7 @@ abstract class UploadBase {
                 */
                $comparableName = str_replace( ' ', '_', $this->mDesiredDestName );
                $comparableName = Title::capitalize( $comparableName, NS_FILE );
+
                if( $this->mDesiredDestName != $filename && $comparableName != $filename )
                        $warnings['badfilename'] = $filename;
 
@@ -348,9 +350,9 @@ abstract class UploadBase {
        }
 
        /**
-        * Really perform the upload. Stores the file in the local repo, watches
+        * Really perform the upload. Stores the file in the local repo, watches 
         * if necessary and runs the UploadComplete hook.
-        *
+        * 
         * @return mixed Status indicating the whether the upload succeeded.
         */
        public function performUpload( $comment, $pageText, $watch, $user ) {
@@ -358,13 +360,8 @@ abstract class UploadBase {
                $status = $this->getLocalFile()->upload( $this->mTempPath, $comment, $pageText,
                        File::DELETE_SOURCE, $this->mFileProps, false, $user );
 
-               if( $status->isGood() && $watch ){
-                       //make sure the watch commit happens inline
-                       $dbw = wfGetDB(DB_MASTER);
-                       $dbw->begin();
-                               $user->addWatch( $this->getLocalFile()->getTitle() );
-                       $dbw->commit();
-               }
+               if( $status->isGood() && $watch )
+                       $user->addWatch( $this->getLocalFile()->getTitle() );
 
                if( $status->isGood() )
                        wfRunHooks( 'UploadComplete', array( &$this ) );
@@ -375,7 +372,7 @@ abstract class UploadBase {
        /**
         * Returns the title of the file to be uploaded. Sets mTitleError in case
         * the name was illegal.
-        *
+        * 
         * @return Title The title of the file or null in case the name was illegal
         */
        public function getTitle() {
@@ -444,7 +441,7 @@ abstract class UploadBase {
        }
 
        /**
-        * Return the local file and initializes if necessary.
+        * Return the local file and initializes if necessary. 
         */
        public function getLocalFile() {
                if( is_null( $this->mLocalFile ) ) {
@@ -472,19 +469,6 @@ abstract class UploadBase {
                return $status;
        }
 
-       /**
-        * Append a file to a stashed file.
-        *
-        * @param string $srcPath Path to file to append from
-        * @param string $toAppendPath Path to file to append to
-        * @return Status Status
-        */
-       public function appendToUploadFile( $srcPath, $toAppendPath ){
-               $repo = RepoGroup::singleton()->getLocalRepo();
-               $status = $repo->append( $srcPath, $toAppendPath );
-               return $status;
-       }
-
        /**
         * Stash a file in a temporary directory for later processing,
         * and save the necessary descriptive info into the session.
@@ -507,7 +491,7 @@ abstract class UploadBase {
                        'mFileSize'       => $this->mFileSize,
                        'mFileProps'      => $this->mFileProps,
                        'version'         => self::SESSION_VERSION,
-               );
+               );              
                return $key;
        }
 
@@ -520,15 +504,6 @@ abstract class UploadBase {
                return $key;
        }
 
-       /**
-        * Remove a temporarily kept file stashed by saveTempUploadedFile().
-        * @return success
-        */
-       public function unsaveUploadedFile() {
-               $repo = RepoGroup::singleton()->getLocalRepo();
-               $success = $repo->freeTemp( $this->mTempPath );
-               return $success;
-       }
 
        /**
         * If we've modified the upload file we need to manually remove it
@@ -612,7 +587,7 @@ abstract class UploadBase {
 
                $match = $magic->isMatchingExtension( $extension, $mime );
 
-               if ( $match === NULL ) {
+               if ( $match === null ) {
                        wfDebug( __METHOD__ . ": no file extension known for mime type $mime, passing file\n" );
                        return true;
                } elseif( $match === true ) {
@@ -662,7 +637,7 @@ abstract class UploadBase {
                elseif( substr( $chunk, 0, 2 ) == "\xff\xfe" )
                        $enc = "UTF-16LE";
                else
-                       $enc = NULL;
+                       $enc = null;
 
                if( $enc )
                        $chunk = iconv( $enc, "ASCII//IGNORE", $chunk );
@@ -785,7 +760,7 @@ abstract class UploadBase {
 
                if ( !$wgAntivirus ) {
                        wfDebug( __METHOD__ . ": virus scanner disabled\n" );
-                       return NULL;
+                       return null;
                }
 
                if ( !$wgAntivirusSetup[$wgAntivirus] ) {
@@ -816,9 +791,7 @@ abstract class UploadBase {
                #NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
                #      that does not seem to be worth the pain.
                #      Ask me (Duesentrieb) about it if it's ever needed.
-               $output = array();
-               $output = wfShellExec("$command 2>&1", $exitCode);
-
+               $output = wfShellExec( "$command 2>&1", $exitCode );
 
                # map exit code to AV_xxx constants.
                $mappedCode = $exitCode;
@@ -830,7 +803,6 @@ abstract class UploadBase {
                        }
                }
 
-
                if ( $mappedCode === AV_SCAN_FAILED ) {
                        # scan failed (code was mapped to false by $exitCodeMap)
                        wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode).\n" );
@@ -838,18 +810,17 @@ abstract class UploadBase {
                        if ( $wgAntivirusRequired ) {
                                return wfMsg( 'virus-scanfailed', array( $exitCode ) );
                        } else {
-                               return NULL;
+                               return null;
                        }
                } else if ( $mappedCode === AV_SCAN_ABORTED ) {
                        # scan failed because filetype is unknown (probably imune)
                        wfDebug( __METHOD__ . ": unsupported file type $file (code $exitCode).\n" );
-                       return NULL;
+                       return null;
                } else if ( $mappedCode === AV_NO_VIRUS ) {
                        # no virus found
                        wfDebug( __METHOD__ . ": file passed virus scan.\n" );
                        return false;
                } else {
-                       $output = join( "\n", $output );
                        $output = trim( $output );
 
                        if ( !$output ) {
@@ -912,9 +883,9 @@ abstract class UploadBase {
                                return true;
                }
 
-               /* Check shared conflicts: if the local file does not exist, but
-                * wfFindFile finds a file, it exists in a shared repository.
-                */
+               /* Check shared conflicts: if the local file does not exist, but 
+                * wfFindFile finds a file, it exists in a shared repository. 
+                */ 
                $file = wfFindFile( $this->getTitle() );
                if ( $file && !$wgUser->isAllowed( 'reupload-shared' ) )
                        return 'fileexists-shared-forbidden';
@@ -944,13 +915,13 @@ abstract class UploadBase {
 
        /**
         * Helper function that does various existence checks for a file.
-        * The following checks are performed:
+        * The following checks are performed: 
         * - The file exists
         * - Article with the same name as the file exists
         * - File exists with normalized extension
         * - The file looks like a thumbnail and the original exists
-        *
-        * @param File $file The file to check
+        * 
+        * @param File $file The file to check 
         * @return mixed False if the file does not exists, else an array
         */
        public static function getExistsWarning( $file ) {
@@ -959,10 +930,10 @@ abstract class UploadBase {
 
                if( $file->getTitle()->getArticleID() )
                        return array( 'warning' => 'page-exists', 'file' => $file );
-
+               
                if ( $file->wasDeleted() && !$file->exists() )
-                       return array( 'warning' => 'was-deleted', 'file' => $file );
-
+                       return array( 'warning' => 'was-deleted', 'file' => $file );            
+                       
                if( strpos( $file->getName(), '.' ) == false ) {
                        $partname = $file->getName();
                        $extension = '';
@@ -996,13 +967,13 @@ abstract class UploadBase {
                                // File does not exist, but we just don't like the name
                                return array( 'warning' => 'thumb-name', 'file' => $file, 'thumbFile' => $file_thb );
                }
-
+               
 
                foreach( self::getFilenamePrefixBlacklist() as $prefix ) {
                        if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix )
                                return array( 'warning' => 'bad-prefix', 'file' => $file, 'prefix' => $prefix );
                }
-
+               
 
 
                return false;