Skip to content

Commit

Permalink
Allow to rebuild the Vimeo cache of custom content elements
Browse files Browse the repository at this point in the history
  • Loading branch information
qzminski committed Jun 15, 2016
1 parent 80baa29 commit de87da2
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 69 deletions.
21 changes: 12 additions & 9 deletions config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
* Register the classes
*/
ClassLoader::addClasses([
'Derhaeuptling\VimeoApi\UserDataContainer' => 'system/modules/vimeo_api/src/UserDataContainer.php',
'Derhaeuptling\VimeoApi\VimeoApi' => 'system/modules/vimeo_api/src/VimeoApi.php',
'Derhaeuptling\VimeoApi\VideoCache' => 'system/modules/vimeo_api/src/VideoCache.php',
'Derhaeuptling\VimeoApi\VimeoVideo' => 'system/modules/vimeo_api/src/VimeoVideo.php',
'Derhaeuptling\VimeoApi\ContentElement\AlbumElement' => 'system/modules/vimeo_api/src/ContentElement/AlbumElement.php',
'Derhaeuptling\VimeoApi\ContentElement\VideoElement' => 'system/modules/vimeo_api/src/ContentElement/VideoElement.php',
'Derhaeuptling\VimeoApi\Maintenance\CacheRebuilder' => 'system/modules/vimeo_api/src/Maintenance/CacheRebuilder.php',
'Derhaeuptling\VimeoApi\Maintenance\CacheRebuilderPopup' => 'system/modules/vimeo_api/src/Maintenance/CacheRebuilderPopup.php',
'Derhaeuptling\VimeoApi\Maintenance\ClearCache' => 'system/modules/vimeo_api/src/Maintenance/ClearCache.php',
'Derhaeuptling\VimeoApi\UserDataContainer' => 'system/modules/vimeo_api/src/UserDataContainer.php',
'Derhaeuptling\VimeoApi\VimeoApi' => 'system/modules/vimeo_api/src/VimeoApi.php',
'Derhaeuptling\VimeoApi\VideoCache' => 'system/modules/vimeo_api/src/VideoCache.php',
'Derhaeuptling\VimeoApi\VimeoVideo' => 'system/modules/vimeo_api/src/VimeoVideo.php',
'Derhaeuptling\VimeoApi\ContentElement\AlbumElement' => 'system/modules/vimeo_api/src/ContentElement/AlbumElement.php',
'Derhaeuptling\VimeoApi\ContentElement\VideoElement' => 'system/modules/vimeo_api/src/ContentElement/VideoElement.php',
'Derhaeuptling\VimeoApi\Maintenance\AlbumCacheRebuilder' => 'system/modules/vimeo_api/src/Maintenance/AlbumCacheRebuilder.php',
'Derhaeuptling\VimeoApi\Maintenance\CacheRebuildInterface' => 'system/modules/vimeo_api/src/Maintenance/CacheRebuildInterface.php',
'Derhaeuptling\VimeoApi\Maintenance\CacheRebuilder' => 'system/modules/vimeo_api/src/Maintenance/CacheRebuilder.php',
'Derhaeuptling\VimeoApi\Maintenance\CacheRebuilderPopup' => 'system/modules/vimeo_api/src/Maintenance/CacheRebuilderPopup.php',
'Derhaeuptling\VimeoApi\Maintenance\ClearCache' => 'system/modules/vimeo_api/src/Maintenance/ClearCache.php',
'Derhaeuptling\VimeoApi\Maintenance\VideoCacheRebuilder' => 'system/modules/vimeo_api/src/Maintenance/VideoCacheRebuilder.php',
]);

/**
Expand Down
6 changes: 6 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
'affected' => [\Derhaeuptling\VimeoApi\VideoCache::getRootFolder()],
];

/**
* Eligible content element types for Vimeo cache rebuild
*/
$GLOBALS['VIMEO_CACHE_REBUILDER']['vimeo_album'] = 'Derhaeuptling\VimeoApi\Maintenance\AlbumCacheRebuilder';
$GLOBALS['VIMEO_CACHE_REBUILDER']['vimeo_video'] = 'Derhaeuptling\VimeoApi\Maintenance\VideoCacheRebuilder';

/**
* Set the default image index for Vimeo
*/
Expand Down
63 changes: 63 additions & 0 deletions src/Maintenance/AlbumCacheRebuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* vimeo_api extension for Contao Open Source CMS
*
* Copyright (C) 2016 derhaeuptling
*
* @author derhaeuptling <https://derhaeuptling.com>
* @author Codefog <http://codefog.pl>
* @author Kamil Kuzminski <kamil.kuzminski@codefog.pl>
* @license LGPL
*/

namespace Derhaeuptling\VimeoApi\Maintenance;

use Contao\Config;
use Contao\ContentModel;
use Derhaeuptling\VimeoApi\VimeoApi;
use Derhaeuptling\VimeoApi\VimeoVideo;

class AlbumCacheRebuilder implements CacheRebuildInterface
{
/**
* Return true if the element is eligible for rebuild
*
* @param array $data
*
* @return bool
*/
public function isEligible(array $data)
{
return $data['vimeo_albumId'] ? true : false;
}

/**
* Rebuild the cache and return true on success, false otherwise
*
* @param VimeoApi $api
* @param ContentModel $contentElement
*
* @return bool
*/
public function rebuild(VimeoApi $api, ContentModel $contentElement)
{
$client = $api->getClient();

if (($album = $api->getAlbum($client, $contentElement->vimeo_albumId)) === null) {
return false;
}

/** @var VimeoVideo $video */
foreach ($api->getAlbumVideos($client, $contentElement->vimeo_albumId) as $video) {
if (($image = $api->getVideoImage($client, $video->getId(), Config::get('vimeo_imageIndex'))) === null) {
return false;
}

$video->setPicturesData($image);
$video->downloadPoster();
}

return true;
}
}
39 changes: 39 additions & 0 deletions src/Maintenance/CacheRebuildInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* vimeo_api extension for Contao Open Source CMS
*
* Copyright (C) 2016 derhaeuptling
*
* @author derhaeuptling <https://derhaeuptling.com>
* @author Codefog <http://codefog.pl>
* @author Kamil Kuzminski <kamil.kuzminski@codefog.pl>
* @license LGPL
*/

namespace Derhaeuptling\VimeoApi\Maintenance;

use Contao\ContentModel;
use Derhaeuptling\VimeoApi\VimeoApi;

interface CacheRebuildInterface
{
/**
* Return true if the element is eligible for rebuild
*
* @param array $data
*
* @return bool
*/
public function isEligible(array $data);

/**
* Rebuild the cache and return true on success, false otherwise
*
* @param VimeoApi $api
* @param ContentModel $contentElement
*
* @return bool
*/
public function rebuild(VimeoApi $api, ContentModel $contentElement);
}
132 changes: 72 additions & 60 deletions src/Maintenance/CacheRebuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ public function run()
switch ($data['ptable']) {
case 'tl_article':
$source = $GLOBALS['TL_LANG']['tl_maintenance']['vimeo.tableSourceRef']['article'];
$path = [$data['page'], $data['article']];
$path = [$data['_page'], $data['_article']];
break;

case 'tl_news':
$source = $GLOBALS['TL_LANG']['tl_maintenance']['vimeo.tableSourceRef']['news'];
$path = [$data['archive'], $data['news']];
$path = [$data['_archive'], $data['_news']];
break;

case 'tl_calendar_events':
$source = $GLOBALS['TL_LANG']['tl_maintenance']['vimeo.tableSourceRef']['event'];
$path = [$data['calendar'], $data['event']];
$path = [$data['_calendar'], $data['_event']];
break;

default:
Expand Down Expand Up @@ -121,28 +121,77 @@ public function run()
* Get the content elements
*
* @return array
*
* @throws \RuntimeException
*/
protected function getContentElements()
{
return Database::getInstance()->execute("
if (!is_array($GLOBALS['VIMEO_CACHE_REBUILDER']) || count($GLOBALS['VIMEO_CACHE_REBUILDER']) < 1) {
return [];
}

$elements = Database::getInstance()->execute("
SELECT
tl_content.id, tl_content.type, tl_content.vimeo_albumId, tl_content.vimeo_videoId, tl_content.ptable,
tl_article.title AS article,
tl_page.title AS page,
tl_news.headline AS news,
tl_news_archive.title AS archive,
tl_calendar_events.title AS event,
tl_calendar.title AS calendar
tl_content.*,
tl_article.title AS _article,
tl_page.title AS _page,
tl_news.headline AS _news,
tl_news_archive.title AS _archive,
tl_calendar_events.title AS _event,
tl_calendar.title AS _calendar
FROM tl_content
LEFT JOIN tl_article ON tl_article.id=tl_content.pid AND tl_content.ptable='tl_article'
LEFT JOIN tl_page ON tl_page.id=tl_article.pid
LEFT JOIN tl_news ON tl_news.id=tl_content.pid AND tl_content.ptable='tl_news'
LEFT JOIN tl_news_archive ON tl_news_archive.id=tl_news.pid
LEFT JOIN tl_calendar_events ON tl_calendar_events.id=tl_content.pid AND tl_content.ptable='tl_calendar_events'
LEFT JOIN tl_calendar ON tl_calendar.id=tl_calendar_events.pid
WHERE (tl_content.type='vimeo_album' OR tl_content.type='vimeo_video')
WHERE tl_content.type IN ('".implode("','", array_keys($GLOBALS['VIMEO_CACHE_REBUILDER']))."')
")
->fetchAllAssoc();

// Check which elements are eligible for the cache rebuild
foreach ($elements as $k => $v) {
$callback = $this->getCallbackInstance($v['type']);

if (!$callback->isEligible($v)) {
unset($elements[$k]);
}
}

return $elements;
}

/**
* Get the callback instance
*
* @param string $type
*
* @return CacheRebuildInterface
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
protected function getCallbackInstance($type)
{
$className = $GLOBALS['VIMEO_CACHE_REBUILDER'][$type];

if (!class_exists($className)) {
throw new \InvalidArgumentException(
sprintf('The class %s does not exist', $className)
);
}

/** @var CacheRebuildInterface $class */
$class = new $className;

if (!($class instanceof CacheRebuildInterface)) {
throw new \RuntimeException(
sprintf('The class %s must implement the CacheRebuildInterface interface', $className)
);
}

return $class;
}

/**
Expand Down Expand Up @@ -174,10 +223,16 @@ protected function rebuildPageCache()
{
$automator = new Automator();
$automator->purgePageCache();

header('HTTP/1.1 200 OK');
die('OK');
}

/**
* Rebuild the Vimeo cache
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
protected function rebuildVimeoCache()
{
Expand All @@ -188,58 +243,15 @@ protected function rebuildVimeoCache()
die('Bad Request');
}

$api = new VimeoApi(new ClearCache());
$client = $api->getClient();

switch ($contentElement->type) {
case 'vimeo_album':
if (($album = $api->getAlbum($client, $contentElement->vimeo_albumId)) === null) {
header('HTTP/1.1 400 Bad Request');
die('Bad Request');
}

/** @var VimeoVideo $video */
foreach ($api->getAlbumVideos($client, $contentElement->vimeo_albumId) as $video) {
if (!$this->rebuildVideoCache($api, $video)) {
header('HTTP/1.1 400 Bad Request');
die('Bad Request');
}
}
break;
$api = new VimeoApi(new ClearCache());
$callback = $this->getCallbackInstance($contentElement->type);

case 'vimeo_video':
if (($video = $api->getVideo($client, $contentElement->vimeo_videoId)) === null
|| !$this->rebuildVideoCache($api, $video)
) {
header('HTTP/1.1 400 Bad Request');
die('Bad Request');
}
break;
if (!$callback->rebuild($api, $contentElement)) {
header('HTTP/1.1 400 Bad Request');
die('Bad Request');
}

header('HTTP/1.1 200 OK');
die('OK');
}

/**
* Rebuild the video cache
*
* @param VimeoApi $api
* @param VimeoVideo $video
*
* @return bool
*/
protected function rebuildVideoCache(VimeoApi $api, VimeoVideo $video)
{
$client = $api->getClient();

if (($image = $api->getVideoImage($client, $video->getId(), Config::get('vimeo_imageIndex'))) === null) {
return false;
}

$video->setPicturesData($image);
$video->downloadPoster();

return true;
}
}
59 changes: 59 additions & 0 deletions src/Maintenance/VideoCacheRebuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* vimeo_api extension for Contao Open Source CMS
*
* Copyright (C) 2016 derhaeuptling
*
* @author derhaeuptling <https://derhaeuptling.com>
* @author Codefog <http://codefog.pl>
* @author Kamil Kuzminski <kamil.kuzminski@codefog.pl>
* @license LGPL
*/

namespace Derhaeuptling\VimeoApi\Maintenance;

use Contao\Config;
use Contao\ContentModel;
use Derhaeuptling\VimeoApi\VimeoApi;

class VideoCacheRebuilder implements CacheRebuildInterface
{
/**
* Return true if the element is eligible for rebuild
*
* @param array $data
*
* @return bool
*/
public function isEligible(array $data)
{
return $data['vimeo_videoId'] ? true : false;
}

/**
* Rebuild the cache and return true on success, false otherwise
*
* @param VimeoApi $api
* @param ContentModel $contentElement
*
* @return bool
*/
public function rebuild(VimeoApi $api, ContentModel $contentElement)
{
$client = $api->getClient();

if (($video = $api->getVideo($client, $contentElement->vimeo_videoId)) === null) {
return false;
}

if (($image = $api->getVideoImage($client, $video->getId(), Config::get('vimeo_imageIndex'))) === null) {
return false;
}

$video->setPicturesData($image);
$video->downloadPoster();

return true;
}
}

0 comments on commit de87da2

Please sign in to comment.