--- /dev/null
+<?php
+
+namespace wcf\http\middleware;
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use wcf\http\LegacyPlaceholderResponse;
+use wcf\system\background\BackgroundQueueHandler;
+
+/**
+ * Adds 'woltlab-background-queue-check: yes' to the response
+ * whenever a check was requested.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2022 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\Http\Middleware
+ * @since 6.0
+ */
+final class TriggerBackgroundQueue implements MiddlewareInterface
+{
+ private const HEADER_NAME = 'woltlab-background-queue-check';
+ private const HEADER_VALUE = 'yes';
+
+ private readonly BackgroundQueueHandler $backgroundQueueHandler;
+
+ public function __construct()
+ {
+ $this->backgroundQueueHandler = BackgroundQueueHandler::getInstance();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+ {
+ $response = $handler->handle($request);
+ if (!$this->backgroundQueueHandler->hasPendingCheck()) {
+ return $response;
+ }
+
+ if ($response instanceof LegacyPlaceholderResponse) {
+ \header(
+ \sprintf('%s: %s', self::HEADER_NAME, self::HEADER_VALUE)
+ );
+
+ return $response;
+ }
+
+ return $response->withHeader(self::HEADER_NAME, self::HEADER_VALUE);
+ }
+}
*/
final class BackgroundQueueHandler extends SingletonFactory
{
+ /**
+ * Indicates that the client should trigger a check for
+ * pending jobs in the background queue.
+ */
+ private bool $hasPendingCheck = false;
+
/**
* Forces checking whether a background queue item is due.
* This means that the AJAX request to BackgroundQueuePerformAction is triggered.
WCF::getTPL()->assign([
'forceBackgroundQueuePerform' => true,
]);
+
+ $this->hasPendingCheck = true;
}
/**
return $statement->fetchSingleColumn();
}
+
+ /**
+ * @since 6.0
+ */
+ public function hasPendingCheck(): bool
+ {
+ return $this->hasPendingCheck;
+ }
}
use wcf\http\middleware\HandleStartupErrors;
use wcf\http\middleware\JsonBody;
use wcf\http\middleware\PreventMimeSniffing;
+use wcf\http\middleware\TriggerBackgroundQueue;
use wcf\http\middleware\Xsrf;
use wcf\http\Pipeline;
use wcf\system\application\ApplicationHandler;
new CheckForExpiredAppEvaluation(),
new CheckForOfflineMode(),
new JsonBody(),
+ new TriggerBackgroundQueue(),
]);
$response = $pipeline->process($psrRequest, $this->getActiveRequest());