Skip to content

Commit 5103ecd

Browse files
Merge pull request #90 from Knowledge-Wiki/json-ld-serialization-updated
Json ld serialization updated
2 parents 720c9d8 + 71a6a01 commit 5103ecd

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
},
2929
"require": {
3030
"php": ">=7.4",
31-
"composer/installers": ">=1.0.1"
31+
"composer/installers": ">=1.0.1",
32+
"easyrdf/easyrdf": "~1.1",
33+
"ml/json-ld": "^1.2"
3234
},
3335
"require-dev": {
3436
"mediawiki/mediawiki-codesniffer": "43.0.0",

src/HookRegistry.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public function getHandlerFor( $name ) {
6161
}
6262

6363
private function addCallbackHandlers( $store, $options ) {
64+
$this->handlers['BeforePageDisplay'] = static function ( $outputPage, $skin ) {
65+
if ( empty( $GLOBALS['wgSemanticMetaTagsDisableJsonLD'] ) ) {
66+
new JsonLDSerializer( $skin->getTitle(), $outputPage );
67+
}
68+
};
69+
6470
/**
6571
* @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput
6672
*/

src/JsonLDSerializer.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* @see https://www.mediawiki.org/wiki/Extension:PageProperties
5+
* @author thomas-topway-it for KM-A
6+
*/
7+
8+
namespace SMT;
9+
10+
use Html;
11+
use OutputPage;
12+
use SpecialPage;
13+
use Title;
14+
15+
class JsonLDSerializer {
16+
/**
17+
* @param Title $title
18+
* @param OutputPage $outputPage
19+
*/
20+
public function __construct( $title, $outputPage ) {
21+
if ( $this->isKnownArticle( $title ) ) {
22+
$this->setJsonLD( $title, $outputPage );
23+
}
24+
}
25+
26+
/**
27+
* @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/PageProperties/+/548d30609c512a79e202dfa7c02a298c66ca34fa/includes/PageProperties.php
28+
* @param Title $title
29+
* @return bool
30+
*/
31+
private function isKnownArticle( $title ) {
32+
return ( $title && $title->canExist() && $title->getArticleID() > 0
33+
&& $title->isKnown() );
34+
}
35+
36+
/**
37+
* @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/PageProperties/+/548d30609c512a79e202dfa7c02a298c66ca34fa/includes/PageProperties.php
38+
* @param Title $title
39+
* @param OutputPage $outputPage
40+
* @return void
41+
*/
42+
public static function setJsonLD( $title, $outputPage ) {
43+
if ( !class_exists( '\EasyRdf\Graph' ) || !class_exists( '\ML\JsonLD\JsonLD' ) ) {
44+
return;
45+
}
46+
47+
// @TODO use directly the function makeExportDataForSubject
48+
// SemanticMediawiki/includes/export/SMW_Exporter.php
49+
$export_rdf = SpecialPage::getTitleFor( 'ExportRDF' );
50+
if ( $export_rdf->isKnown() ) {
51+
$export_url = $export_rdf->getFullURL( [
52+
'page' => $title->getFullText(),
53+
'recursive' => '1',
54+
'backlinks' => 0
55+
] );
56+
57+
try {
58+
$foaf = new \EasyRdf\Graph( $export_url );
59+
$foaf->load();
60+
61+
$format = \EasyRdf\Format::getFormat( 'jsonld' );
62+
$output = $foaf->serialise( $format, [
63+
'compact' => true,
64+
] );
65+
66+
} catch ( Exception $e ) {
67+
self::$Logger->error( 'EasyRdf error: ' . $export_url );
68+
return;
69+
}
70+
71+
// https://hotexamples.com/examples/-/EasyRdf_Graph/serialise/php-easyrdf_graph-serialise-method-examples.html
72+
if ( is_scalar( $output ) ) {
73+
$outputPage->addHeadItem( 'json-ld', Html::Element(
74+
'script', [ 'type' => 'application/ld+json' ], $output
75+
)
76+
);
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)