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

Json ld serialization updated #90

Merged
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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
},
"require": {
"php": ">=7.4",
"composer/installers": ">=1.0.1"
"composer/installers": ">=1.0.1",
"easyrdf/easyrdf": "~1.1",
"ml/json-ld": "^1.2"
},
"require-dev": {
"mediawiki/mediawiki-codesniffer": "43.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/HookRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
}

private function addCallbackHandlers( $store, $options ) {
$this->handlers['BeforePageDisplay'] = static function ( $outputPage, $skin ) {
if ( empty( $GLOBALS['wgSemanticMetaTagsDisableJsonLD'] ) ) {
new JsonLDSerializer( $skin->getTitle(), $outputPage );

Check warning on line 66 in src/HookRegistry.php

View check run for this annotation

Codecov / codecov/patch

src/HookRegistry.php#L65-L66

Added lines #L65 - L66 were not covered by tests
}
};

/**
* @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 Html;
use OutputPage;
use SpecialPage;
use Title;

class JsonLDSerializer {
/**
* @param Title $title
* @param OutputPage $outputPage
*/
public function __construct( $title, $outputPage ) {
if ( $this->isKnownArticle( $title ) ) {
$this->setJsonLD( $title, $outputPage );

Check warning on line 22 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L20-L22

Added lines #L20 - L22 were not covered by tests
}
}

/**
* @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() );

Check warning on line 33 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L31-L33

Added lines #L31 - L33 were not covered by tests
}

/**
* @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;

Check warning on line 44 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L42-L44

Added lines #L42 - L44 were not covered by tests
}

// @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
] );

Check warning on line 55 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L49-L55

Added lines #L49 - L55 were not covered by tests

try {
$foaf = new \EasyRdf\Graph( $export_url );
$foaf->load();

Check warning on line 59 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L58-L59

Added lines #L58 - L59 were not covered by tests

$format = \EasyRdf\Format::getFormat( 'jsonld' );
$output = $foaf->serialise( $format, [
'compact' => true,
] );

Check warning on line 64 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L61-L64

Added lines #L61 - L64 were not covered by tests

} catch ( Exception $e ) {
self::$Logger->error( 'EasyRdf error: ' . $export_url );
return;

Check warning on line 68 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L66-L68

Added lines #L66 - L68 were not covered by tests
}

// 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
)
);

Check warning on line 76 in src/JsonLDSerializer.php

View check run for this annotation

Codecov / codecov/patch

src/JsonLDSerializer.php#L72-L76

Added lines #L72 - L76 were not covered by tests
}
}
}
}
Loading