r95820 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95819‎ | r95820 | r95821 >
Date:22:29, 30 August 2011
Author:yaron
Status:deferred
Tags:
Comment:
Further cleanup and refactoring, including splitting up generatePages() into several methods
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -178,13 +178,14 @@
179179 if ( $prop->getName() == 'InputType' ) {
180180 $sfarray[$prop->getName()] = (string)$prop;
181181 } else {
182 - //Remember these values can be null also. While polulating in the page text, take care of that.
 182+ // Remember these values can be null also.
 183+ // While polulating in the page text, take care of that.
183184 $sfarray[(string)$prop->attributes()->name] = (string)$prop;
184185 }
185186 }
186187 }
187188 }
188 - //Setting value specific to SF in 'sf' index.
 189+ // Setting value specific to SF in 'sf' index.
189190 $object['sf'] = $sfarray;
190191 }
191192 return true;
@@ -244,7 +245,6 @@
245246 }
246247
247248 public static function getFilledHtmlTextForPS( $pageSchemaObj, &$text_extensions ) {
248 - $template_fields = array();
249249 $html_text = "";
250250 $template_all = $pageSchemaObj->getTemplates();
251251 $html_text_array = array();
@@ -288,6 +288,7 @@
289289 $text_extensions['sf_form']= $form_html_text;
290290 return true;
291291 }
 292+
292293 public static function getHtmlTextForPS( &$js_extensions ,&$text_extensions ) {
293294 $html_text = "";
294295 $form_text = "" ;
@@ -307,17 +308,31 @@
308309 $text_extensions['sf_form'] = $form_text;
309310 return true;
310311 }
 312+
 313+ public static function getFormName( $psSchemaObj ) {
 314+ $formData = $psSchemaObj->getObject( 'semanticforms_Form' );
 315+ return $formData['sf']['name'];
 316+ }
 317+
 318+ public static function getFormArray( $psSchemaObj ) {
 319+ $formData = $psSchemaObj->getObject( 'semanticforms_Form' );
 320+ return $formData['sf'];
 321+ }
 322+
311323 /**
312 - */
 324+ * Return the list of pages that Semantic Forms could generate from
 325+ * the current Page Schemas schema.
 326+ */
313327 public static function getPageList( $psSchemaObj, &$genPageList ) {
314328 global $wgOut, $wgUser;
 329+
315330 $template_all = $psSchemaObj->getTemplates();
316331 foreach ( $template_all as $template ) {
317332 $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() );
318333 $genPageList[] = $title;
319334 }
320 - $form_name = $psSchemaObj->getFormName();
321 - if( $form_name == null ) {
 335+ $form_name = self::getFormName( $psSchemaObj );
 336+ if ( $form_name == null ) {
322337 return true;
323338 }
324339 //$form = SFForm::create( $form_name, $form_templates );
@@ -325,62 +340,94 @@
326341 $genPageList[] = $title;
327342 return true;
328343 }
 344+
329345 /**
330 - */
 346+ * Creates wiki-text for a template, based on the contents of a <PageSchema> tag.
 347+ */
 348+ public static function templateTextFromPSTemplateData( $templateName, $templateFromSchema, $categoryName ) {
 349+ $field_all = $templateFromSchema->getFields();
 350+ $template_fields = array();
 351+ foreach( $field_all as $fieldObj ) {
 352+ $smw_array = $fieldObj->getObject('semanticmediawiki_Property');
 353+ $propertyName = $smw_array['smw']['name'];
 354+ $templateField = SFTemplateField::create(
 355+ $fieldObj->getName(),
 356+ $fieldObj->getLabel(),
 357+ $propertyName,
 358+ $fieldObj->isList(),
 359+ $fieldObj->getDelimiter()
 360+ );
 361+ $template_fields[] = $templateField;
 362+ }
 363+ return SFTemplateField::createTemplateText( $templateName,
 364+ $template_fields, null, $categoryName, null, null, null );
 365+ }
 366+
 367+ public static function generateForm( $formName, $formTitle, $formTemplates, $formDataFromSchema ) {
 368+ global $wgUser;
 369+
 370+ $form = SFForm::create( $formName, $formTemplates );
 371+ if ( array_key_exists( 'PageNameFormula', $formDataFromSchema ) ) {
 372+ $form->setPageNameFormula( $formDataFromSchema['PageNameFormula'] );
 373+ }
 374+ if ( array_key_exists( 'CreateTitle', $formDataFromSchema ) ) {
 375+ $form->setCreateTitle( $formDataFromSchema['CreateTitle'] );
 376+ }
 377+ if ( array_key_exists( 'EditTitle', $formDataFromSchema ) ) {
 378+ $form->setEditTitle( $formDataFromSchema['EditTitle'] );
 379+ }
 380+ $formContents = $form->createMarkup();
 381+ $params = array();
 382+ $params['user_id'] = $wgUser->getId();
 383+ $params['page_text'] = $formContents;
 384+ $job = new PSCreatePageJob( $formTitle, $params );
 385+ Job::batchInsert( array( $job ) );
 386+ }
 387+
 388+ /**
 389+ * Generate pages (form and templates) specified in the list.
 390+ */
331391 public static function generatePages( $psSchemaObj, $toGenPageList ) {
332392 global $wgOut, $wgUser;
333 - $template_all = $psSchemaObj->getTemplates();
 393+
 394+ $templatesFromSchema = $psSchemaObj->getTemplates();
334395 $form_templates = array();
335396 $jobs = array();
336 - foreach ( $template_all as $template ) {
337 - $template_array = array();
338 - $template_array['name'] = $template->getName();
339 - $template_array['category_name'] = $psSchemaObj->categoryName;
340 - $field_all = $template->getFields();
341 - $field_count = 0; //counts the number of fields
342 - $template_fields = array();
343 - foreach( $field_all as $fieldObj ) { //for each Field, retrieve smw properties and fill $prop_name , $prop_type
344 - $field_count++;
345 - $sf_array = $fieldObj->getObject('semanticforms_FormInput');//this returns an array with property values filled
346 - $form_input_array = $sf_array['sf'];
347 - $smw_array = $fieldObj->getObject('semanticmediawiki_Property'); //this returns an array with property values filled
348 - $prop_array = $smw_array['smw'];
349 - $field_t = SFTemplateField::create( $fieldObj->getName(), $fieldObj->getLabel(), $prop_array['name'], $fieldObj->isList() ,$fieldObj->getDelimiter());
350 - $template_fields[] = $field_t;
351 - }
352 - $template_text = SFTemplateField::createTemplateText( $template->getName(), $template_fields, null, $psSchemaObj->categoryName, null, null, null );
353 - $title = Title::makeTitleSafe( NS_TEMPLATE, $template->getName() );
354 - $key_title = PageSchemas::titleString( $title );
355 - if ( in_array( $key_title, $toGenPageList ) ) {
 397+ foreach ( $templatesFromSchema as $templateFromSchema ) {
 398+ // Generate every specified template
 399+ $templateName = $templateFromSchema->getName();
 400+ $templateTitle = Title::makeTitleSafe( NS_TEMPLATE, $templateName );
 401+ $fullTemplateName = PageSchemas::titleString( $templateTitle );
 402+ if ( in_array( $fullTemplateName, $toGenPageList ) ) {
 403+ $templateText = self::templateTextFromPSTemplateData( $templateName,
 404+ $templateFromSchema, $psSchemaObj->categoryName );
356405 $params = array();
357406 $params['user_id'] = $wgUser->getId();
358 - $params['page_text'] = $template_text;
359 - $jobs[] = new PSCreatePageJob( $title, $params );
 407+ $params['page_text'] = $templateText;
 408+ $jobs[] = new PSCreatePageJob( $templateTitle, $params );
360409 }
361 - //Creating Form Templates at this time
362 - $form_template = SFTemplateInForm::create( $template->getName(), $template->getLabel(), $template->isMultiple() );
 410+
 411+ // Create template info for form, for use in generating the form
 412+ // (if it will be generated).
 413+ $form_template = SFTemplateInForm::create(
 414+ $templateName,
 415+ $templateFromSchema->getLabel(),
 416+ $templateFromSchema->isMultiple()
 417+ );
363418 $form_templates[] = $form_template;
364419 }
365420 Job::batchInsert( $jobs );
366 - $form_name = $psSchemaObj->getFormName();
367 - $form_array = $psSchemaObj->getFormArray();
368 - if ( $form_name == null ) {
369 - return true;
 421+
 422+ // Create form, if it's specified.
 423+ $form_name = self::getFormName( $psSchemaObj );
 424+ if ( !empty( $form_name ) ) {
 425+ $form_array = self::getFormArray( $psSchemaObj );
 426+ $formTitle = Title::makeTitleSafe( SF_NS_FORM, $form_name );
 427+ $fullFormName = PageSchemas::titleString( $formTitle );
 428+ if ( in_array( $fullFormName, $toGenPageList ) ) {
 429+ self::generateForm( $form_name, $formTitle, $form_templates, $form_array );
 430+ }
370431 }
371 - $form = SFForm::create( $form_name, $form_templates );
372 - $form->setPageNameFormula( $form_array['PageNameFormula'] );
373 - $form->setCreateTitle( $form_array['CreateTitle'] );
374 - $form->setEditTitle( $form_array['EditTitle'] );
375 - $title = Title::makeTitleSafe( SF_NS_FORM, $form->getFormName() );
376 - $key_title = PageSchemas::titleString( $title );
377 - if( in_array($key_title, $toGenPageList )) {
378 - $full_text = $form->createMarkup();
379 - $params = array();
380 - $params['user_id'] = $wgUser->getId();
381 - $params['page_text'] = $full_text;
382 - $jobs = array( new PSCreatePageJob( $title, $params ) );
383 - Job::batchInsert( $jobs );
384 - }
385432 return true;
386433 }
387434