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