summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/ParserFunctions/RecurringEventsParserFunction.php
blob: 9a161e0c09999f9fb1825a2f888bf37dd27d7aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php

namespace SMW\ParserFunctions;

use SMW\MessageFormatter;
use SMW\ParserData;
use SMW\ParserParameterProcessor;
use SMW\RecurringEvents;
use SMW\Subobject;

/**
 * @private This class should not be instantiated directly, please use
 * ParserFunctionFactory::newRecurringEventsParserFunction
 *
 * Class that provides the {{#set_recurring_event}} parser function
 *
 * @see http://semantic-mediawiki.org/wiki/Help:Recurring_events
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class RecurringEventsParserFunction extends SubobjectParserFunction {

	/**
	 * @var RecurringEvents
	 */
	private $recurringEvents;

	/**
	 * @since 1.9
	 *
	 * @param ParserData $parserData
	 * @param Subobject $subobject
	 * @param MessageFormatter $messageFormatter
	 * @param RecurringEvents $recurringEvents
	 */
	public function __construct( ParserData $parserData, Subobject $subobject, MessageFormatter $messageFormatter, RecurringEvents $recurringEvents ) {
		parent::__construct ( $parserData, $subobject, $messageFormatter );
		$this->recurringEvents = $recurringEvents;
	}

	/**
	 * @since 1.9
	 *
	 * @param ParserParameterProcessor $parameters
	 *
	 * @return string|null
	 */
	public function parse( ParserParameterProcessor $parameters ) {

		$this->useFirstElementAsPropertyLabel( true );

		$this->recurringEvents->parse(
			$parameters->toArray()
		);

		$this->messageFormatter->addFromArray(
			$this->recurringEvents->getErrors()
		);

		foreach ( $this->recurringEvents->getDates() as $date_str ) {

			// Override existing parameters array with the returned
			// pre-processed parameters array from recurring events
			$parameters->setParameters( $this->recurringEvents->getParameters() );

			// Add the date string as individual property / value parameter
			$parameters->addParameter(
				$this->recurringEvents->getProperty(),
				$date_str
			);

			// @see SubobjectParserFunction::addDataValuesToSubobject
			// Each new $parameters set will add an additional subobject
			// to the instance
			if ( $this->addDataValuesToSubobject( $parameters ) ) {
				$this->parserData->getSemanticData()->addSubobject( $this->subobject );
			}

			// Collect errors that occurred during processing
			$this->messageFormatter->addFromArray( $this->subobject->getErrors() );
		}

		// Update ParserOutput
		$this->parserData->pushSemanticDataToParserOutput();

		$this->messageFormatter->addFromArray(
			$this->parserData->getErrors()
		);

		return $this->messageFormatter->getHtml();
	}

}