r96910 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96909‎ | r96910 | r96911 >
Date:21:27, 12 September 2011
Author:yaron
Status:resolved (Comments)
Tags:
Comment:
Moved all functions related to the Page Schemas extensions into a new class, SMWPageSchemas, in a new file, SMW_PageSchemas.php
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_PageSchemas.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_PageSchemas.php
@@ -0,0 +1,205 @@
 2+<?php
 3+
 4+/**
 5+ * Functions for handling Semantic MediaWiki data within the Page Schemas
 6+ * extension.
 7+ *
 8+ * @author Ankit Garg
 9+ * @author Yaron Koren
 10+ * @file SMW_PageSchemas.php
 11+ * @ingroup SMW
 12+ */
 13+
 14+class SMWPageSchemas {
 15+
 16+ function parseFieldElements( $field_xml, &$text_object ) {
 17+ foreach ( $field_xml->children() as $tag => $child ) {
 18+ if ( $tag == "semanticmediawiki_Property" ) {
 19+ $text = "";
 20+ $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticMediaWiki", (string)$tag );
 21+ $propName = $child->attributes()->name;
 22+ // this means object has already been initialized by some other extension.
 23+ $text .= PageSchemas::tableMessageRowHTML( "paramAttrMsg", "name", (string)$propName );
 24+ foreach ( $child->children() as $prop => $value ) {
 25+ $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop, (string)$value );
 26+ }
 27+ $text_object['smw'] = $text;
 28+ }
 29+ }
 30+ return true;
 31+ }
 32+
 33+ function getPageList( $psSchemaObj , &$genPageList ) {
 34+ $template_all = $psSchemaObj->getTemplates();
 35+ foreach ( $template_all as $template ) {
 36+ $field_all = $template->getFields();
 37+ $field_count = 0; //counts the number of fields
 38+ foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
 39+ $field_count++;
 40+ $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
 41+ $prop_array = $smw_array['smw'];
 42+ if($prop_array != null){
 43+ $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_array['name'] );
 44+ $genPageList[] = $title;
 45+ }
 46+ }
 47+ }
 48+ return true;
 49+ }
 50+
 51+ function getFieldXML( $request, &$xmlArray ) {
 52+ $templateNum = -1;
 53+ $xmlPerField = array();
 54+ foreach ( $request->getValues() as $var => $val ) {
 55+ if ( substr( $var, 0, 18 ) == 'smw_property_name_' ) {
 56+ $templateNum = substr($var,18,1);
 57+ $xml = '<semanticmediawiki_Property name="'.$val.'" >';
 58+ } elseif ( substr( $var, 0, 18 ) == 'smw_property_type_'){
 59+ $xml .= '<Type>'.$val.'</Type>';
 60+ } elseif ( substr( $var, 0, 11 ) == 'smw_values_') {
 61+ if ( $val != '' ) {
 62+ // replace the comma substitution character that has no chance of
 63+ // being included in the values list - namely, the ASCII beep
 64+ $listSeparator = ',';
 65+ $allowed_values_str = str_replace( "\\$listSeparator", "\a", $val );
 66+ $allowed_values_array = explode( $listSeparator, $allowed_values_str );
 67+ foreach ( $allowed_values_array as $i => $value ) {
 68+ // replace beep back with comma, trim
 69+ $value = str_replace( "\a", $listSeparator, trim( $value ) );
 70+ $xml .= '<AllowedValue>'.$value.'</AllowedValue>';
 71+ }
 72+ }
 73+ $xml .= '</semanticmediawiki_Property>';
 74+ $xmlPerField[] = $xml;
 75+ }
 76+ }
 77+ $xmlArray['smw'] = $xmlPerField;
 78+ return true;
 79+ }
 80+
 81+ function getFieldHTML( $field, &$text_extensions ) {
 82+ global $smwgContLang;
 83+ $datatype_labels = $smwgContLang->getDatatypeLabels();
 84+ if ( !is_null( $field ) ) {
 85+ $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
 86+ $prop_array = $smw_array['smw'];
 87+ } else {
 88+ $prop_array = array();
 89+ }
 90+ $html_text = '<p>Property name: <input size="15" name="smw_property_name_num" value="'.$prop_array['name'].'" >' . "\n";
 91+ $select_body = "";
 92+ foreach ( $datatype_labels as $label ) {
 93+ $optionAttrs = array();
 94+ if ( $label == $prop_array['Type'] ) {
 95+ $optionAttrs['selected'] = 'selected';
 96+ }
 97+ $select_body .= "\t" . Xml::element( 'option', $optionAttrs, $label ) . "\n";
 98+ }
 99+ $propertyDropdownAttrs = array( 'id' => 'property_dropdown', 'name' => 'smw_property_type_num' );
 100+ if ( array_key_exists( 'Type', $prop_array ) ) {
 101+ $propertyDropdownAttrs['value'] = $prop_array['Type'];
 102+ }
 103+ $html_text .= "Type: " . Xml::tags( 'select', $propertyDropdownAttrs, $select_body ) . "</p>\n";
 104+ $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>';
 105+ $allowedValsInputAttrs = array(
 106+ 'size' => 80
 107+ );
 108+ if ( array_key_exists( 'allowed_value_array', $prop_array ) ) {
 109+ $allowed_val_string = implode( ', ', $prop_array['allowed_value_array'] );
 110+ } else {
 111+ $allowed_val_string = '';
 112+ }
 113+ $html_text .= '<p>' . Html::input( 'smw_values_num', $allowed_val_string, 'text', $allowedValsInputAttrs ) . "</p>\n";
 114+
 115+ $text_extensions['smw'] = array( 'Property', '#DEF', $html_text );
 116+
 117+ return true;
 118+ }
 119+
 120+ function generatePages( $psSchemaObj, $toGenPageList ) {
 121+ $template_all = $psSchemaObj->getTemplates();
 122+ foreach ( $template_all as $template ) {
 123+ $field_all = $template->getFields();
 124+ $field_count = 0; //counts the number of fields
 125+ foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
 126+ $field_count++;
 127+ $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
 128+ $prop_array = $smw_array['smw'];
 129+ if($prop_array != null){
 130+ $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_array['name'] );
 131+ $key_title = PageSchemas::titleString( $title );
 132+ if(in_array( $key_title, $toGenPageList )){
 133+ self::createProperty( $prop_array['name'], $prop_array['Type'], $prop_array['allowed_value_array'] ) ;
 134+ }
 135+ }
 136+ }
 137+ }
 138+ return true;
 139+ }
 140+
 141+ function createPropertyText( $property_type, $allowed_value_array ) {
 142+ global $smwgContLang;
 143+ $prop_labels = $smwgContLang->getPropertyLabels();
 144+ $type_tag = "[[{$prop_labels['_TYPE']}::$property_type]]";
 145+ $text = wfMsgForContent( 'ps-property-isproperty', $type_tag );
 146+ if ( $allowed_value_array != null) {
 147+ // replace the comma substitution character that has no chance of
 148+ // being included in the values list - namely, the ASCII beep
 149+ $text .= "\n\n" . wfMsgExt( 'ps-property-allowedvals', array( 'parsemag', 'content' ), count( $allowed_value_array ) );
 150+ foreach ( $allowed_value_array as $i => $value ) {
 151+ // replace beep back with comma, trim
 152+ $value = str_replace( "\a",',' , trim( $value ) );
 153+ if ( method_exists( $smwgContLang, 'getPropertyLabels' ) ) {
 154+ $prop_labels = $smwgContLang->getPropertyLabels();
 155+ $text .= "\n* [[" . $prop_labels['_PVAL'] . "::$value]]";
 156+ } else {
 157+ $spec_props = $smwgContLang->getSpecialPropertiesArray();
 158+ $text .= "\n* [[" . $spec_props[SMW_SP_POSSIBLE_VALUE] . "::$value]]";
 159+ }
 160+ }
 161+ }
 162+ return $text;
 163+ }
 164+
 165+ function createProperty( $prop_name, $prop_type, $allowed_value_array ) {
 166+ global $wgUser;
 167+ $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_name );
 168+ $text = self::createPropertyText( $prop_type, $allowed_value_array );
 169+ $jobs = array();
 170+ $params = array();
 171+ $params['user_id'] = $wgUser->getId();
 172+ $params['page_text'] = $text;
 173+ $jobs[] = new PSCreatePageJob( $title, $params );
 174+ Job::batchInsert( $jobs );
 175+ return true;
 176+ }
 177+
 178+ /**
 179+ * Returns the property based on the XML passed from the Page Schemas extension
 180+ */
 181+ function createPageSchemasObject( $objectName, $xmlForField, &$object ) {
 182+ $smw_array = array();
 183+ if ( $objectName == "semanticmediawiki_Property" ) {
 184+ foreach ( $xmlForField->children() as $tag => $child ) {
 185+ if ( $tag == $objectName ) {
 186+ $propName = $child->attributes()->name;
 187+ //this means object has already been initialized by some other extension.
 188+ $smw_array['name']=(string)$propName;
 189+ $allowed_value_array = array();
 190+ $count = 0;
 191+ foreach ( $child->children() as $prop => $value ) {
 192+ if ( $prop == "AllowedValue" ) {
 193+ $allowed_value_array[$count++] = $value;
 194+ } else {
 195+ $smw_array[$prop] = (string)$value;
 196+ }
 197+ }
 198+ $smw_array['allowed_value_array'] = $allowed_value_array;
 199+ }
 200+ }
 201+ $object['smw'] = $smw_array;
 202+ }
 203+ return true;
 204+ }
 205+}
 206+
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -90,14 +90,12 @@
9191 $wgHooks['SpecialVersionExtensionTypes'][] = 'smwfOldAddSemanticExtensionType';
9292 }
9393
94 - /* Hooks related to Pageschemas extension. TODO: Should be moved to separate file. */
95 - $wgHooks['PageSchemasGetObject'][] = 'smwfCreatePageSchemasObject' ; // Hook for returning PageSchema(extension) object from a given xml
96 - $wgHooks['PageSchemasGeneratePages'][] = 'smwfGeneratePages' ; // Hook for creating Pages
97 - $wgHooks['PageSchemasGetFieldHTML'][] = 'smwfGetFieldHTMLForPS' ; // Hook for retuning html text to PS schema
98 - $wgHooks['PageSchemasGetFieldXML'][] = 'smwfGetFieldXMLForPS';
99 - $wgHooks['PSParseFieldElements'][] = 'smwfParseFieldElements' ; // Hook for creating Pages
100 - $wgHooks['PageSchemasGetPageList'][] = 'smwfGetPageList' ; //Hook for creating Pages
101 - /* End: Hooks related to Pageschemas extension */
 94+ $wgHooks['PageSchemasGetObject'][] = 'SMWPageSchemas::createPageSchemasObject';
 95+ $wgHooks['PageSchemasGeneratePages'][] = 'SMWPageSchemas::generatePages';
 96+ $wgHooks['PageSchemasGetFieldHTML'][] = 'SMWPageSchemas::getFieldHTML';
 97+ $wgHooks['PageSchemasGetFieldXML'][] = 'SMWPageSchemas::getFieldXML';
 98+ $wgHooks['PSParseFieldElements'][] = 'SMWPageSchemas::parseFieldElements';
 99+ $wgHooks['PageSchemasGetPageList'][] = 'SMWPageSchemas::getPageList';
102100 }
103101
104102 /**
@@ -342,6 +340,9 @@
343341 //$wgAutoloadClasses['ApiSMWQuery'] = $smwgIP . 'includes/api/ApiSMWQuery.php';
344342 //$wgAPIModules['smwquery'] = 'ApiSMWQuery';
345343 $wgAutoloadClasses['ApiSMWInfo'] = $smwgIP . 'includes/api/ApiSMWInfo.php';
 344+
 345+ // Other extensions
 346+ $wgAutoloadClasses['SMWPageSchemas'] = $smwgIP . 'includes/SMW_PageSchemas.php';
346347 }
347348
348349 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r97000Follow-up to r96910 - removed now-obsolete Page Schemas-related functionsyaron20:58, 13 September 2011

Comments

#Comment by Nikerabbit (talk | contribs)   05:32, 13 September 2011

Moved? I see only added and changed code.

#Comment by Yaron Koren (talk | contribs)   20:49, 13 September 2011

Oops - you're right! I never deleted the old stuff.

Status & tagging log