Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds json-ld serialization #80

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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|~5.0",
"easyrdf/easyrdf": "~1.1",
"ml/json-ld": "^1.2"
},
"require-dev": {
"mediawiki/semantic-media-wiki": "@dev",
Expand All @@ -51,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",
Expand Down
6 changes: 6 additions & 0 deletions src/HookRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ 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
*/
Expand Down
80 changes: 80 additions & 0 deletions src/JsonLDSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* @see https://www.mediawiki.org/wiki/Extension:PageProperties
* @author thomas-topway-it for Km-a
*/

namespace SMT;

use Title;
use OutputPage;
use SpecialPage;
use Html;

class JsonLDSerializer {
/**
* @param Title $title
* @param OutputPage $outputPage
*/
public function __construct( $title, $outputPage ) {
if ( $this->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
)
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/Unit/HookRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function doTestRegisteredOutputPageParserOutputHandler( $instance ) {
->disableOriginalConstructor()
->getMock();

$context->expects( $this->atLeastOnce() )
$context->expects( $this->any() )
->method( 'getRequest' )
->will( $this->returnValue( $webRequest ) );

Expand Down