summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/semanticdata.access.md
blob: adb14ed68612378c2aa80574f18ba5ab5fb6d3de (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
## Add semantic data to in-memory (#1202)

To avoid having competing data being stored at a different point in time
during a request aimed for the same subject, property value assignments are collected
and stored in-memory before the finale `Store::updateData` process will
be invoked once.


```php
// Create in-memory ParserOutput transfer object
$parserData = ApplicationFactory::getInstance()->newParserData(
	$parser->getTitle(),
	$parser->getOutput()
);

$subject = $parserData->getSubject();
$property = new DIProperty( 'SomeProperty' );

// Add individual instances of a value for a known property
foreach ( $values as $value ) {
	$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
		$property,
		trim( $value ), // Text value
		false,
		$subject
	);

	$parserData->addDataValue( $dataValue );
}

// Add individual instances of a value for a property only known by
// its textual representation
foreach ( $values as $value ) {
	$dataValue = DataValueFactory::getInstance()->newDataValueByText(
		$property,
		trim( $value ), // Text value
		false,
		$subject
	);

	// Adds the object to the SemanticData container you could also use
	// $parserData->getSemanticData()->addPropertyObjectValue( ...)
	$parserData->addDataValue( $dataValue );
}

// Ensures that objects are pushed to the ParserOutput
$parserData->pushSemanticDataToParserOutput();
```

## Access semantic data currently stored in-memory

```php
// Create in-memory ParserOutput transfer object
$parserData = ApplicationFactory::getInstance()->newParserData(
	$parser->getTitle(),
	$parser->getOutput()
);

// Access to the data store in-memory
$semanticData = $parserData->getSemanticData();
```

## Read semantic data from DB

```php
$subject = new DIWikiPage( 'Foo', NS_MAIN );

$semanticData = ApplicationFactory::getInstance()->getStore->getSemanticData(
	$subject
);
```