http://www.mediawiki.org/wiki/Special:Code/MediaWiki/97337
Revision: 97337 Author: yaron Date: 2011-09-16 21:18:36 +0000 (Fri, 16 Sep 2011) Log Message: ----------- Moved all Page Schemas-related static functions from SF_Utils.php into a new file, SF_PageSchemas.php Modified Paths: -------------- trunk/extensions/SemanticForms/SemanticForms.php trunk/extensions/SemanticForms/includes/SF_Utils.php Added Paths: ----------- trunk/extensions/SemanticForms/includes/SF_PageSchemas.php Modified: trunk/extensions/SemanticForms/SemanticForms.php =================================================================== --- trunk/extensions/SemanticForms/SemanticForms.php 2011-09-16 21:14:49 UTC (rev 97336) +++ trunk/extensions/SemanticForms/SemanticForms.php 2011-09-16 21:18:36 UTC (rev 97337) @@ -92,15 +92,14 @@ $wgHooks['MakeGlobalVariablesScript'][] = 'SFFormUtils::setGlobalJSVariables'; // Page Schemas extension hooks -$wgHooks['PageSchemasGetObject'][] = 'SFUtils::createPageSchemasObject' ; -$wgHooks['PageSchemasGeneratePages'][] = 'SFUtils::generatePages' ; -$wgHooks['PSParseFieldElements'][] = 'SFUtils::parseFieldElements' ; -$wgHooks['PageSchemasGetPageList'][] = 'SFUtils::getPageList' ; -$wgHooks['PageSchemasGetSchemaHTML'][] = 'SFUtils::getSchemaHTMLForPS' ; -$wgHooks['PageSchemasGetFieldHTML'][] = 'SFUtils::getFieldHTMLForPS' ; -$wgHooks['PageSchemasGetSchemaXML'][] = 'SFUtils::getSchemaXMLForPS'; -$wgHooks['PageSchemasGetFieldXML'][] = 'SFUtils::getFieldXMLForPS'; -//$wgHooks['getFilledHtmlTextForFieldInputs'][] = 'SFUtils::getFilledHtmlTextForPS' ; +$wgHooks['PageSchemasGetObject'][] = 'SFPageSchemas::createPageSchemasObject' ; +$wgHooks['PageSchemasGeneratePages'][] = 'SFPageSchemas::generatePages' ; +$wgHooks['PSParseFieldElements'][] = 'SFPageSchemas::parseFieldElements' ; +$wgHooks['PageSchemasGetPageList'][] = 'SFPageSchemas::getPageList' ; +$wgHooks['PageSchemasGetSchemaHTML'][] = 'SFPageSchemas::getSchemaHTML' ; +$wgHooks['PageSchemasGetFieldHTML'][] = 'SFPageSchemas::getFieldHTML' ; +$wgHooks['PageSchemasGetSchemaXML'][] = 'SFPageSchemas::getSchemaXML'; +$wgHooks['PageSchemasGetFieldXML'][] = 'SFPageSchemas::getFieldXML'; $wgAPIModules['sfautocomplete'] = 'SFAutocompleteAPI'; $wgAPIModules['sfautoedit'] = 'SFAutoeditAPI'; @@ -154,6 +153,7 @@ $wgAutoloadClasses['SFFormEditPage'] = $sfgIP . '/includes/SF_FormEditPage.php'; $wgAutoloadClasses['SFUtils'] = $sfgIP . '/includes/SF_Utils.php'; $wgAutoloadClasses['SFFormLinker'] = $sfgIP . '/includes/SF_FormLinker.php'; +$wgAutoloadClasses['SFPageSchemas'] = $sfgIP . '/includes/SF_PageSchemas.php'; $wgAutoloadClasses['SFParserFunctions'] = $sfgIP . '/includes/SF_ParserFunctions.php'; $wgAutoloadClasses['SFAutocompleteAPI'] = $sfgIP . '/includes/SF_AutocompleteAPI.php'; $wgAutoloadClasses['SFAutoeditAPI'] = $sfgIP . '/includes/SF_AutoeditAPI.php'; Added: trunk/extensions/SemanticForms/includes/SF_PageSchemas.php =================================================================== --- trunk/extensions/SemanticForms/includes/SF_PageSchemas.php (rev 0) +++ trunk/extensions/SemanticForms/includes/SF_PageSchemas.php 2011-09-16 21:18:36 UTC (rev 97337) @@ -0,0 +1,375 @@ +<?php +/** + * Static functions for Semantic Forms, for use by the Page Schemas extension. + * + * @author Yaron Koren + * @author Ankit Garg + * @file + * @ingroup SF + */ + +class SFPageSchemas { + + /** + * Function to return the property based on the XML passed from the Page Schemas extension + */ + public static function createPageSchemasObject( $objectName, $xmlForField, &$object ) { + $sfarray = array(); + $formName=""; + if ( $objectName == "semanticforms_Form" ) { + foreach ( $xmlForField->children() as $tag => $child ) { + if ( $tag == $objectName ) { + $formName = (string) $child->attributes()->name; + $sfarray['name'] = $formName; + foreach ($child->children() as $tag => $formelem) { + $sfarray[(string)$tag] = (string)$formelem; + } + $object['sf'] = $sfarray; + return true; + } + } + } + if ( $objectName == "semanticforms_FormInput" ) { + foreach ( $xmlForField->children() as $tag => $child ) { + if ( $tag == $objectName ) { + foreach ( $child->children() as $prop ) { + if ( $prop->getName() == 'InputType' ) { + $sfarray[$prop->getName()] = (string)$prop; + } else { + // Remember these values can be null also. + // While polulating in the page text, take care of that. + $sfarray[(string)$prop->attributes()->name] = (string)$prop; + } + } + // Setting value specific to SF in 'sf' index. + $object['sf'] = $sfarray; + return true; + } + } + } + return true; + } + + public static function getSchemaXML( $request, &$xmlArray ) { + foreach ( $request->getValues() as $var => $val ) { + if ( $var == 'sf_form_name' ) { + $xml = '<semanticforms_Form name="'.$val.'" >'; + } elseif ( $var == 'sf_page_name_formula' ) { + $xml .= '<PageNameFormula>'.$val.'</PageNameFormula>'; + } elseif ( $var == 'sf_create_title' ) { + $xml .= '<CreateTitle>'.$val.'</CreateTitle>'; + } elseif ( $var == 'sf_edit_title' ) { + $xml .= '<EditTitle>'.$val.'</EditTitle>'; + $xml .= '</semanticforms_Form>'; + } + } + $xmlArray['sf'] = $xml; + return true; + } + + public static function getFieldXML( $request, &$xmlArray ) { + $xmlPerField = array(); + $fieldNum = -1; + foreach ( $request->getValues() as $var => $val ) { + if ( substr( $var, 0, 14 ) == 'sf_input_type_' ) { + $fieldNum = substr( $var, 14 ); + $xml = '<semanticforms_FormInput>'; + if ( !empty( $val ) ) { + $xml .= '<InputType>' . $val . '</InputType>'; + } + } elseif ( substr( $var, 0, 14 ) == 'sf_key_values_' ) { + if ( $val != '' ) { + // replace the comma substitution character that has no chance of + // being included in the values list - namely, the ASCII beep + $listSeparator = ','; + $key_values_str = str_replace( "\\$listSeparator", "\a", $val ); + $key_values_array = explode( $listSeparator, $key_values_str ); + foreach ( $key_values_array as $i => $value ) { + // replace beep back with comma, trim + $value = str_replace( "\a", $listSeparator, trim( $value ) ); + $param_value = explode( "=", $value ); + if ( $param_value[1] != null ) { + //handles Parameter name="size">20</Parameter> + $xml .= '<Parameter name="'.$param_value[0].'">'.$param_value[1].'</Parameter>'; + } else { + //handles <Parameter name="mandatory" /> + $xml .= '<Parameter name="'.$param_value[0].'"/>'; + } + } + } + $xml .= '</semanticforms_FormInput>'; + $xmlPerField[$fieldNum] = $xml; + } + } + $xmlArray['sf'] = $xmlPerField; + return true; + } + + public static function getSchemaHTML( $pageSchemaObj, &$text_extensions ) { + $form_array = array(); + $hasExistingValues = false; + if ( !is_null( $pageSchemaObj ) ) { + $obj = $pageSchemaObj->getObject('semanticforms_Form'); + if ( array_key_exists( 'sf', $obj ) ) { + $form_array = $obj['sf']; + $hasExistingValues = true; + } + } + if ( array_key_exists( 'name', $form_array ) ) { + $formName = $form_array['name']; + } else { + $formName = ''; + } + $text = "\t<p>" . 'Name:' . ' ' . Html::input( 'sf_form_name', $formName, 'text', array( 'size' => 15 ) ) . "</p>\n"; + if ( array_key_exists( 'PageNameFormula', $form_array ) ) { + $pageNameFormula = $form_array['PageNameFormula']; + } else { + $pageNameFormula = ''; + } + $text .= "\t<p>" . 'Page name formula:' . ' ' . Html::input( 'sf_page_name_formula', $pageNameFormula, 'text', array( 'size' => 20 ) ) . "</p>\n"; + if ( array_key_exists( 'CreateTitle', $form_array ) ) { + $createTitle = $form_array['CreateTitle']; + } else { + $createTitle = ''; + } + $text .= "\t<p>" . 'Title of form for new pages:' . ' ' . Html::input( 'sf_create_title', $createTitle, 'text', array( 'size' => 25 ) ) . "</p>\n"; + if ( array_key_exists( 'EditTitle', $form_array ) ) { + $editTitle = $form_array['EditTitle']; + } else { + $editTitle = ''; + } + $text .= "\t<p>" . 'Title of form for existing pages:' . ' ' . Html::input( 'sf_edit_title', $editTitle, 'text', array( 'size' => 25 ) ) . "</p>\n"; + $text_extensions['sf'] = array( 'Form', '#CF9', $text, $hasExistingValues ); + + return true; + } + + /** + * Returns the HTML for inputs to define a single form field, + * within the Page Schemas 'edit schema' page. + */ + public static function getFieldHTML( $field, &$text_extensions ) { + if ( is_null( $field ) ) { + $fieldValues = array(); + } else { + $sf_array = $field->getObject('semanticforms_FormInput'); //this returns an array with property values filled + if ( array_key_exists( 'sf', $sf_array ) ) { + $fieldValues = $sf_array['sf']; + $hasExistingValues = true; + } else { + $fieldValues = array(); + $hasExistingValues = false; + } + } + + if ( array_key_exists( 'InputType', $fieldValues ) ) { + $inputType = $fieldValues['InputType']; + } else { + $inputType = ''; + } + $inputTypeAttrs = array( 'size' => 15 ); + $inputTypeInput = Html::input( 'sf_input_type_num', $inputType, 'text', $inputTypeAttrs ); + $text = '<p>Input type: ' . $inputTypeInput . '</p>'; + $text .= "\t" . '<p>Parameter name and its value as a key=value pair, separated by commas (if a value contains a comma, replace it with "\,"): For example: size=20,mandatory</p>' . "\n"; + $paramValues = array(); + foreach ( $fieldValues as $param => $value ) { + if ( !empty( $param ) && $param != 'InputType' ) { + if ( !empty( $value ) ) { + $paramValues[] = $param . '=' . $value; + } else { + $paramValues[] = $param; + } + } + } + $param_value_str = implode( ', ', $paramValues ); + $inputParamsAttrs = array( 'size' => 80 ); + $inputParamsInput = Html::input( 'sf_key_values_num', $param_value_str, 'text', $inputParamsAttrs ); + $text .= "\t<p>$inputParamsInput</p>\n"; + $text_extensions['sf'] = array( 'Form input', '#CF9', $text, $hasExistingValues ); + + return true; + } + + public static function getFormName( $psSchemaObj ) { + $formData = $psSchemaObj->getObject( 'semanticforms_Form' ); + return $formData['sf']['name']; + } + + public static function getMainFormInfo( $psSchemaObj ) { + $formData = $psSchemaObj->getObject( 'semanticforms_Form' ); + return $formData['sf']; + } + + public static function getFormFieldInfo( $psTemplateObj, $template_fields ) { + $form_fields = array(); + $fieldsInfo = $psTemplateObj->getFields(); + foreach ( $fieldsInfo as $i => $psFieldObj ) { + $fieldName = $psFieldObj->getName(); + $fieldFormInfo = $psFieldObj->getObject( 'semanticforms_FormInput' ); + if ( !is_null( $fieldFormInfo ) && array_key_exists( 'sf', $fieldFormInfo ) ) { + $formField = SFFormField::create( $i, $template_fields[$i] ); + $fieldFormArray = $fieldFormInfo['sf']; + foreach ($fieldFormArray as $var => $val ) { + if ( $var == 'InputType' ) { + $formField->setInputType( $val ); + } elseif ( $var == 'mandatory' ) { + $formField->setIsMandatory( true ); + } elseif ( $var == 'hidden' ) { + $formField->setIsHidden( true ); + } elseif ( $var == 'restricted' ) { + $formField->setIsRestricted( true ); + } else { + $formField->setFieldArg( $var, $val ); + } + } + $form_fields[] = $formField; + } + } + return $form_fields; + } + + /** + * Return the list of pages that Semantic Forms could generate from + * the current Page Schemas schema. + */ + public static function getPageList( $psSchemaObj, &$genPageList ) { + global $wgOut, $wgUser; + + $template_all = $psSchemaObj->getTemplates(); + foreach ( $template_all as $template ) { + $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() ); + $genPageList[] = $title; + } + $form_name = self::getFormName( $psSchemaObj ); + if ( $form_name == null ) { + return true; + } + //$form = SFForm::create( $form_name, $form_templates ); + $title = Title::makeTitleSafe( SF_NS_FORM, $form_name ); + $genPageList[] = $title; + return true; + } + + /** + * Creates wiki-text for a template, based on the contents of a + * <PageSchema> tag. + */ + public static function getFieldsFromTemplateSchema( $templateFromSchema ) { + $field_all = $templateFromSchema->getFields(); + $template_fields = array(); + foreach( $field_all as $fieldObj ) { + $smw_array = $fieldObj->getObject('semanticmediawiki_Property'); + $propertyName = $smw_array['smw']['name']; + if ( $fieldObj->getLabel() == '' ) { + $fieldLabel = $fieldObj->getName(); + } else { + $fieldLabel = $fieldObj->getLabel(); + } + $templateField = SFTemplateField::create( + $fieldObj->getName(), + $fieldLabel, + $propertyName, + $fieldObj->isList(), + $fieldObj->getDelimiter() + ); + $template_fields[] = $templateField; + } + return $template_fields; + } + + public static function generateForm( $formName, $formTitle, $formTemplates, $formDataFromSchema ) { + global $wgUser; + + $form = SFForm::create( $formName, $formTemplates ); + if ( array_key_exists( 'PageNameFormula', $formDataFromSchema ) ) { + $form->setPageNameFormula( $formDataFromSchema['PageNameFormula'] ); + } + if ( array_key_exists( 'CreateTitle', $formDataFromSchema ) ) { + $form->setCreateTitle( $formDataFromSchema['CreateTitle'] ); + } + if ( array_key_exists( 'EditTitle', $formDataFromSchema ) ) { + $form->setEditTitle( $formDataFromSchema['EditTitle'] ); + } + $formContents = $form->createMarkup(); + $params = array(); + $params['user_id'] = $wgUser->getId(); + $params['page_text'] = $formContents; + $job = new PSCreatePageJob( $formTitle, $params ); + Job::batchInsert( array( $job ) ); + } + + /** + * Generate pages (form and templates) specified in the list. + */ + public static function generatePages( $psSchemaObj, $toGenPageList ) { + global $wgOut, $wgUser; + + $templatesFromSchema = $psSchemaObj->getTemplates(); + $form_templates = array(); + $jobs = array(); + foreach ( $templatesFromSchema as $templateFromSchema ) { + // Generate every specified template + $templateName = $templateFromSchema->getName(); + $templateTitle = Title::makeTitleSafe( NS_TEMPLATE, $templateName ); + $template_fields = array(); + $fullTemplateName = PageSchemas::titleString( $templateTitle ); + $template_fields = self::getFieldsFromTemplateSchema( $templateFromSchema ); + $templateText = SFTemplateField::createTemplateText( $templateName, + $template_fields, null, $psSchemaObj->categoryName, null, null, null ); + if ( in_array( $fullTemplateName, $toGenPageList ) ) { + $params = array(); + $params['user_id'] = $wgUser->getId(); + $params['page_text'] = $templateText; + $jobs[] = new PSCreatePageJob( $templateTitle, $params ); + } + + $form_fields = self::getFormFieldInfo( $templateFromSchema, $template_fields ); + // Create template info for form, for use in generating + // the form (if it will be generated). + $form_template = SFTemplateInForm::create( + $templateName, + $templateFromSchema->getLabel(), + $templateFromSchema->isMultiple(), + null, + $form_fields + ); + $form_templates[] = $form_template; + } + Job::batchInsert( $jobs ); + + // Create form, if it's specified. + $form_name = self::getFormName( $psSchemaObj ); + if ( !empty( $form_name ) ) { + $formInfo = self::getMainFormInfo( $psSchemaObj ); + $formTitle = Title::makeTitleSafe( SF_NS_FORM, $form_name ); + $fullFormName = PageSchemas::titleString( $formTitle ); + if ( in_array( $fullFormName, $toGenPageList ) ) { + self::generateForm( $form_name, $formTitle, $form_templates, $formInfo ); + } + } + return true; + } + + /** + * Parses the field elements in the Page Schemas XML. + */ + public static function parseFieldElements( $field_xml, &$text_object ) { + + foreach ( $field_xml->children() as $tag => $child ) { + if ( $tag == "semanticforms_FormInput" ) { + $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticForms", (string)$tag ); + foreach ( $child->children() as $prop ) { + if ( $prop->getName() == 'InputType' ) { + $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop->getName(), $prop ); + } else { + $prop_name = (string)$prop->attributes()->name; + $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop_name, (string)$prop ); + } + } + $text_object['sf'] = $text; + break; + } + } + return true; + } +} Modified: trunk/extensions/SemanticForms/includes/SF_Utils.php =================================================================== --- trunk/extensions/SemanticForms/includes/SF_Utils.php 2011-09-16 21:14:49 UTC (rev 97336) +++ trunk/extensions/SemanticForms/includes/SF_Utils.php 2011-09-16 21:18:36 UTC (rev 97337) @@ -3,7 +3,6 @@ * Helper functions for the Semantic Forms extension. * * @author Yaron Koren - * @author Ankit Garg * @file * @ingroup SF */ @@ -153,369 +152,6 @@ } } - /** - * Function to return the property based on the XML passed from the Page Schemas extension - */ - public static function createPageSchemasObject( $objectName, $xmlForField, &$object ) { - $sfarray = array(); - $formName=""; - if ( $objectName == "semanticforms_Form" ) { - foreach ( $xmlForField->children() as $tag => $child ) { - if ( $tag == $objectName ) { - $formName = (string) $child->attributes()->name; - $sfarray['name'] = $formName; - foreach ($child->children() as $tag => $formelem) { - $sfarray[(string)$tag] = (string)$formelem; - } - $object['sf'] = $sfarray; - return true; - } - } - } - if ( $objectName == "semanticforms_FormInput" ) { - foreach ( $xmlForField->children() as $tag => $child ) { - if ( $tag == $objectName ) { - foreach ( $child->children() as $prop ) { - if ( $prop->getName() == 'InputType' ) { - $sfarray[$prop->getName()] = (string)$prop; - } else { - // Remember these values can be null also. - // While polulating in the page text, take care of that. - $sfarray[(string)$prop->attributes()->name] = (string)$prop; - } - } - // Setting value specific to SF in 'sf' index. - $object['sf'] = $sfarray; - return true; - } - } - } - return true; - } - - public static function getSchemaXMLForPS( $request, &$xmlArray ) { - foreach ( $request->getValues() as $var => $val ) { - if ( $var == 'sf_form_name' ) { - $xml = '<semanticforms_Form name="'.$val.'" >'; - } elseif ( $var == 'sf_page_name_formula' ) { - $xml .= '<PageNameFormula>'.$val.'</PageNameFormula>'; - } elseif ( $var == 'sf_create_title' ) { - $xml .= '<CreateTitle>'.$val.'</CreateTitle>'; - } elseif ( $var == 'sf_edit_title' ) { - $xml .= '<EditTitle>'.$val.'</EditTitle>'; - $xml .= '</semanticforms_Form>'; - } - } - $xmlArray['sf'] = $xml; - return true; - } - - public static function getFieldXMLForPS( $request, &$xmlArray ) { - $xmlPerField = array(); - $fieldNum = -1; - foreach ( $request->getValues() as $var => $val ) { - if ( substr( $var, 0, 14 ) == 'sf_input_type_' ) { - $fieldNum = substr( $var, 14 ); - $xml = '<semanticforms_FormInput>'; - if ( !empty( $val ) ) { - $xml .= '<InputType>' . $val . '</InputType>'; - } - } elseif ( substr( $var, 0, 14 ) == 'sf_key_values_' ) { - if ( $val != '' ) { - // replace the comma substitution character that has no chance of - // being included in the values list - namely, the ASCII beep - $listSeparator = ','; - $key_values_str = str_replace( "\\$listSeparator", "\a", $val ); - $key_values_array = explode( $listSeparator, $key_values_str ); - foreach ( $key_values_array as $i => $value ) { - // replace beep back with comma, trim - $value = str_replace( "\a", $listSeparator, trim( $value ) ); - $param_value = explode( "=", $value ); - if ( $param_value[1] != null ) { - //handles Parameter name="size">20</Parameter> - $xml .= '<Parameter name="'.$param_value[0].'">'.$param_value[1].'</Parameter>'; - } else { - //handles <Parameter name="mandatory" /> - $xml .= '<Parameter name="'.$param_value[0].'"/>'; - } - } - } - $xml .= '</semanticforms_FormInput>'; - $xmlPerField[$fieldNum] = $xml; - } - } - $xmlArray['sf'] = $xmlPerField; - return true; - } - - public static function getSchemaHTMLForPS( $pageSchemaObj, &$text_extensions ) { - $form_array = array(); - $hasExistingValues = false; - if ( !is_null( $pageSchemaObj ) ) { - $obj = $pageSchemaObj->getObject('semanticforms_Form'); - if ( array_key_exists( 'sf', $obj ) ) { - $form_array = $obj['sf']; - $hasExistingValues = true; - } - } - if ( array_key_exists( 'name', $form_array ) ) { - $formName = $form_array['name']; - } else { - $formName = ''; - } - $text = "\t<p>" . 'Name:' . ' ' . Html::input( 'sf_form_name', $formName, 'text', array( 'size' => 15 ) ) . "</p>\n"; - if ( array_key_exists( 'PageNameFormula', $form_array ) ) { - $pageNameFormula = $form_array['PageNameFormula']; - } else { - $pageNameFormula = ''; - } - $text .= "\t<p>" . 'Page name formula:' . ' ' . Html::input( 'sf_page_name_formula', $pageNameFormula, 'text', array( 'size' => 20 ) ) . "</p>\n"; - if ( array_key_exists( 'CreateTitle', $form_array ) ) { - $createTitle = $form_array['CreateTitle']; - } else { - $createTitle = ''; - } - $text .= "\t<p>" . 'Title of form for new pages:' . ' ' . Html::input( 'sf_create_title', $createTitle, 'text', array( 'size' => 25 ) ) . "</p>\n"; - if ( array_key_exists( 'EditTitle', $form_array ) ) { - $editTitle = $form_array['EditTitle']; - } else { - $editTitle = ''; - } - $text .= "\t<p>" . 'Title of form for existing pages:' . ' ' . Html::input( 'sf_edit_title', $editTitle, 'text', array( 'size' => 25 ) ) . "</p>\n"; - $text_extensions['sf'] = array( 'Form', '#CF9', $text, $hasExistingValues ); - - return true; - } - - /** - * Returns the HTML for inputs to define a single form field, - * within the Page Schemas 'edit schema' page. - */ - public static function getFieldHTMLForPS( $field, &$text_extensions ) { - if ( is_null( $field ) ) { - $fieldValues = array(); - } else { - $sf_array = $field->getObject('semanticforms_FormInput'); //this returns an array with property values filled - if ( array_key_exists( 'sf', $sf_array ) ) { - $fieldValues = $sf_array['sf']; - $hasExistingValues = true; - } else { - $fieldValues = array(); - $hasExistingValues = false; - } - } - - if ( array_key_exists( 'InputType', $fieldValues ) ) { - $inputType = $fieldValues['InputType']; - } else { - $inputType = ''; - } - $inputTypeAttrs = array( 'size' => 15 ); - $inputTypeInput = Html::input( 'sf_input_type_num', $inputType, 'text', $inputTypeAttrs ); - $text = '<p>Input type: ' . $inputTypeInput . '</p>'; - $text .= "\t" . '<p>Parameter name and its value as a key=value pair, separated by commas (if a value contains a comma, replace it with "\,"): For example: size=20,mandatory</p>' . "\n"; - $paramValues = array(); - foreach ( $fieldValues as $param => $value ) { - if ( !empty( $param ) && $param != 'InputType' ) { - if ( !empty( $value ) ) { - $paramValues[] = $param . '=' . $value; - } else { - $paramValues[] = $param; - } - } - } - $param_value_str = implode( ', ', $paramValues ); - $inputParamsAttrs = array( 'size' => 80 ); - $inputParamsInput = Html::input( 'sf_key_values_num', $param_value_str, 'text', $inputParamsAttrs ); - $text .= "\t<p>$inputParamsInput</p>\n"; - $text_extensions['sf'] = array( 'Form input', '#CF9', $text, $hasExistingValues ); - - return true; - } - - public static function getFormName( $psSchemaObj ) { - $formData = $psSchemaObj->getObject( 'semanticforms_Form' ); - return $formData['sf']['name']; - } - - public static function getMainFormInfo( $psSchemaObj ) { - $formData = $psSchemaObj->getObject( 'semanticforms_Form' ); - return $formData['sf']; - } - - public static function getFormFieldInfo( $psTemplateObj, $template_fields ) { - $form_fields = array(); - $fieldsInfo = $psTemplateObj->getFields(); - foreach ( $fieldsInfo as $i => $psFieldObj ) { - $fieldName = $psFieldObj->getName(); - $fieldFormInfo = $psFieldObj->getObject( 'semanticforms_FormInput' ); - if ( !is_null( $fieldFormInfo ) && array_key_exists( 'sf', $fieldFormInfo ) ) { - $formField = SFFormField::create( $i, $template_fields[$i] ); - $fieldFormArray = $fieldFormInfo['sf']; - foreach ($fieldFormArray as $var => $val ) { - if ( $var == 'InputType' ) { - $formField->setInputType( $val ); - } elseif ( $var == 'mandatory' ) { - $formField->setIsMandatory( true ); - } elseif ( $var == 'hidden' ) { - $formField->setIsHidden( true ); - } elseif ( $var == 'restricted' ) { - $formField->setIsRestricted( true ); - } else { - $formField->setFieldArg( $var, $val ); - } - } - $form_fields[] = $formField; - } - } - return $form_fields; - } - - /** - * Return the list of pages that Semantic Forms could generate from - * the current Page Schemas schema. - */ - public static function getPageList( $psSchemaObj, &$genPageList ) { - global $wgOut, $wgUser; - - $template_all = $psSchemaObj->getTemplates(); - foreach ( $template_all as $template ) { - $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() ); - $genPageList[] = $title; - } - $form_name = self::getFormName( $psSchemaObj ); - if ( $form_name == null ) { - return true; - } - //$form = SFForm::create( $form_name, $form_templates ); - $title = Title::makeTitleSafe( SF_NS_FORM, $form_name ); - $genPageList[] = $title; - return true; - } - - /** - * Creates wiki-text for a template, based on the contents of a - * <PageSchema> tag. - */ - public static function templateFieldsFromPSTemplateData( $templateFromSchema ) { - $field_all = $templateFromSchema->getFields(); - $template_fields = array(); - foreach( $field_all as $fieldObj ) { - $smw_array = $fieldObj->getObject('semanticmediawiki_Property'); - $propertyName = $smw_array['smw']['name']; - if ( $fieldObj->getLabel() == '' ) { - $fieldLabel = $fieldObj->getName(); - } else { - $fieldLabel = $fieldObj->getLabel(); - } - $templateField = SFTemplateField::create( - $fieldObj->getName(), - $fieldLabel, - $propertyName, - $fieldObj->isList(), - $fieldObj->getDelimiter() - ); - $template_fields[] = $templateField; - } - return $template_fields; - } - - public static function generateForm( $formName, $formTitle, $formTemplates, $formDataFromSchema ) { - global $wgUser; - - $form = SFForm::create( $formName, $formTemplates ); - if ( array_key_exists( 'PageNameFormula', $formDataFromSchema ) ) { - $form->setPageNameFormula( $formDataFromSchema['PageNameFormula'] ); - } - if ( array_key_exists( 'CreateTitle', $formDataFromSchema ) ) { - $form->setCreateTitle( $formDataFromSchema['CreateTitle'] ); - } - if ( array_key_exists( 'EditTitle', $formDataFromSchema ) ) { - $form->setEditTitle( $formDataFromSchema['EditTitle'] ); - } - $formContents = $form->createMarkup(); - $params = array(); - $params['user_id'] = $wgUser->getId(); - $params['page_text'] = $formContents; - $job = new PSCreatePageJob( $formTitle, $params ); - Job::batchInsert( array( $job ) ); - } - - /** - * Generate pages (form and templates) specified in the list. - */ - public static function generatePages( $psSchemaObj, $toGenPageList ) { - global $wgOut, $wgUser; - - $templatesFromSchema = $psSchemaObj->getTemplates(); - $form_templates = array(); - $jobs = array(); - foreach ( $templatesFromSchema as $templateFromSchema ) { - // Generate every specified template - $templateName = $templateFromSchema->getName(); - $templateTitle = Title::makeTitleSafe( NS_TEMPLATE, $templateName ); - $template_fields = array(); - $fullTemplateName = PageSchemas::titleString( $templateTitle ); - $template_fields = self::templateFieldsFromPSTemplateData( $templateFromSchema ); - $templateText = SFTemplateField::createTemplateText( $templateName, - $template_fields, null, $psSchemaObj->categoryName, null, null, null ); - if ( in_array( $fullTemplateName, $toGenPageList ) ) { - $params = array(); - $params['user_id'] = $wgUser->getId(); - $params['page_text'] = $templateText; - $jobs[] = new PSCreatePageJob( $templateTitle, $params ); - } - - $form_fields = self::getFormFieldInfo( $templateFromSchema, $template_fields ); - // Create template info for form, for use in generating - // the form (if it will be generated). - $form_template = SFTemplateInForm::create( - $templateName, - $templateFromSchema->getLabel(), - $templateFromSchema->isMultiple(), - null, - $form_fields - ); - $form_templates[] = $form_template; - } - Job::batchInsert( $jobs ); - - // Create form, if it's specified. - $form_name = self::getFormName( $psSchemaObj ); - if ( !empty( $form_name ) ) { - $formInfo = self::getMainFormInfo( $psSchemaObj ); - $formTitle = Title::makeTitleSafe( SF_NS_FORM, $form_name ); - $fullFormName = PageSchemas::titleString( $formTitle ); - if ( in_array( $fullFormName, $toGenPageList ) ) { - self::generateForm( $form_name, $formTitle, $form_templates, $formInfo ); - } - } - return true; - } - - /** - * This function parses the field elements in the XML of the pages. Hooks for Page Schemas extension. - */ - public static function parseFieldElements( $field_xml, &$text_object ) { - - foreach ( $field_xml->children() as $tag => $child ) { - if ( $tag == "semanticforms_FormInput" ) { - $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticForms", (string)$tag ); - foreach ( $child->children() as $prop ) { - if ( $prop->getName() == 'InputType' ) { - $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop->getName(), $prop ); - } else { - $prop_name = (string)$prop->attributes()->name; - $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop_name, (string)$prop ); - } - } - $text_object['sf'] = $text; - break; - } - } - return true; - } - public static function initProperties() { global $sfgContLang; _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs