MediaWiki
REL1_23
|
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 00037 public function deleteFileByTitle( $title ) { 00038 if ( $title->exists() ) { 00039 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) ); 00040 $noOldArchive = ""; // yes this really needs to be set this way 00041 $comment = "removing for test"; 00042 $restrictDeletedVersions = false; 00043 $status = FileDeleteForm::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions ); 00044 if ( !$status->isGood() ) { 00045 return false; 00046 } 00047 $page = WikiPage::factory( $title ); 00048 $page->doDeleteArticle( "removing for test" ); 00049 00050 // see if it now doesn't exist; reload 00051 $title = Title::newFromText( $title->getText(), NS_FILE ); 00052 } 00053 00054 return !( $title && $title instanceof Title && $title->exists() ); 00055 } 00056 00064 public function deleteFileByFileName( $fileName ) { 00065 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) ); 00066 } 00067 00075 public function deleteFileByContent( $filePath ) { 00076 $hash = FSFile::getSha1Base36FromPath( $filePath ); 00077 $dupes = RepoGroup::singleton()->findBySha1( $hash ); 00078 $success = true; 00079 foreach ( $dupes as $dupe ) { 00080 $success &= $this->deleteFileByTitle( $dupe->getTitle() ); 00081 } 00082 00083 return $success; 00084 } 00085 00098 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) { 00099 $tmpName = tempnam( wfTempDir(), "" ); 00100 if ( !file_exists( $filePath ) ) { 00101 throw new Exception( "$filePath doesn't exist!" ); 00102 } 00103 00104 if ( !copy( $filePath, $tmpName ) ) { 00105 throw new Exception( "couldn't copy $filePath to $tmpName" ); 00106 } 00107 00108 clearstatcache(); 00109 $size = filesize( $tmpName ); 00110 if ( $size === false ) { 00111 throw new Exception( "couldn't stat $tmpName" ); 00112 } 00113 00114 $_FILES[$fieldName] = array( 00115 'name' => $fileName, 00116 'type' => $type, 00117 'tmp_name' => $tmpName, 00118 'size' => $size, 00119 'error' => null 00120 ); 00121 00122 return true; 00123 } 00124 00125 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) { 00126 $tmpName = tempnam( wfTempDir(), "" ); 00127 // copy the chunk data to temp location: 00128 if ( !file_put_contents( $tmpName, $chunkData ) ) { 00129 throw new Exception( "couldn't copy chunk data to $tmpName" ); 00130 } 00131 00132 clearstatcache(); 00133 $size = filesize( $tmpName ); 00134 if ( $size === false ) { 00135 throw new Exception( "couldn't stat $tmpName" ); 00136 } 00137 00138 $_FILES[$fieldName] = array( 00139 'name' => $fileName, 00140 'type' => $type, 00141 'tmp_name' => $tmpName, 00142 'size' => $size, 00143 'error' => null 00144 ); 00145 } 00146 00147 function clearTempUpload() { 00148 if ( isset( $_FILES['file']['tmp_name'] ) ) { 00149 $tmp = $_FILES['file']['tmp_name']; 00150 if ( file_exists( $tmp ) ) { 00151 unlink( $tmp ); 00152 } 00153 } 00154 } 00155 00159 function clearFakeUploads() { 00160 $_FILES = array(); 00161 } 00162 }