summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/hook.pagecontentsavecomplete.md
blob: 59d68ce6892868bf595183f3602335d25f0bc3a8 (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

## PageContentSaveComplete hook

[#2974](https://github.com/SemanticMediaWiki/SemanticMediaWiki/issues/2974) Creating subobjects using the `PageContentSaveComplete` hook

```php
use Hooks;
use ParserOutput;
use SMW\ApplicationFactory;
use SMW\DataValueFactory;
use SMWContainerSemanticData as ContainerSemanticData;
use SMWDIContainer as DIContainer;
use SMW\DIWikiPage;
use SMW\DIProperty;

Hooks::register( 'PageContentSaveComplete', function( $wikiPage, $user, $content, $summary, $isMinor, $isWatch, $section, $flags, $revision, $status, $baseRevId, $undidRevId ) {

        $applicationFactory = ApplicationFactory::getInstance();
        $mwCollaboratorFactory = $applicationFactory->newMwCollaboratorFactory();

        /**
         * Initialize the ParserOuput object
         */
        $editInfoProvider = $mwCollaboratorFactory->newEditInfoProvider(
                $wikiPage,
                $revision,
                $user
        );

        $parserOutput = $editInfoProvider->fetchEditInfo()->getOutput();

        if ( !$parserOutput instanceof ParserOutput ) {
                return true;
        }

        $parserData = $applicationFactory->newParserData(
                $wikiPage->getTitle(),
                $parserOutput
        );

        // Subject Foo ...
        $subject = $parserData->getSubject();

        // Contains the wikitext, JSON or whatever is stored for this content model
        $nativeData = $content->getNativeData();

        // Identify the content as unique
        $subobjectName = '_MYCUSTOMPREFIX' . md5( $nativeData );

        $subject = new DIWikiPage(
                $subject->getDBkey(),
                $subject->getNamespace(),
                $subject->getInterwiki(),
                $subobjectName
        );

        // Build the subobject by using a separate container object
        $containerSemanticData = new ContainerSemanticData(
                $subject
        );

        /**
         * Start doing all the work required after this
         */

        // If one knows the details you can add it directly
        $containerSemanticData->addPropertyObjectValue(
                new DIProperty( 'PropertyIWantToUse' ),
                new DIWikiPage( 'SomeTextItem', NS_MAIN )
        );

        // If you don't know the type, use the DataValueFactory (see available methods)
        $dataValue = DataValueFactory::getInstance()->newDataValueByText(
           'AnotherPropertyIWantToUse',
           '123'
        );

        $containerSemanticData->addDataValue(
                $dataValue
        );

        /**
         * Done
         */

        // This part is used to add the subobject the the main subject
        // Page: Foo -> gets a _MYCUSTOMPREFIX.... attached
        $parserData->getSemanticData()->addPropertyObjectValue(
                new DIProperty( DIProperty::TYPE_SUBOBJECT ),
                new DIContainer( $containerSemanticData )
        );

        $parserData->pushSemanticDataToParserOutput();

        return true;

} );

```