Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
UploadFormInitDescriptor | |
0.00% |
0 / 63 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
handle | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
process | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
setDefaultNamespace | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
setNamespaceSelectOptions | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
setFieldDefinitions | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
6 | |||
modifyDescriptor | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace NSFileRepo\Hooks; |
4 | |
5 | class UploadFormInitDescriptor { |
6 | |
7 | /** |
8 | * |
9 | * @var \Config |
10 | */ |
11 | protected $config = null; |
12 | |
13 | /** |
14 | * |
15 | * @var \IContextSource |
16 | */ |
17 | protected $context = null; |
18 | |
19 | /** |
20 | * |
21 | * @var array |
22 | */ |
23 | protected $descriptor = []; |
24 | |
25 | /** |
26 | * |
27 | * @param \IContextSource $context |
28 | * @param \Config $config |
29 | * @param array $descriptor |
30 | */ |
31 | public function __construct( \IContextSource $context, \Config $config, &$descriptor ) { |
32 | $this->context = $context; |
33 | $this->config = $config; |
34 | $this->descriptor =& $descriptor; |
35 | } |
36 | |
37 | /** |
38 | * Add fields to Special:Upload |
39 | * @param array $descriptor |
40 | * @return boolean |
41 | */ |
42 | public static function handle( &$descriptor ) { |
43 | $instance = new self( |
44 | \RequestContext::getMain(), |
45 | new \NSFileRepo\Config(), |
46 | $descriptor |
47 | ); |
48 | |
49 | return $instance->process(); |
50 | } |
51 | |
52 | public function process() { |
53 | $this->setDefaultNamespace(); |
54 | $this->setNamespaceSelectOptions(); |
55 | $this->setFieldDefinitions(); |
56 | $this->modifyDescriptor(); |
57 | |
58 | return true; |
59 | } |
60 | |
61 | protected $selectedNamespace = ''; |
62 | |
63 | protected function setDefaultNamespace() { |
64 | $this->selectedNamespace = '-'; |
65 | //"wpDestFile" is set on query string. e.g after click on redlink or on re-upload |
66 | if( !empty( $this->descriptor['DestFile']['default'] ) ) { |
67 | $target = $this->descriptor['DestFile']['default']; |
68 | $target = str_replace( '_', ' ', $target ); |
69 | $targetPieces = explode( ':', $target ); |
70 | |
71 | $nsText = ''; |
72 | if( count( $targetPieces) > 1 ) { |
73 | $nsText = str_replace( ' ', '_', $targetPieces[0] ); |
74 | $target = $targetPieces[1]; |
75 | } |
76 | |
77 | $this->descriptor['DestFile']['default'] = $target; |
78 | $this->selectedNamespace = $nsText; |
79 | } |
80 | } |
81 | |
82 | protected $namespaceSelectOptions = []; |
83 | |
84 | protected function setNamespaceSelectOptions() { |
85 | $namespaceList = new \NSFileRepo\NamespaceList( |
86 | $this->context->getUser(), |
87 | $this->config, |
88 | $this->context->getLanguage() |
89 | ); |
90 | |
91 | foreach( $namespaceList->getEditable() as $nsId => $namespace ) { |
92 | $this->namespaceSelectOptions[$namespace->getDisplayName()] |
93 | = $namespace->getCanonicalName(); |
94 | if( $nsId === NS_MAIN ) { |
95 | $this->namespaceSelectOptions[$namespace->getDisplayName()] = '-'; |
96 | } |
97 | } |
98 | } |
99 | |
100 | protected $fieldDef = []; |
101 | |
102 | protected function setFieldDefinitions() { |
103 | $this->fieldDef = [ |
104 | 'NSFR_Namespace' => [ |
105 | 'label' => wfMessage('namespace')->plain(), |
106 | 'section' => 'description', |
107 | 'class' => 'HTMLSelectField', |
108 | 'options' => $this->namespaceSelectOptions, |
109 | 'required' => true, |
110 | 'default' => $this->selectedNamespace |
111 | ], |
112 | 'NSFR_DestFile' => [ |
113 | 'type' => 'text', |
114 | 'section' => 'description', |
115 | 'label-message' => 'nsfilerepo-upload-target', |
116 | 'size' => 60, |
117 | 'default' => '', |
118 | 'readonly' => true, |
119 | 'nodata' => false, |
120 | ], |
121 | ]; |
122 | |
123 | //Prevent change of this fields value when it's a reupload |
124 | if ( isset( $this->descriptor['ForReUpload'] ) ) { |
125 | $this->fieldDef['NSFR_Namespace']['disabled'] = true; |
126 | $this->fieldDef['NSFR_Namespace']['help-message'] |
127 | = 'nsfilerepo-reupload-namespaceselector-disabled-helptext'; |
128 | } |
129 | } |
130 | |
131 | protected function modifyDescriptor() { |
132 | $pos = array_search( 'UploadDescription', array_keys( $this->descriptor ) ); |
133 | |
134 | $this->descriptor = |
135 | array_slice( $this->descriptor, 0, $pos, true ) + |
136 | $this->fieldDef + |
137 | array_slice( $this->descriptor, $pos, count( $this->descriptor ) - 1, true ) ; |
138 | } |
139 | } |