Renamed LogController methods and moved to API
This commit is contained in:
parent
aeb73e2a5c
commit
7c1b8ca4d5
8 changed files with 126 additions and 64 deletions
|
@ -55,6 +55,11 @@ $context->simpleActionName = null;
|
||||||
\Chibi\Router::register(['AuthController', 'logoutAction'], 'POST', '/auth/logout');
|
\Chibi\Router::register(['AuthController', 'logoutAction'], 'POST', '/auth/logout');
|
||||||
\Chibi\Router::register(['AuthController', 'logoutAction'], 'GET', '/auth/logout');
|
\Chibi\Router::register(['AuthController', 'logoutAction'], 'GET', '/auth/logout');
|
||||||
|
|
||||||
|
\Chibi\Router::register(['LogController', 'listView'], 'GET', '/logs');
|
||||||
|
\Chibi\Router::register(['LogController', 'logView'], 'GET', '/log/{name}', ['name' => '[0-9a-zA-Z._-]+']);
|
||||||
|
\Chibi\Router::register(['LogController', 'logView'], 'GET', '/log/{name}/{page}', ['name' => '[0-9a-zA-Z._-]+', 'page' => '\d*']);
|
||||||
|
\Chibi\Router::register(['LogController', 'logView'], 'GET', '/log/{name}/{page}/{filter}', ['name' => '[0-9a-zA-Z._-]+', 'page' => '\d*', 'filter' => '.*']);
|
||||||
|
|
||||||
$postValidation =
|
$postValidation =
|
||||||
[
|
[
|
||||||
'tag' => '[^\/]*',
|
'tag' => '[^\/]*',
|
||||||
|
@ -86,11 +91,6 @@ $postValidation =
|
||||||
|
|
||||||
foreach (['GET', 'POST'] as $method)
|
foreach (['GET', 'POST'] as $method)
|
||||||
{
|
{
|
||||||
\Chibi\Router::register(['LogController', 'listAction'], $method, '/logs');
|
|
||||||
\Chibi\Router::register(['LogController', 'viewAction'], $method, '/log/{name}', ['name' => '[0-9a-zA-Z._-]+']);
|
|
||||||
\Chibi\Router::register(['LogController', 'viewAction'], $method, '/log/{name}/{page}', ['name' => '[0-9a-zA-Z._-]+', 'page' => '\d*']);
|
|
||||||
\Chibi\Router::register(['LogController', 'viewAction'], $method, '/log/{name}/{page}/{filter}', ['name' => '[0-9a-zA-Z._-]+', 'page' => '\d*', 'filter' => '.*']);
|
|
||||||
|
|
||||||
\Chibi\Router::register(['PostController', 'uploadAction'], $method, '/posts/upload', $postValidation);
|
\Chibi\Router::register(['PostController', 'uploadAction'], $method, '/posts/upload', $postValidation);
|
||||||
\Chibi\Router::register(['PostController', 'toggleTagAction'], $method, '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
|
\Chibi\Router::register(['PostController', 'toggleTagAction'], $method, '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
|
||||||
\Chibi\Router::register(['PostController', 'viewAction'], $method, '/post/{id}', $postValidation);
|
\Chibi\Router::register(['PostController', 'viewAction'], $method, '/post/{id}', $postValidation);
|
||||||
|
|
|
@ -1,35 +1,24 @@
|
||||||
<?php
|
<?php
|
||||||
class LogController
|
class LogController
|
||||||
{
|
{
|
||||||
public function listAction()
|
public function listView()
|
||||||
{
|
{
|
||||||
$context = getContext();
|
$ret = Api::run(new ListLogsJob(), []);
|
||||||
Access::assert(Privilege::ListLogs);
|
getContext()->transport->logs = $ret;
|
||||||
|
|
||||||
$path = TextHelper::absolutePath(getConfig()->main->logsPath);
|
|
||||||
|
|
||||||
$logs = [];
|
|
||||||
foreach (glob($path . DS . '*.log') as $log)
|
|
||||||
$logs []= basename($log);
|
|
||||||
|
|
||||||
usort($logs, function($a, $b)
|
|
||||||
{
|
|
||||||
return strnatcasecmp($b, $a); //reverse natcasesort
|
|
||||||
});
|
|
||||||
|
|
||||||
$context->transport->logs = $logs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function viewAction($name, $page = 1, $filter = '')
|
public function logView($name, $page = 1, $filter = '')
|
||||||
{
|
{
|
||||||
$context = getContext();
|
$context = getContext();
|
||||||
|
$context->viewName = 'log-view';
|
||||||
|
|
||||||
//redirect requests in form of ?query=... to canonical address
|
//redirect requests in form of ?query=... to canonical address
|
||||||
$formQuery = InputHelper::get('query');
|
$formQuery = InputHelper::get('query');
|
||||||
if ($formQuery !== null)
|
if ($formQuery !== null)
|
||||||
{
|
{
|
||||||
\Chibi\Util\Url::forward(
|
\Chibi\Util\Url::forward(
|
||||||
\Chibi\Router::linkTo(
|
\Chibi\Router::linkTo(
|
||||||
['LogController', 'viewAction'],
|
['LogController', 'logView'],
|
||||||
[
|
[
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'filter' => $formQuery,
|
'filter' => $formQuery,
|
||||||
|
@ -38,51 +27,30 @@ class LogController
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Access::assert(Privilege::ViewLog);
|
$ret = Api::run(
|
||||||
|
new GetLogJob(),
|
||||||
//parse input
|
[
|
||||||
$page = max(1, intval($page));
|
JobArgs::PAGE_NUMBER => $page,
|
||||||
$name = str_replace(['/', '\\'], '', $name); //paranoia mode
|
JobArgs::LOG_ID => $name,
|
||||||
$path = TextHelper::absolutePath(getConfig()->main->logsPath . DS . $name);
|
JobArgs::QUERY => $filter,
|
||||||
if (!file_exists($path))
|
]);
|
||||||
throw new SimpleNotFoundException('Specified log doesn\'t exist');
|
|
||||||
|
|
||||||
//load lines
|
|
||||||
$lines = file_get_contents($path);
|
|
||||||
$lines = explode(PHP_EOL, str_replace(["\r", "\n"], PHP_EOL, $lines));
|
|
||||||
$lines = array_reverse($lines);
|
|
||||||
|
|
||||||
if (!empty($filter))
|
|
||||||
{
|
|
||||||
$lines = array_filter($lines, function($line) use ($filter)
|
|
||||||
{
|
|
||||||
return stripos($line, $filter) !== false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$lineCount = count($lines);
|
|
||||||
$logsPerPage = intval(getConfig()->browsing->logsPerPage);
|
|
||||||
$pageCount = ceil($lineCount / $logsPerPage);
|
|
||||||
$page = min($pageCount, $page);
|
|
||||||
|
|
||||||
$lines = array_slice($lines, ($page - 1) * $logsPerPage, $logsPerPage);
|
|
||||||
|
|
||||||
//stylize important lines
|
//stylize important lines
|
||||||
foreach ($lines as &$line)
|
foreach ($ret->lines as &$line)
|
||||||
if (strpos($line, 'flag') !== false)
|
if (strpos($line, 'flag') !== false)
|
||||||
$line = '**' . $line . '**';
|
$line = '**' . $line . '**';
|
||||||
unset($line);
|
unset($line);
|
||||||
|
|
||||||
$lines = join(PHP_EOL, $lines);
|
$ret->lines = join(PHP_EOL, $ret->lines);
|
||||||
$lines = TextHelper::parseMarkdown($lines, true);
|
$ret->lines = TextHelper::parseMarkdown($ret->lines, true);
|
||||||
$lines = trim($lines);
|
$ret->lines = trim($ret->lines);
|
||||||
|
|
||||||
$context->transport->paginator = new StdClass;
|
$context->transport->paginator = new StdClass;
|
||||||
$context->transport->paginator->page = $page;
|
$context->transport->paginator->page = $ret->page;
|
||||||
$context->transport->paginator->pageCount = $pageCount;
|
$context->transport->paginator->pageCount = $ret->pageCount;
|
||||||
$context->transport->paginator->entityCount = $lineCount;
|
$context->transport->paginator->entityCount = $ret->lineCount;
|
||||||
$context->transport->paginator->entities = $lines;
|
$context->transport->paginator->entities = $ret->lines;
|
||||||
$context->transport->lines = $lines;
|
$context->transport->lines = $ret->lines;
|
||||||
$context->transport->filter = $filter;
|
$context->transport->filter = $filter;
|
||||||
$context->transport->name = $name;
|
$context->transport->name = $name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,5 @@ class JobArgs
|
||||||
const TEXT = 'text';
|
const TEXT = 'text';
|
||||||
const PAGE_NUMBER = 'page-number';
|
const PAGE_NUMBER = 'page-number';
|
||||||
const QUERY = 'query';
|
const QUERY = 'query';
|
||||||
|
const LOG_ID = 'log-id';
|
||||||
}
|
}
|
||||||
|
|
59
src/Jobs/GetLogJob.php
Normal file
59
src/Jobs/GetLogJob.php
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
<?php
|
||||||
|
class GetLogJob extends AbstractJob
|
||||||
|
{
|
||||||
|
public function execute()
|
||||||
|
{
|
||||||
|
$page = $this->getArgument(JobArgs::PAGE_NUMBER);
|
||||||
|
$name = $this->getArgument(JobArgs::LOG_ID);
|
||||||
|
$query = $this->getArgument(JobArgs::QUERY);
|
||||||
|
|
||||||
|
//parse input
|
||||||
|
$page = max(1, intval($page));
|
||||||
|
$name = str_replace(['/', '\\'], '', $name); //paranoia mode
|
||||||
|
$path = TextHelper::absolutePath(getConfig()->main->logsPath . DS . $name);
|
||||||
|
if (!file_exists($path))
|
||||||
|
throw new SimpleNotFoundException('Specified log doesn\'t exist');
|
||||||
|
|
||||||
|
//load lines
|
||||||
|
$lines = file_get_contents($path);
|
||||||
|
$lines = explode(PHP_EOL, str_replace(["\r", "\n"], PHP_EOL, $lines));
|
||||||
|
$lines = array_reverse($lines);
|
||||||
|
|
||||||
|
if (!empty($filter))
|
||||||
|
{
|
||||||
|
$lines = array_filter($lines, function($line) use ($filter)
|
||||||
|
{
|
||||||
|
return stripos($line, $filter) !== false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$lineCount = count($lines);
|
||||||
|
$logsPerPage = intval(getConfig()->browsing->logsPerPage);
|
||||||
|
$pageCount = ceil($lineCount / $logsPerPage);
|
||||||
|
$page = min($pageCount, $page);
|
||||||
|
|
||||||
|
$lines = array_slice($lines, ($page - 1) * $logsPerPage, $logsPerPage);
|
||||||
|
|
||||||
|
$ret = new StdClass;
|
||||||
|
$ret->lines = $lines;
|
||||||
|
$ret->lineCount = $lineCount;
|
||||||
|
$ret->page = $page;
|
||||||
|
$ret->pageCount = $pageCount;
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiresPrivilege()
|
||||||
|
{
|
||||||
|
return Privilege::ViewLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiresAuthentication()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiresConfirmedEmail()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
34
src/Jobs/ListLogsJob.php
Normal file
34
src/Jobs/ListLogsJob.php
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
class ListLogsJob extends AbstractJob
|
||||||
|
{
|
||||||
|
public function execute()
|
||||||
|
{
|
||||||
|
$path = TextHelper::absolutePath(getConfig()->main->logsPath);
|
||||||
|
|
||||||
|
$logs = [];
|
||||||
|
foreach (glob($path . DS . '*.log') as $log)
|
||||||
|
$logs []= basename($log);
|
||||||
|
|
||||||
|
usort($logs, function($a, $b)
|
||||||
|
{
|
||||||
|
return strnatcasecmp($b, $a); //reverse natcasesort
|
||||||
|
});
|
||||||
|
|
||||||
|
return $logs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiresPrivilege()
|
||||||
|
{
|
||||||
|
return Privilege::ListLogs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiresAuthentication()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function requiresConfirmedEmail()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,7 +43,7 @@ Assets::addScript('core.js');
|
||||||
<span>Queries: <?= count(\Chibi\Database::getLogs()) ?></span>
|
<span>Queries: <?= count(\Chibi\Database::getLogs()) ?></span>
|
||||||
<span><a href="<?= SZURU_LINK ?>">szurubooru v<?= SZURU_VERSION ?></a></span>
|
<span><a href="<?= SZURU_LINK ?>">szurubooru v<?= SZURU_VERSION ?></a></span>
|
||||||
<?php if (Access::check(Privilege::ListLogs)): ?>
|
<?php if (Access::check(Privilege::ListLogs)): ?>
|
||||||
<span><a href="<?= \Chibi\Router::linkTo(['LogController', 'listAction']) ?>">Logs</a></span>
|
<span><a href="<?= \Chibi\Router::linkTo(['LogController', 'listView']) ?>">Logs</a></span>
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
<hr>
|
<hr>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -8,7 +8,7 @@ $this->context->subTitle = 'latest logs';
|
||||||
<ul>
|
<ul>
|
||||||
<?php foreach ($this->context->transport->logs as $log): ?>
|
<?php foreach ($this->context->transport->logs as $log): ?>
|
||||||
<li>
|
<li>
|
||||||
<a href="<?= \Chibi\Router::linkTo(['LogController', 'viewAction'], ['name' => $log]) ?>">
|
<a href="<?= \Chibi\Router::linkTo(['LogController', 'logView'], ['name' => $log]) ?>">
|
||||||
<?= $log ?>
|
<?= $log ?>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -4,7 +4,7 @@ Assets::setSubTitle('logs (' . $this->context->transport->name . ')');
|
||||||
|
|
||||||
<?php if (empty($this->context->transport->lines)): ?>
|
<?php if (empty($this->context->transport->lines)): ?>
|
||||||
<p class="alert alert-warning">
|
<p class="alert alert-warning">
|
||||||
This log is empty. <a href="<?= \Chibi\Router::linkTo(['LogController', 'listAction']) ?>">Go back</a>
|
This log is empty. <a href="<?= \Chibi\Router::linkTo(['LogController', 'listView']) ?>">Go back</a>
|
||||||
</p>
|
</p>
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<?php
|
<?php
|
||||||
|
@ -14,7 +14,7 @@ Assets::setSubTitle('logs (' . $this->context->transport->name . ')');
|
||||||
|
|
||||||
<form method="get"
|
<form method="get"
|
||||||
action="<?= \Chibi\Router::linkTo(
|
action="<?= \Chibi\Router::linkTo(
|
||||||
['LogController', 'viewAction'],
|
['LogController', 'logView'],
|
||||||
['name' => $this->context->transport->name]) ?>">
|
['name' => $this->context->transport->name]) ?>">
|
||||||
|
|
||||||
Keep only lines that contain:
|
Keep only lines that contain:
|
||||||
|
|
Loading…
Reference in a new issue