Skip to content

Commit

Permalink
TTK-23478 migration sf4 (pumukit#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurujai authored Oct 5, 2021
1 parent c828cae commit 3c3d71d
Show file tree
Hide file tree
Showing 46 changed files with 761 additions and 1,822 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Code validation

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php-versions: ['7.4']

steps:
- name: Checkout
uses: actions/checkout@v2

- name: PHP validator
run: find . -type f -name "*.php" -print0 | xargs -0 -n1 -P8 php -l

- name: PHPCSFixer
uses: docker://oskarstark/php-cs-fixer-ga:3.1.0
with:
args: --config=.php-cs-fixer.dist.php --diff --dry-run
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.swp
*.py[co]
\#*\#
.php-cs-fixer.cache
23 changes: 23 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

$config = new PhpCsFixer\Config();
return $config->setRules([
'@Symfony' => true,
'@PhpCsFixer' => true,
'array_syntax' => ['syntax' => 'short'],
'is_null' => true,
'list_syntax' => ['syntax' => 'short'],
'modernize_types_casting' => true,
'ternary_to_null_coalescing' => true,
'combine_nested_dirname' => true,
'phpdoc_types_order' => [
'null_adjustment' => 'always_last',
'sort_algorithm' => 'none',
],
])
->setFinder($finder)
->setRiskyAllowed(true)
;
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

108 changes: 51 additions & 57 deletions Command/MultipleOpencastHostImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,46 @@

namespace Pumukit\OpencastBundle\Command;

use Doctrine\ODM\MongoDB\DocumentManager;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\Regex;
use Psr\Log\LoggerInterface;
use Pumukit\OpencastBundle\Services\ClientService;
use Pumukit\OpencastBundle\Services\OpencastImportService;
use Pumukit\SchemaBundle\Document\MultimediaObject;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 MultipleOpencastHostImportCommand extends ContainerAwareCommand
class MultipleOpencastHostImportCommand extends Command
{
private $documentManager;
private $opencastImportService;
private $user;
private $password;
private $host;
private $id;
private $force;
private $master;
/** @var ClientService */
private $clientService;
private $secondsToSleep;
private $logger;

public function __construct(
DocumentManager $documentManager,
OpencastImportService $opencastImportService,
LoggerInterface $logger,
int $secondsToSleep
) {
$this->documentManager = $documentManager;
$this->opencastImportService = $opencastImportService;
$this->secondsToSleep = $secondsToSleep;
$this->logger = $logger;

parent::__construct();
}

protected function configure(): void
{
Expand Down Expand Up @@ -73,9 +94,6 @@ protected function configure(): void

protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->opencastImportService = $this->getContainer()->get('pumukit_opencast.import');
$this->secondsToSleep = $this->getContainer()->getParameter('pumukit_opencast.seconds_to_sleep_on_commands');

$this->user = trim($input->getOption('user'));
$this->password = trim($input->getOption('password'));
$this->host = trim($input->getOption('host'));
Expand All @@ -95,35 +113,31 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
false,
true,
null,
$this->getContainer()->get('logger'),
$this->logger,
null
);
}

/**
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->checkInputs();

if ($this->checkOpencastStatus($this->clientService)) {
if ($this->checkOpencastStatus()) {
$multimediaObjects = $this->getMultimediaObjects();
if ($this->force) {
if ($this->master) {
$this->importMasterTracks($output, $this->clientService, $this->opencastImportService, $multimediaObjects);
$this->importMasterTracks($output, $multimediaObjects);
} else {
$this->importBroadcastTracks($output, $this->clientService, $this->opencastImportService, $multimediaObjects);
$this->importBroadcastTracks($output, $multimediaObjects);
}
} else {
$this->showMultimediaObjects($output, $this->opencastImportService, $this->clientService, $multimediaObjects, $this->master);
$this->showMultimediaObjects($output, $multimediaObjects, $this->master);
}
}

return 0;
}

/**
* @throws \Exception
*/
private function checkInputs(): void
{
if (!$this->user || !$this->password || !$this->host) {
Expand All @@ -138,12 +152,9 @@ private function checkInputs(): void
}
}

/**
* @throws \Exception
*/
private function checkOpencastStatus(ClientService $clientService): bool
private function checkOpencastStatus(): bool
{
if ($clientService->getAdminUrl()) {
if ($this->clientService->getAdminUrl()) {
return true;
}

Expand All @@ -152,22 +163,18 @@ private function checkOpencastStatus(ClientService $clientService): bool

private function getMultimediaObjects(): array
{
$dm = $this->getContainer()->get('doctrine_mongodb.odm.document_manager');
$criteria = [
'properties.opencasturl' => new \MongoRegex("/{$this->host}/i"),
'properties.opencasturl' => new Regex($this->host, 'i'),
];

if ($this->id) {
$criteria['_id'] = new \MongoId($this->id);
$criteria['_id'] = new ObjectId($this->id);
}

return $dm->getRepository(MultimediaObject::class)->findBy($criteria);
return $this->documentManager->getRepository(MultimediaObject::class)->findBy($criteria);
}

/**
* @throws \Exception
*/
private function importBroadcastTracks(OutputInterface $output, ClientService $clientService, OpencastImportService $opencastImportService, array $multimediaObjects): void
private function importBroadcastTracks(OutputInterface $output, array $multimediaObjects): void
{
$output->writeln(
[
Expand All @@ -183,8 +190,6 @@ private function importBroadcastTracks(OutputInterface $output, ClientService $c
sleep($this->secondsToSleep);
$this->importTrackOnMultimediaObject(
$output,
$clientService,
$opencastImportService,
$multimediaObject,
false
);
Expand All @@ -194,10 +199,7 @@ private function importBroadcastTracks(OutputInterface $output, ClientService $c
}
}

/**
* @throws \Exception
*/
private function importMasterTracks(OutputInterface $output, ClientService $clientService, OpencastImportService $opencastImportService, array $multimediaObjects)
private function importMasterTracks(OutputInterface $output, array $multimediaObjects): void
{
$output->writeln(
[
Expand All @@ -213,8 +215,6 @@ private function importMasterTracks(OutputInterface $output, ClientService $clie
sleep($this->secondsToSleep);
$this->importTrackOnMultimediaObject(
$output,
$clientService,
$opencastImportService,
$multimediaObject,
true
);
Expand All @@ -224,31 +224,25 @@ private function importMasterTracks(OutputInterface $output, ClientService $clie
}
}

/**
* @throws \Exception
*/
private function importTrackOnMultimediaObject(OutputInterface $output, ClientService $clientService, OpencastImportService $opencastImportService, MultimediaObject $multimediaObject, bool $master)
private function importTrackOnMultimediaObject(OutputInterface $output, MultimediaObject $multimediaObject, bool $master): void
{
if ($master) {
$mediaPackage = $clientService->getMasterMediaPackage($multimediaObject->getProperty('opencast'));
$mediaPackage = $this->clientService->getMasterMediaPackage($multimediaObject->getProperty('opencast'));
$trackTags = ['master'];
} else {
$mediaPackage = $clientService->getMediaPackage($multimediaObject->getProperty('opencast'));
$mediaPackage = $this->clientService->getMediaPackage($multimediaObject->getProperty('opencast'));
$trackTags = ['display'];
}

try {
$opencastImportService->importTracksFromMediaPackage($mediaPackage, $multimediaObject, $trackTags);
$this->showMessage($output, $opencastImportService, $multimediaObject, $mediaPackage);
$this->opencastImportService->importTracksFromMediaPackage($mediaPackage, $multimediaObject, $trackTags);
$this->showMessage($output, $multimediaObject, $mediaPackage);
} catch (\Exception $exception) {
$output->writeln('<error>Error - MMobj: '.$multimediaObject->getId().' and mediaPackage: '.$multimediaObject->getProperty('opencast').' with this error: '.$exception->getMessage().'</error>');
}
}

/**
* @throws \Exception
*/
private function showMultimediaObjects(OutputInterface $output, OpencastImportService $opencastImportService, ClientService $clientService, array $multimediaObjects, bool $master): void
private function showMultimediaObjects(OutputInterface $output, array $multimediaObjects, bool $master): void
{
$message = '<info> **** Finding Multimedia Objects **** </info>';
if ($master) {
Expand All @@ -265,19 +259,19 @@ private function showMultimediaObjects(OutputInterface $output, OpencastImportSe

foreach ($multimediaObjects as $multimediaObject) {
if ($master) {
$mediaPackage = $clientService->getMasterMediaPackage($multimediaObject->getProperty('opencast'));
$this->showMessage($output, $opencastImportService, $multimediaObject, $mediaPackage);
$mediaPackage = $this->clientService->getMasterMediaPackage($multimediaObject->getProperty('opencast'));
$this->showMessage($output, $multimediaObject, $mediaPackage);
} else {
$mediaPackage = $clientService->getMediaPackage($multimediaObject->getProperty('opencast'));
$this->showMessage($output, $opencastImportService, $multimediaObject, $mediaPackage);
$mediaPackage = $this->clientService->getMediaPackage($multimediaObject->getProperty('opencast'));
$this->showMessage($output, $multimediaObject, $mediaPackage);
}
}
}

private function showMessage(OutputInterface $output, OpencastImportService $opencastImportService, MultimediaObject $multimediaObject, array $mediaPackage): void
private function showMessage(OutputInterface $output, MultimediaObject $multimediaObject, array $mediaPackage): void
{
$media = $opencastImportService->getMediaPackageField($mediaPackage, 'media');
$tracks = $opencastImportService->getMediaPackageField($media, 'track');
$media = $this->opencastImportService->getMediaPackageField($mediaPackage, 'media');
$tracks = $this->opencastImportService->getMediaPackageField($media, 'track');
$tracksCount = 1;
if (isset($tracks[0])) {
$tracksCount = count($tracks);
Expand Down
39 changes: 28 additions & 11 deletions Command/OpencastBatchImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@

namespace Pumukit\OpencastBundle\Command;

use Doctrine\ODM\MongoDB\DocumentManager;
use Pumukit\OpencastBundle\Services\ClientService;
use Pumukit\OpencastBundle\Services\OpencastImportService;
use Pumukit\SchemaBundle\Document\MultimediaObject;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 OpencastBatchImportCommand extends ContainerAwareCommand
class OpencastBatchImportCommand extends Command
{
protected function configure()
private $documentManager;
private $opencastClientService;
private $opencastImportService;
private $opencastBatchImportInverted;

public function __construct(DocumentManager $documentManager, ClientService $opencastClientService, OpencastImportService $opencastImportService, $opencastBatchImportInverted)
{
$this->documentManager = $documentManager;
$this->opencastClientService = $opencastClientService;
$this->opencastImportService = $opencastImportService;
$this->opencastBatchImportInverted = $opencastBatchImportInverted;
parent::__construct();
}

protected function configure(): void
{
$this
->setName('pumukit:opencast:batchimport')
Expand All @@ -19,17 +36,16 @@ protected function configure()
;
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$startTime = microtime(true);
$opencastClientService = $this->getContainer()->get('pumukit_opencast.client');
$mediaPackages = $opencastClientService->getMediaPackages('', 1, 0);
$mediaPackages = $this->opencastClientService->getMediaPackages('', 1, 0);

$invert = $input->getOption('invert');
if (('0' === $invert) || ('1' === $invert)) {
$invert = ('1' === $invert);
} else {
$invert = $this->getContainer()->getParameter('pumukit_opencast.batchimport_inverted');
$invert = $this->opencastBatchImportInverted;
}

$totalMediaPackages = $mediaPackages[0];
Expand All @@ -40,22 +56,23 @@ protected function execute(InputInterface $input, OutputInterface $output)

while ($batchPlace < $totalMediaPackages) {
$output->writeln('Importing recordings '.$batchPlace.' to '.($batchPlace + $batchSize));
$mediaPackages = $opencastClientService->getMediaPackages('', $batchSize, $batchPlace);
$mediaPackages = $this->opencastClientService->getMediaPackages('', $batchSize, $batchPlace);

$opencastImportService = $this->getContainer()->get('pumukit_opencast.import');
$repositoryMultimediaObjects = $this->getContainer()->get('doctrine_mongodb')->getRepository(MultimediaObject::class);
$repositoryMultimediaObjects = $this->documentManager->getRepository(MultimediaObject::class);

foreach ($mediaPackages[1] as $mediaPackage) {
$output->writeln('Importing mediapackage: '.$mediaPackage['id']);
if ($repositoryMultimediaObjects->findOneBy(['properties.opencast' => $mediaPackage['id']])) {
$output->writeln('Mediapackage '.$mediaPackage['id'].' has already been imported, skipping to next mediapackage');
} else {
$opencastImportService->importRecording($mediaPackage['id'], $invert);
$this->opencastImportService->importRecording($mediaPackage['id'], $invert);
}
}
$batchPlace += $batchSize;
}
$stopTime = microtime(true);
$output->writeln('Finished importing '.$totalMediaPackages.' recordings in '.($stopTime - $startTime).' seconds');

return 0;
}
}
Loading

0 comments on commit 3c3d71d

Please sign in to comment.