2011-04-06 19:50:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-31 22:52:31 +00:00
|
|
|
* Abstract class to support upload tests
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2012-10-08 10:56:20 +00:00
|
|
|
'wgEnableUploads' => true,
|
|
|
|
|
'wgEnableAPI' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2012-10-08 10:56:20 +00:00
|
|
|
|
2011-04-06 19:50:54 +00:00
|
|
|
$this->clearFakeUploads();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function -- remove files and associated articles by Title
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param Title $title Title to be removed
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
|
|
|
|
public function deleteFileByTitle( $title ) {
|
|
|
|
|
if ( $title->exists() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$file = wfFindFile( $title, [ 'ignoreRedirect' => true ] );
|
2011-04-06 19:50:54 +00:00
|
|
|
$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
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $fileName Filename to be removed
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @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
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $filePath Path to file on the filesystem
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @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
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $fieldName Name this would have in the upload form
|
|
|
|
|
* @param string $fileName Name to title this
|
2014-07-24 14:04:48 +00:00
|
|
|
* @param string $type MIME type
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $filePath Path where to find file contents
|
2013-11-15 14:32:12 +00:00
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @return bool
|
2011-04-06 19:50:54 +00:00
|
|
|
*/
|
|
|
|
|
function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
|
2015-02-11 01:42:33 +00:00
|
|
|
$tmpName = $this->getNewTempFile();
|
2011-04-06 19:50:54 +00:00
|
|
|
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" );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$_FILES[$fieldName] = [
|
2013-02-14 11:56:23 +00:00
|
|
|
'name' => $fileName,
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'tmp_name' => $tmpName,
|
|
|
|
|
'size' => $size,
|
|
|
|
|
'error' => null
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
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 ) {
|
2015-02-11 01:42:33 +00:00
|
|
|
$tmpName = $this->getNewTempFile();
|
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
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$_FILES[$fieldName] = [
|
2013-02-14 11:56:23 +00:00
|
|
|
'name' => $fileName,
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'tmp_name' => $tmpName,
|
|
|
|
|
'size' => $size,
|
|
|
|
|
'error' => null
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2011-12-27 05:06:41 +00:00
|
|
|
}
|
2011-04-06 19:50:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove traces of previous fake uploads
|
|
|
|
|
*/
|
|
|
|
|
function clearFakeUploads() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$_FILES = [];
|
2011-04-06 19:50:54 +00:00
|
|
|
}
|
|
|
|
|
}
|