Skip to content

Commit

Permalink
Merge pull request #332 from ubermichael/main
Browse files Browse the repository at this point in the history
new command to update title pub dates from AAS MARC dates
  • Loading branch information
ubermichael authored Aug 3, 2022
2 parents 5fc2c46 + f8d4c75 commit 53d4b11
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 2 deletions.
136 changes: 136 additions & 0 deletions src/Command/UpdateAasDatesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

declare(strict_types=1);

/*
* (c) 2022 Michael Joyce <mjoyce@sfu.ca>
* This source file is subject to the GPL v2, bundled
* with this source code in the file LICENSE.
*/

namespace App\Command;

use App\Entity\AasMarc;
use App\Entity\Source;
use App\Entity\Title;
use App\Entity\TitleSource;
use App\Repository\AasMarcRepository;
use App\Services\MarcManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class UpdateAasDatesCommand extends Command {
private EntityManagerInterface $em;

private int $n = 0;

protected static $defaultName = 'wphp:update:aas-dates';

protected static string $defaultDescription = 'Update the imported AAS records dates';

private AasMarcRepository $aasRepo;

private $save = false;

protected function configure() : void {
$this->setDescription(self::$defaultDescription);
$this->addOption('save', null, InputOption::VALUE_NONE, 'Save changes to database');
}

protected function dot(bool $finished = false) : void {
if($this->save) {
$this->em->flush();
}
$this->em->clear();
if ($finished) {
echo "\nfinished\n";
}
}

protected function execute(InputInterface $input, OutputInterface $output) : int {
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
$this->save = $input->getOption('save');
$dql = <<<'ENDQUERY'
SELECT t FROM App\Entity\Title t
ENDQUERY;
$query = $this->em->createQuery($dql);

// Iterate over ever title because Doctrine doesn't iterate well with
// MEMBER OF queries.
foreach ($query->toIterable() as $title) {
/** @var Title $title */

// don't mangle existing pubDates.
if($title->getPubdate()) {
$this->dot();
continue;
}

// search for the title source. 75 is the database ID for AAS.
$titleSource = null;
foreach ($title->getTitleSources() as $ts) {
/** @var TitleSource $ts */
if (75 === $ts->getSource()->getId()) {
$titleSource = $ts;
}
}
if ( ! $titleSource) {
$this->dot();
continue;
}

// find the AASMarc title ID (stored as `cid` in the database)
/** @var AasMarc $marc001 */
$marc001 = $this->aasRepo->findOneBy([
'field' => '001',
'fieldData' => $titleSource->getIdentifier()
]);
$titleId = $marc001->getTitleId();

// Use the title ID to find the date
/** @var AasMarc $marc260c */
$marc260c = $this->aasRepo->findOneBy([
'field' => '260',
'subfield' => 'c',
'titleId' => $titleId,
]);
if( ! $marc260c) {
$output->writeln("Cannot find AAS pubdate for {$title->getId()}");
$this->dot();
continue;
}
$dateField = $marc260c->getFieldData();
$matches = [];
if( ! preg_match('/(\d{4})/', $dateField, $matches)) {
$output->writeln("Cannot find year for {$title->getId()} in {$dateField}");
$this->dot();
continue;
}
$pubDate = $matches[1];
$title->setPubDate($pubDate);
$output->writeln("Setting date for {$title->getId()} to {$pubDate}");
$this->dot();
}
$this->dot(true);

return 0;
}

/**
* @required
*/
public function setEntityManager(EntityManagerInterface $em) : void {
$this->em = $em;
}

/**
* @required
*/
public function setAasMarcRepository(AasMarcRepository $aasRepo) {
$this->aasRepo = $aasRepo;
}

}
4 changes: 2 additions & 2 deletions src/Entity/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class Source {
/**
* @var bool
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
Expand Down Expand Up @@ -89,7 +89,7 @@ public function __toString() : string {
/**
* Get id.
*
* @return bool
* @return int
*/
public function getId() {
return $this->id;
Expand Down

0 comments on commit 53d4b11

Please sign in to comment.