From 5a8accce1f2af9cf119a8303de87632eaa39ec7b Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Thu, 9 Jan 2025 22:49:44 +0400 Subject: [PATCH 1/6] add require --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5c1bb8c..9b99bb7 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,10 @@ }, "require": { "php": ">=7.4", - "composer/installers": ">=1.0.1" + "composer/installers": ">=1.0.1", + "mediawiki/semantic-media-wiki": "~3.1|~4.0|~5.0", + "easyrdf/easyrdf": "~1.1", + "ml/json-ld": "^1.2" }, "require-dev": { "mediawiki/mediawiki-codesniffer": "43.0.0", From a8fc3402c9a47f7460efef8283f7536cc092e274 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Thu, 9 Jan 2025 22:51:01 +0400 Subject: [PATCH 2/6] add handler JsonLDSerializer --- src/HookRegistry.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/HookRegistry.php b/src/HookRegistry.php index 5bc52b3..5e6e34f 100644 --- a/src/HookRegistry.php +++ b/src/HookRegistry.php @@ -61,6 +61,13 @@ public function getHandlerFor( $name ) { } private function addCallbackHandlers( $store, $options ) { + + $this->handlers['BeforePageDisplay'] = function ( $outputPage, $skin ) { + if ( empty( $GLOBALS['wgSemanticMetaTagsDisableJsonLD'] ) ) { + new JsonLDSerializer( $skin->getTitle(), $outputPage ); + } + }; + /** * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput */ From 22c6f52f67447f2e36f653fd1d2f402ed3102125 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Thu, 9 Jan 2025 22:53:07 +0400 Subject: [PATCH 3/6] add class JsonLDSerializer --- src/JsonLDSerializer.php | 80 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/JsonLDSerializer.php diff --git a/src/JsonLDSerializer.php b/src/JsonLDSerializer.php new file mode 100644 index 0000000..6bc9b01 --- /dev/null +++ b/src/JsonLDSerializer.php @@ -0,0 +1,80 @@ +isKnownArticle( $title ) ) { + $this->setJsonLD( $title, $outputPage ); + } + } + + /** + * @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/PageProperties/+/548d30609c512a79e202dfa7c02a298c66ca34fa/includes/PageProperties.php + * @param Title $title + * @return bool + */ + private function isKnownArticle( $title ) { + return ( $title && $title->canExist() && $title->getArticleID() > 0 + && $title->isKnown() ); + } + + /** + * @see https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/PageProperties/+/548d30609c512a79e202dfa7c02a298c66ca34fa/includes/PageProperties.php + * @param Title $title + * @param OutputPage $outputPage + * @return void + */ + public static function setJsonLD( $title, $outputPage ) { + if ( !class_exists( '\EasyRdf\Graph' ) || !class_exists( '\ML\JsonLD\JsonLD' ) ) { + return; + } + + // @TODO use directly the function makeExportDataForSubject + // SemanticMediawiki/includes/export/SMW_Exporter.php + $export_rdf = SpecialPage::getTitleFor( 'ExportRDF' ); + if ( $export_rdf->isKnown() ) { + $export_url = $export_rdf->getFullURL( [ + 'page' => $title->getFullText(), + 'recursive' => '1', + 'backlinks' => 0 + ] ); + + try { + $foaf = new \EasyRdf\Graph( $export_url ); + $foaf->load(); + + $format = \EasyRdf\Format::getFormat( 'jsonld' ); + $output = $foaf->serialise( $format, [ + 'compact' => true, + ] ); + + } catch ( Exception $e ) { + self::$Logger->error( 'EasyRdf error: ' . $export_url ); + return; + } + + // https://hotexamples.com/examples/-/EasyRdf_Graph/serialise/php-easyrdf_graph-serialise-method-examples.html + if ( is_scalar( $output ) ) { + $outputPage->addHeadItem( 'json-ld', Html::Element( + 'script', [ 'type' => 'application/ld+json' ], $output + ) + ); + } + } + } +} From 6f7fbbd5e78d95eebe202744715453ce5d8654ce Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Thu, 9 Jan 2025 23:47:42 +0400 Subject: [PATCH 4/6] fix phpcs --- src/HookRegistry.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/HookRegistry.php b/src/HookRegistry.php index 5e6e34f..93cb0f1 100644 --- a/src/HookRegistry.php +++ b/src/HookRegistry.php @@ -61,8 +61,7 @@ public function getHandlerFor( $name ) { } private function addCallbackHandlers( $store, $options ) { - - $this->handlers['BeforePageDisplay'] = function ( $outputPage, $skin ) { + $this->handlers['BeforePageDisplay'] = static function ( $outputPage, $skin ) { if ( empty( $GLOBALS['wgSemanticMetaTagsDisableJsonLD'] ) ) { new JsonLDSerializer( $skin->getTitle(), $outputPage ); } From 3228a162bd447e630fb54dfa42a48b800c61dcc6 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Thu, 9 Jan 2025 23:48:26 +0400 Subject: [PATCH 5/6] fix phpcs --- src/JsonLDSerializer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JsonLDSerializer.php b/src/JsonLDSerializer.php index 6bc9b01..f489d2c 100644 --- a/src/JsonLDSerializer.php +++ b/src/JsonLDSerializer.php @@ -7,10 +7,10 @@ namespace SMT; -use Title; +use Html; use OutputPage; use SpecialPage; -use Html; +use Title; class JsonLDSerializer { /** From 71a6a01b701154971b9d847fa744b224c1f7adfa Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Fri, 10 Jan 2025 13:12:38 +0400 Subject: [PATCH 6/6] remove require SMW --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 9b99bb7..d8ae90d 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "require": { "php": ">=7.4", "composer/installers": ">=1.0.1", - "mediawiki/semantic-media-wiki": "~3.1|~4.0|~5.0", "easyrdf/easyrdf": "~1.1", "ml/json-ld": "^1.2" },