1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Colligator\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Colligator\Collection; |
6
|
|
|
use Colligator\Document; |
7
|
|
|
use Colligator\EnrichmentService; |
8
|
|
|
use Colligator\Search\DocumentsIndex; |
9
|
|
|
use Illuminate\Console\Command; |
10
|
|
|
|
11
|
|
|
class EnrichDocuments extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name and signature of the console command. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'colligator:enrich |
19
|
|
|
{service : Name of the service (e.g. "googlebooks")} |
20
|
|
|
{--f|force : Enrich documents that haven\'t changed} |
21
|
|
|
{--collection= : Collection id, for enriching only documents belonging to a single collection}'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Enrich documents using some service.'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Sleep time in seconds between requests. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
public $sleepTime = 5; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var DocumentsIndex |
39
|
|
|
*/ |
40
|
|
|
public $docIndex; |
41
|
|
|
|
42
|
|
|
protected $services; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new command instance. |
46
|
|
|
*/ |
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
parent::__construct(); |
50
|
|
|
|
51
|
|
|
$this->services = [ |
52
|
|
|
\Colligator\GoogleBooksService::$serviceName => 'Colligator\GoogleBooksService', |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getDocumentsToBeChecked($serviceName, $force = false, $collectionId = 0) |
57
|
|
|
{ |
58
|
|
|
|
59
|
|
|
if ($collectionId > 0) { |
60
|
|
|
$docs = Collection::findOrFail($collectionId)->documents(); |
61
|
|
|
} else { |
62
|
|
|
$docs = Document::query(); |
63
|
|
|
} |
64
|
|
|
if ($force) { |
65
|
|
|
return $docs->get(); |
66
|
|
|
} |
67
|
|
|
return $docs->whereDoesntHave('enrichments', function($query) use ($serviceName) { |
68
|
|
|
$query->where('service_name', '=', $serviceName); |
69
|
|
|
})->get(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function handleDocuments(EnrichmentService $service, $docs) |
73
|
|
|
{ |
74
|
|
|
\Log::info('[EnrichDocuments] Starting job. ' . count($docs) . ' documents to be checked using the "' . $service::$serviceName . '" service.'); |
75
|
|
|
|
76
|
|
|
// $this->output->progressStart(count($docs)); |
|
|
|
|
77
|
|
|
foreach ($docs as $doc) { |
78
|
|
|
$service->enrich($doc); |
79
|
|
|
|
80
|
|
|
// Update ElasticSearch |
81
|
|
|
$this->docIndex->index($doc); |
82
|
|
|
|
83
|
|
|
// $this->output->progressAdvance(); |
|
|
|
|
84
|
|
|
sleep($this->sleepTime); |
85
|
|
|
} |
86
|
|
|
// $this->output->progressFinish(); |
|
|
|
|
87
|
|
|
\Log::info('[EnrichDocuments] Job complete.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Execute the console command. |
92
|
|
|
* |
93
|
|
|
* @param DocumentsIndex $docIndex |
94
|
|
|
*/ |
95
|
|
|
public function handle(DocumentsIndex $docIndex) |
96
|
|
|
{ |
97
|
|
|
$formatter = new \Monolog\Formatter\LineFormatter(null, null, null, true); |
|
|
|
|
98
|
|
|
$handler = new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG); |
99
|
|
|
$handler->setFormatter($formatter); |
100
|
|
|
$monolog = \Log::getMonolog(); |
101
|
|
|
$monolog->pushHandler($handler); |
102
|
|
|
|
103
|
|
|
$this->docIndex = $docIndex; |
104
|
|
|
$serviceName = $this->argument('service'); |
105
|
|
|
$force = $this->option('force'); |
106
|
|
|
$verbose = $this->option('verbose'); |
107
|
|
|
$collectionId = intval($this->option('collection')); |
108
|
|
|
|
109
|
|
|
if (!isset($this->services[$serviceName])) { |
110
|
|
|
$this->error('Service "' . $serviceName . '" is not defined. Available servies: "' . implode('", "', array_keys($this->services)) . '".'); |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($verbose) { |
115
|
|
|
\DB::listen(function ($query, $bindings) { |
116
|
|
|
$this->comment('Query: ' . $query . '. Bindings: ' . implode(', ', $bindings)); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$collectionHelp = ($collectionId > 0) ? ' in collection ' . $collectionId : ''; |
121
|
|
|
|
122
|
|
|
$docs = $this->getDocumentsToBeChecked($serviceName, $force, $collectionId); |
|
|
|
|
123
|
|
|
|
124
|
|
|
if (!count($docs)) { |
125
|
|
|
$this->info('No new documents' . $collectionHelp . '. Exiting.'); |
126
|
|
|
\Log::info('[EnrichDocuments] No new documents to be checked.'); |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$service = $this->getLaravel()->make($this->services[$serviceName]); |
131
|
|
|
$this->handleDocuments($service, $docs); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.