pdsinterop/flysystem-rdf

Flysystem 插件,用于在不同序列化格式之间转换 RDF 数据。

v0.5.0 2022-08-22 14:36 UTC

This package is auto-updated.

Last update: 2024-09-20 13:23:21 UTC


README

Project stage: Development License Latest Version Maintained

PDS Interop standard-readme compliant keep-a-changelog compliant

Flysystem 插件,用于在不同序列化格式之间转换 RDF 数据。

使用 RDF 时,您会注意到存在几种不同的流行序列化格式。为了避免在多个格式中存储数据,更容易将数据存储在一个格式中,并在需要时将其转换为其他格式。

此项目包含一个与 Flysystem 一起使用的插件,用于执行此操作。

该转换使用 EasyRDF。此插件提供 EasyRDF 支持的所有格式。

目前支持的格式包括

  • JSON-LD
  • N-triples
  • Notation 3 / N3
  • RDF/XML
  • Turtle

目录

背景

此项目是 PDS Interop 的 PHP 项目系列的一部分。它被 Solid-Nextcloud 应用程序和独立的 PHP Solid 服务器所使用。

由于功能似乎对其他项目也很有用,因此它被实现为一个独立的包。

安装

建议的安装方法是通过 composer

composer require pdsinterop/flysystem-rdf

支持 PHP 版本 7.3 及更高版本。此包需要启用 mbstring 扩展才能正常工作。

使用

此包提供从文件系统读取存储在一种 RDF 格式中的文件并将其作为另一种格式读取的功能。

这些功能由一个插件和一个适配器提供。

插件最适合轻量级场景,您只需将文件转换为另一种格式即可。

适配器最适合完整功能的 Flysystem 使用,因为它还正确处理对 hasgetMimeType 的调用(插件不这样做)

适配器

要使用适配器,请实例化它,将其添加到 Flysystem 文件系统,并添加辅助插件。

<?php

// Create Formats objects
$formats = new \Pdsinterop\Rdf\Formats();

// Use an adapter of your choice
$adapter = new League\Flysystem\Adapter\Local('/path/to/files/');

// Create the RDF Adapter
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf(
    $adapter,
    new \EasyRdf\Graph(),
    $formats,
    'server'
);

// Create Flysystem as usual, adding the RDF Adapter
$filesystem = new League\Flysystem\Filesystem($rdfAdapter);

// Add the `AsMime` plugin to convert contents based on a provided MIME type, 
$filesystem->addPlugin(new \Pdsinterop\Rdf\Flysystem\Plugin\AsMime($formats));

// Read the contents of a file in the format it was stored in
$content = $filesystem->read('/foaf.rdf');

// Read the contents of a file in another format from what was stored in
$convertedContents = $filesystem
    ->asMime('text/turtle')
    ->read('/foaf.rdf');

// Get the MIME type of the format the requested mime-type would return
// This is especially useful for RDF formats that can be requested with several
// different MIME types.
$convertedMimeType = $filesystem
    ->asMime('text/turtle')
    ->getMimetype('/foaf.rdf');

// This also works for `has`
$hasConvertedContents = $filesystem
    ->asMime('text/turtle')
    ->has('/foaf.ttl');

// Without using the plugin, this will be false
$hasContents = $filesystem->has('/foaf.ttl');

适配器会将找到的任何引用文件的 .meta 文件添加到该文件的元数据中,您可以使用 $filesystem->getMetadata($path) 获取这些元数据。

插件

要使用插件,请实例化它并将其添加到 Flysystem 文件系统。

然后可以调用 readRdf 函数以获取特定格式的 RDF 文件

<?php

// Create Flysystem as usual, adding an adapter of your choice
$adapter = new League\Flysystem\Adapter\Local('/path/to/files/');
$filesystem = new League\Flysystem\Filesystem($adapter);

// create and add the RdF Plugin
$plugin = new \Pdsinterop\Rdf\Flysystem\Plugin\ReadRdf(new \EasyRdf\Graph());
$filesystem->addPlugin($plugin);

// Read the contents of a RDF file in another format from what was stored in
$content = $filesystem->readRdf('/foaf.rdf', \Pdsinterop\Rdf\Enum\Format::TURTLE);

开发

  • 请勿忘记使用 composer 安装所需的依赖项。

  • 这里的大部分逻辑都涉及 EasyRdf 和/或 FlySystem。您需要熟悉它们的工作方式。

  • 测试位于 tests/ 目录中。它们在 GitHub actions 中为任何拉取请求运行。要运行它们,请调用 ./bin/phpunit

  • 使用 github_changelog_generator 和提供的 配置文件 可以自动生成更改日志。

贡献

您可以通过在 GitHub 上打开问题来提供问题或反馈。

所有 PDS Interop 项目都是开源的,并且对社区友好。欢迎任何贡献!有关更多详细信息,请阅读贡献指南

所有 PDS Interop 项目都遵守 代码宣言 及其 行为准则。贡献者应遵守其条款。

所有贡献者名单请见GitHub上的贡献者列表

有关变更列表,请参阅变更日志GitHub发行页面

许可证

PDS Interop创建的所有代码均采用MIT许可证授权。