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

Add dev task to update all existing OAI records #7

Merged
merged 2 commits into from
Mar 28, 2022
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
69 changes: 67 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [OAI Record Managers](#oai-record-managers)
* [OAI fields](#oai-fields)
* [Routing](#routing)
* [Populating your initial set of OAI Records](#populating-your-initial-set-of-oai-records)
* [License](#license)
* [Maintainers](#maintainers)
* [Development and contribution](#development-and-contribution)
Expand Down Expand Up @@ -58,7 +59,7 @@ for this verb.

### List Metadata Formats

TBA
The response for this endpoint is generated based on the config you specify for `OaiController::$supported_formats`.

### List Sets

Expand All @@ -70,7 +71,13 @@ TBA

### List Records

TBA
This endpoint requires that the request includes a `metadataPrefix` parameter value that matches one of the configs
that you have specified for `OaiController::$supported_formats`.

The output of this endpoint is based on your current OAI Records

Filter support: TBA
Resumption tokens: TBA

### Get Record

Expand Down Expand Up @@ -160,6 +167,64 @@ SilverStripe\Control\Director:
'api/v1/oai': MyApp\OpenArchive\OaiController
```

## Populating your initial set of OAI Records

Because the module can't know what `DataObjects` you wish to apply the `OaiRecordManager` to, we have opted **not** to
add any dev task to populate initial `OaiRecords`. However, below is an example build task that you might adapt/own/use.

In this example we have applied the `OaiRecordManager` to `Page` and `File`.

```php
<?php

namespace App\Tasks;

... imports

class CreateInitialOaiRecords extends BuildTask
{

private static $segment = 'create-initial-oai-records'; // phpcs:ignore

protected $title = 'Create Initial OAI Records'; // phpcs:ignore

protected $description = 'Create/update OAI Records for all Pages and Documents'; // phpcs:ignore

/**
* @param HTTPRequest $request
* @return void
*/
public function run($request) // phpcs:ignore SlevomatCodingStandard.TypeHints
{
$classes = [
Page::class,
File::class,
];

foreach ($classes as $class) {
// Set our stage to LIVE so that we only fetch DataObjects that are available on the frontend. This isn't
// totally necessary since the Queued Job will validate this itself, but it saves us from queueing Jobs that
// we know we don't need
/** @var DataList|OaiRecordManager[] $dataObjects */
$dataObjects = Versioned::withVersionedMode(static function () use ($class): DataList {
Versioned::set_stage(Versioned::LIVE);

return DataObject::get($class);
});

// Easy as, just triggerOaiRecordUpdate(). This method + the queued job will take care of the rest
foreach ($dataObjects as $dataObject) {
$dataObject->triggerOaiRecordUpdate();
}
}

echo 'Finished queueing OAI Record update Jobs';
}

}

```

## License

See [License](license.md)
Expand Down
39 changes: 39 additions & 0 deletions src/Tasks/UpdateOaiRecords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Terraformers\OpenArchive\Tasks;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataList;
use Symbiote\QueuedJobs\Services\QueuedJobService;
use Terraformers\OpenArchive\Jobs\OaiRecordUpdateJob;
use Terraformers\OpenArchive\Models\OaiRecord;

class UpdateOaiRecords extends BuildTask
{

private static $segment = 'update-oai-records'; // phpcs:ignore

protected $title = 'Update OAI Records'; // phpcs:ignore

protected $description = 'Queue update jobs for all existing OAI Records'; // phpcs:ignore

/**
* @param HTTPRequest $request
*/
public function run($request) // phpcs:ignore
{
/** @var DataList|OaiRecord[] $oaiRecords */
$oaiRecords = OaiRecord::get();

foreach ($oaiRecords as $oaiRecord) {
$job = OaiRecordUpdateJob::create();
$job->hydrate($oaiRecord->RecordClass, $oaiRecord->RecordID);

QueuedJobService::singleton()->queueJob($job);
}

echo 'Jobs queued to update all existing OAI Records';
}

}