summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/SMW_PageSchemas.php
blob: 8143ace2f016cfd6d5771fe4764365253336ccca (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php

/**
 * Functions for handling Semantic MediaWiki data within the Page Schemas
 * extension.
 *
 * @author Ankit Garg
 * @author Yaron Koren
 * @ingroup SMW
 */

class SMWPageSchemas extends PSExtensionHandler {

	public static function getDisplayColor() {
		return '#DEF';
	}

	public static function getTemplateDisplayString() {
		return 'Connecting property';
	}

	public static function getFieldDisplayString() {
		return 'Semantic property';
	}

	/**
	 * Returns the display info for the "connecting property" (if any)
	 * of the #subobject call (if any) in this template.
	 */
	public static function getTemplateDisplayValues( $templateXML ) {
		foreach ( $templateXML->children() as $tag => $child ) {
			if ( $tag == "semanticmediawiki_ConnectingProperty" ) {
				$propName = $child->attributes()->name;
				$values = [];
				return [ $propName, $values ];
			}
		}
		return null;
	}

	/**
	 * Returns the display info for the property (if any is defined)
	 * for a single field in the Page Schemas XML.
	 */
	public static function getFieldDisplayValues( $fieldXML ) {
		foreach ( $fieldXML->children() as $tag => $child ) {
			if ( $tag == "semanticmediawiki_Property" ) {
				$propName = $child->attributes()->name;
				$values = [];
				foreach ( $child->children() as $prop => $value ) {
					$values[$prop] = (string)$value;
				}
				return [ $propName, $values ];
			}
		}
		return null;
	}

	/**
	 * Returns the set of SMW property data from the entire page schema.
	 */
	static function getAllPropertyData( $pageSchemaObj ) {
		$propertyDataArray = [];
		$psTemplates = $pageSchemaObj->getTemplates();
		foreach ( $psTemplates as $psTemplate ) {
			$psTemplateFields = $psTemplate->getFields();
			foreach ( $psTemplateFields as $psTemplateField ) {
				$prop_array = $psTemplateField->getObject('semanticmediawiki_Property');
				if ( empty( $prop_array ) ) {
					continue;
				}
				// If property name is blank, set it to the
				// field name.
				if ( !array_key_exists( 'name', $prop_array ) || empty( $prop_array['name'] ) ) {
					$prop_array['name'] = $psTemplateField->getName();
				}
				$propertyDataArray[] = $prop_array;
			}
		}
		return $propertyDataArray;
	}

	/**
	 * Constructs XML for the "connecting property", based on what was
	 * submitted in the 'edit schema' form.
	 */
	public static function createTemplateXMLFromForm() {
		global $wgRequest;

		$xmlPerTemplate = [];
		foreach ( $wgRequest->getValues() as $var => $val ) {
			if ( substr( $var, 0, 24 ) == 'smw_connecting_property_' ) {
				$templateNum = substr( $var, 24 );
				$xml = '<semanticmediawiki_ConnectingProperty name="' . $val . '" />';
				$xmlPerTemplate[$templateNum] = $xml;
			}
		}
		return $xmlPerTemplate;
	}

	static function getConnectingPropertyName( $psTemplate ) {
		// TODO - there should be a more direct way to get
		// this data.
		$smwConnectingPropertyArray = $psTemplate->getObject( 'semanticmediawiki_ConnectingProperty' );
		return PageSchemas::getValueFromObject( $smwConnectingPropertyArray, 'name' );
	}

	/**
	 * Sets the list of property pages defined by the passed-in
	 * Page Schemas object.
	 */
	public static function getPagesToGenerate( $pageSchemaObj ) {
		$pagesToGenerate = [];

		$psTemplates = $pageSchemaObj->getTemplates();
		foreach ( $psTemplates as $psTemplate ) {
			$smwConnectingPropertyName = self::getConnectingPropertyName( $psTemplate );
			if ( is_null( $smwConnectingPropertyName ) ) {
				continue;
			}
			$pagesToGenerate[] = Title::makeTitleSafe( SMW_NS_PROPERTY, $smwConnectingPropertyName );
		}

		$propertyDataArray = self::getAllPropertyData( $pageSchemaObj );
		foreach ( $propertyDataArray as $propertyData ) {
			$title = Title::makeTitleSafe( SMW_NS_PROPERTY, $propertyData['name'] );
			$pagesToGenerate[] = $title;
		}
		return $pagesToGenerate;
	}

	/**
	 * Constructs XML for the SMW property, based on what was submitted
	 * in the 'edit schema' form.
	 */
	public static function createFieldXMLFromForm() {
		global $wgRequest;

		$fieldNum = -1;
		$xmlPerField = [];
		foreach ( $wgRequest->getValues() as $var => $val ) {
			if ( substr( $var, 0, 18 ) == 'smw_property_name_' ) {
				$fieldNum = substr( $var, 18 );
				$xml = '<semanticmediawiki_Property name="' . $val . '" >';
			} elseif ( substr( $var, 0, 18 ) == 'smw_property_type_'){
				$xml .= '<Type>' . $val . '</Type>';
			} elseif ( substr( $var, 0, 16 ) == 'smw_linked_form_') {
				if ( $val !== '' ) {
					$xml .= '<LinkedForm>' . $val . '</LinkedForm>';
				}
			} elseif ( substr( $var, 0, 11 ) == 'smw_values_') {
				if ( $val !== '' ) {
					// replace the comma substitution character that has no chance of
					// being included in the values list - namely, the ASCII beep
					$listSeparator = ',';
					$allowed_values_str = str_replace( "\\$listSeparator", "\a", $val );
					$allowed_values_array = explode( $listSeparator, $allowed_values_str );
					foreach ( $allowed_values_array as $value ) {
						// replace beep back with comma, trim
						$value = str_replace( "\a", $listSeparator, trim( $value ) );
						$xml .= '<AllowedValue>' . $value . '</AllowedValue>';
					}
				}
				$xml .= '</semanticmediawiki_Property>';
				$xmlPerField[$fieldNum] = $xml;
			}
		}
		return $xmlPerField;
	}

	/**
	 * Returns the HTML necessary for getting information about the
	 * "connecting property" within the Page Schemas 'editschema' page.
	 */
	public static function getTemplateEditingHTML( $psTemplate) {
		// Only display this if the Semantic Internal Objects extension
		// isn't displaying something similar.
		if ( class_exists( 'SIOPageSchemas' ) ) {
			return null;
		}

		$prop_array = [];
		$hasExistingValues = false;
		if ( !is_null( $psTemplate ) ) {
			$prop_array = $psTemplate->getObject( 'semanticmediawiki_ConnectingProperty' );
			if ( !is_null( $prop_array ) ) {
				$hasExistingValues = true;
			}
		}
		$text = '<p>' . 'Name of property to connect this template\'s fields to the rest of the page:' . ' ' . '(should only be used if this template can have multiple instances)' . ' ';
		$propName = PageSchemas::getValueFromObject( $prop_array, 'name' );
		$text .= Html::input( 'smw_connecting_property_num', $propName, [ 'size' => 15 ] ) . "\n";

		return [ $text, $hasExistingValues ];
	}

	/**
	 * Returns the HTML necessary for getting information about a regular
	 * semantic property within the Page Schemas 'editschema' page.
	 */
	public static function getFieldEditingHTML( $psTemplateField ) {
		global $smwgContLang;

		$prop_array = [];
		$hasExistingValues = false;
		if ( !is_null( $psTemplateField ) ) {
			$prop_array = $psTemplateField->getObject('semanticmediawiki_Property');
			if ( !is_null( $prop_array ) ) {
				$hasExistingValues = true;
			}
		}
		$html_text = '<p>' . wfMessage( 'ps-optional-name' )->text() . ' ';
		$propName = PageSchemas::getValueFromObject( $prop_array, 'name' );
		$html_text .= Html::input( 'smw_property_name_num', $propName, [ 'size' => 15 ] ) . "\n";
		$propType = PageSchemas::getValueFromObject( $prop_array, 'Type' );
		$select_body = "";
		$datatype_labels = $smwgContLang->getDatatypeLabels();
		foreach ( $datatype_labels as $label ) {
			$optionAttrs = [];
			if ( $label == $propType) {
				$optionAttrs['selected'] = 'selected';
			}
			$select_body .= "\t" . Xml::element( 'option', $optionAttrs, $label ) . "\n";
		}
		$propertyDropdownAttrs = [
			'id' => 'property_dropdown',
			'name' => 'smw_property_type_num',
			'value' => $propType
		];
		$html_text .= "Type: " . Xml::tags( 'select', $propertyDropdownAttrs, $select_body ) . "</p>\n";

		// This can't be last, because of the hacky way the XML is
		// ocnstructed from this form's output.
		if ( defined( 'SF_VERSION' ) ) {
			$html_text .= '<p>' . wfMessage( 'sf_createproperty_linktoform' )->text() . ' ';
			$linkedForm = PageSchemas::getValueFromObject( $prop_array, 'LinkedForm' );
			$html_text .= Html::input( 'smw_linked_form_num', $linkedForm, [ 'size' => 15 ] ) . "\n";
			$html_text .= "(for Page properties only)</p>\n";
		}

		$html_text .= '<p>If you want this property to only be allowed to have certain values, enter the list of allowed values, separated by commas (if a value contains a comma, replace it with "\,"):</p>';
		$allowedValsInputAttrs = [
			'size' => 80
		];
		$allowedValues = PageSchemas::getValueFromObject( $prop_array, 'allowed_values' );
		if ( is_null( $allowedValues ) ) {
			$allowed_val_string = '';
		} else {
			$allowed_val_string = implode( ', ', $allowedValues );
		}
		$html_text .= '<p>' . Html::input( 'smw_values_num', $allowed_val_string, 'text', $allowedValsInputAttrs ) . "</p>\n";

		return [ $html_text, $hasExistingValues ];
	}

	/**
	 * Creates the property page for each property specified in the
	 * passed-in Page Schemas XML object.
	 */
	public static function generatePages( $pageSchemaObj, $selectedPages ) {
		global $smwgContLang, $wgUser;

		$datatypeLabels = $smwgContLang->getDatatypeLabels();
		$pageTypeLabel = $datatypeLabels['_wpg'];

		$jobs = [];
		$jobParams = [];
		$jobParams['user_id'] = $wgUser->getId();

		// First, create jobs for all "connecting properties".
		$psTemplates = $pageSchemaObj->getTemplates();
		foreach ( $psTemplates as $psTemplate ) {
			$smwConnectingPropertyName = self::getConnectingPropertyName( $psTemplate );
			if ( is_null( $smwConnectingPropertyName ) ) {
				continue;
			}
			$propTitle = Title::makeTitleSafe( SMW_NS_PROPERTY, $smwConnectingPropertyName );
			if ( !in_array( $propTitle, $selectedPages ) ) {
				continue;
			}

			$jobParams['page_text'] = self::createPropertyText( $pageTypeLabel, null, null );
			$jobs[] = new PSCreatePageJob( $propTitle, $jobParams );
		}

		// Second, create jobs for all regular properties.
		$propertyDataArray = self::getAllPropertyData( $pageSchemaObj );
		foreach ( $propertyDataArray as $propertyData ) {
			$propTitle = Title::makeTitleSafe( SMW_NS_PROPERTY, $propertyData['name'] );
			if ( !in_array( $propTitle, $selectedPages ) ) {
				continue;
			}
			$propertyType = array_key_exists( 'Type', $propertyData ) ? $propertyData['Type'] : null;
			$propertyAllowedValues = array_key_exists( 'allowed_values', $propertyData ) ? $propertyData['allowed_values'] : null;
			$propertyLinkedForm = array_key_exists( 'LinkedForm', $propertyData ) ? $propertyData['LinkedForm'] : null;
			$jobParams['page_text'] = self::createPropertyText( $propertyType, $propertyAllowedValues, $propertyLinkedForm );
			$jobs[] = new PSCreatePageJob( $propTitle, $jobParams );
		}
		if ( class_exists( 'JobQueueGroup' ) ) {
			JobQueueGroup::singleton()->push( $jobs );
		} else {
			// MW <= 1.20
			Job::batchInsert( $jobs );
		}
	}

	/**
	 * Creates the text for a property page.
	 */
	static public function createPropertyText( $propertyType, $allowedValues, $linkedForm = null ) {
		/**
		 * @var SMWLanguage $smwgContLang
		 */
		global $smwgContLang, $wgContLang;

		$propLabels = $smwgContLang->getPropertyLabels();
		$hasTypeLabel = $propLabels['_TYPE'];
		$typeTag = "[[$hasTypeLabel::$propertyType]]";
		$text = wfMessage( 'smw-createproperty-isproperty', $typeTag )->inContentLanguage()->text();

		if ( $linkedForm !== '' && defined( 'SF_VERSION' ) ) {
			global $sfgContLang;
			$sfPropLabels = $sfgContLang->getPropertyLabels();
			$defaultFormTag = "[[{$sfPropLabels[SF_SP_HAS_DEFAULT_FORM]}::$linkedForm]]";
			$text .= ' ' . wfMessage( 'sf_property_linkstoform', $defaultFormTag )->inContentLanguage()->text();
		}

		if ( $allowedValues != null) {
			$text .= "\n\n" . wfMessage( 'smw-createproperty-allowedvals', $wgContLang->formatNum( count( $allowedValues ) ) )->inContentLanguage()->text();

			foreach ( $allowedValues as $value ) {
				$prop_labels = $smwgContLang->getPropertyLabels();
				$text .= "\n* [[" . $prop_labels['_PVAL'] . "::$value]]";
			}
		}

		return $text;
	}

	/**
	 * Returns either the "connecting property", or a field property, based
	 * on the XML passed from the Page Schemas extension.
	*/
	public static function createPageSchemasObject( $tagName, $xml ) {
		if ( $tagName == "semanticmediawiki_ConnectingProperty" ) {
			foreach ( $xml->children() as $tag => $child ) {
				if ( $tag == $tagName ) {
					$smw_array = [];
					$propName = $child->attributes()->name;
					$smw_array['name'] = (string)$propName;
					foreach ( $child->children() as $prop => $value ) {
						$smw_array[$prop] = (string)$value;
					}
					return $smw_array;
				}
			}
		} elseif ( $tagName == "semanticmediawiki_Property" ) {
			foreach ( $xml->children() as $tag => $child ) {
				if ( $tag == $tagName ) {
					$smw_array = [];
					$propName = $child->attributes()->name;
					$smw_array['name'] = (string)$propName;
					$allowed_values = [];
					$count = 0;
					foreach ( $child->children() as $prop => $value ) {
						if ( $prop == "AllowedValue" ) {
							$allowed_values[$count++] = $value;
						} else {
							$smw_array[$prop] = (string)$value;
						}
					}
					$smw_array['allowed_values'] = $allowed_values;
					return $smw_array;
				}
			}
		}
		return null;
	}
}