-
Notifications
You must be signed in to change notification settings - Fork 2
/
ResourceUploadBackends.php
77 lines (68 loc) · 2.66 KB
/
ResourceUploadBackends.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Generic function to add the comment linking back to the original title
*/
function getResourceComment() {
global $wgResourcesCategory, $wgContLang, $wgRequest;
$referer = $wgRequest->getText(ADD_RESOURCE_REFERER_FIELD);
$linktext = wfMessage('resources')->inContentLanguage()->text();
$pageText = "\n\n<!-- Don't edit below this line! -->\n[[" . $referer . "]] ";
$pageText .= "([[" . SpecialPage::getTitleFor( 'Resources' )
. '/' . $referer . '|' . $linktext . ']])';
if ( $wgResourcesCategory != NULL && gettype($wgResourcesCategory) == "string" ) {
$categoryText = $wgContLang->getNSText ( NS_CATEGORY );
$pageText .= "\n[[" . $categoryText . ":" .
$wgResourcesCategory . "]]";
}
return $pageText;
}
/**
* Upload handler class that extends the normal UploadFromFile class to modify
* the desired destination name and add the generic comment
*/
class UploadResourceFromFile extends UploadFromFile {
/**
* Modify the desired destination name.
*/
function initializeFromRequest( &$request ) {
$desiredDestName = $request->getText( 'wpDestFile' );
if( !$desiredDestName ) {
$desiredDestName = $request->getFileName( 'wpUploadFile' );
}
$referer = $request->getText(ADD_RESOURCE_REFERER_FIELD);
# filenames can never contain slashes, but the referer might contain them, if
# its a subpage:
$prefix = preg_replace('/\//', '-', $referer);
$destName = $prefix . ' - ' . $desiredDestName;
$init = parent::initializeFromRequest( $request );
$this->mDesiredDestName = $destName;
return $init;
}
/**
* Append the generic comment.
*
* @param $comment string The comment describing the change in the changelog
* @param $pageText string The text of the page. This string is modified
* with a link back to the original article referred to by the
* ADD_RESOURCE_REFERER_FIELD variable.
*/
function performUpload( $comment, $pageText, $watch, $user ) {
$pageText .= getResourceComment();
return parent::performUpload( $comment, $pageText, $watch, $user );
}
}
/**
* This class is used when a file is uploaded via Special:AddResource and a
* warning is displayed for some reason. In this case, Special:Upload gets
* loaded and the file is in fact already uploaded (as temporary file).
*/
class UploadResourceFromStash extends UploadFromStash {
/**
* This function just appends the generic comment
*/
function performUpload( $comment, $pageText, $watch, $user ) {
$pageText .= getResourceComment();
return parent::performUpload( $comment, $pageText, $watch, $user );
}
}
?>