diff --git a/public_html/dispatch.php b/public_html/dispatch.php index 57ab3284..482dea41 100644 --- a/public_html/dispatch.php +++ b/public_html/dispatch.php @@ -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); diff --git a/src/Controllers/LogController.php b/src/Controllers/LogController.php index 51872e3d..9466e57b 100644 --- a/src/Controllers/LogController.php +++ b/src/Controllers/LogController.php @@ -1,35 +1,24 @@ 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; } diff --git a/src/Jobs/Abstraction/JobArgs.php b/src/Jobs/Abstraction/JobArgs.php index 80d849ce..6a6963b1 100644 --- a/src/Jobs/Abstraction/JobArgs.php +++ b/src/Jobs/Abstraction/JobArgs.php @@ -6,4 +6,5 @@ class JobArgs const TEXT = 'text'; const PAGE_NUMBER = 'page-number'; const QUERY = 'query'; + const LOG_ID = 'log-id'; } diff --git a/src/Jobs/GetLogJob.php b/src/Jobs/GetLogJob.php new file mode 100644 index 00000000..912ddfb0 --- /dev/null +++ b/src/Jobs/GetLogJob.php @@ -0,0 +1,59 @@ +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; + } +} diff --git a/src/Jobs/ListLogsJob.php b/src/Jobs/ListLogsJob.php new file mode 100644 index 00000000..0d364e03 --- /dev/null +++ b/src/Jobs/ListLogsJob.php @@ -0,0 +1,34 @@ +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; + } +} diff --git a/src/Views/layout-normal.phtml b/src/Views/layout-normal.phtml index 5f76cdd8..42661525 100644 --- a/src/Views/layout-normal.phtml +++ b/src/Views/layout-normal.phtml @@ -43,7 +43,7 @@ Assets::addScript('core.js'); Queries:  szurubooru v - Logs + Logs
diff --git a/src/Views/log-list.phtml b/src/Views/log-list.phtml index d5750210..bbab28f6 100644 --- a/src/Views/log-list.phtml +++ b/src/Views/log-list.phtml @@ -8,7 +8,7 @@ $this->context->subTitle = 'latest logs';