1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colligator\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Colligator\Jobs\OaiPmhHarvest as OaiPmhHarvestJob; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
9
|
|
|
|
10
|
|
|
class OaiPmhHarvest extends Command |
11
|
|
|
{ |
12
|
|
|
use DispatchesJobs; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'colligator:harvest-oaipmh |
20
|
|
|
{name? : Name of the harvest config as defined in configs/oaipmh.php} |
21
|
|
|
{--from= : Start date on ISO format YYYY-MM-DD} |
22
|
|
|
{--until= : End date on ISO format YYYY-MM-DD} |
23
|
|
|
{--resume= : Resumption token} |
24
|
|
|
{--from-dump : Just re-index from dump} |
25
|
|
|
{--daily : Harvest records modified yesterday. Cannot be combined with --from / --until}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Harvest records from OAI-PMH service and store as XML files.'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new command instance. |
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Output a list of the configurations. |
44
|
|
|
*/ |
45
|
|
|
public function listConfigurations() |
46
|
|
|
{ |
47
|
|
|
$this->comment(''); |
48
|
|
|
$this->comment('Available configurations:'); |
49
|
|
|
$config = \Config::get('oaipmh.harvests', null); |
50
|
|
|
foreach (array_keys($config) as $key) { |
51
|
|
|
$this->comment(' - ' . $key); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function validate() |
56
|
|
|
{ |
57
|
|
|
if (empty($this->argument('name'))) { |
58
|
|
|
$this->listConfigurations(); |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
$harvestConfig = \Config::get('oaipmh.harvests.' . $this->argument('name'), null); |
63
|
|
|
if (is_null($harvestConfig)) { |
64
|
|
|
$this->error('Unknown configuration specified.'); |
65
|
|
|
$this->listConfigurations(); |
66
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
View Code Duplication |
if ($this->option('daily')) { |
70
|
|
|
if ($this->option('from') || $this->option('until')) { |
71
|
|
|
$this->error('--daily cannot be combined with --from / --until.'); |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
if ($this->option('from-dump')) { |
77
|
|
View Code Duplication |
if ($this->option('from') || $this->option('until') || $this->option('resume') || $this->option('daily')) { |
78
|
|
|
$this->error('--from-dump cannot be combined with other options.'); |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
View Code Duplication |
if ($this->option('from')) { |
84
|
|
|
if (!preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $this->option('from'))) { |
85
|
|
|
$this->error('--from must be on ISO-format YYYY-MM-DD.'); |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
View Code Duplication |
if ($this->option('until')) { |
91
|
|
|
if (!preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $this->option('until'))) { |
92
|
|
|
$this->error('--until must be on ISO-format YYYY-MM-DD.'); |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Execute the console command. |
103
|
|
|
*/ |
104
|
|
|
public function handle() |
105
|
|
|
{ |
106
|
|
|
if (!$this->validate()) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$harvestName = $this->argument('name'); |
111
|
|
|
$harvestConfig = \Config::get('oaipmh.harvests.' . $harvestName, null); |
112
|
|
|
|
113
|
|
|
$this->comment(sprintf('Dispatching new harvest job')); |
114
|
|
|
|
115
|
|
|
if ($this->option('from-dump')) { |
116
|
|
|
$this->comment(' - From local dump'); |
117
|
|
|
} else { |
118
|
|
|
$this->comment(' - Repo: ' . $harvestConfig['url']); |
119
|
|
|
$this->comment(' - Schema: ' . $harvestConfig['schema']); |
120
|
|
|
$this->comment(' - Set: ' . $harvestConfig['set']); |
121
|
|
|
|
122
|
|
|
foreach (['from', 'until', 'resume', 'daily'] as $key) { |
123
|
|
|
if (!is_null($this->option($key))) { |
124
|
|
|
$this->comment(sprintf(' - %s: %s', ucfirst($key), $this->option($key))); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$from = $this->option('from'); |
130
|
|
|
$until = $this->option('until'); |
131
|
|
|
if ($this->option('daily')) { |
132
|
|
|
$from = Carbon::now()->subDay()->toDateString(); |
133
|
|
|
$until = $from; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->dispatch( |
137
|
|
|
new OaiPmhHarvestJob( |
138
|
|
|
$harvestName, |
139
|
|
|
$harvestConfig, |
140
|
|
|
$from, |
141
|
|
|
$until, |
142
|
|
|
$this->option('resume'), |
143
|
|
|
$this->option('from-dump') |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|