summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/ParserFunctions/DeclareParserFunction.php
blob: e0dc8cb22d3ca5557c7e3d5e7594c2337886f8d0 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

namespace SMW\ParserFunctions;

use Parser;
use PPFrame;
use SMW\DataValueFactory;
use SMW\ParserData;
use SMWPropertyValue as PropertyValue;

/**
 * Class that provides the {{#declare}} parser function
 *
 * @see http://semantic-mediawiki.org/wiki/Help:Argument_declaration_in_templates
 *
 * @license GNU GPL v2+
 * @since   1.5.3
 *
 * @author Markus Krötzsch
 * @author Jeroen De Dauw
 */
class DeclareParserFunction {

	/**
	 * @var ParserData
	 */
	private $parserData;

	/**
	 * @var DIWikiPage
	 */
	private $subject;

	/**
	 * @since 2.1
	 *
	 * @param ParserData $parserData
	 */
	public function __construct( ParserData $parserData ) {
		$this->parserData = $parserData;
	}

	/**
	 * @since 2.1
	 *
	 * @param PPFrame $frame
	 * @param array $args
	 */
	public function parse( PPFrame $frame, array $args ) {

		// @todo Save as metadata
		if ( !$frame->isTemplate() ) {
			return '';
		}

		$this->subject = $this->parserData->getSemanticData()->getSubject();

		foreach ( $args as $arg ) {
			if ( trim( $arg ) !== '' ) {
				$expanded = trim( $frame->expand( $arg ) );
				$parts = explode( '=', $expanded, 2 );

				if ( count( $parts ) == 1 ) {
					$propertystring = $expanded;
					$argumentname = $expanded;
				} else {
					$propertystring = $parts[0];
					$argumentname = $parts[1];
				}

				$propertyValue = DataValueFactory::getInstance()->newPropertyValueByLabel( $propertystring );
				$argument = $frame->getArgument( $argumentname );
				$valuestring = $frame->expand( $argument );

				if ( $propertyValue->isValid() ) {
					$this->matchValueArgument( $propertyValue, $propertystring, $valuestring );
				}
			}
		}

		$this->parserData->pushSemanticDataToParserOutput();

		return '';
	}

	private function matchValueArgument( PropertyValue $propertyValue, $propertystring, $valuestring ) {

		if ( $propertyValue->getPropertyTypeID() === '_wpg' ) {
			$matches = [];
			preg_match_all( '/\[\[([^\[\]]*)\]\]/u', $valuestring, $matches );
			$objects = $matches[1];

			if ( count( $objects ) == 0 ) {
				if ( trim( $valuestring ) !== '' ) {
					$this->addDataValue( $propertystring, $valuestring );
				}
			} else {
				foreach ( $objects as $object ) {
					$this->addDataValue( $propertystring, $object );
				}
			}
		} elseif ( trim( $valuestring ) !== '' ) {
			$this->addDataValue( $propertystring, $valuestring );
		}

		// $value = \SMW\DataValueFactory::getInstance()->newDataValueByProperty( $property->getDataItem(), $valuestring );
		// if (!$value->isValid()) continue;
	}

	private function addDataValue( $property, $value ) {

		$dataValue = DataValueFactory::getInstance()->newDataValueByText(
			$property,
			$value,
			false,
			$this->subject
		);

		$this->parserData->addDataValue( $dataValue );
	}

}