MediaWiki  1.23.6
ApiTestCaseUpload.php
Go to the documentation of this file.
1 <?php
2 
7 abstract class ApiTestCaseUpload extends ApiTestCase {
11  protected function setUp() {
12  parent::setUp();
13 
14  $this->setMwGlobals( array(
15  'wgEnableUploads' => true,
16  'wgEnableAPI' => true,
17  ) );
18 
20 
21  $this->clearFakeUploads();
22  }
23 
24  protected function tearDown() {
25  $this->clearTempUpload();
26 
27  parent::tearDown();
28  }
29 
37  public function deleteFileByTitle( $title ) {
38  if ( $title->exists() ) {
39  $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
40  $noOldArchive = ""; // yes this really needs to be set this way
41  $comment = "removing for test";
42  $restrictDeletedVersions = false;
43  $status = FileDeleteForm::doDelete( $title, $file, $noOldArchive, $comment, $restrictDeletedVersions );
44  if ( !$status->isGood() ) {
45  return false;
46  }
47  $page = WikiPage::factory( $title );
48  $page->doDeleteArticle( "removing for test" );
49 
50  // see if it now doesn't exist; reload
51  $title = Title::newFromText( $title->getText(), NS_FILE );
52  }
53 
54  return !( $title && $title instanceof Title && $title->exists() );
55  }
56 
64  public function deleteFileByFileName( $fileName ) {
65  return $this->deleteFileByTitle( Title::newFromText( $fileName, NS_FILE ) );
66  }
67 
75  public function deleteFileByContent( $filePath ) {
76  $hash = FSFile::getSha1Base36FromPath( $filePath );
77  $dupes = RepoGroup::singleton()->findBySha1( $hash );
78  $success = true;
79  foreach ( $dupes as $dupe ) {
80  $success &= $this->deleteFileByTitle( $dupe->getTitle() );
81  }
82 
83  return $success;
84  }
85 
98  function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
99  $tmpName = tempnam( wfTempDir(), "" );
100  if ( !file_exists( $filePath ) ) {
101  throw new Exception( "$filePath doesn't exist!" );
102  }
103 
104  if ( !copy( $filePath, $tmpName ) ) {
105  throw new Exception( "couldn't copy $filePath to $tmpName" );
106  }
107 
108  clearstatcache();
109  $size = filesize( $tmpName );
110  if ( $size === false ) {
111  throw new Exception( "couldn't stat $tmpName" );
112  }
113 
114  $_FILES[$fieldName] = array(
115  'name' => $fileName,
116  'type' => $type,
117  'tmp_name' => $tmpName,
118  'size' => $size,
119  'error' => null
120  );
121 
122  return true;
123  }
124 
125  function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
126  $tmpName = tempnam( wfTempDir(), "" );
127  // copy the chunk data to temp location:
128  if ( !file_put_contents( $tmpName, $chunkData ) ) {
129  throw new Exception( "couldn't copy chunk data to $tmpName" );
130  }
131 
132  clearstatcache();
133  $size = filesize( $tmpName );
134  if ( $size === false ) {
135  throw new Exception( "couldn't stat $tmpName" );
136  }
137 
138  $_FILES[$fieldName] = array(
139  'name' => $fileName,
140  'type' => $type,
141  'tmp_name' => $tmpName,
142  'size' => $size,
143  'error' => null
144  );
145  }
146 
147  function clearTempUpload() {
148  if ( isset( $_FILES['file']['tmp_name'] ) ) {
149  $tmp = $_FILES['file']['tmp_name'];
150  if ( file_exists( $tmp ) ) {
151  unlink( $tmp );
152  }
153  }
154  }
155 
159  function clearFakeUploads() {
160  $_FILES = array();
161  }
162 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfSetupSession
wfSetupSession( $sessionId=false)
Initialise php session.
Definition: GlobalFunctions.php:3532
NS_FILE
const NS_FILE
Definition: Defines.php:85
ApiTestCaseUpload\deleteFileByContent
deleteFileByContent( $filePath)
Helper function – given a file on the filesystem, find matching content in the db (and associated art...
Definition: ApiTestCaseUpload.php:75
$success
$success
Definition: Utf8Test.php:91
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null)
Really delete the file.
Definition: FileDeleteForm.php:141
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
ApiTestCaseUpload\clearFakeUploads
clearFakeUploads()
Remove traces of previous fake uploads.
Definition: ApiTestCaseUpload.php:159
ApiTestCaseUpload\fakeUploadFile
fakeUploadFile( $fieldName, $fileName, $type, $filePath)
Fake an upload by dumping the file into temp space, and adding info to $_FILES.
Definition: ApiTestCaseUpload.php:98
FSFile\getSha1Base36FromPath
static getSha1Base36FromPath( $path)
Get a SHA-1 hash of a file in the local filesystem, in base-36 lower case encoding,...
Definition: FSFile.php:259
ApiTestCaseUpload\deleteFileByTitle
deleteFileByTitle( $title)
Helper function – remove files and associated articles by Title.
Definition: ApiTestCaseUpload.php:37
ApiTestCaseUpload\fakeUploadChunk
fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData)
Definition: ApiTestCaseUpload.php:125
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiTestCaseUpload\clearTempUpload
clearTempUpload()
Definition: ApiTestCaseUpload.php:147
$comment
$comment
Definition: importImages.php:107
ApiTestCaseUpload\deleteFileByFileName
deleteFileByFileName( $fileName)
Helper function – remove files and associated articles with a particular filename.
Definition: ApiTestCaseUpload.php:64
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
ApiTestCase
Definition: ApiTestCase.php:3
$size
$size
Definition: RandomTest.php:75
$hash
return false to override stock group addition can be modified try getUserPermissionsErrors userCan checks are continued by internal code can override on output return false to not delete it return false to override the default password checks & $hash
Definition: hooks.txt:2697
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Title
Represents a title within MediaWiki.
Definition: Title.php:35
wfTempDir
wfTempDir()
Tries to get the system directory for temporary files.
Definition: GlobalFunctions.php:2564
ApiTestCaseUpload
Definition: ApiTestCaseUpload.php:7
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
wfFindFile
wfFindFile( $title, $options=array())
Find a file.
Definition: GlobalFunctions.php:3702
ApiTestCaseUpload\setUp
setUp()
Fixture – run before every test.
Definition: ApiTestCaseUpload.php:11
ApiTestCaseUpload\tearDown
tearDown()
Definition: ApiTestCaseUpload.php:24
$type
$type
Definition: testCompression.php:46