PHP code example of ottosmops / oai-pmh

1. Go to this page and download the library: Download ottosmops/oai-pmh library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ottosmops / oai-pmh example snippets




namespace App\Services\Oai;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Ottosmops\OaiPmh\Contracts\OaiRepositoryContract;
use Ottosmops\OaiPmh\DTOs\Identifier;
use Ottosmops\OaiPmh\DTOs\ListResult;
use Ottosmops\OaiPmh\DTOs\Record;

class ArticleOaiRepository implements OaiRepositoryContract
{
    public function get(
        int $page,
        int $limit,
        ?Carbon $from = null,
        ?Carbon $until = null,
        ?string $set = null,
    ): ListResult {
        $query = \App\Models\Article::query()
            ->when($from, fn ($q, $f) => $q->where('updated_at', '>=', $f))
            ->when($until, fn ($q, $u) => $q->where('updated_at', '<=', $u))
            ->when($set, fn ($q, $s) => $q->where('category', $s));

        $total = $query->count();

        $records = $query
            ->orderBy('updated_at')
            ->skip($page * $limit)
            ->take($limit)
            ->get()
            ->map(fn ($article) => $this->toRecord($article));

        return new ListResult(records: $records, total: $total);
    }

    public function getRecordForId(string $identifier): ?Record
    {
        // Parse "oai:<namespace>:<local>" and look up by local ID
        $parsed = Identifier::tryParse($identifier);
        if ($parsed === null) {
            return null;
        }
        $article = \App\Models\Article::find($parsed->localIdentifier);

        return $article ? $this->toRecord($article) : null;
    }

    public function getEarliestDatestamp(): ?Carbon
    {
        return \App\Models\Article::query()->min('updated_at')
            ? Carbon::parse(\App\Models\Article::query()->min('updated_at'))
            : null;
    }

    private function toRecord(\App\Models\Article $article): Record
    {
        return new Record(
            identifier: new Identifier('journal.example.org', (string) $article->id),
            datestamp: $article->updated_at,
            sets: [$article->category],
            metadataXml: view('oai.oai_dc', ['article' => $article])->render(),
        );
    }
}

return [
    'repository_name' => 'Example Journal',
    'admin_email' => ['[email protected]'],
    'sample_identifier' => 'oai:journal.example.org:42',

    'metadataFormats' => [
        'oai_dc' => [
            'repository' => \App\Services\Oai\ArticleOaiRepository::class,
            'schema' => 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
            'namespace' => 'http://www.openarchives.org/OAI/2.0/oai_dc/',
        ],
    ],

    'sets' => [
        ['spec' => 'research', 'name' => 'Research Articles'],
        ['spec' => 'opinion', 'name' => 'Opinion Pieces'],
    ],
];

use Ottosmops\OaiPmh\Http\Controllers\OaiController;

Route::match(['get', 'post'], '/oai', OaiController::class);

use Ottosmops\OaiPmh\Oai;

Oai::routes(); // registers GET|POST /oai

Record::deleted(
    identifier: new Identifier('ns', 'local-123'),
    datestamp: Carbon::now(),
    sets: ['fonds'],
);
bash
php artisan vendor:publish --tag=oai-pmh-config
bash
php artisan vendor:publish --tag=oai-pmh-views