Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 130 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
CargoPageSchemas | |
0.00% |
0 / 130 |
|
0.00% |
0 / 13 |
2756 | |
0.00% |
0 / 1 |
registerClass | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
createPageSchemasObject | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
110 | |||
getDisplayColor | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTemplateDisplayString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTemplateValues | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
getTemplateDisplayValues | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
getFieldDisplayString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isTemplateDataMultipleInstanceOnly | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTemplateEditingHTML | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getFieldEditingHTML | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
72 | |||
createTemplateXMLFromForm | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
createFieldXMLFromForm | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
56 | |||
getFieldDisplayValues | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | /** |
3 | * Static functions for use by the Page Schemas extension. |
4 | * |
5 | * @author Yaron Koren |
6 | */ |
7 | |
8 | use MediaWiki\Html\Html; |
9 | |
10 | class CargoPageSchemas extends PSExtensionHandler { |
11 | public static function registerClass() { |
12 | global $wgPageSchemasHandlerClasses; |
13 | $wgPageSchemasHandlerClasses[] = 'CargoPageSchemas'; |
14 | return true; |
15 | } |
16 | |
17 | /** |
18 | * Returns an object containing information on a Cargo field, |
19 | * based on XML from the Page Schemas extension. |
20 | */ |
21 | public static function createPageSchemasObject( $tagName, $xml ) { |
22 | $cargoArray = []; |
23 | if ( $tagName == "cargo_TemplateDetails" ) { |
24 | foreach ( $xml->children() as $tag => $child ) { |
25 | if ( $tag == $tagName ) { |
26 | foreach ( $child->children() as $tag => $elem ) { |
27 | $cargoArray[$tag] = (string)$elem; |
28 | } |
29 | return $cargoArray; |
30 | } |
31 | } |
32 | } |
33 | if ( $tagName == "cargo_Field" ) { |
34 | foreach ( $xml->children() as $tag => $child ) { |
35 | if ( $tag != $tagName ) { |
36 | continue; |
37 | } |
38 | $allowedValues = []; |
39 | foreach ( $child->children() as $prop => $value ) { |
40 | if ( $prop == "AllowedValue" ) { |
41 | $allowedValues[] = (string)$value; |
42 | } else { |
43 | $cargoArray[$prop] = (string)$value; |
44 | } |
45 | } |
46 | $cargoArray['AllowedValues'] = $allowedValues; |
47 | return $cargoArray; |
48 | } |
49 | } |
50 | |
51 | return null; |
52 | } |
53 | |
54 | public static function getDisplayColor() { |
55 | return '#e9cdff'; |
56 | } |
57 | |
58 | public static function getTemplateDisplayString() { |
59 | return wfMessage( 'specialpages-group-cargo' )->escaped(); |
60 | } |
61 | |
62 | public static function getTemplateValues( $psTemplate ) { |
63 | // TODO - fix this. |
64 | $values = []; |
65 | if ( $psTemplate instanceof PSTemplate ) { |
66 | $psTemplate = $psTemplate->getXML(); |
67 | } |
68 | foreach ( $psTemplate->children() as $tag => $child ) { |
69 | if ( $tag == "cargo_TemplateDetails" ) { |
70 | foreach ( $child->children() as $prop ) { |
71 | $values[$prop->getName()] = (string)$prop; |
72 | } |
73 | } |
74 | } |
75 | return $values; |
76 | } |
77 | |
78 | /** |
79 | * Displays Cargo details for one template in the Page Schemas XML. |
80 | */ |
81 | public static function getTemplateDisplayValues( $templateXML ) { |
82 | $templateValues = self::getTemplateValues( $templateXML ); |
83 | if ( count( $templateValues ) == 0 ) { |
84 | return null; |
85 | } |
86 | |
87 | $displayValues = []; |
88 | foreach ( $templateValues as $key => $value ) { |
89 | if ( $key == 'Table' ) { |
90 | $propName = 'Table'; |
91 | } |
92 | $displayValues[$propName] = $value; |
93 | } |
94 | return [ null, $displayValues ]; |
95 | } |
96 | |
97 | public static function getFieldDisplayString() { |
98 | return wfMessage( 'cargo-pageschemas-cargofield' )->escaped(); |
99 | } |
100 | |
101 | public static function isTemplateDataMultipleInstanceOnly() { |
102 | return false; |
103 | } |
104 | |
105 | public static function getTemplateEditingHTML( $psTemplate ) { |
106 | $hasExistingValues = false; |
107 | $tableName = null; |
108 | if ( $psTemplate !== null ) { |
109 | $cargoArray = $psTemplate->getObject( 'cargo_TemplateDetails' ); |
110 | if ( $cargoArray !== null ) { |
111 | $hasExistingValues = true; |
112 | $tableName = PageSchemas::getValueFromObject( $cargoArray, 'Table' ); |
113 | } |
114 | } |
115 | |
116 | $text = "\t<p>" . wfMessage( 'cargo-pageschemas-tablename' )->escaped() . ' ' . |
117 | Html::input( 'cargo_template_table_name_num', $tableName, 'text', [ 'size' => 30 ] ) . "</p>\n"; |
118 | |
119 | return [ $text, $hasExistingValues ]; |
120 | } |
121 | |
122 | /** |
123 | * Returns the HTML for setting the options for the Cargo section |
124 | * in Page Schemas' "edit schema" page. |
125 | */ |
126 | public static function getFieldEditingHTML( $psField ) { |
127 | global $wgCargoFieldTypes; |
128 | |
129 | $cargoArray = []; |
130 | $hasExistingValues = false; |
131 | if ( $psField !== null ) { |
132 | $cargoArray = $psField->getObject( 'cargo_Field' ); |
133 | if ( $cargoArray !== null ) { |
134 | $hasExistingValues = true; |
135 | } |
136 | } |
137 | |
138 | $fieldType = PageSchemas::getValueFromObject( $cargoArray, 'Type' ); |
139 | |
140 | $allowedValues = PageSchemas::getValueFromObject( $cargoArray, 'AllowedValues' ); |
141 | if ( $allowedValues === null ) { |
142 | $allowedValuesString = ''; |
143 | } else { |
144 | $allowedValuesString = implode( ', ', $allowedValues ); |
145 | } |
146 | |
147 | $typeLabel = wfMessage( 'pf_createproperty_proptype' )->escaped(); |
148 | if ( $typeLabel == '' ) { |
149 | $typeLabel = 'Type:'; |
150 | } |
151 | $allowedValuesLabel = wfMessage( 'pf_createproperty_allowedvalsinput' )->escaped(); |
152 | if ( $allowedValuesLabel == '' ) { |
153 | $allowedValuesLabel = 'Allowed values:'; |
154 | } |
155 | |
156 | $html_text = "<p>$typeLabel "; |
157 | |
158 | $selectBody = ''; |
159 | foreach ( $wgCargoFieldTypes as $type ) { |
160 | $optionAttrs = [ 'value' => $type ]; |
161 | if ( $type == $fieldType ) { |
162 | $optionAttrs['selected'] = true; |
163 | } |
164 | $selectBody .= Html::element( 'option', $optionAttrs, $type ) . "\n"; |
165 | } |
166 | $html_text .= Html::rawElement( 'select', [ 'name' => 'cargo_field_type_num' ], $selectBody ) . "\n"; |
167 | $html_text .= "<p>$allowedValuesLabel<br />\n"; |
168 | $html_text .= Html::input( 'cargo_field_allowed_values_num', $allowedValuesString, 'text', [ 'size' => 100 ] ); |
169 | $html_text .= "\t</p>\n"; |
170 | |
171 | return [ $html_text, $hasExistingValues ]; |
172 | } |
173 | |
174 | /** |
175 | * Creates Page Schemas XML from Cargo information on templates. |
176 | */ |
177 | public static function createTemplateXMLFromForm() { |
178 | global $wgRequest; |
179 | |
180 | $xmlPerTemplate = []; |
181 | foreach ( $wgRequest->getValues() as $var => $val ) { |
182 | $val = str_replace( [ '<', '>' ], [ '<', '>' ], $val ); |
183 | if ( substr( $var, 0, 26 ) == 'cargo_template_table_name_' ) { |
184 | $templateNum = substr( $var, 26 ); |
185 | $xml = '<cargo_TemplateDetails>'; |
186 | if ( $val ) { |
187 | $xml .= "<Table>$val</Table>"; |
188 | } |
189 | $xml .= '</cargo_TemplateDetails>'; |
190 | $xmlPerTemplate[$templateNum] = $xml; |
191 | } |
192 | } |
193 | return $xmlPerTemplate; |
194 | } |
195 | |
196 | public static function createFieldXMLFromForm() { |
197 | global $wgRequest; |
198 | |
199 | $fieldNum = -1; |
200 | $xmlPerField = []; |
201 | foreach ( $wgRequest->getValues() as $var => $val ) { |
202 | if ( substr( $var, 0, 17 ) == 'cargo_field_type_' ) { |
203 | $xml = '<cargo_Field>'; |
204 | $fieldNum = substr( $var, 17 ); |
205 | if ( $val ) { |
206 | $xml .= "<Type>$val</Type>"; |
207 | } |
208 | } elseif ( substr( $var, 0, 27 ) == 'cargo_field_allowed_values_' ) { |
209 | if ( $val ) { |
210 | // Replace the comma substitution character that has no chance of |
211 | // being included in the values list - namely, the ASCII beep. |
212 | $listSeparator = ','; |
213 | $allowedValuesStr = str_replace( "\\$listSeparator", "\a", $val ); |
214 | $allowedValuesArray = explode( $listSeparator, $allowedValuesStr ); |
215 | foreach ( $allowedValuesArray as $value ) { |
216 | // Replace beep back with comma, trim. |
217 | $value = str_replace( "\a", $listSeparator, trim( $value ) ); |
218 | $xml .= '<AllowedValue>' . $value . '</AllowedValue>'; |
219 | } |
220 | } |
221 | $xml .= '</cargo_Field>'; |
222 | $xmlPerField[$fieldNum] = $xml; |
223 | } |
224 | } |
225 | |
226 | return $xmlPerField; |
227 | } |
228 | |
229 | /** |
230 | * Displays the information about the Cargo field (if any) |
231 | * for one field in the Page Schemas XML. |
232 | */ |
233 | public static function getFieldDisplayValues( $field_xml ) { |
234 | foreach ( $field_xml->children() as $tag => $child ) { |
235 | if ( $tag == "cargo_Field" ) { |
236 | $values = []; |
237 | $allowedValues = []; |
238 | foreach ( $child->children() as $prop => $value ) { |
239 | if ( $prop == "AllowedValue" ) { |
240 | $allowedValues[] = $value; |
241 | } else { |
242 | $values[$prop] = $value; |
243 | } |
244 | } |
245 | $allowedValuesStr = implode( ', ', $allowedValues ); |
246 | $allowedValuesLabel = wfMessage( 'pf_createclass_allowedvalues' )->escaped(); |
247 | if ( $allowedValuesLabel == '' ) { |
248 | $allowedValuesLabel = 'Allowed values:'; |
249 | } |
250 | $values[$allowedValuesLabel] = $allowedValuesStr; |
251 | return [ null, $values ]; |
252 | } |
253 | } |
254 | return null; |
255 | } |
256 | } |