setHeaders(); $this->printCreateTemplateForm( $query ); } public static function getAllPropertyNames() { $all_properties = array(); // Set limit on results - we don't want a massive dropdown // of properties, if there are a lot of properties in this wiki. // getProperties() functions stop requiring a limit $options = new SMWRequestOptions(); $options->limit = 500; $used_properties = PFUtils::getSMWStore()->getPropertiesSpecial( $options ); if ( $used_properties instanceof SMW\SQLStore\PropertiesCollector ) { // SMW 1.9+ $used_properties = $used_properties->runCollector(); } foreach ( $used_properties as $property ) { // Skip over properties that are errors. (This // shouldn't happen, but it sometimes does.) if ( !method_exists( $property[0], 'getKey' ) ) { continue; } $propName = $property[0]->getKey(); if ( $propName{0} != '_' ) { $all_properties[] = str_replace( '_', ' ', $propName ); } } $unused_properties = PFUtils::getSMWStore()->getUnusedPropertiesSpecial( $options ); if ( $unused_properties instanceof SMW\SQLStore\UnusedPropertiesCollector ) { // SMW 1.9+ $unused_properties = $unused_properties->runCollector(); } foreach ( $unused_properties as $property ) { // Skip over properties that are errors. (This // shouldn't happen, but it sometimes does.) if ( !method_exists( $property, 'getKey' ) ) { continue; } $all_properties[] = str_replace( '_', ' ', $property->getKey() ); } // Sort properties list alphabetically, and get unique values // (for SQLStore3, getPropertiesSpecial() seems to get unused // properties as well). sort( $all_properties ); $all_properties = array_unique( $all_properties ); return $all_properties; } public static function printPropertiesComboBox( $all_properties, $id, $selected_property = null ) { $selectBody = "\n"; foreach ( $all_properties as $prop_name ) { $optionAttrs = array( 'value' => $prop_name ); if ( $selected_property == $prop_name ) { $optionAttrs['selected'] = 'selected'; } $selectBody .= Html::element( 'option', $optionAttrs, $prop_name ) . "\n"; } return Html::rawElement( 'select', array( 'id' => "semantic_property_$id", 'name' => "semantic_property_$id", 'class' => 'pfComboBox' ), $selectBody ) . "\n"; } static function printFieldTypeDropdown( $id ) { global $wgCargoFieldTypes; $selectBody = ''; foreach ( $wgCargoFieldTypes as $type ) { $optionAttrs = array( 'value' => $type ); $selectBody .= Html::element( 'option', $optionAttrs, $type ) . "\n"; } return Html::rawElement( 'select', array( 'id' => "field_type_$id", 'name' => "field_type_$id", ), $selectBody ) . "\n"; } public static function printFieldEntryBox( $id, $all_properties, $display = true ) { $fieldString = $display ? '' : 'id="starterField" style="display: none"'; $text = "\t
\n"; $text .= "\t
\n"; $text .= "\t

   \n"; $text .= "\t   \n"; if ( defined( 'SMW_VERSION' ) ) { $dropdown_html = self::printPropertiesComboBox( $all_properties, $id ); $text .= "\t

\n"; } elseif ( defined( 'CARGO_VERSION' ) ) { $dropdown_html = self::printFieldTypeDropdown( $id ); $text .= "\t

\n"; } $text .= "\t

" . '   \n"; $text .= "\t" . '\n"; $text .= "\t

\n"; if ( !defined( 'SMW_VERSION' ) && defined( 'CARGO_VERSION' ) ) { $text .= "\t

\n"; $text .= "   \n"; $text .= "\t

\n"; $text .= "\t

\n"; $text .= "\t\n"; $text .= "\t'; $text .= "\t

\n"; } $text .= "\t
\n"; $text .= "\t" . '' . "\n"; $text .= <<
END; return $text; } static function printTemplateStyleButton( $formatStr, $formatMsg, $htmlFieldName, $curSelection ) { $attrs = array( 'id' => $formatStr ); if ( $formatStr === $curSelection ) { $attrs['checked'] = true; } return "\t" . Html::input( $htmlFieldName, $formatStr, 'radio', $attrs ) . ' ' . Html::element( 'label', array( 'for' => $formatStr ), wfMessage( $formatMsg )->escaped() ) . "\n"; } static function printTemplateStyleInput( $htmlFieldName, $curSelection = null ) { if ( !$curSelection ) { $curSelection = 'standard'; } $text = "\t

" . wfMessage( 'pf_createtemplate_outputformat' )->escaped() . "\n"; $text .= self::printTemplateStyleButton( 'standard', 'pf_createtemplate_standardformat', $htmlFieldName, $curSelection ); $text .= self::printTemplateStyleButton( 'infobox', 'pf_createtemplate_infoboxformat', $htmlFieldName, $curSelection ); $text .= self::printTemplateStyleButton( 'plain', 'pf_createtemplate_plainformat', $htmlFieldName, $curSelection ); $text .= self::printTemplateStyleButton( 'sections', 'pf_createtemplate_sectionsformat', $htmlFieldName, $curSelection ); $text .= "

\n"; return $text; } function printCreateTemplateForm( $query ) { $out = $this->getOutput(); $req = $this->getRequest(); if ( !is_null( $query ) ) { $presetTemplateName = str_replace( '_', ' ', $query ); $out->setPageTitle( wfMessage( 'pf-createtemplate-with-name', $presetTemplateName )->text() ); $template_name = $presetTemplateName; } else { $presetTemplateName = null; $template_name = $req->getVal( 'template_name' ); } $out->addModules( array( 'ext.pageforms.main', 'ext.pageforms.PF_CreateTemplate' ) ); $text = ''; $save_page = $req->getCheck( 'wpSave' ); $preview_page = $req->getCheck( 'wpPreview' ); if ( $save_page || $preview_page ) { $validToken = $this->getUser()->matchEditToken( $req->getVal( 'csrf' ), 'CreateTemplate' ); if ( !$validToken ) { $text = "This appears to be a cross-site request forgery; canceling save."; $out->addHTML( $text ); return; } $fields = array(); // Cycle through the query values, setting the // appropriate local variables. foreach ( $req->getValues() as $var => $val ) { $var_elements = explode( "_", $var ); // We only care about query variables of the form "a_b". if ( count( $var_elements ) != 2 ) { continue; } list( $field_field, $id ) = $var_elements; if ( $field_field == 'name' && $id != 'starter' ) { $field = PFTemplateField::create( $val, $req->getVal( 'label_' . $id ), $req->getVal( 'semantic_property_' . $id ), $req->getCheck( 'is_list_' . $id ), $req->getVal( 'delimiter_' . $id ) ); $field->setFieldType( $req->getVal( 'field_type_' . $id ) ); if ( defined( 'CARGO_VERSION' ) ) { if ( $req->getCheck( 'is_hierarchy_' . $id ) ) { $hierarchyStructureStr = $req->getVal( 'hierarchy_structure_' . $id ); $field->setHierarchyStructure( $hierarchyStructureStr ); } else { $allowedValuesStr = $req->getVal( 'allowed_values_' . $id ); $possibleValues = CargoUtils::smartSplit( ',', $allowedValuesStr ); $field->setPossibleValues( $possibleValues ); } } $fields[] = $field; } } // Assemble the template text, and submit it as a wiki // page. $out->setArticleBodyOnly( true ); $title = Title::makeTitleSafe( NS_TEMPLATE, $template_name ); $category = $req->getVal( 'category' ); $cargo_table = $req->getVal( 'cargo_table' ); $aggregating_property = $req->getVal( 'semantic_property_aggregation' ); $aggregation_label = $req->getVal( 'aggregation_label' ); $template_format = $req->getVal( 'template_format' ); $pfTemplate = new PFTemplate( $template_name, $fields ); $pfTemplate->setCategoryName( $category ); if ( $req->getBool( 'use_cargo' ) ) { $pfTemplate->mCargoTable = $cargo_table; } $pfTemplate->setAggregatingInfo( $aggregating_property, $aggregation_label ); $pfTemplate->setFormat( $template_format ); $full_text = $pfTemplate->createText(); $text = PFUtils::printRedirectForm( $title, $full_text, "", $save_page, $preview_page, false, false, false, null, null ); $out->addHTML( $text ); return; } $text .= '
' . "\n"; if ( is_null( $presetTemplateName ) ) { // Set 'title' field, in case there's no URL niceness. $text .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; $text .= "\t

" . wfMessage( 'pf_createtemplate_namelabel' )->escaped() . '

' . "\n"; } $text .= "\t

" . wfMessage( 'pf_createtemplate_categorylabel' )->escaped() . '

' . "\n"; if ( !defined( 'SMW_VERSION' ) && defined( 'CARGO_VERSION' ) ) { $text .= "\t

\n"; $text .= "\t

' . "\n"; } $text .= "\t
\n"; $text .= "\t" . Html::element( 'legend', null, wfMessage( 'pf_createtemplate_templatefields' )->text() ) . "\n"; $text .= "\t" . Html::element( 'p', null, wfMessage( 'pf_createtemplate_fieldsdesc' )->text() ) . "\n"; if ( defined( 'SMW_VERSION' ) ) { $all_properties = self::getAllPropertyNames(); } else { $all_properties = array(); } $text .= '
' . "\n"; $text .= self::printFieldEntryBox( "1", $all_properties ); $text .= self::printFieldEntryBox( "starter", $all_properties, false ); $text .= "
\n"; $add_field_button = Html::input( null, wfMessage( 'pf_createtemplate_addfield' )->text(), 'button', array( 'class' => "createTemplateAddField" ) ); $text .= Html::rawElement( 'p', null, $add_field_button ) . "\n"; $text .= "\t
\n"; if ( defined( 'SMW_VERSION' ) ) { $text .= "\t
\n"; $text .= "\t" . Html::element( 'legend', null, wfMessage( 'pf_createtemplate_aggregation' )->text() ) . "\n"; $text .= "\t" . Html::element( 'p', null, wfMessage( 'pf_createtemplate_aggregationdesc' )->text() ) . "\n"; $text .= "\t

" . wfMessage( 'pf_createtemplate_semanticproperty' )->escaped() . ' ' . self::printPropertiesComboBox( $all_properties, "aggregation" ) . "

\n"; $text .= "\t

" . wfMessage( 'pf_createtemplate_aggregationlabel' )->escaped() . ' ' . Html::input( 'aggregation_label', null, 'text', array( 'size' => '25' ) ) . "

\n"; $text .= "\t
\n"; } $text .= self::printTemplateStyleInput( 'template_format' ); $text .= "\t" . Html::hidden( 'csrf', $this->getUser()->getEditToken( 'CreateTemplate' ) ) . "\n"; $save_button_text = wfMessage( 'savearticle' )->escaped(); $preview_button_text = wfMessage( 'preview' )->escaped(); $text .= << END; $out->addHTML( $text ); } protected function getGroupName() { return 'pf_group'; } }