* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org/wiki/indexing:oai-pmh Wiki */ namespace VuFindHarvest\OaiPmh; use VuFindHarvest\ConsoleOutput\WriterAwareTrait; use function count; /** * OAI-PMH Harvest Tool * * @category VuFind * @package Harvest_Tools * @author Demian Katz * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @link https://vufind.org/wiki/indexing:oai-pmh Wiki */ class SetLoader { use WriterAwareTrait; /** * Low-level OAI-PMH communicator * * @var Communicator */ protected $communicator; /** * Constructor. * * @param Communicator $communicator Low-level API client */ public function __construct(Communicator $communicator) { $this->communicator = $communicator; } /** * Make an OAI-PMH request. Die if there is an error; return a SimpleXML object * on success. * * @param string $verb OAI-PMH verb to execute. * @param array $params GET parameters for ListRecords method. * * @return object SimpleXML-formatted response. */ protected function sendRequest($verb, $params = []) { $result = $this->communicator->request($verb, $params); // Detect errors and die if one is found: if ($result->error) { $attribs = $result->error->attributes(); throw new \Exception( "OAI-PMH error -- code: {$attribs['code']}, " . "value: {$result->error}" ); } return $result; } /** * Load set list from the server. * * @return array */ public function getNames() { $this->write('Loading set list... '); // On the first pass through the following loop, we want to get the // first page of sets without using a resumption token: $params = []; $setNames = []; // Grab set information until we have it all (at which point we will // break out of this otherwise-infinite loop): do { // Process current page of results: $response = $this->sendRequest('ListSets', $params); if (isset($response->ListSets->set)) { foreach ($response->ListSets->set as $current) { $spec = (string)$current->setSpec; $name = (string)$current->setName; if (!empty($spec)) { $setNames[$spec] = $name; } } } // Is there a resumption token? If so, continue looping; if not, // we're done! $params['resumptionToken'] = !empty($response->ListSets->resumptionToken) ? (string)$response->ListSets->resumptionToken : ''; } while (!empty($params['resumptionToken'])); $this->writeLine('found ' . count($setNames)); return $setNames; } }