r96901 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96900‎ | r96901 | r96902 >
Date:20:51, 12 September 2011
Author:yaron
Status:deferred
Tags:
Comment:
Rewrote some of the Page Schemas-interfacing code: names and structure of the hooks changed; also fixed some whitespace formatting
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -2,7 +2,7 @@
33
44 /**
55 * Global functions used for setting up the Semantic MediaWiki extension.
6 - *
 6+ *
77 * @file SMW_Setup.php
88 * @ingroup SMW
99 */
@@ -81,7 +81,7 @@
8282 $wgHooks['AdminLinks'][] = 'smwfAddToAdminLinks';
8383 $wgHooks['ParserFirstCallInit'][] = 'SMWSMWDoc::staticInit';
8484 $wgHooks['LanguageGetMagic'][] = 'SMWSMWDoc::staticMagic';
85 -
 85+
8686 if ( version_compare( $wgVersion, '1.17alpha', '>=' ) ) {
8787 // For MediaWiki 1.17 alpha and later.
8888 $wgHooks['ExtensionTypes'][] = 'smwfAddSemanticExtensionType';
@@ -90,11 +90,10 @@
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
 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
9696 $wgHooks['PageSchemasGeneratePages'][] = 'smwfGeneratePages' ; // Hook for creating Pages
97 - $wgHooks['getHtmlTextForFieldInputs'][] = 'smwfgetHtmlTextForPS' ; // Hook for retuning html text to PS schema
98 - $wgHooks['getFilledHtmlTextForFieldInputs'][] = 'smwfgetFilledHtmlTextForPS' ; //Hook for retuning html text to PS schema
 97+ $wgHooks['PageSchemasGetFieldHTML'][] = 'smwfGetFieldHTMLForPS' ; // Hook for retuning html text to PS schema
9998 $wgHooks['PageSchemasGetFieldXML'][] = 'smwfGetFieldXMLForPS';
10099 $wgHooks['PSParseFieldElements'][] = 'smwfParseFieldElements' ; // Hook for creating Pages
101100 $wgHooks['PageSchemasGetPageList'][] = 'smwfGetPageList' ; //Hook for creating Pages
@@ -112,18 +111,18 @@
113112 'remoteBasePath' => $smwgScriptPath,
114113 'group' => 'ext.smw'
115114 );
116 -
 115+
117116 $wgResourceModules['ext.smw'] = $moduleTemplate + array(
118117 'scripts' => array(
119118 'resources/ext.smw.js',
120119 'resources/ext.smw.compat.js',
121120 ),
122121 );
123 -
 122+
124123 $wgResourceModules['ext.smw.style'] = $moduleTemplate + array(
125124 'styles' => 'skins/SMW_custom.css'
126125 );
127 -
 126+
128127 $wgResourceModules['ext.smw.tooltips'] = $moduleTemplate + array(
129128 'scripts' => 'skins/SMW_tooltip.js',
130129 'dependencies' => array(
@@ -165,7 +164,7 @@
166165 );
167166 $wgResourceModules['jquery.ui.dialog'] = $moduleTemplate + array(
168167 'scripts' => 'libs/jquery-ui/jquery.ui.dialog.min.js',
169 - 'dependencies' => array( 'jquery.ui.core', 'jquery.ui.widget', 'jquery.ui.position',
 168+ 'dependencies' => array( 'jquery.ui.core', 'jquery.ui.widget', 'jquery.ui.position',
170169 'jquery.ui.button' /*, 'jquery.ui.draggable', 'jquery.ui.mouse', 'jquery.ui.resizable'*/ )
171170 );
172171 }
@@ -264,7 +263,7 @@
265264 // Parameter classes
266265 $parDir = $smwgIP . 'includes/params/';
267266 $wgAutoloadClasses['SMWParamFormat'] = $parDir . 'SMW_ParamFormat.php';
268 -
 267+
269268 // Parser hooks
270269 $phDir = $smwgIP . 'includes/parserhooks/';
271270 $wgAutoloadClasses['SMWAsk'] = $phDir . 'SMW_Ask.php';
@@ -275,7 +274,7 @@
276275 $wgAutoloadClasses['SMWSetRecurringEvent'] = $phDir . 'SMW_SetRecurringEvent.php';
277276 $wgAutoloadClasses['SMWDeclare'] = $phDir . 'SMW_Declare.php';
278277 $wgAutoloadClasses['SMWSMWDoc'] = $phDir . 'SMW_SMWDoc.php';
279 -
 278+
280279 // Stores & queries
281280 $wgAutoloadClasses['SMWQueryProcessor'] = $smwgIP . 'includes/SMW_QueryProcessor.php';
282281 $wgAutoloadClasses['SMWQueryParser'] = $smwgIP . 'includes/SMW_QueryParser.php';
@@ -614,13 +613,13 @@
615614 foreach ( $field_xml->children() as $tag => $child ) {
616615 if ( $tag == "semanticmediawiki_Property" ) {
617616 $text = "";
618 - $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticMediaWiki", (string)$tag );
619 - $propName = $child->attributes()->name;
620 - //this means object has already been initialized by some other extension.
621 - $text .= PageSchemas::tableMessageRowHTML( "paramAttrMsg", "name", (string)$propName );
622 - foreach ( $child->children() as $prop => $value ) {
623 - $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop, (string)$value );
624 - }
 617+ $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticMediaWiki", (string)$tag );
 618+ $propName = $child->attributes()->name;
 619+ //this means object has already been initialized by some other extension.
 620+ $text .= PageSchemas::tableMessageRowHTML( "paramAttrMsg", "name", (string)$propName );
 621+ foreach ( $child->children() as $prop => $value ) {
 622+ $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop, (string)$value );
 623+ }
625624 $text_object['smw']=$text;
626625 }
627626 }
@@ -632,7 +631,7 @@
633632 foreach ( $template_all as $template ) {
634633 $field_all = $template->getFields();
635634 $field_count = 0; //counts the number of fields
636 - foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
 635+ foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
637636 $field_count++;
638637 $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
639638 $prop_array = $smw_array['smw'];
@@ -675,73 +674,53 @@
676675 return true;
677676 }
678677
679 -function smwfgetFilledHtmlTextForPS( $pageSchemaObj, &$text_extensions ){
 678+function smwfGetFieldHTMLForPS( $field, &$text_extensions ) {
680679 global $smwgContLang;
681680 $datatype_labels = $smwgContLang->getDatatypeLabels();
682 - $html_text = "";
683 - $template_all = $pageSchemaObj->getTemplates();
684 - $html_text_array = array();
685 - foreach ( $template_all as $template ) {
686 - $field_all = $template->getFields();
687 - $field_count = 0; //counts the number of fields
688 - foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
689 - $field_count++;
690 - $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
691 - $prop_array = $smw_array['smw'];
692 - if($prop_array != null){
693 - $html_text .= '<fieldset style="background: #DEF;"><legend>Property</legend>';
694 - $html_text .= '<p> Property Name: <input size="15" name="smw_property_name_starter" value="'.$prop_array['name'].'" >Type: ';
695 - $select_body = "";
696 - foreach ( $datatype_labels as $label ) {
697 - if( $label == $prop_array['Type'] ){
698 - $select_body .= " " . '<option selected>'.$label.'</option>' . "\n";
699 - }else{
700 - $select_body .= " " . Xml::element( 'option', null, $label ) . "\n";
701 - }
702 - }
703 - $html_text .= Xml::tags( 'select', array( 'id' => 'property_dropdown', 'name' => 'smw_property_type_starter','value' =>$prop_array['Type'] ), $select_body );
704 - $html_text .= ' </p>
705 - <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>';
706 - $allowed_val_string = "";
707 - foreach( $prop_array['allowed_value_array'] as $allowed_value ){
708 - $allowed_val_string .= $allowed_value.', ';
709 - }
710 -
711 - $html_text .= '<p><input name="smw_values_starter" size="80" value="'.$allowed_val_string.'" ></p></fieldset>';
712 - $html_text_array[] = $html_text;
713 - }
714 - }
 681+ if ( !is_null( $field ) ) {
 682+ $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
 683+ $prop_array = $smw_array['smw'];
 684+ } else {
 685+ $prop_array = array();
715686 }
716 - $text_extensions['smw'] = $html_text_array;
717 - return true;
718 -
719 -}
720 -function smwfgetHtmlTextForPS( &$js_extensions ,&$text_extensions ) {
721 - global $smwgContLang;
722 - $datatype_labels = $smwgContLang->getDatatypeLabels();
723 - $html_text = "";
724 - $html_text .= '<fieldset style="background: #DEF;"><legend>Property</legend>
725 - <p> Property Name: <input size="15" name="smw_property_name_starter">Type: ';
 687+ $html_text = '<p>Property name: <input size="15" name="smw_property_name_num" value="'.$prop_array['name'].'" >' . "\n";
726688 $select_body = "";
727689 foreach ( $datatype_labels as $label ) {
728 - $select_body .= " " . Xml::element( 'option', null, $label ) . "\n";
 690+ $optionAttrs = array();
 691+ if ( $label == $prop_array['Type'] ) {
 692+ $optionAttrs['selected'] = 'selected';
 693+ }
 694+ $select_body .= "\t" . Xml::element( 'option', $optionAttrs, $label ) . "\n";
729695 }
730 - $html_text .= Xml::tags( 'select', array( 'id' => 'property_dropdown', 'name' => 'smw_property_type_starter' ), $select_body );
731 - $html_text .= ' </p>
732 - <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>
733 - <p><input value="" name="smw_values_starter" size="80"></p></fieldset>';
 696+ $propertyDropdownAttrs = array( 'id' => 'property_dropdown', 'name' => 'smw_property_type_num' );
 697+ if ( array_key_exists( 'Type', $prop_array ) ) {
 698+ $propertyDropdownAttrs['value'] = $prop_array['Type'];
 699+ }
 700+ $html_text .= "Type: " . Xml::tags( 'select', $propertyDropdownAttrs, $select_body ) . "</p>\n";
 701+ $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>';
 702+ $allowedValsInputAttrs = array(
 703+ 'size' => 80
 704+ );
 705+ if ( array_key_exists( 'allowed_value_array', $prop_array ) ) {
 706+ $allowed_val_string = implode( ', ', $prop_array['allowed_value_array'] );
 707+ } else {
 708+ $allowed_val_string = '';
 709+ }
 710+ $html_text .= '<p>' . Html::input( 'smw_values_num', $allowed_val_string, 'text', $allowedValsInputAttrs ) . "</p>\n";
734711
735 - $text_extensions['smw'] = $html_text;
 712+ $text_extensions['smw'] = array( 'Property', '#DEF', $html_text );
 713+
736714 return true;
737715 }
 716+
738717 function smwfGeneratePages( $psSchemaObj, $toGenPageList ) {
739718 $template_all = $psSchemaObj->getTemplates();
740719 foreach ( $template_all as $template ) {
741720 $field_all = $template->getFields();
742721 $field_count = 0; //counts the number of fields
743 - foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
 722+ foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
744723 $field_count++;
745 - $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
 724+ $smw_array = $field->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
746725 $prop_array = $smw_array['smw'];
747726 if($prop_array != null){
748727 $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_array['name'] );
@@ -754,34 +733,35 @@
755734 }
756735 return true;
757736 }
 737+
758738 function smwfCreatePropertyText( $property_type, $allowed_value_array ) {
759 - global $smwgContLang;
760 - $prop_labels = $smwgContLang->getPropertyLabels();
761 - $type_tag = "[[{$prop_labels['_TYPE']}::$property_type]]";
762 - $text = wfMsgForContent( 'ps-property-isproperty', $type_tag );
763 - if ( $allowed_value_array != null) {
764 - // replace the comma substitution character that has no chance of
765 - // being included in the values list - namely, the ASCII beep
766 - $text .= "\n\n" . wfMsgExt( 'ps-property-allowedvals', array( 'parsemag', 'content' ), count( $allowed_value_array ) );
767 - foreach ( $allowed_value_array as $i => $value ) {
768 - // replace beep back with comma, trim
769 - $value = str_replace( "\a",',' , trim( $value ) );
770 - if ( method_exists( $smwgContLang, 'getPropertyLabels' ) ) {
771 - $prop_labels = $smwgContLang->getPropertyLabels();
772 - $text .= "\n* [[" . $prop_labels['_PVAL'] . "::$value]]";
773 - } else {
774 - $spec_props = $smwgContLang->getSpecialPropertiesArray();
775 - $text .= "\n* [[" . $spec_props[SMW_SP_POSSIBLE_VALUE] . "::$value]]";
776 - }
 739+ global $smwgContLang;
 740+ $prop_labels = $smwgContLang->getPropertyLabels();
 741+ $type_tag = "[[{$prop_labels['_TYPE']}::$property_type]]";
 742+ $text = wfMsgForContent( 'ps-property-isproperty', $type_tag );
 743+ if ( $allowed_value_array != null) {
 744+ // replace the comma substitution character that has no chance of
 745+ // being included in the values list - namely, the ASCII beep
 746+ $text .= "\n\n" . wfMsgExt( 'ps-property-allowedvals', array( 'parsemag', 'content' ), count( $allowed_value_array ) );
 747+ foreach ( $allowed_value_array as $i => $value ) {
 748+ // replace beep back with comma, trim
 749+ $value = str_replace( "\a",',' , trim( $value ) );
 750+ if ( method_exists( $smwgContLang, 'getPropertyLabels' ) ) {
 751+ $prop_labels = $smwgContLang->getPropertyLabels();
 752+ $text .= "\n* [[" . $prop_labels['_PVAL'] . "::$value]]";
 753+ } else {
 754+ $spec_props = $smwgContLang->getSpecialPropertiesArray();
 755+ $text .= "\n* [[" . $spec_props[SMW_SP_POSSIBLE_VALUE] . "::$value]]";
777756 }
778757 }
779 - return $text;
780758 }
 759+ return $text;
 760+}
 761+
781762 function smwfCreateProperty( $prop_name, $prop_type, $allowed_value_array ) {
782763 global $wgUser;
783764 $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_name );
784765 $text = smwfCreatePropertyText( $prop_type, $allowed_value_array );
785 - #$text = "This is a property of type [[Has type:Number]].";
786766 $jobs = array();
787767 $params = array();
788768 $params['user_id'] = $wgUser->getId();
@@ -792,14 +772,14 @@
793773 }
794774
795775 /**
796 -* Function to return the Property based on the xml passed from the PageSchema extension
 776+* Function to return the Property based on the xml passed from the PageSchema extension
797777 */
798778 function smwfCreatePageSchemasObject( $objectName, $xmlForField, &$object ) {
799779 $smw_array = array();
800780 if ( $objectName == "semanticmediawiki_Property" ) {
801781 foreach ( $xmlForField->children() as $tag => $child ) {
802782 if ( $tag == $objectName ) {
803 - $propName = $child->attributes()->name;
 783+ $propName = $child->attributes()->name;
804784 //this means object has already been initialized by some other extension.
805785 $smw_array['name']=(string)$propName;
806786 $allowed_value_array = array();
@@ -872,7 +852,7 @@
873853 * This hook registers parser functions and hooks to the given parser. It is
874854 * called during SMW initialisation. Note that parser hooks are something different
875855 * than MW hooks in general, which explains the two-level registration.
876 - *
 856+ *
877857 * @since 1.5.3
878858 */
879859 function smwfRegisterParserFunctions( Parser &$parser ) {