r97337 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97336‎ | r97337 | r97338 >
Date:21:18, 16 September 2011
Author:yaron
Status:ok
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_PageSchemas.php (added) (history)
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/SemanticForms.php
@@ -92,15 +92,14 @@
9393 $wgHooks['MakeGlobalVariablesScript'][] = 'SFFormUtils::setGlobalJSVariables';
9494
9595 // Page Schemas extension hooks
96 -$wgHooks['PageSchemasGetObject'][] = 'SFUtils::createPageSchemasObject' ;
97 -$wgHooks['PageSchemasGeneratePages'][] = 'SFUtils::generatePages' ;
98 -$wgHooks['PSParseFieldElements'][] = 'SFUtils::parseFieldElements' ;
99 -$wgHooks['PageSchemasGetPageList'][] = 'SFUtils::getPageList' ;
100 -$wgHooks['PageSchemasGetSchemaHTML'][] = 'SFUtils::getSchemaHTMLForPS' ;
101 -$wgHooks['PageSchemasGetFieldHTML'][] = 'SFUtils::getFieldHTMLForPS' ;
102 -$wgHooks['PageSchemasGetSchemaXML'][] = 'SFUtils::getSchemaXMLForPS';
103 -$wgHooks['PageSchemasGetFieldXML'][] = 'SFUtils::getFieldXMLForPS';
104 -//$wgHooks['getFilledHtmlTextForFieldInputs'][] = 'SFUtils::getFilledHtmlTextForPS' ;
 96+$wgHooks['PageSchemasGetObject'][] = 'SFPageSchemas::createPageSchemasObject' ;
 97+$wgHooks['PageSchemasGeneratePages'][] = 'SFPageSchemas::generatePages' ;
 98+$wgHooks['PSParseFieldElements'][] = 'SFPageSchemas::parseFieldElements' ;
 99+$wgHooks['PageSchemasGetPageList'][] = 'SFPageSchemas::getPageList' ;
 100+$wgHooks['PageSchemasGetSchemaHTML'][] = 'SFPageSchemas::getSchemaHTML' ;
 101+$wgHooks['PageSchemasGetFieldHTML'][] = 'SFPageSchemas::getFieldHTML' ;
 102+$wgHooks['PageSchemasGetSchemaXML'][] = 'SFPageSchemas::getSchemaXML';
 103+$wgHooks['PageSchemasGetFieldXML'][] = 'SFPageSchemas::getFieldXML';
105104
106105 $wgAPIModules['sfautocomplete'] = 'SFAutocompleteAPI';
107106 $wgAPIModules['sfautoedit'] = 'SFAutoeditAPI';
@@ -154,6 +153,7 @@
155154 $wgAutoloadClasses['SFFormEditPage'] = $sfgIP . '/includes/SF_FormEditPage.php';
156155 $wgAutoloadClasses['SFUtils'] = $sfgIP . '/includes/SF_Utils.php';
157156 $wgAutoloadClasses['SFFormLinker'] = $sfgIP . '/includes/SF_FormLinker.php';
 157+$wgAutoloadClasses['SFPageSchemas'] = $sfgIP . '/includes/SF_PageSchemas.php';
158158 $wgAutoloadClasses['SFParserFunctions'] = $sfgIP . '/includes/SF_ParserFunctions.php';
159159 $wgAutoloadClasses['SFAutocompleteAPI'] = $sfgIP . '/includes/SF_AutocompleteAPI.php';
160160 $wgAutoloadClasses['SFAutoeditAPI'] = $sfgIP . '/includes/SF_AutoeditAPI.php';
Index: trunk/extensions/SemanticForms/includes/SF_PageSchemas.php
@@ -0,0 +1,375 @@
 2+<?php
 3+/**
 4+ * Static functions for Semantic Forms, for use by the Page Schemas extension.
 5+ *
 6+ * @author Yaron Koren
 7+ * @author Ankit Garg
 8+ * @file
 9+ * @ingroup SF
 10+ */
 11+
 12+class SFPageSchemas {
 13+
 14+ /**
 15+ * Function to return the property based on the XML passed from the Page Schemas extension
 16+ */
 17+ public static function createPageSchemasObject( $objectName, $xmlForField, &$object ) {
 18+ $sfarray = array();
 19+ $formName="";
 20+ if ( $objectName == "semanticforms_Form" ) {
 21+ foreach ( $xmlForField->children() as $tag => $child ) {
 22+ if ( $tag == $objectName ) {
 23+ $formName = (string) $child->attributes()->name;
 24+ $sfarray['name'] = $formName;
 25+ foreach ($child->children() as $tag => $formelem) {
 26+ $sfarray[(string)$tag] = (string)$formelem;
 27+ }
 28+ $object['sf'] = $sfarray;
 29+ return true;
 30+ }
 31+ }
 32+ }
 33+ if ( $objectName == "semanticforms_FormInput" ) {
 34+ foreach ( $xmlForField->children() as $tag => $child ) {
 35+ if ( $tag == $objectName ) {
 36+ foreach ( $child->children() as $prop ) {
 37+ if ( $prop->getName() == 'InputType' ) {
 38+ $sfarray[$prop->getName()] = (string)$prop;
 39+ } else {
 40+ // Remember these values can be null also.
 41+ // While polulating in the page text, take care of that.
 42+ $sfarray[(string)$prop->attributes()->name] = (string)$prop;
 43+ }
 44+ }
 45+ // Setting value specific to SF in 'sf' index.
 46+ $object['sf'] = $sfarray;
 47+ return true;
 48+ }
 49+ }
 50+ }
 51+ return true;
 52+ }
 53+
 54+ public static function getSchemaXML( $request, &$xmlArray ) {
 55+ foreach ( $request->getValues() as $var => $val ) {
 56+ if ( $var == 'sf_form_name' ) {
 57+ $xml = '<semanticforms_Form name="'.$val.'" >';
 58+ } elseif ( $var == 'sf_page_name_formula' ) {
 59+ $xml .= '<PageNameFormula>'.$val.'</PageNameFormula>';
 60+ } elseif ( $var == 'sf_create_title' ) {
 61+ $xml .= '<CreateTitle>'.$val.'</CreateTitle>';
 62+ } elseif ( $var == 'sf_edit_title' ) {
 63+ $xml .= '<EditTitle>'.$val.'</EditTitle>';
 64+ $xml .= '</semanticforms_Form>';
 65+ }
 66+ }
 67+ $xmlArray['sf'] = $xml;
 68+ return true;
 69+ }
 70+
 71+ public static function getFieldXML( $request, &$xmlArray ) {
 72+ $xmlPerField = array();
 73+ $fieldNum = -1;
 74+ foreach ( $request->getValues() as $var => $val ) {
 75+ if ( substr( $var, 0, 14 ) == 'sf_input_type_' ) {
 76+ $fieldNum = substr( $var, 14 );
 77+ $xml = '<semanticforms_FormInput>';
 78+ if ( !empty( $val ) ) {
 79+ $xml .= '<InputType>' . $val . '</InputType>';
 80+ }
 81+ } elseif ( substr( $var, 0, 14 ) == 'sf_key_values_' ) {
 82+ if ( $val != '' ) {
 83+ // replace the comma substitution character that has no chance of
 84+ // being included in the values list - namely, the ASCII beep
 85+ $listSeparator = ',';
 86+ $key_values_str = str_replace( "\\$listSeparator", "\a", $val );
 87+ $key_values_array = explode( $listSeparator, $key_values_str );
 88+ foreach ( $key_values_array as $i => $value ) {
 89+ // replace beep back with comma, trim
 90+ $value = str_replace( "\a", $listSeparator, trim( $value ) );
 91+ $param_value = explode( "=", $value );
 92+ if ( $param_value[1] != null ) {
 93+ //handles Parameter name="size">20</Parameter>
 94+ $xml .= '<Parameter name="'.$param_value[0].'">'.$param_value[1].'</Parameter>';
 95+ } else {
 96+ //handles <Parameter name="mandatory" />
 97+ $xml .= '<Parameter name="'.$param_value[0].'"/>';
 98+ }
 99+ }
 100+ }
 101+ $xml .= '</semanticforms_FormInput>';
 102+ $xmlPerField[$fieldNum] = $xml;
 103+ }
 104+ }
 105+ $xmlArray['sf'] = $xmlPerField;
 106+ return true;
 107+ }
 108+
 109+ public static function getSchemaHTML( $pageSchemaObj, &$text_extensions ) {
 110+ $form_array = array();
 111+ $hasExistingValues = false;
 112+ if ( !is_null( $pageSchemaObj ) ) {
 113+ $obj = $pageSchemaObj->getObject('semanticforms_Form');
 114+ if ( array_key_exists( 'sf', $obj ) ) {
 115+ $form_array = $obj['sf'];
 116+ $hasExistingValues = true;
 117+ }
 118+ }
 119+ if ( array_key_exists( 'name', $form_array ) ) {
 120+ $formName = $form_array['name'];
 121+ } else {
 122+ $formName = '';
 123+ }
 124+ $text = "\t<p>" . 'Name:' . ' ' . Html::input( 'sf_form_name', $formName, 'text', array( 'size' => 15 ) ) . "</p>\n";
 125+ if ( array_key_exists( 'PageNameFormula', $form_array ) ) {
 126+ $pageNameFormula = $form_array['PageNameFormula'];
 127+ } else {
 128+ $pageNameFormula = '';
 129+ }
 130+ $text .= "\t<p>" . 'Page name formula:' . ' ' . Html::input( 'sf_page_name_formula', $pageNameFormula, 'text', array( 'size' => 20 ) ) . "</p>\n";
 131+ if ( array_key_exists( 'CreateTitle', $form_array ) ) {
 132+ $createTitle = $form_array['CreateTitle'];
 133+ } else {
 134+ $createTitle = '';
 135+ }
 136+ $text .= "\t<p>" . 'Title of form for new pages:' . ' ' . Html::input( 'sf_create_title', $createTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
 137+ if ( array_key_exists( 'EditTitle', $form_array ) ) {
 138+ $editTitle = $form_array['EditTitle'];
 139+ } else {
 140+ $editTitle = '';
 141+ }
 142+ $text .= "\t<p>" . 'Title of form for existing pages:' . ' ' . Html::input( 'sf_edit_title', $editTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
 143+ $text_extensions['sf'] = array( 'Form', '#CF9', $text, $hasExistingValues );
 144+
 145+ return true;
 146+ }
 147+
 148+ /**
 149+ * Returns the HTML for inputs to define a single form field,
 150+ * within the Page Schemas 'edit schema' page.
 151+ */
 152+ public static function getFieldHTML( $field, &$text_extensions ) {
 153+ if ( is_null( $field ) ) {
 154+ $fieldValues = array();
 155+ } else {
 156+ $sf_array = $field->getObject('semanticforms_FormInput'); //this returns an array with property values filled
 157+ if ( array_key_exists( 'sf', $sf_array ) ) {
 158+ $fieldValues = $sf_array['sf'];
 159+ $hasExistingValues = true;
 160+ } else {
 161+ $fieldValues = array();
 162+ $hasExistingValues = false;
 163+ }
 164+ }
 165+
 166+ if ( array_key_exists( 'InputType', $fieldValues ) ) {
 167+ $inputType = $fieldValues['InputType'];
 168+ } else {
 169+ $inputType = '';
 170+ }
 171+ $inputTypeAttrs = array( 'size' => 15 );
 172+ $inputTypeInput = Html::input( 'sf_input_type_num', $inputType, 'text', $inputTypeAttrs );
 173+ $text = '<p>Input type: ' . $inputTypeInput . '</p>';
 174+ $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";
 175+ $paramValues = array();
 176+ foreach ( $fieldValues as $param => $value ) {
 177+ if ( !empty( $param ) && $param != 'InputType' ) {
 178+ if ( !empty( $value ) ) {
 179+ $paramValues[] = $param . '=' . $value;
 180+ } else {
 181+ $paramValues[] = $param;
 182+ }
 183+ }
 184+ }
 185+ $param_value_str = implode( ', ', $paramValues );
 186+ $inputParamsAttrs = array( 'size' => 80 );
 187+ $inputParamsInput = Html::input( 'sf_key_values_num', $param_value_str, 'text', $inputParamsAttrs );
 188+ $text .= "\t<p>$inputParamsInput</p>\n";
 189+ $text_extensions['sf'] = array( 'Form input', '#CF9', $text, $hasExistingValues );
 190+
 191+ return true;
 192+ }
 193+
 194+ public static function getFormName( $psSchemaObj ) {
 195+ $formData = $psSchemaObj->getObject( 'semanticforms_Form' );
 196+ return $formData['sf']['name'];
 197+ }
 198+
 199+ public static function getMainFormInfo( $psSchemaObj ) {
 200+ $formData = $psSchemaObj->getObject( 'semanticforms_Form' );
 201+ return $formData['sf'];
 202+ }
 203+
 204+ public static function getFormFieldInfo( $psTemplateObj, $template_fields ) {
 205+ $form_fields = array();
 206+ $fieldsInfo = $psTemplateObj->getFields();
 207+ foreach ( $fieldsInfo as $i => $psFieldObj ) {
 208+ $fieldName = $psFieldObj->getName();
 209+ $fieldFormInfo = $psFieldObj->getObject( 'semanticforms_FormInput' );
 210+ if ( !is_null( $fieldFormInfo ) && array_key_exists( 'sf', $fieldFormInfo ) ) {
 211+ $formField = SFFormField::create( $i, $template_fields[$i] );
 212+ $fieldFormArray = $fieldFormInfo['sf'];
 213+ foreach ($fieldFormArray as $var => $val ) {
 214+ if ( $var == 'InputType' ) {
 215+ $formField->setInputType( $val );
 216+ } elseif ( $var == 'mandatory' ) {
 217+ $formField->setIsMandatory( true );
 218+ } elseif ( $var == 'hidden' ) {
 219+ $formField->setIsHidden( true );
 220+ } elseif ( $var == 'restricted' ) {
 221+ $formField->setIsRestricted( true );
 222+ } else {
 223+ $formField->setFieldArg( $var, $val );
 224+ }
 225+ }
 226+ $form_fields[] = $formField;
 227+ }
 228+ }
 229+ return $form_fields;
 230+ }
 231+
 232+ /**
 233+ * Return the list of pages that Semantic Forms could generate from
 234+ * the current Page Schemas schema.
 235+ */
 236+ public static function getPageList( $psSchemaObj, &$genPageList ) {
 237+ global $wgOut, $wgUser;
 238+
 239+ $template_all = $psSchemaObj->getTemplates();
 240+ foreach ( $template_all as $template ) {
 241+ $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() );
 242+ $genPageList[] = $title;
 243+ }
 244+ $form_name = self::getFormName( $psSchemaObj );
 245+ if ( $form_name == null ) {
 246+ return true;
 247+ }
 248+ //$form = SFForm::create( $form_name, $form_templates );
 249+ $title = Title::makeTitleSafe( SF_NS_FORM, $form_name );
 250+ $genPageList[] = $title;
 251+ return true;
 252+ }
 253+
 254+ /**
 255+ * Creates wiki-text for a template, based on the contents of a
 256+ * <PageSchema> tag.
 257+ */
 258+ public static function getFieldsFromTemplateSchema( $templateFromSchema ) {
 259+ $field_all = $templateFromSchema->getFields();
 260+ $template_fields = array();
 261+ foreach( $field_all as $fieldObj ) {
 262+ $smw_array = $fieldObj->getObject('semanticmediawiki_Property');
 263+ $propertyName = $smw_array['smw']['name'];
 264+ if ( $fieldObj->getLabel() == '' ) {
 265+ $fieldLabel = $fieldObj->getName();
 266+ } else {
 267+ $fieldLabel = $fieldObj->getLabel();
 268+ }
 269+ $templateField = SFTemplateField::create(
 270+ $fieldObj->getName(),
 271+ $fieldLabel,
 272+ $propertyName,
 273+ $fieldObj->isList(),
 274+ $fieldObj->getDelimiter()
 275+ );
 276+ $template_fields[] = $templateField;
 277+ }
 278+ return $template_fields;
 279+ }
 280+
 281+ public static function generateForm( $formName, $formTitle, $formTemplates, $formDataFromSchema ) {
 282+ global $wgUser;
 283+
 284+ $form = SFForm::create( $formName, $formTemplates );
 285+ if ( array_key_exists( 'PageNameFormula', $formDataFromSchema ) ) {
 286+ $form->setPageNameFormula( $formDataFromSchema['PageNameFormula'] );
 287+ }
 288+ if ( array_key_exists( 'CreateTitle', $formDataFromSchema ) ) {
 289+ $form->setCreateTitle( $formDataFromSchema['CreateTitle'] );
 290+ }
 291+ if ( array_key_exists( 'EditTitle', $formDataFromSchema ) ) {
 292+ $form->setEditTitle( $formDataFromSchema['EditTitle'] );
 293+ }
 294+ $formContents = $form->createMarkup();
 295+ $params = array();
 296+ $params['user_id'] = $wgUser->getId();
 297+ $params['page_text'] = $formContents;
 298+ $job = new PSCreatePageJob( $formTitle, $params );
 299+ Job::batchInsert( array( $job ) );
 300+ }
 301+
 302+ /**
 303+ * Generate pages (form and templates) specified in the list.
 304+ */
 305+ public static function generatePages( $psSchemaObj, $toGenPageList ) {
 306+ global $wgOut, $wgUser;
 307+
 308+ $templatesFromSchema = $psSchemaObj->getTemplates();
 309+ $form_templates = array();
 310+ $jobs = array();
 311+ foreach ( $templatesFromSchema as $templateFromSchema ) {
 312+ // Generate every specified template
 313+ $templateName = $templateFromSchema->getName();
 314+ $templateTitle = Title::makeTitleSafe( NS_TEMPLATE, $templateName );
 315+ $template_fields = array();
 316+ $fullTemplateName = PageSchemas::titleString( $templateTitle );
 317+ $template_fields = self::getFieldsFromTemplateSchema( $templateFromSchema );
 318+ $templateText = SFTemplateField::createTemplateText( $templateName,
 319+ $template_fields, null, $psSchemaObj->categoryName, null, null, null );
 320+ if ( in_array( $fullTemplateName, $toGenPageList ) ) {
 321+ $params = array();
 322+ $params['user_id'] = $wgUser->getId();
 323+ $params['page_text'] = $templateText;
 324+ $jobs[] = new PSCreatePageJob( $templateTitle, $params );
 325+ }
 326+
 327+ $form_fields = self::getFormFieldInfo( $templateFromSchema, $template_fields );
 328+ // Create template info for form, for use in generating
 329+ // the form (if it will be generated).
 330+ $form_template = SFTemplateInForm::create(
 331+ $templateName,
 332+ $templateFromSchema->getLabel(),
 333+ $templateFromSchema->isMultiple(),
 334+ null,
 335+ $form_fields
 336+ );
 337+ $form_templates[] = $form_template;
 338+ }
 339+ Job::batchInsert( $jobs );
 340+
 341+ // Create form, if it's specified.
 342+ $form_name = self::getFormName( $psSchemaObj );
 343+ if ( !empty( $form_name ) ) {
 344+ $formInfo = self::getMainFormInfo( $psSchemaObj );
 345+ $formTitle = Title::makeTitleSafe( SF_NS_FORM, $form_name );
 346+ $fullFormName = PageSchemas::titleString( $formTitle );
 347+ if ( in_array( $fullFormName, $toGenPageList ) ) {
 348+ self::generateForm( $form_name, $formTitle, $form_templates, $formInfo );
 349+ }
 350+ }
 351+ return true;
 352+ }
 353+
 354+ /**
 355+ * Parses the field elements in the Page Schemas XML.
 356+ */
 357+ public static function parseFieldElements( $field_xml, &$text_object ) {
 358+
 359+ foreach ( $field_xml->children() as $tag => $child ) {
 360+ if ( $tag == "semanticforms_FormInput" ) {
 361+ $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticForms", (string)$tag );
 362+ foreach ( $child->children() as $prop ) {
 363+ if ( $prop->getName() == 'InputType' ) {
 364+ $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop->getName(), $prop );
 365+ } else {
 366+ $prop_name = (string)$prop->attributes()->name;
 367+ $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop_name, (string)$prop );
 368+ }
 369+ }
 370+ $text_object['sf'] = $text;
 371+ break;
 372+ }
 373+ }
 374+ return true;
 375+ }
 376+}
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -3,7 +3,6 @@
44 * Helper functions for the Semantic Forms extension.
55 *
66 * @author Yaron Koren
7 - * @author Ankit Garg
87 * @file
98 * @ingroup SF
109 */
@@ -153,369 +152,6 @@
154153 }
155154 }
156155
157 - /**
158 - * Function to return the property based on the XML passed from the Page Schemas extension
159 - */
160 - public static function createPageSchemasObject( $objectName, $xmlForField, &$object ) {
161 - $sfarray = array();
162 - $formName="";
163 - if ( $objectName == "semanticforms_Form" ) {
164 - foreach ( $xmlForField->children() as $tag => $child ) {
165 - if ( $tag == $objectName ) {
166 - $formName = (string) $child->attributes()->name;
167 - $sfarray['name'] = $formName;
168 - foreach ($child->children() as $tag => $formelem) {
169 - $sfarray[(string)$tag] = (string)$formelem;
170 - }
171 - $object['sf'] = $sfarray;
172 - return true;
173 - }
174 - }
175 - }
176 - if ( $objectName == "semanticforms_FormInput" ) {
177 - foreach ( $xmlForField->children() as $tag => $child ) {
178 - if ( $tag == $objectName ) {
179 - foreach ( $child->children() as $prop ) {
180 - if ( $prop->getName() == 'InputType' ) {
181 - $sfarray[$prop->getName()] = (string)$prop;
182 - } else {
183 - // Remember these values can be null also.
184 - // While polulating in the page text, take care of that.
185 - $sfarray[(string)$prop->attributes()->name] = (string)$prop;
186 - }
187 - }
188 - // Setting value specific to SF in 'sf' index.
189 - $object['sf'] = $sfarray;
190 - return true;
191 - }
192 - }
193 - }
194 - return true;
195 - }
196 -
197 - public static function getSchemaXMLForPS( $request, &$xmlArray ) {
198 - foreach ( $request->getValues() as $var => $val ) {
199 - if ( $var == 'sf_form_name' ) {
200 - $xml = '<semanticforms_Form name="'.$val.'" >';
201 - } elseif ( $var == 'sf_page_name_formula' ) {
202 - $xml .= '<PageNameFormula>'.$val.'</PageNameFormula>';
203 - } elseif ( $var == 'sf_create_title' ) {
204 - $xml .= '<CreateTitle>'.$val.'</CreateTitle>';
205 - } elseif ( $var == 'sf_edit_title' ) {
206 - $xml .= '<EditTitle>'.$val.'</EditTitle>';
207 - $xml .= '</semanticforms_Form>';
208 - }
209 - }
210 - $xmlArray['sf'] = $xml;
211 - return true;
212 - }
213 -
214 - public static function getFieldXMLForPS( $request, &$xmlArray ) {
215 - $xmlPerField = array();
216 - $fieldNum = -1;
217 - foreach ( $request->getValues() as $var => $val ) {
218 - if ( substr( $var, 0, 14 ) == 'sf_input_type_' ) {
219 - $fieldNum = substr( $var, 14 );
220 - $xml = '<semanticforms_FormInput>';
221 - if ( !empty( $val ) ) {
222 - $xml .= '<InputType>' . $val . '</InputType>';
223 - }
224 - } elseif ( substr( $var, 0, 14 ) == 'sf_key_values_' ) {
225 - if ( $val != '' ) {
226 - // replace the comma substitution character that has no chance of
227 - // being included in the values list - namely, the ASCII beep
228 - $listSeparator = ',';
229 - $key_values_str = str_replace( "\\$listSeparator", "\a", $val );
230 - $key_values_array = explode( $listSeparator, $key_values_str );
231 - foreach ( $key_values_array as $i => $value ) {
232 - // replace beep back with comma, trim
233 - $value = str_replace( "\a", $listSeparator, trim( $value ) );
234 - $param_value = explode( "=", $value );
235 - if ( $param_value[1] != null ) {
236 - //handles Parameter name="size">20</Parameter>
237 - $xml .= '<Parameter name="'.$param_value[0].'">'.$param_value[1].'</Parameter>';
238 - } else {
239 - //handles <Parameter name="mandatory" />
240 - $xml .= '<Parameter name="'.$param_value[0].'"/>';
241 - }
242 - }
243 - }
244 - $xml .= '</semanticforms_FormInput>';
245 - $xmlPerField[$fieldNum] = $xml;
246 - }
247 - }
248 - $xmlArray['sf'] = $xmlPerField;
249 - return true;
250 - }
251 -
252 - public static function getSchemaHTMLForPS( $pageSchemaObj, &$text_extensions ) {
253 - $form_array = array();
254 - $hasExistingValues = false;
255 - if ( !is_null( $pageSchemaObj ) ) {
256 - $obj = $pageSchemaObj->getObject('semanticforms_Form');
257 - if ( array_key_exists( 'sf', $obj ) ) {
258 - $form_array = $obj['sf'];
259 - $hasExistingValues = true;
260 - }
261 - }
262 - if ( array_key_exists( 'name', $form_array ) ) {
263 - $formName = $form_array['name'];
264 - } else {
265 - $formName = '';
266 - }
267 - $text = "\t<p>" . 'Name:' . ' ' . Html::input( 'sf_form_name', $formName, 'text', array( 'size' => 15 ) ) . "</p>\n";
268 - if ( array_key_exists( 'PageNameFormula', $form_array ) ) {
269 - $pageNameFormula = $form_array['PageNameFormula'];
270 - } else {
271 - $pageNameFormula = '';
272 - }
273 - $text .= "\t<p>" . 'Page name formula:' . ' ' . Html::input( 'sf_page_name_formula', $pageNameFormula, 'text', array( 'size' => 20 ) ) . "</p>\n";
274 - if ( array_key_exists( 'CreateTitle', $form_array ) ) {
275 - $createTitle = $form_array['CreateTitle'];
276 - } else {
277 - $createTitle = '';
278 - }
279 - $text .= "\t<p>" . 'Title of form for new pages:' . ' ' . Html::input( 'sf_create_title', $createTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
280 - if ( array_key_exists( 'EditTitle', $form_array ) ) {
281 - $editTitle = $form_array['EditTitle'];
282 - } else {
283 - $editTitle = '';
284 - }
285 - $text .= "\t<p>" . 'Title of form for existing pages:' . ' ' . Html::input( 'sf_edit_title', $editTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
286 - $text_extensions['sf'] = array( 'Form', '#CF9', $text, $hasExistingValues );
287 -
288 - return true;
289 - }
290 -
291 - /**
292 - * Returns the HTML for inputs to define a single form field,
293 - * within the Page Schemas 'edit schema' page.
294 - */
295 - public static function getFieldHTMLForPS( $field, &$text_extensions ) {
296 - if ( is_null( $field ) ) {
297 - $fieldValues = array();
298 - } else {
299 - $sf_array = $field->getObject('semanticforms_FormInput'); //this returns an array with property values filled
300 - if ( array_key_exists( 'sf', $sf_array ) ) {
301 - $fieldValues = $sf_array['sf'];
302 - $hasExistingValues = true;
303 - } else {
304 - $fieldValues = array();
305 - $hasExistingValues = false;
306 - }
307 - }
308 -
309 - if ( array_key_exists( 'InputType', $fieldValues ) ) {
310 - $inputType = $fieldValues['InputType'];
311 - } else {
312 - $inputType = '';
313 - }
314 - $inputTypeAttrs = array( 'size' => 15 );
315 - $inputTypeInput = Html::input( 'sf_input_type_num', $inputType, 'text', $inputTypeAttrs );
316 - $text = '<p>Input type: ' . $inputTypeInput . '</p>';
317 - $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";
318 - $paramValues = array();
319 - foreach ( $fieldValues as $param => $value ) {
320 - if ( !empty( $param ) && $param != 'InputType' ) {
321 - if ( !empty( $value ) ) {
322 - $paramValues[] = $param . '=' . $value;
323 - } else {
324 - $paramValues[] = $param;
325 - }
326 - }
327 - }
328 - $param_value_str = implode( ', ', $paramValues );
329 - $inputParamsAttrs = array( 'size' => 80 );
330 - $inputParamsInput = Html::input( 'sf_key_values_num', $param_value_str, 'text', $inputParamsAttrs );
331 - $text .= "\t<p>$inputParamsInput</p>\n";
332 - $text_extensions['sf'] = array( 'Form input', '#CF9', $text, $hasExistingValues );
333 -
334 - return true;
335 - }
336 -
337 - public static function getFormName( $psSchemaObj ) {
338 - $formData = $psSchemaObj->getObject( 'semanticforms_Form' );
339 - return $formData['sf']['name'];
340 - }
341 -
342 - public static function getMainFormInfo( $psSchemaObj ) {
343 - $formData = $psSchemaObj->getObject( 'semanticforms_Form' );
344 - return $formData['sf'];
345 - }
346 -
347 - public static function getFormFieldInfo( $psTemplateObj, $template_fields ) {
348 - $form_fields = array();
349 - $fieldsInfo = $psTemplateObj->getFields();
350 - foreach ( $fieldsInfo as $i => $psFieldObj ) {
351 - $fieldName = $psFieldObj->getName();
352 - $fieldFormInfo = $psFieldObj->getObject( 'semanticforms_FormInput' );
353 - if ( !is_null( $fieldFormInfo ) && array_key_exists( 'sf', $fieldFormInfo ) ) {
354 - $formField = SFFormField::create( $i, $template_fields[$i] );
355 - $fieldFormArray = $fieldFormInfo['sf'];
356 - foreach ($fieldFormArray as $var => $val ) {
357 - if ( $var == 'InputType' ) {
358 - $formField->setInputType( $val );
359 - } elseif ( $var == 'mandatory' ) {
360 - $formField->setIsMandatory( true );
361 - } elseif ( $var == 'hidden' ) {
362 - $formField->setIsHidden( true );
363 - } elseif ( $var == 'restricted' ) {
364 - $formField->setIsRestricted( true );
365 - } else {
366 - $formField->setFieldArg( $var, $val );
367 - }
368 - }
369 - $form_fields[] = $formField;
370 - }
371 - }
372 - return $form_fields;
373 - }
374 -
375 - /**
376 - * Return the list of pages that Semantic Forms could generate from
377 - * the current Page Schemas schema.
378 - */
379 - public static function getPageList( $psSchemaObj, &$genPageList ) {
380 - global $wgOut, $wgUser;
381 -
382 - $template_all = $psSchemaObj->getTemplates();
383 - foreach ( $template_all as $template ) {
384 - $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() );
385 - $genPageList[] = $title;
386 - }
387 - $form_name = self::getFormName( $psSchemaObj );
388 - if ( $form_name == null ) {
389 - return true;
390 - }
391 - //$form = SFForm::create( $form_name, $form_templates );
392 - $title = Title::makeTitleSafe( SF_NS_FORM, $form_name );
393 - $genPageList[] = $title;
394 - return true;
395 - }
396 -
397 - /**
398 - * Creates wiki-text for a template, based on the contents of a
399 - * <PageSchema> tag.
400 - */
401 - public static function templateFieldsFromPSTemplateData( $templateFromSchema ) {
402 - $field_all = $templateFromSchema->getFields();
403 - $template_fields = array();
404 - foreach( $field_all as $fieldObj ) {
405 - $smw_array = $fieldObj->getObject('semanticmediawiki_Property');
406 - $propertyName = $smw_array['smw']['name'];
407 - if ( $fieldObj->getLabel() == '' ) {
408 - $fieldLabel = $fieldObj->getName();
409 - } else {
410 - $fieldLabel = $fieldObj->getLabel();
411 - }
412 - $templateField = SFTemplateField::create(
413 - $fieldObj->getName(),
414 - $fieldLabel,
415 - $propertyName,
416 - $fieldObj->isList(),
417 - $fieldObj->getDelimiter()
418 - );
419 - $template_fields[] = $templateField;
420 - }
421 - return $template_fields;
422 - }
423 -
424 - public static function generateForm( $formName, $formTitle, $formTemplates, $formDataFromSchema ) {
425 - global $wgUser;
426 -
427 - $form = SFForm::create( $formName, $formTemplates );
428 - if ( array_key_exists( 'PageNameFormula', $formDataFromSchema ) ) {
429 - $form->setPageNameFormula( $formDataFromSchema['PageNameFormula'] );
430 - }
431 - if ( array_key_exists( 'CreateTitle', $formDataFromSchema ) ) {
432 - $form->setCreateTitle( $formDataFromSchema['CreateTitle'] );
433 - }
434 - if ( array_key_exists( 'EditTitle', $formDataFromSchema ) ) {
435 - $form->setEditTitle( $formDataFromSchema['EditTitle'] );
436 - }
437 - $formContents = $form->createMarkup();
438 - $params = array();
439 - $params['user_id'] = $wgUser->getId();
440 - $params['page_text'] = $formContents;
441 - $job = new PSCreatePageJob( $formTitle, $params );
442 - Job::batchInsert( array( $job ) );
443 - }
444 -
445 - /**
446 - * Generate pages (form and templates) specified in the list.
447 - */
448 - public static function generatePages( $psSchemaObj, $toGenPageList ) {
449 - global $wgOut, $wgUser;
450 -
451 - $templatesFromSchema = $psSchemaObj->getTemplates();
452 - $form_templates = array();
453 - $jobs = array();
454 - foreach ( $templatesFromSchema as $templateFromSchema ) {
455 - // Generate every specified template
456 - $templateName = $templateFromSchema->getName();
457 - $templateTitle = Title::makeTitleSafe( NS_TEMPLATE, $templateName );
458 - $template_fields = array();
459 - $fullTemplateName = PageSchemas::titleString( $templateTitle );
460 - $template_fields = self::templateFieldsFromPSTemplateData( $templateFromSchema );
461 - $templateText = SFTemplateField::createTemplateText( $templateName,
462 - $template_fields, null, $psSchemaObj->categoryName, null, null, null );
463 - if ( in_array( $fullTemplateName, $toGenPageList ) ) {
464 - $params = array();
465 - $params['user_id'] = $wgUser->getId();
466 - $params['page_text'] = $templateText;
467 - $jobs[] = new PSCreatePageJob( $templateTitle, $params );
468 - }
469 -
470 - $form_fields = self::getFormFieldInfo( $templateFromSchema, $template_fields );
471 - // Create template info for form, for use in generating
472 - // the form (if it will be generated).
473 - $form_template = SFTemplateInForm::create(
474 - $templateName,
475 - $templateFromSchema->getLabel(),
476 - $templateFromSchema->isMultiple(),
477 - null,
478 - $form_fields
479 - );
480 - $form_templates[] = $form_template;
481 - }
482 - Job::batchInsert( $jobs );
483 -
484 - // Create form, if it's specified.
485 - $form_name = self::getFormName( $psSchemaObj );
486 - if ( !empty( $form_name ) ) {
487 - $formInfo = self::getMainFormInfo( $psSchemaObj );
488 - $formTitle = Title::makeTitleSafe( SF_NS_FORM, $form_name );
489 - $fullFormName = PageSchemas::titleString( $formTitle );
490 - if ( in_array( $fullFormName, $toGenPageList ) ) {
491 - self::generateForm( $form_name, $formTitle, $form_templates, $formInfo );
492 - }
493 - }
494 - return true;
495 - }
496 -
497 - /**
498 - * This function parses the field elements in the XML of the pages. Hooks for Page Schemas extension.
499 - */
500 - public static function parseFieldElements( $field_xml, &$text_object ) {
501 -
502 - foreach ( $field_xml->children() as $tag => $child ) {
503 - if ( $tag == "semanticforms_FormInput" ) {
504 - $text = PageSchemas::tableMessageRowHTML( "paramAttr", "SemanticForms", (string)$tag );
505 - foreach ( $child->children() as $prop ) {
506 - if ( $prop->getName() == 'InputType' ) {
507 - $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop->getName(), $prop );
508 - } else {
509 - $prop_name = (string)$prop->attributes()->name;
510 - $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $prop_name, (string)$prop );
511 - }
512 - }
513 - $text_object['sf'] = $text;
514 - break;
515 - }
516 - }
517 - return true;
518 - }
519 -
520156 public static function initProperties() {
521157 global $sfgContLang;
522158

Status & tagging log