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(),
);
}
}