2011-04-06 19:50:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* * Abstract class to support upload tests
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
abstract class ApiTestCaseUpload extends ApiTestCase {
|
|
|
|
|
/**
|
|
|
|
|
* Fixture -- run before every test
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2011-04-06 19:50:54 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgEnableUploads' => true,
|
|
|
|
|
'wgEnableAPI' => true,
|
|
|
|
|
) );
|
|
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
wfSetupSession();
|
|
|
|
|
|
|
|
|
|
$this->clearFakeUploads();
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function tearDown() {
|
2011-12-08 14:03:04 +00:00
|
|
|
$this->clearTempUpload();
|
2012-10-08 10:56:20 +00:00
|
|
|
|
|
|
|
|
parent::tearDown();
|
2011-12-08 14:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
/**
|
|
|
|
|
* Helper function -- remove files and associated articles by Title
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @param Title $title title to be removed
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
|
|
|
|
public function deleteFileByTitle( $title ) {
|
|
|
|
|
if ( $title->exists() ) {
|
|
|
|
|
$file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
|
|
|
|
|
$noOldArchive = ""; // yes this really needs to be set this way
|
|
|
|
|
$comment = "removing for test";
|
|
|
|
|
$restrictDeletedVersions = false;
|
2014-04-24 15:05:10 +00:00
|
|
|
$status = FileDeleteForm::doDelete(
|
|
|
|
|
$title,
|
|
|
|
|
$file,
|
|
|
|
|
$noOldArchive,
|
|
|
|
|
$comment,
|
|
|
|
|
$restrictDeletedVersions
|
|
|
|
|
);
|
|
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-04-24 15:05:10 +00:00
|
|
|
|
2012-01-27 21:21:19 +00:00
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
|
$page->doDeleteArticle( "removing for test" );
|
2011-04-06 19:50:54 +00:00
|
|
|
|
|
|
|
|
// see if it now doesn't exist; reload
|
2011-04-07 14:54:38 +00:00
|
|
|
$title = Title::newFromText( $title->getText(), NS_FILE );
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
return !( $title && $title instanceof Title && $title->exists() );
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function -- remove files and associated articles with a particular filename
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @param string $fileName filename to be removed
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
|
|
|
|
public function deleteFileByFileName( $fileName ) {
|
|
|
|
|
return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-24 15:05:10 +00:00
|
|
|
* Helper function -- given a file on the filesystem, find matching
|
|
|
|
|
* content in the db (and associated articles) and remove them.
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @param string $filePath path to file on the filesystem
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
|
|
|
|
public function deleteFileByContent( $filePath ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$hash = FSFile::getSha1Base36FromPath( $filePath );
|
2011-04-06 19:50:54 +00:00
|
|
|
$dupes = RepoGroup::singleton()->findBySha1( $hash );
|
|
|
|
|
$success = true;
|
|
|
|
|
foreach ( $dupes as $dupe ) {
|
|
|
|
|
$success &= $this->deleteFileByTitle( $dupe->getTitle() );
|
|
|
|
|
}
|
2013-04-26 12:00:22 +00:00
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fake an upload by dumping the file into temp space, and adding info to $_FILES.
|
|
|
|
|
* (This is what PHP would normally do).
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @param string $fieldName name this would have in the upload form
|
|
|
|
|
* @param string $fileName name to title this
|
|
|
|
|
* @param string $type mime type
|
|
|
|
|
* @param string $filePath path where to find file contents
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @return bool
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
|
|
|
|
function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
|
|
|
|
|
$tmpName = tempnam( wfTempDir(), "" );
|
|
|
|
|
if ( !file_exists( $filePath ) ) {
|
|
|
|
|
throw new Exception( "$filePath doesn't exist!" );
|
2013-02-14 11:56:23 +00:00
|
|
|
}
|
2011-04-06 19:50:54 +00:00
|
|
|
|
|
|
|
|
if ( !copy( $filePath, $tmpName ) ) {
|
|
|
|
|
throw new Exception( "couldn't copy $filePath to $tmpName" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearstatcache();
|
|
|
|
|
$size = filesize( $tmpName );
|
|
|
|
|
if ( $size === false ) {
|
|
|
|
|
throw new Exception( "couldn't stat $tmpName" );
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
$_FILES[$fieldName] = array(
|
|
|
|
|
'name' => $fileName,
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'tmp_name' => $tmpName,
|
|
|
|
|
'size' => $size,
|
|
|
|
|
'error' => null
|
2011-04-06 19:50:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-10-08 10:56:20 +00:00
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
|
2011-12-27 05:06:41 +00:00
|
|
|
$tmpName = tempnam( wfTempDir(), "" );
|
2013-01-28 10:27:15 +00:00
|
|
|
// copy the chunk data to temp location:
|
2011-12-27 05:06:41 +00:00
|
|
|
if ( !file_put_contents( $tmpName, $chunkData ) ) {
|
|
|
|
|
throw new Exception( "couldn't copy chunk data to $tmpName" );
|
|
|
|
|
}
|
2013-01-28 10:27:15 +00:00
|
|
|
|
2011-12-27 05:06:41 +00:00
|
|
|
clearstatcache();
|
|
|
|
|
$size = filesize( $tmpName );
|
|
|
|
|
if ( $size === false ) {
|
|
|
|
|
throw new Exception( "couldn't stat $tmpName" );
|
|
|
|
|
}
|
2013-01-28 10:27:15 +00:00
|
|
|
|
2013-02-14 11:56:23 +00:00
|
|
|
$_FILES[$fieldName] = array(
|
|
|
|
|
'name' => $fileName,
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'tmp_name' => $tmpName,
|
|
|
|
|
'size' => $size,
|
|
|
|
|
'error' => null
|
2011-12-27 05:06:41 +00:00
|
|
|
);
|
|
|
|
|
}
|
2011-04-06 19:50:54 +00:00
|
|
|
|
2011-12-08 14:03:04 +00:00
|
|
|
function clearTempUpload() {
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( isset( $_FILES['file']['tmp_name'] ) ) {
|
2011-12-08 14:03:04 +00:00
|
|
|
$tmp = $_FILES['file']['tmp_name'];
|
2013-02-14 11:56:23 +00:00
|
|
|
if ( file_exists( $tmp ) ) {
|
2011-12-08 14:03:04 +00:00
|
|
|
unlink( $tmp );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
/**
|
|
|
|
|
* Remove traces of previous fake uploads
|
|
|
|
|
*/
|
|
|
|
|
function clearFakeUploads() {
|
|
|
|
|
$_FILES = array();
|
|
|
|
|
}
|
|
|
|
|
}
|