-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomUploadFields.php
201 lines (151 loc) · 5.47 KB
/
CustomUploadFields.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* CustomUploadFields Extension
*
* Adds additional fields to the Upload form
*
* In order to add new fields, edit efCustomUploadFieldsForm
* In order to determine how they're processed, edit efCustomUploadFieldsProcess
* Because there is no way to properly edit $pageText,
* you must edit includes/specials/SpecialUpload.php to properly install this extension
* REMEMBER TO BACKUP!
* In the getInitialPageText function, edit it so it says the following:
if ( $license != '' ) {
$pageText = $license;
* Will try to find a way to do it without having to edit the Special page
*/
if ( !defined( 'MEDIAWIKI' ) ) die( 'Invalid entry point.' );
$wgExtensionCredits[ 'specialpage' ][] = array(
'path' => __FILE__,
'name' => 'CustomUploadFields',
'author' => '[http://www.zeldawiki.org Abdullah Abduldayem]',
'decriptionmsg' => 'Adds custom fields to the [[Special:Upload|upload form]]'
);
// Register internationalisation file
$wgExtensionMessagesFiles[ 'CustomUploadFields' ] = dirname( __FILE__ ) . '/CustomUploadFields.i18n.php';
// Register required hooks
$wgHooks[ 'UploadForm:initial' ][] = 'efCustomUploadFieldsForm';
$wgHooks[ 'UploadForm:BeforeProcessing' ][] = 'efCustomUploadFieldsProcess';
$wgHooks[ 'BeforePageDisplay' ][] = 'onBeforePageDisplay';
//Override the default Special:Upload with our own modified class
global $wgAutoloadLocalClasses;
$wgAutoloadLocalClasses['SpecialUpload'] = dirname( __FILE__ ) . '/SpecialUploadModified.php';
function onBeforePageDisplay( OutputPage &$out, Skin &$skin ) {
// Hide the old Licenses Field.
// $out->addInlineScript would not work, so we're adding the style via HTML
$out->addHTML('
<style>
.mw-htmlform-field-Licenses{display:none;}
</style>');
return true;
}
/**
* efCustomUploadFieldsForm
*
* Entries are in the form:
* createFormField ( $uploadFormObj, Type, Name);
* Type: Either 'dropdown' or 'input'
* Name: The name of the new field
* ==Dropdown menus==
* Options are stored in Mediawiki:Name
* Field label is stored in Mediawiki:Name-label
* Default value is stored in Mediawiki:Name-none
*
* When processing, getText from 'wp'+name (All lowercase)
*/
function efCustomUploadFieldsForm( $uploadFormObj ) {
// Bring the licenses above the Edittools. Original hidden with CSS. No Ajax previews
if ($uploadFormObj->mForReUpload == false) {
createFormField ( $uploadFormObj, 'dropdown', 'licenses');
createFormField ( $uploadFormObj, 'dropdown', 'filetype');
createFormField ( $uploadFormObj, 'dropdown', 'games');
createFormField ( $uploadFormObj, 'input', 'source');
}
return true;
}
/**
* efCustomUploadFieldsProcess
*
* First, getText from your newly created element(s)
* In order to append your changes to the sent data, use the following:
* $uploadFormObj->mComment .= '[...]';
* To replace the sent data entirely, use:
* $uploadFormObj->mComment = '[...]';
*/
function efCustomUploadFieldsProcess( $uploadFormObj ) {
global $wgRequest;
$summary = $wgRequest->getText( 'wpUploadDescription' );
//$license = $wgRequest->getText( 'wpLicense' );
$source = $wgRequest->getText( 'wpsource' );
$game = $wgRequest->getText( 'wpgames' );
$type = $wgRequest->getText( 'wpfiletype' );
$license = $wgRequest->getText( 'wplicenses' );
$uploadFormObj->mLicense =
"{{FileInfo\n" .
'|summary= ' . $summary . "\n".
'|source= ' . $source . "\n".
'|type= ' . $type . "\n".
'|game= ' . $game . "\n".
'|licensing= ' . $license . "\n".
'}}';
return true;
}
//-----------------------------------------------------------------------
function createFormField ( $uploadFormObj, $type, $msg ) {
$field =
Xml::openElement( 'tr' ) .
Xml::openElement( 'td', array( 'align' => 'right' ) ) .
Xml::label( wfMsg( $msg . '-label' ), 'wp'. $msg ) .
Xml::closeElement( 'td' ) .
Xml::openElement( 'td' );
switch ($type) {
case 'dropdown':
$field .= Xml::openElement( 'select', array( 'name' => 'wp'. $msg , 'id' => 'wp'. $msg ) );
//Default Message
$field .= Xml::option( wfMsg( $msg . '-none' ), '' );
//Custom Options
$field .= getDropdownOptions( $msg );
$field .= Xml::closeElement( 'select' );
break;
case 'input':
$field .= Xml::input( 'wp'. $msg , 60);
break;
}
$field .= Xml::closeElement( 'td' ) .
Xml::closeElement( 'tr' );
$old = $uploadFormObj->uploadFormTextAfterSummary;
$uploadFormObj->uploadFormTextAfterSummary = $field . $old;
}
function getDropdownOptions( $str ) {
$lines = explode( "\n", msg ($str) );
$return = "";
foreach ( $lines as $line ) {
if ( strpos( $line, '*' ) !== 0 )
continue;
else {
list( $level, $line ) = trimStars( $line );
if ( strpos( $line, '|' ) !== false ) {
list( $val, $line ) = getPipeValue( $line );
$line = str_repeat( /*   */ "\xc2\xa0", $level * 2 ) . $line;
$return .= Xml::option( $line, $val);
} else {
$line = str_repeat( /*   */ "\xc2\xa0", $level * 2 ) . $line;
$return .= Xml::element( 'option', array('disabled' => 'disabled', 'style' => 'color: GrayText'), $line ) ;
}
}
}
return $return;
}
function msg( $str ) {
$out = wfMsg( $str );
return wfEmptyMsg( $str, $out ) ? $str : $out;
}
function trimStars( $str ) {
$numStars = strspn( $str, '*' );
return array( $numStars-1, ltrim( substr( $str, $numStars ), ' ' ) );
}
function getPipeValue( $str ) {
$pipePos = strpos( $str, '|' );
$str = ltrim( $str, ' ');
return array( substr($str, 0, $pipePos), substr($str, $pipePos+1) );
}