From 8c2538d31cfddf291e915145997c15efeb108538 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Tue, 9 Jan 2024 01:04:42 +0400 Subject: [PATCH 1/7] add easyrdf --- composer.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0cba9a2..24849e6 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,9 @@ "require": { "php": ">=7.1", "composer/installers": "1.*,>=1.0.1", - "mediawiki/semantic-media-wiki": "~3.1|~4.0" + "mediawiki/semantic-media-wiki": "~3.1|~4.0", + "easyrdf/easyrdf": "*", + "ml/json-ld": "^1.2" }, "require-dev": { "mediawiki/semantic-media-wiki": "@dev", From d0d0de4e03c77831fcf2a432fd8f8e86cc3cfbd0 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Tue, 9 Jan 2024 01:06:05 +0400 Subject: [PATCH 2/7] add hook BeforePageDisplay --- src/HookRegistry.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/HookRegistry.php b/src/HookRegistry.php index 2e88cfe..cac6d02 100644 --- a/src/HookRegistry.php +++ b/src/HookRegistry.php @@ -62,6 +62,12 @@ public function getHandlerFor( $name ) { private function addCallbackHandlers( $store, $options ) { + $this->handlers['BeforePageDisplay'] = function ( $outputPage, $skin ) { + if ( !isset( $GLOBALS['wgSemanticMetaTagsDisableJsonLD'] ) ) { + new JsonLDSerializer( $skin->getTitle(), $outputPage ); + } + }; + /** * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput */ From 0e27dd5e21cf5333d63aea461200d520bc967980 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Tue, 9 Jan 2024 01:07:00 +0400 Subject: [PATCH 3/7] Add files via upload --- src/JsonLDSerializer.php | 79 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/JsonLDSerializer.php diff --git a/src/JsonLDSerializer.php b/src/JsonLDSerializer.php new file mode 100644 index 0000000..e50e7c2 --- /dev/null +++ b/src/JsonLDSerializer.php @@ -0,0 +1,79 @@ +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 1e248c1664e9f2f2f56bd9d5ce8146d88a63ab41 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Fri, 29 Mar 2024 14:07:25 +0400 Subject: [PATCH 4/7] add invoke hooks --- .../Integration/MetaTagsContentGenerationIntegrationTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php b/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php index 029e31e..2d7cd2a 100644 --- a/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php +++ b/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php @@ -29,6 +29,10 @@ protected function setUp() : void { $this->pageCreator = UtilityFactory::getInstance()->newpageCreator(); $this->pageDeleter = UtilityFactory::getInstance()->newPageDeleter(); + $this->mwHooksHandler = $this->testEnvironment->getUtilityFactory()->newMwHooksHandler(); + $this->mwHooksHandler->deregisterListedHooks(); + $this->mwHooksHandler->invokeHooksFromRegistry(); + $metaTagsBlacklist = [ 'robots' ]; From 2946b919c8f738bdedf22a4ffa6a5e4c5f119448 Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Fri, 29 Mar 2024 14:08:24 +0400 Subject: [PATCH 5/7] getRequest $this->any() --- tests/phpunit/Unit/HookRegistryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/Unit/HookRegistryTest.php b/tests/phpunit/Unit/HookRegistryTest.php index 7e97bfb..f3df624 100644 --- a/tests/phpunit/Unit/HookRegistryTest.php +++ b/tests/phpunit/Unit/HookRegistryTest.php @@ -69,7 +69,7 @@ public function doTestRegisteredOutputPageParserOutputHandler( $instance ) { ->disableOriginalConstructor() ->getMock(); - $context->expects( $this->atLeastOnce() ) + $context->expects( $this->any() ) ->method( 'getRequest' ) ->will( $this->returnValue( $webRequest ) ); From 60cfd280144445000997a413d633cd2628f4b74b Mon Sep 17 00:00:00 2001 From: thomas-topway-it Date: Sat, 3 Aug 2024 14:12:07 +0400 Subject: [PATCH 6/7] add credits --- src/JsonLDSerializer.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/JsonLDSerializer.php b/src/JsonLDSerializer.php index e50e7c2..45c8015 100644 --- a/src/JsonLDSerializer.php +++ b/src/JsonLDSerializer.php @@ -1,5 +1,10 @@ Date: Thu, 9 Jan 2025 22:34:37 +0400 Subject: [PATCH 7/7] fix version and wgSemanticMetaTagsDisableJsonLD condition --- composer.json | 9 ++++++--- src/HookRegistry.php | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 24849e6..e6d425d 100644 --- a/composer.json +++ b/composer.json @@ -29,8 +29,8 @@ "require": { "php": ">=7.1", "composer/installers": "1.*,>=1.0.1", - "mediawiki/semantic-media-wiki": "~3.1|~4.0", - "easyrdf/easyrdf": "*", + "mediawiki/semantic-media-wiki": "~3.1|~4.0|~5.0", + "easyrdf/easyrdf": "~1.1", "ml/json-ld": "^1.2" }, "require-dev": { @@ -53,7 +53,10 @@ } }, "config": { - "process-timeout": 0 + "process-timeout": 0, + "allow-plugins": { + "composer/installers": false + } }, "scripts":{ "test": "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist", diff --git a/src/HookRegistry.php b/src/HookRegistry.php index cac6d02..43b5020 100644 --- a/src/HookRegistry.php +++ b/src/HookRegistry.php @@ -63,7 +63,7 @@ public function getHandlerFor( $name ) { private function addCallbackHandlers( $store, $options ) { $this->handlers['BeforePageDisplay'] = function ( $outputPage, $skin ) { - if ( !isset( $GLOBALS['wgSemanticMetaTagsDisableJsonLD'] ) ) { + if ( empty( $GLOBALS['wgSemanticMetaTagsDisableJsonLD'] ) ) { new JsonLDSerializer( $skin->getTitle(), $outputPage ); } };