MediaWiki  REL1_19
ApiTestCaseUpload.php
Go to the documentation of this file.
00001 <?php
00002 
00007 abstract class ApiTestCaseUpload extends ApiTestCase {
00011         public function setUp() {
00012                 global $wgEnableUploads, $wgEnableAPI;
00013                 parent::setUp();
00014 
00015                 $wgEnableUploads = true;
00016                 $wgEnableAPI = true;
00017                 wfSetupSession();
00018 
00019                 $this->clearFakeUploads();
00020         }
00021 
00022         public function tearDown() {
00023                 $this->clearTempUpload();
00024         }
00025 
00030         public function deleteFileByTitle( $title ) {
00031                 if ( $title->exists() ) {
00032                         $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
00033                         $noOldArchive = ""; // yes this really needs to be set this way
00034                         $comment = "removing for test";
00035                         $restrictDeletedVersions = false;
00036                         $status = FileDeleteForm::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
00037                         if ( !$status->isGood() ) {
00038                                 return false;
00039                         }
00040                         $page = WikiPage::factory( $title );
00041                         $page->doDeleteArticle( "removing for test" );
00042 
00043                         // see if it now doesn't exist; reload
00044                         $title = Title::newFromText( $title->getText(), NS_FILE );
00045                 }
00046                 return ! ( $title && $title instanceof Title && $title->exists() );
00047         }
00048 
00053         public function deleteFileByFileName( $fileName ) {
00054                 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
00055         }
00056 
00057 
00062         public function deleteFileByContent( $filePath ) {
00063                 $hash = FSFile::getSha1Base36FromPath( $filePath );
00064                 $dupes = RepoGroup::singleton()->findBySha1( $hash );
00065                 $success = true;
00066                 foreach ( $dupes as $dupe ) {
00067                         $success &= $this->deleteFileByTitle( $dupe->getTitle() );
00068                 }
00069                 return $success;
00070         }
00071 
00080         function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
00081                 $tmpName = tempnam( wfTempDir(), "" );
00082                 if ( !file_exists( $filePath ) ) {
00083                         throw new Exception( "$filePath doesn't exist!" );
00084                 };
00085 
00086                 if ( !copy( $filePath, $tmpName ) ) {
00087                         throw new Exception( "couldn't copy $filePath to $tmpName" );
00088                 }
00089 
00090                 clearstatcache();
00091                 $size = filesize( $tmpName );
00092                 if ( $size === false ) {
00093                         throw new Exception( "couldn't stat $tmpName" );
00094                 }
00095 
00096                 $_FILES[ $fieldName ] = array(
00097                         'name'          => $fileName,
00098                         'type'          => $type,
00099                         'tmp_name'      => $tmpName,
00100                         'size'          => $size,
00101                         'error'         => null
00102                 );
00103 
00104                 return true;
00105 
00106         }
00107         function fakeUploadChunk(  $fieldName, $fileName, $type, & $chunkData ){
00108                 $tmpName = tempnam( wfTempDir(), "" );
00109                 // copy the chunk data to temp location: 
00110                 if ( !file_put_contents( $tmpName, $chunkData ) ) {
00111                         throw new Exception( "couldn't copy chunk data to $tmpName" );
00112                 }
00113                 
00114                 clearstatcache();
00115                 $size = filesize( $tmpName );
00116                 if ( $size === false ) {
00117                         throw new Exception( "couldn't stat $tmpName" );
00118                 }
00119                 
00120                 $_FILES[ $fieldName ] = array(
00121                         'name'          => $fileName,
00122                         'type'          => $type,
00123                         'tmp_name'      => $tmpName,
00124                         'size'          => $size,
00125                         'error'         => null
00126                 );
00127         }
00128 
00129         function clearTempUpload() {
00130                 if( isset( $_FILES['file']['tmp_name'] ) ) {
00131                         $tmp = $_FILES['file']['tmp_name'];
00132                         if( file_exists( $tmp ) ) {
00133                                 unlink( $tmp );
00134                         }
00135                 }
00136         }
00137 
00141         function clearFakeUploads() {
00142                 $_FILES = array();
00143         }
00144 
00145 
00146 
00147 
00148 }