Skip to content

Commit

Permalink
Process picwall rendering through Slim controller + UT
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHoaro committed Jul 23, 2020
1 parent bee3323 commit 485b168
Show file tree
Hide file tree
Showing 15 changed files with 342 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -14,7 +14,7 @@ indent_size = 4
indent_size = 2

[*.php]
max_line_length = 100
max_line_length = 120

[Dockerfile]
max_line_length = 80
Expand Down
2 changes: 1 addition & 1 deletion application/bookmark/Bookmark.php
Expand Up @@ -346,7 +346,7 @@ public function setTags($tags)
/**
* Get the Thumbnail.
*
* @return string|bool
* @return string|bool|null
*/
public function getThumbnail()
{
Expand Down
5 changes: 5 additions & 0 deletions application/container/ContainerBuilder.php
Expand Up @@ -7,6 +7,7 @@
use Shaarli\Bookmark\BookmarkFileService;
use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\History;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\PageBuilder;
Expand Down Expand Up @@ -76,6 +77,10 @@ public function build(): ShaarliContainer
return new PluginManager($container->conf);
};

$container['formatterFactory'] = function (ShaarliContainer $container): FormatterFactory {
return new FormatterFactory($container->conf, $container->loginManager->isLoggedIn());
};

return $container;
}
}
2 changes: 2 additions & 0 deletions application/container/ShaarliContainer.php
Expand Up @@ -6,6 +6,7 @@

use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\History;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\PageBuilder;
Expand All @@ -23,6 +24,7 @@
* @property BookmarkServiceInterface $bookmarkService
* @property PageBuilder $pageBuilder
* @property PluginManager $pluginManager
* @property FormatterFactory $formatterFactory
*/
class ShaarliContainer extends Container
{
Expand Down
72 changes: 72 additions & 0 deletions application/front/controllers/PictureWallController.php
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller;

use Shaarli\Front\Exception\ThumbnailsDisabledException;
use Shaarli\Thumbnailer;
use Slim\Http\Request;
use Slim\Http\Response;

/**
* Class PicturesWallController
*
* Slim controller used to render the pictures wall page.
* If thumbnails mode is set to NONE, we just render the template without any image.
*
* @package Front\Controller
*/
class PictureWallController extends ShaarliController
{
public function index(Request $request, Response $response): Response
{
if ($this->container->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) === Thumbnailer::MODE_NONE) {
throw new ThumbnailsDisabledException();
}

$this->assignView(
'pagetitle',
t('Picture wall') .' - '. $this->container->conf->get('general.title', 'Shaarli')
);

// Optionally filter the results:
$links = $this->container->bookmarkService->search($request->getQueryParams());
$linksToDisplay = [];

// Get only bookmarks which have a thumbnail.
// Note: we do not retrieve thumbnails here, the request is too heavy.
$formatter = $this->container->formatterFactory->getFormatter('raw');
foreach ($links as $key => $link) {
if (!empty($link->getThumbnail())) {
$linksToDisplay[] = $formatter->format($link);
}
}

$data = $this->executeHooks($linksToDisplay);
foreach ($data as $key => $value) {
$this->assignView($key, $value);
}

return $response->write($this->render('picwall'));
}

/**
* @param mixed[] $linksToDisplay List of formatted bookmarks
*
* @return mixed[] Template data after active plugins render_picwall hook execution.
*/
protected function executeHooks(array $linksToDisplay): array
{
$data = [
'linksToDisplay' => $linksToDisplay,
];
$this->container->pluginManager->executeHooks(
'render_picwall',
$data,
['loggedin' => $this->container->loginManager->isLoggedIn()]
);

return $data;
}
}
15 changes: 15 additions & 0 deletions application/front/exceptions/ThumbnailsDisabledException.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Shaarli\Front\Exception;

class ThumbnailsDisabledException extends ShaarliException
{
public function __construct()
{
$message = t('Picture wall unavailable (thumbnails are disabled).');

parent::__construct($message, 400);
}
}
18 changes: 17 additions & 1 deletion application/updater/Updater.php
Expand Up @@ -2,8 +2,8 @@

namespace Shaarli\Updater;

use Shaarli\Config\ConfigManager;
use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Updater\Exception\UpdaterException;

/**
Expand Down Expand Up @@ -111,4 +111,20 @@ public function getDoneUpdates()
{
return $this->doneUpdates;
}

/**
* With the Slim routing system, default header link should be `./` instead of `?`.
* Otherwise you can not go back to the home page. Example: `/picture-wall` -> `/picture-wall?` instead of `/`.
*/
public function updateMethodRelativeHomeLink(): bool
{
$link = trim($this->conf->get('general.header_link'));
if ($link[0] === '?') {
$link = './'. ltrim($link, '?');

$this->conf->set('general.header_link', $link, true, true);
}

return true;
}
}
4 changes: 2 additions & 2 deletions doc/md/RSS-feeds.md
Expand Up @@ -21,8 +21,8 @@ For example, if you want to subscribe only to links tagged `photography`:
- Click on the `RSS Feed` button.
- You are presented with an RSS feed showing only these links. Subscribe to it to receive only updates with this tag.
- The same method **also works for a full-text search** (_Search_ box) **and for the Picture Wall** (want to only see pictures about `nature`?)
- You can also build the URLs manually:
- You can also build the URLs manually:
- `https://my.shaarli.domain/?do=rss&searchtags=nature`
- `https://my.shaarli.domain/links/?do=picwall&searchterm=poney`
- `https://my.shaarli.domain/links/picture-wall?searchterm=poney`

![](images/rss-filter-1.png) ![](images/rss-filter-2.png)
2 changes: 1 addition & 1 deletion doc/md/Translations.md
Expand Up @@ -42,7 +42,7 @@ http://<replace_domain>/?post
http://<replace_domain>/?do=export
http://<replace_domain>/?do=import
http://<replace_domain>/login
http://<replace_domain>/?do=picwall
http://<replace_domain>/picture-wall
http://<replace_domain>/?do=pluginadmin
http://<replace_domain>/?do=tagcloud
http://<replace_domain>/?do=taglist
Expand Down
33 changes: 2 additions & 31 deletions index.php
Expand Up @@ -610,37 +610,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM

// -------- Picture wall
if ($targetPage == Router::$PAGE_PICWALL) {
$PAGE->assign('pagetitle', t('Picture wall') .' - '. $conf->get('general.title', 'Shaarli'));
if (! $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) === Thumbnailer::MODE_NONE) {
$PAGE->assign('linksToDisplay', []);
$PAGE->renderPage('picwall');
exit;
}

// Optionally filter the results:
$links = $bookmarkService->search($_GET);
$linksToDisplay = [];

// Get only bookmarks which have a thumbnail.
// Note: we do not retrieve thumbnails here, the request is too heavy.
$factory = new FormatterFactory($conf, $loginManager->isLoggedIn());
$formatter = $factory->getFormatter();
foreach ($links as $key => $link) {
if ($link->getThumbnail() !== false) {
$linksToDisplay[] = $formatter->format($link);
}
}

$data = [
'linksToDisplay' => $linksToDisplay,
];
$pluginManager->executeHooks('render_picwall', $data, ['loggedin' => $loginManager->isLoggedIn()]);

foreach ($data as $key => $value) {
$PAGE->assign($key, $value);
}

$PAGE->renderPage('picwall');
header('Location: ./picture-wall');
exit;
}

Expand Down Expand Up @@ -1944,6 +1914,7 @@ function install($conf, $sessionManager, $loginManager)

$app->group('', function () {
$this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login');
$this->get('/picture-wall', '\Shaarli\Front\Controller\PictureWallController:index')->setName('picwall');
})->add('\Shaarli\Front\ShaarliMiddleware');

$response = $app->run(true);
Expand Down
4 changes: 4 additions & 0 deletions tests/container/ContainerBuilderTest.php
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\History;
use Shaarli\Render\PageBuilder;
use Shaarli\Security\LoginManager;
Expand All @@ -30,7 +31,9 @@ public function setUp(): void
{
$this->conf = new ConfigManager('tests/utils/config/configJson');
$this->sessionManager = $this->createMock(SessionManager::class);

$this->loginManager = $this->createMock(LoginManager::class);
$this->loginManager->method('isLoggedIn')->willReturn(true);

$this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager);
}
Expand All @@ -45,5 +48,6 @@ public function testBuildContainer(): void
static::assertInstanceOf(History::class, $container->history);
static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService);
static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
static::assertInstanceOf(FormatterFactory::class, $container->formatterFactory);
}
}

0 comments on commit 485b168

Please sign in to comment.