| Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php |
| — | — | @@ -57,6 +57,13 @@ |
| 58 | 58 | |
| 59 | 59 | $wgExtensionFunctions[] = 'smwfSetupExtension'; |
| 60 | 60 | // FIXME: Can be removed when new style magic words are used (introduced in r52503) |
| | 61 | + $wgHooks['PageSchemasGetObject'][] = 'smwfCreatePageSchemasObject' ; //Hook for returning PageSchema(extension) object from a given xml |
| | 62 | + $wgHooks['PageSchemasGeneratePages'][] = 'smwfGeneratePages' ; //Hook for creating Pages |
| | 63 | + $wgHooks['getHtmlTextForFieldInputs'][] = 'smwfgetHtmlTextForPS' ; //Hook for retuning html text to PS schema |
| | 64 | + $wgHooks['getFilledHtmlTextForFieldInputs'][] = 'smwfgetFilledHtmlTextForPS' ; //Hook for retuning html text to PS schema |
| | 65 | + $wgHooks['getXmlTextForFieldInputs'][] = 'smwfgetXMLTextForPS' ; //Hook for retuning html text to PS schema |
| | 66 | + $wgHooks['PSParseFieldElements'][] = 'smwfParseFieldElements' ; //Hook for creating Pages |
| | 67 | + $wgHooks['PageSchemasGetPageList'][] = 'smwfGetPageList' ; //Hook for creating Pages |
| 61 | 68 | $wgHooks['LanguageGetMagic'][] = 'smwfAddMagicWords'; // setup names for parser functions (needed here) |
| 62 | 69 | $wgExtensionMessagesFiles['SemanticMediaWiki'] = $smwgIP . 'languages/SMW_Messages.php'; // register messages (requires MW=>1.11) |
| 63 | 70 | |
| — | — | @@ -526,7 +533,216 @@ |
| 527 | 534 | /***** language settings *****/ |
| 528 | 535 | /**********************************************/ |
| 529 | 536 | |
| | 537 | +function smwfParseFieldElements( $field_xml, &$text_object ) { |
| | 538 | + foreach ( $field_xml->children() as $tag => $child ) { |
| | 539 | + if ( $tag == "Property" ) { |
| | 540 | + $text = ""; |
| | 541 | + $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticMediaWiki", (string)$tag ); |
| | 542 | + $propName = $child->attributes()->name; |
| | 543 | + //this means object has already been initialized by some other extension. |
| | 544 | + $text .= PageSchemas::tableMessageRowHTML( "paramAttrMsg", "name", (string)$propName ); |
| | 545 | + foreach ( $child->children() as $prop => $value ) { |
| | 546 | + $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop, (string)$value ); |
| | 547 | + } |
| | 548 | + $text_object['smw']=$text; |
| | 549 | + } |
| | 550 | + } |
| | 551 | + return true; |
| | 552 | +} |
| | 553 | +function smwfGetPageList( $psSchemaObj , &$genPageList ) { |
| | 554 | + $template_all = $psSchemaObj->getTemplates(); |
| | 555 | + foreach ( $template_all as $template ) { |
| | 556 | + $field_all = $template->getFields(); |
| | 557 | + $field_count = 0; //counts the number of fields |
| | 558 | + foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type |
| | 559 | + $field_count++; |
| | 560 | + $smw_array = $field->getObject('Property'); //this returns an array with property values filled |
| | 561 | + $prop_array = $smw_array['smw']; |
| | 562 | + if($prop_array != null){ |
| | 563 | + $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_array['name'] ); |
| | 564 | + $genPageList[] = $title; |
| | 565 | + } |
| | 566 | + } |
| | 567 | + } |
| | 568 | + return true; |
| | 569 | +} |
| | 570 | +function smwfgetXMLTextForPS( $wgRequest, &$text_extensions ){ |
| | 571 | + |
| | 572 | + $Xmltext = ""; |
| | 573 | + $templateNum = -1; |
| | 574 | + $xml_text_array = array(); |
| | 575 | + foreach ( $wgRequest->getValues() as $var => $val ) { |
| | 576 | + if(substr($var,0,18) == 'smw_property_name_'){ |
| | 577 | + $templateNum = substr($var,18,1); |
| | 578 | + $Xmltext .= '<semanticmediawiki:Property name="'.$val.'">'; |
| | 579 | + }else if(substr($var,0,18) == 'smw_property_type_'){ |
| | 580 | + $Xmltext .= '<Type>'.$val.'</Type>'; |
| | 581 | + }else if(substr($var,0,11) == 'smw_values_'){ |
| | 582 | + if ( $val != '' ) { |
| | 583 | + // replace the comma substitution character that has no chance of |
| | 584 | + // being included in the values list - namely, the ASCII beep |
| | 585 | + $listSeparator = ','; |
| | 586 | + $allowed_values_str = str_replace( "\\$listSeparator", "\a", $val ); |
| | 587 | + $allowed_values_array = explode( $listSeparator, $allowed_values_str ); |
| | 588 | + foreach ( $allowed_values_array as $i => $value ) { |
| | 589 | + // replace beep back with comma, trim |
| | 590 | + $value = str_replace( "\a", $listSeparator, trim( $value ) ); |
| | 591 | + $Xmltext .= '<AllowedValue>'.$value.'</AllowedValue>'; |
| | 592 | + } |
| | 593 | + } |
| | 594 | + $Xmltext .= '</semanticmediawiki:Property>'; |
| | 595 | + $xml_text_array[] = $Xmltext; |
| | 596 | + $Xmltext = ''; |
| | 597 | + } |
| | 598 | + } |
| | 599 | + $text_extensions['smw'] = $xml_text_array; |
| | 600 | + return true; |
| | 601 | +} |
| | 602 | +function smwfgetFilledHtmlTextForPS( $pageSchemaObj, &$text_extensions ){ |
| | 603 | + global $smwgContLang; |
| | 604 | + $datatype_labels = $smwgContLang->getDatatypeLabels(); |
| | 605 | + $html_text = ""; |
| | 606 | + $template_all = $pageSchemaObj->getTemplates(); |
| | 607 | + $html_text_array = array(); |
| | 608 | + foreach ( $template_all as $template ) { |
| | 609 | + $field_all = $template->getFields(); |
| | 610 | + $field_count = 0; //counts the number of fields |
| | 611 | + foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type |
| | 612 | + $field_count++; |
| | 613 | + $smw_array = $field->getObject('Property'); //this returns an array with property values filled |
| | 614 | + $prop_array = $smw_array['smw']; |
| | 615 | + if($prop_array != null){ |
| | 616 | + $html_text .= '<fieldset style="background: #00FFFF;"><legend>Property</legend>'; |
| | 617 | + $html_text .= '<p> Property Name: <input size="15" name="smw_property_name_starter" value="'.$prop_array['name'].'" >Type: '; |
| | 618 | + $select_body = ""; |
| | 619 | + foreach ( $datatype_labels as $label ) { |
| | 620 | + if( $label == $prop_array['Type'] ){ |
| | 621 | + $select_body .= " " . '<option selected>'.$label.'</option>' . "\n"; |
| | 622 | + }else{ |
| | 623 | + $select_body .= " " . Xml::element( 'option', null, $label ) . "\n"; |
| | 624 | + } |
| | 625 | + } |
| | 626 | + $html_text .= Xml::tags( 'select', array( 'id' => 'property_dropdown', 'name' => 'smw_property_type_starter','value' =>$prop_array['Type'] ), $select_body ); |
| | 627 | + $html_text .= ' </p> |
| | 628 | + <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>'; |
| | 629 | + $allowed_val_string = ""; |
| | 630 | + foreach( $prop_array['allowed_value_array'] as $allowed_value ){ |
| | 631 | + $allowed_val_string .= $allowed_value.', '; |
| | 632 | + } |
| | 633 | + |
| | 634 | + $html_text .= '<p><input name="smw_values_starter" size="80" value="'.$allowed_val_string.'" ></p></fieldset>'; |
| | 635 | + $html_text_array[] = $html_text; //<fieldset style="background: #00FFFF;"> |
| | 636 | + } |
| | 637 | + } |
| | 638 | + } |
| | 639 | + $text_extensions['smw'] = $html_text_array; |
| | 640 | + return true; |
| | 641 | + |
| | 642 | +} |
| | 643 | +function smwfgetHtmlTextForPS( &$js_extensions ,&$text_extensions ) { |
| | 644 | + global $smwgContLang; |
| | 645 | + $datatype_labels = $smwgContLang->getDatatypeLabels(); |
| | 646 | + $html_text = ""; |
| | 647 | + $html_text .= '<fieldset style="background: #00FFFF;"><legend>Property</legend> |
| | 648 | + <p> Property Name: <input size="15" name="smw_property_name_starter">Type: '; |
| | 649 | + $select_body = ""; |
| | 650 | + foreach ( $datatype_labels as $label ) { |
| | 651 | + $select_body .= " " . Xml::element( 'option', null, $label ) . "\n"; |
| | 652 | + } |
| | 653 | + $html_text .= Xml::tags( 'select', array( 'id' => 'property_dropdown', 'name' => 'smw_property_type_starter' ), $select_body ); |
| | 654 | + $html_text .= ' </p> |
| | 655 | + <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> |
| | 656 | + <p><input value="" name="smw_values_starter" size="80"></p></fieldset>'; |
| | 657 | + |
| | 658 | + $text_extensions['smw'] = $html_text; |
| | 659 | + return true; |
| | 660 | +} |
| | 661 | +function smwfGeneratePages( $psSchemaObj, $toGenPageList ) { |
| | 662 | + $template_all = $psSchemaObj->getTemplates(); |
| | 663 | + foreach ( $template_all as $template ) { |
| | 664 | + $field_all = $template->getFields(); |
| | 665 | + $field_count = 0; //counts the number of fields |
| | 666 | + foreach( $field_all as $field ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type |
| | 667 | + $field_count++; |
| | 668 | + $smw_array = $field->getObject('Property'); //this returns an array with property values filled |
| | 669 | + $prop_array = $smw_array['smw']; |
| | 670 | + if($prop_array != null){ |
| | 671 | + $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_array['name'] ); |
| | 672 | + $key_title = PageSchemas::titleString( $title ); |
| | 673 | + if(in_array( $key_title, $toGenPageList )){ |
| | 674 | + smwfCreateProperty( $prop_array['name'], $prop_array['Type'], $prop_array['allowed_value_array'] ) ; |
| | 675 | + } |
| | 676 | + } |
| | 677 | + } |
| | 678 | + } |
| | 679 | + return true; |
| | 680 | +} |
| | 681 | +function smwfCreatePropertyText( $property_type, $allowed_value_array ) { |
| | 682 | + global $smwgContLang; |
| | 683 | + $prop_labels = $smwgContLang->getPropertyLabels(); |
| | 684 | + $type_tag = "[[{$prop_labels['_TYPE']}::$property_type]]"; |
| | 685 | + $text = wfMsgForContent( 'ps-property-isproperty', $type_tag ); |
| | 686 | + if ( $allowed_value_array != null) { |
| | 687 | + // replace the comma substitution character that has no chance of |
| | 688 | + // being included in the values list - namely, the ASCII beep |
| | 689 | + $text .= "\n\n" . wfMsgExt( 'ps-property-allowedvals', array( 'parsemag', 'content' ), count( $allowed_value_array ) ); |
| | 690 | + foreach ( $allowed_value_array as $i => $value ) { |
| | 691 | + // replace beep back with comma, trim |
| | 692 | + $value = str_replace( "\a",',' , trim( $value ) ); |
| | 693 | + if ( method_exists( $smwgContLang, 'getPropertyLabels' ) ) { |
| | 694 | + $prop_labels = $smwgContLang->getPropertyLabels(); |
| | 695 | + $text .= "\n* [[" . $prop_labels['_PVAL'] . "::$value]]"; |
| | 696 | + } else { |
| | 697 | + $spec_props = $smwgContLang->getSpecialPropertiesArray(); |
| | 698 | + $text .= "\n* [[" . $spec_props[SMW_SP_POSSIBLE_VALUE] . "::$value]]"; |
| | 699 | + } |
| | 700 | + } |
| | 701 | + } |
| | 702 | + return $text; |
| | 703 | + } |
| | 704 | +function smwfCreateProperty( $prop_name, $prop_type, $allowed_value_array ) { |
| | 705 | + global $wgOut, $wgUser; |
| | 706 | + $title = Title::makeTitleSafe( SMW_NS_PROPERTY, $prop_name ); |
| | 707 | + $text = smwfCreatePropertyText( $prop_type, $allowed_value_array ); |
| | 708 | + #$text = "This is a property of type [[Has type:Number]]."; |
| | 709 | + $jobs = array(); |
| | 710 | + $params = array(); |
| | 711 | + $params['user_id'] = $wgUser->getId(); |
| | 712 | + $params['page_text'] = $text; |
| | 713 | + $jobs[] = new PSCreatePageJob( $title, $params ); |
| | 714 | + Job::batchInsert( $jobs ); |
| | 715 | + return true; |
| | 716 | +} |
| | 717 | + |
| 530 | 718 | /** |
| | 719 | +* Function to return the Property based on the xml passed from the PageSchema extension |
| | 720 | +*/ |
| | 721 | +function smwfCreatePageSchemasObject( $objectName, $xmlForField, &$object ) { |
| | 722 | + $smw_array = array(); |
| | 723 | + if ( $objectName == "Property" ) { |
| | 724 | + foreach ( $xmlForField->children() as $tag => $child ) { |
| | 725 | + if ( $tag == $objectName ) { |
| | 726 | + $propName = $child->attributes()->name; |
| | 727 | + //this means object has already been initialized by some other extension. |
| | 728 | + $smw_array['name']=(string)$propName; |
| | 729 | + $allowed_value_array = array(); |
| | 730 | + $count = 0; |
| | 731 | + foreach ( $child->children() as $prop => $value ) { |
| | 732 | + if ( $prop == "AllowedValue" ) { |
| | 733 | + $allowed_value_array[$count++] = $value; |
| | 734 | + } else { |
| | 735 | + $smw_array[$prop] = (string)$value; |
| | 736 | + } |
| | 737 | + } |
| | 738 | + $smw_array['allowed_value_array'] = $allowed_value_array; |
| | 739 | + } |
| | 740 | + } |
| | 741 | + $object['smw'] = $smw_array; |
| | 742 | + } |
| | 743 | + return true; |
| | 744 | +} |
| | 745 | + |
| | 746 | +/** |
| 531 | 747 | * Set up (possibly localised) names for SMW's parser functions. |
| 532 | 748 | * @todo Can be removed when new style magic words are used (introduced in r52503). |
| 533 | 749 | */ |