r99690 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99689‎ | r99690 | r99691 >
Date:16:32, 13 October 2011
Author:yaron
Status:deferred
Tags:
Comment:
Overhaul of code that interacts with the Page Schemas extension, included added handling for template-specific schema tags
Modified paths:
  • /trunk/extensions/SemanticForms/SemanticForms.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_PageSchemas.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/SemanticForms.php
@@ -93,12 +93,16 @@
9494
9595 // Page Schemas extension hooks
9696 $wgHooks['PageSchemasGetObject'][] = 'SFPageSchemas::createPageSchemasObject' ;
 97+$wgHooks['PageSchemasGetPageList'][] = 'SFPageSchemas::getPageList' ;
9798 $wgHooks['PageSchemasGeneratePages'][] = 'SFPageSchemas::generatePages' ;
98 -$wgHooks['PSParseFieldElements'][] = 'SFPageSchemas::parseFieldElements' ;
99 -$wgHooks['PageSchemasGetPageList'][] = 'SFPageSchemas::getPageList' ;
 99+$wgHooks['PageSchemasGetSchemaDisplayInfo'][] = 'SFPageSchemas::getFormDisplayInfo' ;
 100+$wgHooks['PageSchemasGetTemplateDisplayInfo'][] = 'SFPageSchemas::getTemplateDisplayInfo' ;
 101+$wgHooks['PageSchemasGetFieldDisplayInfo'][] = 'SFPageSchemas::getFormInputDisplayInfo' ;
100102 $wgHooks['PageSchemasGetSchemaHTML'][] = 'SFPageSchemas::getSchemaHTML' ;
 103+$wgHooks['PageSchemasGetTemplateHTML'][] = 'SFPageSchemas::getTemplateHTML' ;
101104 $wgHooks['PageSchemasGetFieldHTML'][] = 'SFPageSchemas::getFieldHTML' ;
102105 $wgHooks['PageSchemasGetSchemaXML'][] = 'SFPageSchemas::getSchemaXML';
 106+$wgHooks['PageSchemasGetTemplateXML'][] = 'SFPageSchemas::getTemplateXML';
103107 $wgHooks['PageSchemasGetFieldXML'][] = 'SFPageSchemas::getFieldXML';
104108
105109 $wgAPIModules['sfautocomplete'] = 'SFAutocompleteAPI';
Index: trunk/extensions/SemanticForms/includes/SF_PageSchemas.php
@@ -30,6 +30,17 @@
3131 }
3232 }
3333 }
 34+ if ( $objectName == "semanticforms_TemplateDetails" ) {
 35+ foreach ( $xmlForField->children() as $tag => $child ) {
 36+ if ( $tag == $objectName ) {
 37+ foreach ( $child->children() as $tag => $formelem ) {
 38+ $sfarray[(string)$tag] = (string)$formelem;
 39+ }
 40+ $object['sf'] = $sfarray;
 41+ return true;
 42+ }
 43+ }
 44+ }
3445 if ( $objectName == "semanticforms_FormInput" ) {
3546 foreach ( $xmlForField->children() as $tag => $child ) {
3647 if ( $tag == $objectName ) {
@@ -76,8 +87,33 @@
7788 }
7889
7990 /**
80 - * Creates Page Schemas XML for a specific form field.
 91+ * Creates Page Schemas XML for form information on templates.
8192 */
 93+ public static function getTemplateXML( $request, &$xmlArray ) {
 94+ $xmlPerTemplate = array();
 95+ $templateNum = -1;
 96+ foreach ( $request->getValues() as $var => $val ) {
 97+ if ( substr( $var, 0, 18 ) == 'sf_template_label_' ) {
 98+ $templateNum = substr( $var, 18 );
 99+ $xml = '<semanticforms_TemplateDetails>';
 100+ if ( !empty( $val ) ) {
 101+ $xml .= "<Label>$val</Label>";
 102+ }
 103+ } elseif ( substr( $var, 0, 23 ) == 'sf_template_addanother_' ) {
 104+ if ( !empty( $val ) ) {
 105+ $xml .= "<AddAnotherText>$val</AddAnotherText>";
 106+ }
 107+ $xml .= '</semanticforms_TemplateDetails>';
 108+ $xmlPerTemplate[$templateNum] = $xml;
 109+ }
 110+ }
 111+ $xmlArray['sf'] = $xmlPerTemplate;
 112+ return true;
 113+ }
 114+
 115+ /**
 116+ * Creates Page Schemas XML for form fields.
 117+ */
82118 public static function getFieldXML( $request, &$xmlArray ) {
83119 $xmlPerField = array();
84120 $fieldNum = -1;
@@ -126,35 +162,97 @@
127163 $hasExistingValues = true;
128164 }
129165 }
 166+
 167+ // Get all the values from the page schema.
130168 if ( array_key_exists( 'name', $form_array ) ) {
131169 $formName = $form_array['name'];
132170 } else {
133171 $formName = '';
134172 }
135 - $text = "\t<p>" . 'Name:' . ' ' . Html::input( 'sf_form_name', $formName, 'text', array( 'size' => 15 ) ) . "</p>\n";
136173 if ( array_key_exists( 'PageNameFormula', $form_array ) ) {
137174 $pageNameFormula = $form_array['PageNameFormula'];
138175 } else {
139176 $pageNameFormula = '';
140177 }
141 - $text .= "\t<p>" . wfMsg( 'sf-pageschemas-pagenameformula' ) . ' ' . Html::input( 'sf_page_name_formula', $pageNameFormula, 'text', array( 'size' => 20 ) ) . "</p>\n";
142178 if ( array_key_exists( 'CreateTitle', $form_array ) ) {
143179 $createTitle = $form_array['CreateTitle'];
144180 } else {
145181 $createTitle = '';
146182 }
147 - $text .= "\t<p>" . wfMsg( 'sf-pageschemas-createtitle' ) . ' ' . Html::input( 'sf_create_title', $createTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
148183 if ( array_key_exists( 'EditTitle', $form_array ) ) {
149184 $editTitle = $form_array['EditTitle'];
150185 } else {
151186 $editTitle = '';
152187 }
153 - $text .= "\t<p>" . wfMsg( 'sf-pageschemas-edittitle' ) . ' ' . Html::input( 'sf_edit_title', $editTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
 188+
 189+ $text = "\t<p>" . wfMsg( 'ps-namelabel' ) . ' ' . Html::input( 'sf_form_name', $formName, 'text', array( 'size' => 15 ) ) . "</p>\n";
 190+ // The checkbox isn't actually a field in the page schema -
 191+ // we set it based on whether or not a page formula has been
 192+ // specified.
 193+ $twoStepProcessAttrs = array( 'id' => 'sf-two-step-process' );
 194+ if ( $pageNameFormula == '' ) {
 195+ $twoStepProcessAttrs['checked'] = true;
 196+ }
 197+ $text .= '<p>' . Html::input( 'sf_two_step_process', null, 'checkbox', $twoStepProcessAttrs );
 198+ $text .= ' Users must enter the page name before getting to the form (default)';
 199+ $text .= "</p>\n";
 200+ $text .= "\t<p id=\"sf-page-name-formula\">" . wfMsg( 'sf-pageschemas-pagenameformula' ) . ' ' . Html::input( 'sf_page_name_formula', $pageNameFormula, 'text', array( 'size' => 30 ) ) . "</p>\n";
 201+ $text .= "\t<p>" . wfMsg( 'sf-pageschemas-createtitle' ) . ' ' . Html::input( 'sf_create_title', $createTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
 202+ $text .= "\t<p id=\"sf-edit-title\">" . wfMsg( 'sf-pageschemas-edittitle' ) . ' ' . Html::input( 'sf_edit_title', $editTitle, 'text', array( 'size' => 25 ) ) . "</p>\n";
 203+
 204+ // Javascript for getting the checkbox to hide certain fields
 205+ $text .= <<<END
 206+<script type="text/javascript">
 207+jQuery.fn.toggleFormDataDisplay = function() {
 208+ if (jQuery(this).is(":checked")) {
 209+ jQuery('#sf-page-name-formula').css('display', 'none');
 210+ jQuery('#sf-edit-title').css('display', 'block');
 211+ } else {
 212+ jQuery('#sf-page-name-formula').css('display', 'block');
 213+ jQuery('#sf-edit-title').css('display', 'none');
 214+ }
 215+}
 216+jQuery('#sf-two-step-process').toggleFormDataDisplay();
 217+jQuery('#sf-two-step-process').click( function() {
 218+ jQuery(this).toggleFormDataDisplay();
 219+} );
 220+</script>
 221+
 222+END;
 223+
154224 $extensionsHTML['sf'] = array( 'Form', '#CF9', $text, $hasExistingValues );
155225
156226 return true;
157227 }
158228
 229+ public static function getTemplateHTML( $templateObj, &$extensionsHTML ) {
 230+ $form_array = array();
 231+ $hasExistingValues = false;
 232+ $templateLabel = null;
 233+ $addAnotherText = null;
 234+ if ( !is_null( $templateObj ) ) {
 235+ $obj = $templateObj->getObject( 'semanticforms_TemplateDetails' );
 236+ if ( array_key_exists( 'sf', $obj ) ) {
 237+ $form_array = $obj['sf'];
 238+ $hasExistingValues = true;
 239+ }
 240+ if ( array_key_exists( 'Label', $form_array ) ) {
 241+ $templateLabel = $form_array['Label'];
 242+ }
 243+ if ( array_key_exists( 'AddAnotherText', $form_array ) ) {
 244+ $addAnotherText = $form_array['AddAnotherText'];
 245+ }
 246+ }
 247+
 248+ $text = "\t<p>" . "The following fields are useful if there can be multiple instances of this template." . "</p>\n";
 249+ $text .= "\t<p>" . 'Label:' . ' ' . Html::input( 'sf_template_label_num', $templateLabel, 'text', array( 'size' => 15 ) ) . "</p>\n";
 250+ $text .= "\t<p>" . 'Text of button to add another instance (default is "Add another"):' . ' ' . Html::input( 'sf_template_addanother_num', $addAnotherText, 'text', array( 'size' => 25 ) ) . "</p>\n";
 251+
 252+ $extensionsHTML['sf'] = array( 'Details for template in form', '#CF9', $text, $hasExistingValues );
 253+
 254+ return true;
 255+ }
 256+
159257 /**
160258 * Returns the HTML for inputs to define a single form field,
161259 * within the Page Schemas 'edit schema' page.
@@ -310,10 +408,12 @@
311409 * Creates a form page, when called from the 'generatepages' page
312410 * of Page Schemas.
313411 */
314 - public static function generateForm( $formName, $formTitle, $formTemplates, $formDataFromSchema ) {
 412+ public static function generateForm( $formName, $formTitle,
 413+ $formTemplates, $formDataFromSchema, $categoryName ) {
315414 global $wgUser;
316415
317416 $form = SFForm::create( $formName, $formTemplates );
 417+ $form->setAssociatedCategory( $categoryName );
318418 if ( array_key_exists( 'PageNameFormula', $formDataFromSchema ) ) {
319419 $form->setPageNameFormula( $formDataFromSchema['PageNameFormula'] );
320420 }
@@ -338,6 +438,7 @@
339439 global $wgOut, $wgUser;
340440
341441 $templatesFromSchema = $psSchemaObj->getTemplates();
 442+
342443 $form_templates = array();
343444 $jobs = array();
344445 foreach ( $templatesFromSchema as $templateFromSchema ) {
@@ -347,21 +448,40 @@
348449 $template_fields = array();
349450 $fullTemplateName = PageSchemas::titleString( $templateTitle );
350451 $template_fields = self::getFieldsFromTemplateSchema( $templateFromSchema );
 452+ $internalObjProperty = SIOPageSchemas::getInternalObjectPropertyName( $templateFromSchema );
 453+ // TODO - actually, the category-setting should be
 454+ // smarter than this: if there's more than one
 455+ // template in the schema, it should probably be only
 456+ // the first non-multiple template that includes the
 457+ // category tag.
 458+ if ( $templateFromSchema->isMultiple() ) {
 459+ $categoryName = null;
 460+ } else {
 461+ $categoryName = $psSchemaObj->getCategoryName();
 462+ }
351463 $templateText = SFTemplateField::createTemplateText( $templateName,
352 - $template_fields, null, $psSchemaObj->categoryName, null, null, null );
 464+ $template_fields, $internalObjProperty, $categoryName, null, null, null );
353465 if ( in_array( $fullTemplateName, $toGenPageList ) ) {
 466+ // print_r($templateFromSchema);
 467+ //print "$templateText";
354468 $params = array();
355469 $params['user_id'] = $wgUser->getId();
356470 $params['page_text'] = $templateText;
357471 $jobs[] = new PSCreatePageJob( $templateTitle, $params );
358472 }
359473
 474+ $templateValues = self::getTemplateValues( $templateFromSchema );
 475+ if ( array_key_exists( 'Label', $templateValues ) ) {
 476+ $templateLabel = $templateValues['Label'];
 477+ } else {
 478+ $templateLabel = null;
 479+ }
360480 $form_fields = self::getFormFieldInfo( $templateFromSchema, $template_fields );
361481 // Create template info for form, for use in generating
362482 // the form (if it will be generated).
363483 $form_template = SFTemplateInForm::create(
364484 $templateName,
365 - $templateFromSchema->getLabel(),
 485+ $templateLabel,
366486 $templateFromSchema->isMultiple(),
367487 null,
368488 $form_fields
@@ -371,35 +491,88 @@
372492 Job::batchInsert( $jobs );
373493
374494 // Create form, if it's specified.
375 - $form_name = self::getFormName( $psSchemaObj );
376 - if ( !empty( $form_name ) ) {
 495+ $formName = self::getFormName( $psSchemaObj );
 496+ if ( !empty( $formName ) ) {
377497 $formInfo = self::getMainFormInfo( $psSchemaObj );
378 - $formTitle = Title::makeTitleSafe( SF_NS_FORM, $form_name );
 498+ $formTitle = Title::makeTitleSafe( SF_NS_FORM, $formName );
379499 $fullFormName = PageSchemas::titleString( $formTitle );
380500 if ( in_array( $fullFormName, $toGenPageList ) ) {
381 - self::generateForm( $form_name, $formTitle, $form_templates, $formInfo );
 501+ self::generateForm( $formName, $formTitle,
 502+ $form_templates, $formInfo, $categoryName );
382503 }
383504 }
384505 return true;
385506 }
386507
 508+ public static function getFormDisplayInfo( $schemaXML, &$text_object ) {
 509+ foreach ( $schemaXML->children() as $tag => $child ) {
 510+ if ( $tag == "semanticforms_Form" ) {
 511+ $formName = $child->attributes()->name;
 512+ $values = array();
 513+ foreach ( $child->children() as $tagName => $prop ) {
 514+ $values[$tagName] = (string)$prop;
 515+ }
 516+ $text_object['sf'] = array( 'Form', $formName, '#CF9', $values );
 517+ break;
 518+ }
 519+ }
 520+ return true;
 521+ }
 522+
 523+ public static function getTemplateValues( $psTemplate ) {
 524+ $values = array();
 525+ if ( $psTemplate instanceof PSTemplate ) {
 526+ $psTemplate = $psTemplate->templateXML;
 527+ }
 528+ foreach ( $psTemplate->children() as $tag => $child ) {
 529+ if ( $tag == "semanticforms_TemplateDetails" ) {
 530+ foreach ( $child->children() as $prop ) {
 531+ $values[$prop->getName()] = (string)$prop;
 532+ }
 533+ }
 534+ }
 535+ return $values;
 536+ }
 537+
387538 /**
388 - * Parses the field elements in the Page Schemas XML.
 539+ * Displays form details for one template in the Page Schemas XML.
389540 */
390 - public static function parseFieldElements( $field_xml, &$text_object ) {
 541+ public static function getTemplateDisplayInfo( $templateXML, &$text_object ) {
 542+ $templateValues = self::getTemplateValues( $templateXML );
 543+ if ( count( $templateValues ) == 0 ) {
 544+ return true;
 545+ }
391546
392 - foreach ( $field_xml->children() as $tag => $child ) {
 547+ $displayValues = array();
 548+ foreach ( $templateValues as $key => $value ) {
 549+ if ( $key == 'Label' ) {
 550+ $propName = 'Label';
 551+ } elseif ( $key == 'AddAnotherText' ) {
 552+ $propName = "'Add another' button";
 553+ }
 554+ $displayValues[$propName] = $value;
 555+ }
 556+ $text_object['sf'] = array( 'Details for template in form', null, '#CF9', $displayValues );
 557+ return true;
 558+ }
 559+
 560+ /**
 561+ * Displays data on a single form input in the Page Schemas XML.
 562+ */
 563+ public static function getFormInputDisplayInfo( $fieldXML, &$text_object ) {
 564+ foreach ( $fieldXML->children() as $tag => $child ) {
393565 if ( $tag == "semanticforms_FormInput" ) {
394 - $text = PageSchemas::tableMessageRowHTML( "paramAttr", wfMsg( 'specialpages-group-sf_group' ), (string)$tag );
 566+ $inputName = $child->attributes()->name;
 567+ $values = array();
395568 foreach ( $child->children() as $prop ) {
396569 if ( $prop->getName() == 'InputType' ) {
397570 $propName = 'Input type';
398571 } else {
399572 $propName = (string)$prop->attributes()->name;
400573 }
401 - $text .= PageSchemas::tableMessageRowHTML("paramAttrMsg", $propName, (string)$prop );
 574+ $values[$propName] = (string)$prop;
402575 }
403 - $text_object['sf'] = $text;
 576+ $text_object['sf'] = array( 'Form input', $inputName, '#CF9', $values );
404577 break;
405578 }
406579 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r99703Bug fixes for r99690yaron17:06, 13 October 2011