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'], '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 =
|
||||
[
|
||||
'tag' => '[^\/]*',
|
||||
|
@ -86,11 +91,6 @@ $postValidation =
|
|||
|
||||
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', 'toggleTagAction'], $method, '/post/{id}/toggle-tag/{tag}/{enable}', $postValidation);
|
||||
\Chibi\Router::register(['PostController', 'viewAction'], $method, '/post/{id}', $postValidation);
|
||||
|
|
|
@ -1,35 +1,24 @@
|
|||
<?php
|
||||
class LogController
|
||||
{
|
||||
public function listAction()
|
||||
public function listView()
|
||||
{
|
||||
$context = getContext();
|
||||
Access::assert(Privilege::ListLogs);
|
||||
|
||||
$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;
|
||||
$ret = Api::run(new ListLogsJob(), []);
|
||||
getContext()->transport->logs = $ret;
|
||||
}
|
||||
|
||||
public function viewAction($name, $page = 1, $filter = '')
|
||||
public function logView($name, $page = 1, $filter = '')
|
||||
{
|
||||
$context = getContext();
|
||||
$context->viewName = 'log-view';
|
||||
|
||||
//redirect requests in form of ?query=... to canonical address
|
||||
$formQuery = InputHelper::get('query');
|
||||
if ($formQuery !== null)
|
||||
{
|
||||
\Chibi\Util\Url::forward(
|
||||
\Chibi\Router::linkTo(
|
||||
['LogController', 'viewAction'],
|
||||
['LogController', 'logView'],
|
||||
[
|
||||
'name' => $name,
|
||||
'filter' => $formQuery,
|
||||
|
@ -38,51 +27,30 @@ class LogController
|
|||
return;
|
||||
}
|
||||
|
||||
Access::assert(Privilege::ViewLog);
|
||||
|
||||
//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 = Api::run(
|
||||
new GetLogJob(),
|
||||
[
|
||||
JobArgs::PAGE_NUMBER => $page,
|
||||
JobArgs::LOG_ID => $name,
|
||||
JobArgs::QUERY => $filter,
|
||||
]);
|
||||
|
||||
//stylize important lines
|
||||
foreach ($lines as &$line)
|
||||
foreach ($ret->lines as &$line)
|
||||
if (strpos($line, 'flag') !== false)
|
||||
$line = '**' . $line . '**';
|
||||
unset($line);
|
||||
|
||||
$lines = join(PHP_EOL, $lines);
|
||||
$lines = TextHelper::parseMarkdown($lines, true);
|
||||
$lines = trim($lines);
|
||||
$ret->lines = join(PHP_EOL, $ret->lines);
|
||||
$ret->lines = TextHelper::parseMarkdown($ret->lines, true);
|
||||
$ret->lines = trim($ret->lines);
|
||||
|
||||
$context->transport->paginator = new StdClass;
|
||||
$context->transport->paginator->page = $page;
|
||||
$context->transport->paginator->pageCount = $pageCount;
|
||||
$context->transport->paginator->entityCount = $lineCount;
|
||||
$context->transport->paginator->entities = $lines;
|
||||
$context->transport->lines = $lines;
|
||||
$context->transport->paginator->page = $ret->page;
|
||||
$context->transport->paginator->pageCount = $ret->pageCount;
|
||||
$context->transport->paginator->entityCount = $ret->lineCount;
|
||||
$context->transport->paginator->entities = $ret->lines;
|
||||
$context->transport->lines = $ret->lines;
|
||||
$context->transport->filter = $filter;
|
||||
$context->transport->name = $name;
|
||||
}
|
||||
|
|
|
@ -6,4 +6,5 @@ class JobArgs
|
|||
const TEXT = 'text';
|
||||
const PAGE_NUMBER = 'page-number';
|
||||
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><a href="<?= SZURU_LINK ?>">szurubooru v<?= SZURU_VERSION ?></a></span>
|
||||
<?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 ?>
|
||||
<hr>
|
||||
</div>
|
||||
|
|
|
@ -8,7 +8,7 @@ $this->context->subTitle = 'latest logs';
|
|||
<ul>
|
||||
<?php foreach ($this->context->transport->logs as $log): ?>
|
||||
<li>
|
||||
<a href="<?= \Chibi\Router::linkTo(['LogController', 'viewAction'], ['name' => $log]) ?>">
|
||||
<a href="<?= \Chibi\Router::linkTo(['LogController', 'logView'], ['name' => $log]) ?>">
|
||||
<?= $log ?>
|
||||
</a>
|
||||
</li>
|
||||
|
|
|
@ -4,7 +4,7 @@ Assets::setSubTitle('logs (' . $this->context->transport->name . ')');
|
|||
|
||||
<?php if (empty($this->context->transport->lines)): ?>
|
||||
<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>
|
||||
<?php else: ?>
|
||||
<?php
|
||||
|
@ -14,7 +14,7 @@ Assets::setSubTitle('logs (' . $this->context->transport->name . ')');
|
|||
|
||||
<form method="get"
|
||||
action="<?= \Chibi\Router::linkTo(
|
||||
['LogController', 'viewAction'],
|
||||
['LogController', 'logView'],
|
||||
['name' => $this->context->transport->name]) ?>">
|
||||
|
||||
Keep only lines that contain:
|
||||
|
|
Loading…
Reference in a new issue