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