r100012 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100011‎ | r100012 | r100013 >
Date:01:41, 17 October 2011
Author:yaron
Status:deferred
Tags:
Comment:
Modified to reflect changes in Page Schemas v0.3
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_PageSchemas.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_PageSchemas.php
@@ -10,41 +10,47 @@
1111 * @ingroup SMW
1212 */
1313
14 -class SMWPageSchemas {
 14+class SMWPageSchemas extends PSExtensionHandler {
1515
 16+ public static function getDisplayColor() {
 17+ return '#DEF';
 18+ }
 19+
 20+ public static function getFieldDisplayString() {
 21+ return 'Semantic property';
 22+ }
 23+
1624 /**
1725 * Returns the display info for the property (if any is defined)
1826 * for a single field in the Page Schemas XML.
1927 */
20 - function getPropertyDisplayInfo( $field_xml, &$text_object ) {
21 - foreach ( $field_xml->children() as $tag => $child ) {
 28+ public static function getFieldDisplayValues( $fieldXML ) {
 29+ foreach ( $fieldXML->children() as $tag => $child ) {
2230 if ( $tag == "semanticmediawiki_Property" ) {
2331 $propName = $child->attributes()->name;
2432 $values = array();
2533 foreach ( $child->children() as $prop => $value ) {
2634 $values[$prop] = (string)$value;
2735 }
28 - $text_object['smw'] = array( 'Semantic property', $propName, '#DEF', $values );
29 - break;
 36+ return array( $propName, $values );
3037 }
3138 }
32 - return true;
 39+ return null;
3340 }
3441
3542 /**
3643 * Returns the set of SMW property data from the entire page schema.
3744 */
38 - static function getAllPropertyData( $psSchemaObj ) {
 45+ static function getAllPropertyData( $pageSchemaObj ) {
3946 $propertyDataArray = array();
40 - $psTemplates = $psSchemaObj->getTemplates();
 47+ $psTemplates = $pageSchemaObj->getTemplates();
4148 foreach ( $psTemplates as $psTemplate ) {
4249 $psTemplateFields = $psTemplate->getFields();
4350 foreach ( $psTemplateFields as $psTemplateField ) {
44 - $smw_array = $psTemplateField->getObject('semanticmediawiki_Property');
45 - if ( !array_key_exists( 'smw', $smw_array ) ) {
 51+ $prop_array = $psTemplateField->getObject('semanticmediawiki_Property');
 52+ if ( empty( $prop_array ) ) {
4653 continue;
4754 }
48 - $prop_array = $smw_array['smw'];
4955 if ( !array_key_exists( 'name', $prop_array ) ) {
5056 continue;
5157 }
@@ -59,25 +65,28 @@
6066
6167 /**
6268 * Sets the list of property pages defined by the passed-in
63 - * Page Schemas XML object.
 69+ * Page Schemas object.
6470 */
65 - function getPageList( $psSchemaObj , &$genPageList ) {
66 - $propertyDataArray = self::getAllPropertyData( $psSchemaObj );
 71+ public static function getPagesToGenerate( $pageSchemaObj ) {
 72+ $pagesToGenerate = array();
 73+ $propertyDataArray = self::getAllPropertyData( $pageSchemaObj );
6774 foreach ( $propertyDataArray as $propertyData ) {
6875 $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $propertyData['name'] );
69 - $genPageList[] = $title;
 76+ $pagesToGenerate[] = $title;
7077 }
71 - return true;
 78+ return $pagesToGenerate;
7279 }
7380
7481 /**
7582 * Constructs XML for the SMW property, based on what was submitted
7683 * in the 'edit schema' form.
7784 */
78 - function getFieldXML( $request, &$xmlArray ) {
 85+ public static function createFieldXMLFromForm() {
 86+ global $wgRequest;
 87+
7988 $fieldNum = -1;
8089 $xmlPerField = array();
81 - foreach ( $request->getValues() as $var => $val ) {
 90+ foreach ( $wgRequest->getValues() as $var => $val ) {
8291 if ( substr( $var, 0, 18 ) == 'smw_property_name_' ) {
8392 $fieldNum = substr( $var, 18 );
8493 $xml = '<semanticmediawiki_Property name="' . $val . '" >';
@@ -100,38 +109,28 @@
101110 $xmlPerField[$fieldNum] = $xml;
102111 }
103112 }
104 - $xmlArray['smw'] = $xmlPerField;
105 - return true;
 113+ return $xmlPerField;
106114 }
107115
108116 /**
109117 * Returns the HTML necessary for getting information about the
110118 * semantic property within the Page Schemas 'editschema' page.
111119 */
112 - function getFieldHTML( $field, &$fieldHTMLFromExtensions ) {
 120+ public static function getFieldEditingHTML( $psTemplateField ) {
113121 global $smwgContLang;
114122
115123 $prop_array = array();
116124 $hasExistingValues = false;
117 - if ( !is_null( $field ) ) {
118 - $smw_array = $field->getObject('semanticmediawiki_Property');
119 - if ( array_key_exists( 'smw', $smw_array ) ) {
120 - $prop_array = $smw_array['smw'];
 125+ if ( !is_null( $psTemplateField ) ) {
 126+ $prop_array = $psTemplateField->getObject('semanticmediawiki_Property');
 127+ if ( !is_null( $prop_array ) ) {
121128 $hasExistingValues = true;
122129 }
123130 }
124131 $html_text = '<p>' . wfMsg( 'ps-optional-name' ) . ' ';
125 - if ( array_key_exists( 'name', $prop_array ) ) {
126 - $propName = $prop_array['name'];
127 - } else {
128 - $propName = null;
129 - }
 132+ $propName = PageSchemas::getValueFromObject( $prop_array, 'name' );
130133 $html_text .= Html::input( 'smw_property_name_num', $propName, array( 'size' => 15 ) ) . "\n";
131 - if ( array_key_exists( 'Type', $prop_array ) ) {
132 - $propType = $prop_array['Type'];
133 - } else {
134 - $propType = null;
135 - }
 134+ $propType = PageSchemas::getValueFromObject( $prop_array, 'Type' );
136135 $select_body = "";
137136 $datatype_labels = $smwgContLang->getDatatypeLabels();
138137 foreach ( $datatype_labels as $label ) {
@@ -151,50 +150,51 @@
152151 $allowedValsInputAttrs = array(
153152 'size' => 80
154153 );
155 - if ( array_key_exists( 'allowed_values', $prop_array ) ) {
156 - $allowed_val_string = implode( ', ', $prop_array['allowed_values'] );
157 - } else {
 154+ $allowedValues = PageSchemas::getValueFromObject( $prop_array, 'allowed_values' );
 155+ if ( is_null( $allowedValues ) ) {
158156 $allowed_val_string = '';
 157+ } else {
 158+ $allowed_val_string = implode( ', ', $allowedValues );
159159 }
160160 $html_text .= '<p>' . Html::input( 'smw_values_num', $allowed_val_string, 'text', $allowedValsInputAttrs ) . "</p>\n";
161161
162 - $fieldHTMLFromExtensions['smw'] = array( 'Semantic property', '#DEF', $html_text, $hasExistingValues );
163 -
164 - return true;
 162+ return array( $html_text, $hasExistingValues );
165163 }
166164
167165 /**
168166 * Creates the property page for each property specified in the
169167 * passed-in Page Schemas XML object.
170168 */
171 - function generatePages( $psSchemaObj, $selectedPageList ) {
172 - $propertyDataArray = self::getAllPropertyData( $psSchemaObj );
 169+ public static function generatePages( $pageSchemaObj, $selectedPages ) {
 170+ global $wgUser;
 171+
 172+ $jobs = array();
 173+ $jobParams = array();
 174+ $jobParams['user_id'] = $wgUser->getId();
 175+ $propertyDataArray = self::getAllPropertyData( $pageSchemaObj );
173176 foreach ( $propertyDataArray as $propertyData ) {
174 - $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $propertyData['name'] );
175 - if ( !in_array( $title, $selectedPageList ) ) {
 177+ $propTitle = Title::makeTitleSafe( SMW_NS_PROPERTY, $propertyData['name'] );
 178+ if ( !in_array( $propTitle, $selectedPages ) ) {
176179 continue;
177180 }
178 - self::createProperty( $propertyData['name'], $propertyData['Type'], $propertyData['allowed_values'] );
 181+ $jobParams['page_text'] = self::createPropertyText( $propertyData['Type'], $propertyData['allowed_values'] );
 182+ $jobs[] = new PSCreatePageJob( $propTitle, $jobParams );
179183 }
180 - return true;
 184+ Job::batchInsert( $jobs );
181185 }
182186
183187 /**
184188 * Creates the text for a property page.
185189 */
186 - function createPropertyText( $property_type, $allowed_values ) {
 190+ function createPropertyText( $propertyType, $allowedValues ) {
187191 global $smwgContLang;
188 - $prop_labels = $smwgContLang->getPropertyLabels();
189 - $type_tag = "[[{$prop_labels['_TYPE']}::$property_type]]";
190 - $text = wfMsgForContent( 'ps-property-isproperty', $type_tag );
191 - if ( $allowed_values != null) {
192 - // Replace the comma with a substitution character
193 - // that has no chance of being included in the values
194 - // list - namely, the ASCII beep.
195 - $text .= "\n\n" . wfMsgExt( 'ps-property-allowedvals', array( 'parsemag', 'content' ), count( $allowed_values ) );
196 - foreach ( $allowed_values as $i => $value ) {
197 - // Replace beep back with comma, trim.
198 - $value = str_replace( "\a",',' , trim( $value ) );
 192+ $propLabels = $smwgContLang->getPropertyLabels();
 193+ $hasTypeLabel = $propLabels['_TYPE'];
 194+ $typeTag = "[[$hasTypeLabel::$propertyType]]";
 195+ $text = wfMsgForContent( 'ps-property-isproperty', $typeTag );
 196+ if ( $allowedValues != null) {
 197+ $text .= "\n\n" . wfMsgExt( 'ps-property-allowedvals', array( 'parsemag', 'content' ), count( $allowedValues ) );
 198+ foreach ( $allowedValues as $i => $value ) {
199199 if ( method_exists( $smwgContLang, 'getPropertyLabels' ) ) {
200200 $prop_labels = $smwgContLang->getPropertyLabels();
201201 $text .= "\n* [[" . $prop_labels['_PVAL'] . "::$value]]";
@@ -207,28 +207,15 @@
208208 return $text;
209209 }
210210
211 - function createProperty( $prop_name, $prop_type, $allowed_values ) {
212 - global $wgUser;
213 -
214 - $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_name );
215 - $params = array();
216 - $params['user_id'] = $wgUser->getId();
217 - $params['page_text'] = self::createPropertyText( $prop_type, $allowed_values );
218 - $jobs = array();
219 - $jobs[] = new PSCreatePageJob( $title, $params );
220 - Job::batchInsert( $jobs );
221 - return true;
222 - }
223 -
224211 /**
225212 * Returns the property based on the XML passed from the Page Schemas
226213 * extension.
227214 */
228 - function createPageSchemasObject( $objectName, $xmlForField, &$object ) {
229 - $smw_array = array();
230 - if ( $objectName == "semanticmediawiki_Property" ) {
231 - foreach ( $xmlForField->children() as $tag => $child ) {
232 - if ( $tag == $objectName ) {
 215+ public static function createPageSchemasObject( $tagName, $xml ) {
 216+ if ( $tagName == "semanticmediawiki_Property" ) {
 217+ foreach ( $xml->children() as $tag => $child ) {
 218+ if ( $tag == $tagName ) {
 219+ $smw_array = array();
233220 $propName = $child->attributes()->name;
234221 $smw_array['name'] = (string)$propName;
235222 $allowed_values = array();
@@ -241,11 +228,10 @@
242229 }
243230 }
244231 $smw_array['allowed_values'] = $allowed_values;
245 - $object['smw'] = $smw_array;
246 - return true;
 232+ return $smw_array;
247233 }
248234 }
249235 }
250 - return true;
 236+ return null;
251237 }
252238 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -73,6 +73,11 @@
7474 return true;
7575 }
7676
 77+// Register class with the Page Schemas extension
 78+if ( isset( $wgPageSchemasHandlerClasses ) ) {
 79+ $wgPageSchemasHandlerClasses[] = 'SMWPageSchemas';
 80+}
 81+
7782 /**
7883 * Register all SMW hooks with MediaWiki.
7984 */
@@ -93,13 +98,6 @@
9499 // For pre-MediaWiki 1.17 alpha.
95100 $wgHooks['SpecialVersionExtensionTypes'][] = 'smwfOldAddSemanticExtensionType';
96101 }
97 -
98 - $wgHooks['PageSchemasGetObject'][] = 'SMWPageSchemas::createPageSchemasObject';
99 - $wgHooks['PageSchemasGeneratePages'][] = 'SMWPageSchemas::generatePages';
100 - $wgHooks['PageSchemasGetFieldHTML'][] = 'SMWPageSchemas::getFieldHTML';
101 - $wgHooks['PageSchemasGetFieldXML'][] = 'SMWPageSchemas::getFieldXML';
102 - $wgHooks['PageSchemasGetFieldDisplayInfo'][] = 'SMWPageSchemas::getPropertyDisplayInfo';
103 - $wgHooks['PageSchemasGetPageList'][] = 'SMWPageSchemas::getPageList';
104102 }
105103
106104 /**