MediaWiki
REL1_22
|
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 00051 return !( $title && $title instanceof Title && $title->exists() ); 00052 } 00053 00058 public function deleteFileByFileName( $fileName ) { 00059 return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) ); 00060 } 00061 00066 public function deleteFileByContent( $filePath ) { 00067 $hash = FSFile::getSha1Base36FromPath( $filePath ); 00068 $dupes = RepoGroup::singleton()->findBySha1( $hash ); 00069 $success = true; 00070 foreach ( $dupes as $dupe ) { 00071 $success &= $this->deleteFileByTitle( $dupe->getTitle() ); 00072 } 00073 00074 return $success; 00075 } 00076 00085 function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) { 00086 $tmpName = tempnam( wfTempDir(), "" ); 00087 if ( !file_exists( $filePath ) ) { 00088 throw new Exception( "$filePath doesn't exist!" ); 00089 } 00090 00091 if ( !copy( $filePath, $tmpName ) ) { 00092 throw new Exception( "couldn't copy $filePath to $tmpName" ); 00093 } 00094 00095 clearstatcache(); 00096 $size = filesize( $tmpName ); 00097 if ( $size === false ) { 00098 throw new Exception( "couldn't stat $tmpName" ); 00099 } 00100 00101 $_FILES[$fieldName] = array( 00102 'name' => $fileName, 00103 'type' => $type, 00104 'tmp_name' => $tmpName, 00105 'size' => $size, 00106 'error' => null 00107 ); 00108 00109 return true; 00110 } 00111 00112 function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) { 00113 $tmpName = tempnam( wfTempDir(), "" ); 00114 // copy the chunk data to temp location: 00115 if ( !file_put_contents( $tmpName, $chunkData ) ) { 00116 throw new Exception( "couldn't copy chunk data to $tmpName" ); 00117 } 00118 00119 clearstatcache(); 00120 $size = filesize( $tmpName ); 00121 if ( $size === false ) { 00122 throw new Exception( "couldn't stat $tmpName" ); 00123 } 00124 00125 $_FILES[$fieldName] = array( 00126 'name' => $fileName, 00127 'type' => $type, 00128 'tmp_name' => $tmpName, 00129 'size' => $size, 00130 'error' => null 00131 ); 00132 } 00133 00134 function clearTempUpload() { 00135 if ( isset( $_FILES['file']['tmp_name'] ) ) { 00136 $tmp = $_FILES['file']['tmp_name']; 00137 if ( file_exists( $tmp ) ) { 00138 unlink( $tmp ); 00139 } 00140 } 00141 } 00142 00146 function clearFakeUploads() { 00147 $_FILES = array(); 00148 } 00149 }