76 lines
1.6 KiB
PHP
76 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Models\Media;
|
|
|
|
class reindexElasticMedia extends Command
|
|
{
|
|
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'medias:reindex {injectfrom?}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Reindex la base médias dans Elasticsearch';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Reindexation de la base médias dans Elasticsearch...');
|
|
$bReprise = false;
|
|
$startFrom = $this->argument('injectfrom');
|
|
if($startFrom != "") {
|
|
$bReprise = true;
|
|
} else {
|
|
$startFrom = 1;
|
|
}
|
|
if (Media::indexExists() && !$bReprise)
|
|
Media::deleteIndex();
|
|
|
|
if(!$bReprise)
|
|
Media::createIndex();
|
|
|
|
$medias = Media::all();
|
|
$bar = $this->output->createProgressBar(count($medias));
|
|
$bar->start();
|
|
if($bReprise)
|
|
$bar->advance($startFrom);
|
|
|
|
$i=1;
|
|
foreach ($medias as $media) {
|
|
if($i >= $startFrom) {
|
|
// ---- index le media dans Elasticsearch
|
|
$media->addToIndex();
|
|
$bar->advance();
|
|
}
|
|
unset($media);
|
|
$i++;
|
|
}
|
|
$bar->finish();
|
|
$this->line("");
|
|
$this->info('Fin du traitement');
|
|
}
|
|
}
|