MediaWiki
REL1_24
|
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( 00044 $title, 00045 $file, 00046 $noOldArchive, 00047 $comment, 00048 $restrictDeletedVersions 00049 ); 00050 00051 if ( !$status->isGood() ) { 00052 return false; 00053 } 00054 00055 $page = WikiPage::factory( $title ); 00056 $page->doDeleteArticle( "removing for test" ); 00057 00058 // see if it now doesn't exist; reload 00059 $title = Title::newFromText( $title->getText(), NS_FILE ); 00060 } 00061 00062 return !( $title && $title instanceof Title && $title->exists() ); 00063 } 00064 00072 public function deleteFileByFileName( $fileName ) { 00073 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) ); 00074 } 00075 00084 public function deleteFileByContent( $filePath ) { 00085 $hash = FSFile::getSha1Base36FromPath( $filePath ); 00086 $dupes = RepoGroup::singleton()->findBySha1( $hash ); 00087 $success = true; 00088 foreach ( $dupes as $dupe ) { 00089 $success &= $this->deleteFileByTitle( $dupe->getTitle() ); 00090 } 00091 00092 return $success; 00093 } 00094 00107 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) { 00108 $tmpName = tempnam( wfTempDir(), "" ); 00109 if ( !file_exists( $filePath ) ) { 00110 throw new Exception( "$filePath doesn't exist!" ); 00111 } 00112 00113 if ( !copy( $filePath, $tmpName ) ) { 00114 throw new Exception( "couldn't copy $filePath to $tmpName" ); 00115 } 00116 00117 clearstatcache(); 00118 $size = filesize( $tmpName ); 00119 if ( $size === false ) { 00120 throw new Exception( "couldn't stat $tmpName" ); 00121 } 00122 00123 $_FILES[$fieldName] = array( 00124 'name' => $fileName, 00125 'type' => $type, 00126 'tmp_name' => $tmpName, 00127 'size' => $size, 00128 'error' => null 00129 ); 00130 00131 return true; 00132 } 00133 00134 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) { 00135 $tmpName = tempnam( wfTempDir(), "" ); 00136 // copy the chunk data to temp location: 00137 if ( !file_put_contents( $tmpName, $chunkData ) ) { 00138 throw new Exception( "couldn't copy chunk data to $tmpName" ); 00139 } 00140 00141 clearstatcache(); 00142 $size = filesize( $tmpName ); 00143 if ( $size === false ) { 00144 throw new Exception( "couldn't stat $tmpName" ); 00145 } 00146 00147 $_FILES[$fieldName] = array( 00148 'name' => $fileName, 00149 'type' => $type, 00150 'tmp_name' => $tmpName, 00151 'size' => $size, 00152 'error' => null 00153 ); 00154 } 00155 00156 function clearTempUpload() { 00157 if ( isset( $_FILES['file']['tmp_name'] ) ) { 00158 $tmp = $_FILES['file']['tmp_name']; 00159 if ( file_exists( $tmp ) ) { 00160 unlink( $tmp ); 00161 } 00162 } 00163 } 00164 00168 function clearFakeUploads() { 00169 $_FILES = array(); 00170 } 00171 }