diff --git a/scripts/find-posts.php b/scripts/find-posts.php index c19b8fdc..30d7c534 100644 --- a/scripts/find-posts.php +++ b/scripts/find-posts.php @@ -4,8 +4,8 @@ require_once __DIR__ . '/../src/core.php'; Access::disablePrivilegeChecking(); array_shift($argv); +$query = join(' ', $argv); -$query = array_shift($argv); $posts = PostSearchService::getEntities($query, null, null); foreach ($posts as $post) { diff --git a/scripts/generate-thumbs.php b/scripts/generate-thumbs.php index eda8e318..e1f54f21 100644 --- a/scripts/generate-thumbs.php +++ b/scripts/generate-thumbs.php @@ -3,13 +3,19 @@ require_once __DIR__ . '/../src/core.php'; Access::disablePrivilegeChecking(); -$options = getopt('f', ['force']); -$force = (isset($options['f']) or isset($options['force'])); +$query = []; +$force = false; -$args = array_search('--', $argv); -$args = array_splice($argv, $args ? ++$args : (count($argv) - count($options))); +array_shift($argv); +foreach ($argv as $arg) +{ + if ($arg == '-f' or $arg == '--force') + $force = true; + else + $query []= $arg; +} +$query = join(' ', $query); -$query = array_shift($args); $posts = PostSearchService::getEntities($query, null, null); $entityCount = PostSearchService::getEntityCount($query, null, null); $i = 0; diff --git a/scripts/process-detached-files.php b/scripts/process-detached-files.php index 8a9551a9..04405f20 100644 --- a/scripts/process-detached-files.php +++ b/scripts/process-detached-files.php @@ -3,30 +3,34 @@ require_once __DIR__ . '/../src/core.php'; Access::disablePrivilegeChecking(); -function usage() +$usage = function() { - echo 'Usage: ' . basename(__FILE__); - echo ' -print|-purge|-move DIR' . PHP_EOL; + echo 'Usage: ' . basename(__FILE__) . PHP_EOL; + echo ' -p|--print OR' . PHP_EOL; + echo ' -d|--delete OR' . PHP_EOL; + echo ' -m|--move [TARGET]' . PHP_EOL; return true; -} +}; array_shift($argv); if (empty($argv)) - usage() and die; + $usage() and die; $action = array_shift($argv); switch ($action) { - case '-print': + case '-p': + case '--print': $func = function($name) { echo $name . PHP_EOL; }; break; - case '-move': + case '-m': + case '--move': if (empty($argv)) - usage() and die; + $usage() and die; $dir = array_shift($argv); if (!file_exists($dir)) mkdir($dir, 0755, true); @@ -41,7 +45,8 @@ switch ($action) }; break; - case '-purge': + case '-d': + case '--delete': $func = function($name) { echo $name . PHP_EOL; diff --git a/scripts/process-old-users.php b/scripts/process-old-users.php deleted file mode 100644 index 95bbed06..00000000 --- a/scripts/process-old-users.php +++ /dev/null @@ -1,54 +0,0 @@ -getId() . PHP_EOL; - echo 'Name: ' . $user->getName() . PHP_EOL; - echo 'E-mail: ' . $user->getUnconfirmedEmail() . PHP_EOL; - echo 'Date joined: ' . date('Y-m-d H:i:s', $user->getJoinTime()) . PHP_EOL; - echo PHP_EOL; -} - -$action = array_shift($argv); -switch ($action) -{ - case '-print': - $func = 'printUser'; - break; - - case '-purge': - $func = function($user) - { - printUser($user); - UserModel::remove($user); - }; - break; - - default: - die('Unknown action' . PHP_EOL); -} - -$users = UserSearchService::getEntities(null, null, null); -foreach ($users as $user) -{ - if (!$user->getConfirmedEmail() - and !$user->getLastLoginTime() - and ((time() - $user->getJoinTime()) > 21 * 24 * 60 * 60)) - { - $func($user); - } -} diff --git a/src/Access.php b/src/Access.php index 290a3230..67ec50f8 100644 --- a/src/Access.php +++ b/src/Access.php @@ -1,12 +1,14 @@ privileges as $key => $minAccessRankName) { if (strpos($key, '.') === false) diff --git a/tests/Tests/MiscTests/ScriptTest.php b/tests/Tests/MiscTests/ScriptTest.php new file mode 100644 index 00000000..797477b8 --- /dev/null +++ b/tests/Tests/MiscTests/ScriptTest.php @@ -0,0 +1,99 @@ +scriptsPath = Core::getConfig()->rootDir . DS . 'scripts' . DS; + } + + public function testFindPosts() + { + $posts = $this->postMocker->mockMultiple(3); + $output = $this->execute($this->scriptsPath . 'find-posts.php', []); + $this->assert->isTrue(strpos($output, $posts[0]->getName()) !== false); + $this->assert->isTrue(strpos($output, $posts[1]->getName()) !== false); + $this->assert->isTrue(strpos($output, $posts[2]->getName()) !== false); + } + + public function testFindPostsFilter() + { + $posts = $this->postMocker->mockMultiple(3); + $output = $this->execute($this->scriptsPath . 'find-posts.php', ['idmin:' . $posts[1]->getId()]); + $this->assert->isTrue(strpos($output, $posts[0]->getName()) === false); + $this->assert->isTrue(strpos($output, $posts[1]->getName()) !== false); + $this->assert->isTrue(strpos($output, $posts[2]->getName()) !== false); + } + + public function testGenerateThumbs() + { + $posts = $this->postMocker->mockMultiple(3); + $this->assert->isFalse(file_exists($posts[0]->getThumbnailPath())); + $this->assert->isFalse(file_exists($posts[1]->getThumbnailPath())); + $this->assert->isFalse(file_exists($posts[2]->getThumbnailPath())); + $output = $this->execute($this->scriptsPath . 'generate-thumbs.php', []); + $this->assert->isTrue(strpos($output, TextHelper::reprPost($posts[0])) !== false); + $this->assert->isTrue(strpos($output, TextHelper::reprPost($posts[1])) !== false); + $this->assert->isTrue(strpos($output, TextHelper::reprPost($posts[2])) !== false); + $this->assert->isTrue(strpos($output, 'Don\'t forget to check access rights') !== false); + $this->assert->isTrue(file_exists($posts[0]->getThumbnailPath())); + $this->assert->isTrue(file_exists($posts[1]->getThumbnailPath())); + $this->assert->isTrue(file_exists($posts[2]->getThumbnailPath())); + } + + public function testDetachedFilesPrint() + { + $post = $this->postMocker->mockSingle(); + touch(Core::getConfig()->main->filesPath . DS . 'rubbish1'); + touch(Core::getConfig()->main->filesPath . DS . 'rubbish2'); + $output = $this->execute($this->scriptsPath . 'process-detached-files.php', ['-p']); + $this->assert->isTrue(strpos($output, 'rubbish1') !== false); + $this->assert->isTrue(strpos($output, 'rubbish2') !== false); + $this->assert->isFalse(strpos($output, $post->getName())); + $this->assert->isTrue(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish1')); + $this->assert->isTrue(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish2')); + } + + public function testDetachedFilesRemove() + { + $post = $this->postMocker->mockSingle(); + touch(Core::getConfig()->main->filesPath . DS . 'rubbish1'); + touch(Core::getConfig()->main->filesPath . DS . 'rubbish2'); + $output = $this->execute($this->scriptsPath . 'process-detached-files.php', ['-d']); + $this->assert->isTrue(strpos($output, 'rubbish1') !== false); + $this->assert->isTrue(strpos($output, 'rubbish2') !== false); + $this->assert->isFalse(strpos($output, $post->getName())); + $this->assert->isFalse(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish1')); + $this->assert->isFalse(file_exists(Core::getConfig()->main->filesPath . DS . 'rubbish2')); + } + + public function testDetachedFilesMove() + { + $post = $this->postMocker->mockSingle(); + touch(Core::getConfig()->main->filesPath . DS . 'rubbish1'); + touch(Core::getConfig()->main->filesPath . DS . 'rubbish2'); + $target = sys_get_temp_dir(); + $output = $this->execute($this->scriptsPath . 'process-detached-files.php', ['-m', $target]); + $this->assert->isTrue(strpos($output, 'rubbish1') !== false); + $this->assert->isTrue(strpos($output, 'rubbish2') !== false); + $this->assert->isFalse(strpos($output, $post->getName())); + $this->assert->isTrue(file_exists($target . DS . 'rubbish1')); + $this->assert->isTrue(file_exists($target . DS . 'rubbish2')); + unlink($target . DS . 'rubbish1'); + unlink($target . DS . 'rubbish2'); + } + + private function execute($scriptPath, array $arguments) + { + $argv = array_merge([$scriptPath], $arguments); + + ob_start(); + include($scriptPath); + $output = ob_get_contents(); + ob_end_clean(); + + return $output; + } +}