r71605 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71604‎ | r71605 | r71606 >
Date:00:39, 25 August 2010
Author:yaron
Status:deferred
Tags:
Comment:
Moved most of the handling for #set_recurring_event into a new helper function, getDatesForRecurringEvent()
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_ParserExtensions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_ParserExtensions.php
@@ -357,43 +357,21 @@
358358 }
359359
360360 /**
361 - * Function for handling the {{\#set_recurring_event }} parser function.
362 - * This is used for defining a set of date values for a page that
363 - * represents a recurring event.
364 - * Like with the #set function, all annotations happen silently.
 361+ * Helper function used by doSetRecurringEvent(), as well as the
 362+ * Semantic Internal Objects extension
365363 *
366 - * Usage:
367 - * {{\#set_recurring_event:
368 - * property = Has date
369 - * | start = January 4, 2010
370 - * | end = June 7, 2010
371 - * | unit = week
372 - * | period = 1
373 - * | include = March 16, 2010;March 23, 2010
374 - * | exclude = March 15, 2010;March 22, 2010
375 - * }}
376 - * This sets a "Has date" value for every Monday within the specified
377 - * six-month period, except for two Mondays which are excluded and
378 - * two Tuesdays that are saved in their place.
379 - *
380 - * There's also a 'week number' parameter, which is only valid when
381 - * the 'unit' parameter is set to 'month'. This one dictates that the
382 - * event should always happen on the n-th week of each month, instead
383 - * of a specific numbered date. Negative values for 'week number'
384 - * indicate the n-th last week of a month instead.
385 - *
386364 * @param Parser &$parser The current parser
387 - * @return nothing
 365+ * @return either null, or an array of main property name, set of
 366+ * all date strings, and the unused params
388367 */
389 - static public function doSetRecurringEvent( &$parser ) {
390 - $params = func_get_args();
391 - array_shift( $params ); // We already know the $parser ...
392 -
 368+ static public function getDatesForRecurringEvent( $params ) {
393369 // Initialize variables.
 370+ $all_date_strings = array();
 371+ $unused_params = array();
394372 $property_name = $start_date = $end_date = $unit = $period = $week_num = null;
395373 $included_dates = array();
396374 $excluded_dates_jd = array();
397 -
 375+
398376 // Set values from the parameters.
399377 foreach ( $params as $param ) {
400378 $parts = explode( '=', trim( $param ) );
@@ -432,6 +410,8 @@
433411 $excluded_dates_jd[] = $date->getValueKey();
434412 }
435413 break;
 414+ default:
 415+ $unused_params[] = $param;
436416 }
437417 }
438418
@@ -470,13 +450,13 @@
471451 $exclude_date = ( in_array( $cur_date_jd, $excluded_dates_jd ) );
472452
473453 if ( !$exclude_date ) {
474 - $cur_date_str = $cur_date->getLongWikiText();
475 - SMWParseData::addProperty( $property_name, $cur_date_str, false, $parser, true );
 454+ $all_date_strings[] = $cur_date->getLongWikiText();
476455 }
477456
478457 // Now get the next date.
479 - // Handling is different depending on whether it's month/year or week/day
480 - // since the latter is a set number of days while the former isn't.
 458+ // Handling is different depending on whether it's
 459+ // month/year or week/day since the latter is a set
 460+ // number of days while the former isn't.
481461 if ( $unit === 'year' || $unit == 'month' ) {
482462 $cur_year = $cur_date->getYear();
483463 $cur_month = $cur_date->getMonth();
@@ -537,7 +517,7 @@
538518 }
539519 } while ( !$right_month || !$right_week);
540520 } else { // $unit == 'day' or 'week'
541 - // Sssume 'day' if it's none of the above.
 521+ // Assume 'day' if it's none of the above.
542522 $cur_date_jd += ( $unit === 'week' ) ? 7 * $period : $period;
543523 $cur_date = SMWDataValueFactory::newTypeIDValue( '_dat', $cur_date_jd );
544524 }
@@ -553,10 +533,57 @@
554534 } while ( !$reached_end_date );
555535
556536 // Handle the 'include' dates as well.
557 - foreach ( $included_dates as $date_str ) {
 537+ $all_date_strings = array_merge( $all_date_strings, $included_dates);
 538+ return array( $property_name, $all_date_strings, $unused_params );
 539+ }
 540+
 541+ /**
 542+ * Function for handling the {{\#set_recurring_event }} parser function.
 543+ * This is used for defining a set of date values for a page that
 544+ * represents a recurring event.
 545+ * Like with the #set function, all annotations happen silently.
 546+ *
 547+ * Usage:
 548+ * {{\#set_recurring_event:
 549+ * property = Has date
 550+ * | start = January 4, 2010
 551+ * | end = June 7, 2010
 552+ * | unit = week
 553+ * | period = 1
 554+ * | include = March 16, 2010;March 23, 2010
 555+ * | exclude = March 15, 2010;March 22, 2010
 556+ * }}
 557+ * This sets a "Has date" value for every Monday within the specified
 558+ * six-month period, except for two Mondays which are excluded and
 559+ * two Tuesdays that are saved in their place.
 560+ *
 561+ * There's also a 'week number' parameter, which is only valid when
 562+ * the 'unit' parameter is set to 'month'. This one dictates that the
 563+ * event should always happen on the n-th week of each month, instead
 564+ * of a specific numbered date. Negative values for 'week number'
 565+ * indicate the n-th last week of a month instead.
 566+ *
 567+ * @param Parser &$parser The current parser
 568+ * @return nothing
 569+ */
 570+ static public function doSetRecurringEvent( &$parser ) {
 571+ $params = func_get_args();
 572+ array_shift( $params ); // We already know the $parser ...
 573+
 574+ // Almost all of the work gets done by
 575+ // getDatesForRecurringEvent().
 576+ $results = self::getDatesForRecurringEvent( $params );
 577+ if ( $results == null ) {
 578+ return null;
 579+ }
 580+
 581+ list( $property, $all_date_strings, $unused_params ) = $results;
 582+
 583+ // Do the actual saving of the data.
 584+ foreach ( $all_date_strings as $date_str ) {
558585 SMWParseData::addProperty( $property_name, $date_str, false, $parser, true );
559586 }
560 -
 587+
561588 SMWOutputs::commitToParser( $parser ); // Not obviously required, but let us be sure.
562589 }
563590
@@ -621,4 +648,4 @@
622649 return '';
623650 }
624651
625 -}
\ No newline at end of file
 652+}

Status & tagging log