formatting

This commit is contained in:
Shish 2019-11-02 19:57:34 +00:00
parent aabc69033b
commit 55c6854003
29 changed files with 128 additions and 106 deletions

View File

@ -186,7 +186,7 @@ class Database
public function execute(string $query, array $args=[], bool $scoreql = false): PDOStatement
{
try {
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
@ -222,7 +222,7 @@ class Database
*/
public function get_all(string $query, array $args=[], bool $scoreql = false): array
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
@ -237,7 +237,7 @@ class Database
*/
public function get_all_iterable(string $query, array $args=[], bool $scoreql = false): PDOStatement
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
$_start = microtime(true);
@ -251,7 +251,7 @@ class Database
*/
public function get_row(string $query, array $args=[], bool $scoreql = false): ?array
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
$_start = microtime(true);
@ -266,7 +266,7 @@ class Database
*/
public function exists(string $query, array $args=[], bool $scoreql = false): bool
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
$_start = microtime(true);
@ -280,7 +280,7 @@ class Database
*/
public function get_col(string $query, array $args=[], bool $scoreql = false): array
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
$_start = microtime(true);
@ -294,7 +294,7 @@ class Database
*/
public function get_col_iterable(string $query, array $args=[], bool $scoreql = false): Generator
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
$_start = microtime(true);
@ -310,7 +310,7 @@ class Database
*/
public function get_pairs(string $query, array $args=[], bool $scoreql = false): array
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
$_start = microtime(true);
@ -324,7 +324,7 @@ class Database
*/
public function get_one(string $query, array $args=[], bool $scoreql = false)
{
if($scoreql===true) {
if ($scoreql===true) {
$query = $this->scoreql_to_sql($query);
}
$_start = microtime(true);

View File

@ -34,7 +34,7 @@ abstract class DBEngine
return 'CREATE TABLE '.$name.' ('.$data.')';
}
public abstract function set_timeout(PDO $db, int $time);
abstract public function set_timeout(PDO $db, int $time);
}
class MySQL extends DBEngine
@ -76,7 +76,6 @@ class MySQL extends DBEngine
// These only apply to read-only queries, which appears to be the best we can to mysql-wise
$db->exec("SET SESSION MAX_EXECUTION_TIME=".$time.";");
}
}
class PostgreSQL extends DBEngine
@ -123,7 +122,6 @@ class PostgreSQL extends DBEngine
{
$db->exec("SET statement_timeout TO ".$time.";");
}
}
// shimmie functions for export to sqlite

View File

@ -151,7 +151,7 @@ class Image
if (!$result) {
$querylet = Image::build_search_querylet($tag_conditions, $img_conditions);
$querylet->append(new Querylet(" ORDER BY ".(Image::$order_sql ?: "images.".$config->get_string(IndexConfig::ORDER))));
if($limit!=null) {
if ($limit!=null) {
$querylet->append(new Querylet(" LIMIT :limit ", ["limit" => $limit]));
$querylet->append(new Querylet(" OFFSET :offset ", ["offset"=>$start]));
}
@ -730,9 +730,11 @@ class Image
"INSERT INTO tags(tag) VALUES (:tag)",
["tag"=>$tag]
);
$database->execute($database->scoreql_to_sql(
$database->execute(
$database->scoreql_to_sql(
"INSERT INTO image_tags(image_id, tag_id)
VALUES(:id, (SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)))"),
VALUES(:id, (SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)))"
),
["id"=>$this->id, "tag"=>$tag]
);
} else {

View File

@ -101,18 +101,18 @@ class Tag
public static function compare(array $tags1, array $tags2): bool
{
if(count($tags1)!==count($tags2)) {
if (count($tags1)!==count($tags2)) {
return false;
}
$tags1 = array_map("strtolower",$tags1);
$tags2 = array_map("strtolower",$tags2);
$tags1 = array_map("strtolower", $tags1);
$tags2 = array_map("strtolower", $tags2);
natcasesort($tags1);
natcasesort($tags2);
for($i = 0; $i < count($tags1); $i++) {
if($tags1[$i]!==$tags2[$i]) {
for ($i = 0; $i < count($tags1); $i++) {
if ($tags1[$i]!==$tags2[$i]) {
var_dump($tags1);
var_dump($tags2);
return false;
@ -140,7 +140,7 @@ class Tag
foreach ($tags as $tag) {
try {
$tag = Tag::sanitize($tag);
} catch(Exception $e) {
} catch (Exception $e) {
flash_message($e->getMessage());
continue;
}

View File

@ -83,5 +83,4 @@ abstract class Permissions
public const CRON_ADMIN = "cron_admin";
public const APPROVE_IMAGE = "approve_image";
public const APPROVE_COMMENT = "approve_comment";
}

View File

@ -352,16 +352,18 @@ function join_url(string $base, string ...$paths)
function get_dir_contents(string $dir): array
{
if(empty($dir)) {
if (empty($dir)) {
throw new Exception("dir required");
}
if(!is_dir($dir)) {
if (!is_dir($dir)) {
return [];
}
$results = array_diff(
scandir(
$dir),
['..', '.']);
$dir
),
['..', '.']
);
return $results;
}
@ -378,7 +380,8 @@ function scan_dir(string $path): array
$path,
FilesystemIterator::KEY_AS_PATHNAME |
FilesystemIterator::CURRENT_AS_FILEINFO |
FilesystemIterator::SKIP_DOTS);
FilesystemIterator::SKIP_DOTS
);
foreach (new RecursiveIteratorIterator($ite) as $filename => $cur) {
try {
$filesize = $cur->getSize();

View File

@ -74,20 +74,23 @@ class Approval extends Extension
$action = $event->action;
$event->redirect = true;
if($action==="approval") {
if ($action==="approval") {
$approval_action = $_POST["approval_action"];
switch ($approval_action) {
case "approve_all":
$database->set_timeout(300000); // These updates can take a little bit
$database->execute($database->scoreql_to_sql(
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE approved = SCORE_BOOL_N"),
$database->execute(
$database->scoreql_to_sql(
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE approved = SCORE_BOOL_N"
),
["approved_by_id"=>$user->id]
);
break;
case "disapprove_all":
$database->set_timeout(300000); // These updates can take a little bit
$database->execute($database->scoreql_to_sql(
"UPDATE images SET approved = SCORE_BOOL_N, approved_by_id = NULL WHERE approved = SCORE_BOOL_Y"));
"UPDATE images SET approved = SCORE_BOOL_N, approved_by_id = NULL WHERE approved = SCORE_BOOL_Y"
));
break;
default:
@ -100,7 +103,7 @@ class Approval extends Extension
{
global $user, $page, $config;
if ( $config->get_bool(ApprovalConfig::IMAGES) && $event->image->approved===false && !$user->can(Permissions::APPROVE_IMAGE)) {
if ($config->get_bool(ApprovalConfig::IMAGES) && $event->image->approved===false && !$user->can(Permissions::APPROVE_IMAGE)) {
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/list"));
}
@ -109,9 +112,9 @@ class Approval extends Extension
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
if($event->parent=="posts") {
if($user->can(Permissions::APPROVE_IMAGE)) {
$event->add_nav_link("posts_unapproved", new Link('/post/list/approved%3Ano/1'), "Pending Approval",null, 60);
if ($event->parent=="posts") {
if ($user->can(Permissions::APPROVE_IMAGE)) {
$event->add_nav_link("posts_unapproved", new Link('/post/list/approved%3Ano/1'), "Pending Approval", null, 60);
}
}
}
@ -122,7 +125,7 @@ class Approval extends Extension
{
global $user, $database, $config;
if($config->get_bool(ApprovalConfig::IMAGES)) {
if ($config->get_bool(ApprovalConfig::IMAGES)) {
$matches = [];
if (is_null($event->term) && $this->no_approval_query($event->context)) {
@ -168,8 +171,10 @@ class Approval extends Extension
{
global $database, $user;
$database->execute($database->scoreql_to_sql(
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE id = :id AND approved = SCORE_BOOL_N"),
$database->execute(
$database->scoreql_to_sql(
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE id = :id AND approved = SCORE_BOOL_N"
),
["approved_by_id"=>$user->id, "id"=>$image_id]
);
}
@ -178,8 +183,10 @@ class Approval extends Extension
{
global $database, $user;
$database->execute($database->scoreql_to_sql(
"UPDATE images SET approved = SCORE_BOOL_N, approved_by_id = NULL WHERE id = :id AND approved = SCORE_BOOL_Y"),
$database->execute(
$database->scoreql_to_sql(
"UPDATE images SET approved = SCORE_BOOL_N, approved_by_id = NULL WHERE id = :id AND approved = SCORE_BOOL_Y"
),
["id"=>$image_id]
);
}
@ -187,7 +194,7 @@ class Approval extends Extension
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
{
global $user, $config;
if($user->can(Permissions::APPROVE_IMAGE) && $config->get_bool(ApprovalConfig::IMAGES)) {
if ($user->can(Permissions::APPROVE_IMAGE) && $config->get_bool(ApprovalConfig::IMAGES)) {
$event->add_part($this->theme->get_image_admin_html($event->image));
}
}
@ -197,7 +204,7 @@ class Approval extends Extension
global $user, $config;
if ($user->can(Permissions::APPROVE_IMAGE)&& $config->get_bool(ApprovalConfig::IMAGES)) {
if(in_array("approved:no", $event->search_terms)) {
if (in_array("approved:no", $event->search_terms)) {
$event->add_action("bulk_approve_image", "Approve", "a");
} else {
$event->add_action("bulk_disapprove_image", "Disapprove");

View File

@ -4,7 +4,7 @@ class ApprovalTheme extends Themelet
{
public function get_image_admin_html(Image $image)
{
if($image->approved===true) {
if ($image->approved===true) {
$html = "
".make_form(make_link('disapprove_image/'.$image->id), 'POST')."
<input type='hidden' name='image_id' value='$image->id'>

View File

@ -22,7 +22,6 @@ abstract class CronUploaderConfig
$config->set_string(self::KEY, $upload_key);
}
}
public static function get_user(): int

View File

@ -22,7 +22,7 @@ class CronUploader extends Extension
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
if($event->parent=="system") {
if ($event->parent=="system") {
$event->add_nav_link("cron_docs", new Link('cron_upload'), "Cron Upload");
}
}
@ -58,7 +58,7 @@ class CronUploader extends Extension
$sb->add_int_option(CronUploaderConfig::COUNT, "Upload per run", true);
$sb->add_text_option(CronUploaderConfig::DIR, "Root dir", true);
$sb->add_text_option(CronUploaderConfig::KEY, "Key", true);
$sb->add_choice_option(CronUploaderConfig::USER, $users,"User", true);
$sb->add_choice_option(CronUploaderConfig::USER, $users, "User", true);
$sb->end_table();
$sb->add_label("<a href='$documentation_link'>Read the documentation</a> for cron setup instructions.");
@ -130,7 +130,7 @@ class CronUploader extends Extension
$results = get_dir_contents($stage_dir);
if (count($results) == 0) {
if(rmdir($stage_dir)===false) {
if (rmdir($stage_dir)===false) {
flash_message("Nothing to stage from $folder, cannot remove folder");
} else {
flash_message("Nothing to stage from $folder, removing folder");
@ -203,24 +203,29 @@ class CronUploader extends Extension
}
$this->theme->display_documentation(
$running, $queue_dirinfo, $uploaded_dirinfo, $failed_dirinfo,
$this->get_cron_cmd(), $this->get_cron_url(), $logs
$running,
$queue_dirinfo,
$uploaded_dirinfo,
$failed_dirinfo,
$this->get_cron_cmd(),
$this->get_cron_url(),
$logs
);
}
function get_queue_dir()
public function get_queue_dir()
{
$dir = CronUploaderConfig::get_dir();
return join_path($dir, self::QUEUE_DIR);
}
function get_uploaded_dir()
public function get_uploaded_dir()
{
$dir = CronUploaderConfig::get_dir();
return join_path($dir, self::UPLOADED_DIR);
}
function get_failed_dir()
public function get_failed_dir()
{
$dir = CronUploaderConfig::get_dir();
return join_path($dir, self::FAILED_DIR);
@ -262,7 +267,7 @@ class CronUploader extends Extension
throw new SCoreException("Cron upload key incorrect");
}
$user_id = CronUploaderConfig::get_user();
if(empty($user_id)) {
if (empty($user_id)) {
throw new SCoreException("Cron upload user not set");
}
$user = User::by_id($user_id);
@ -329,8 +334,6 @@ class CronUploader extends Extension
$this->move_uploaded($img[0], $img[1], $output_subdir, true);
$this->log_message(SCORE_LOG_ERROR, "(" . gettype($e) . ") " . $e->getMessage());
$this->log_message(SCORE_LOG_ERROR, $e->getTraceAsString());
}
}
@ -348,14 +351,13 @@ class CronUploader extends Extension
flock($lockfile, LOCK_UN);
fclose($lockfile);
}
}
private function move_uploaded(string $path, string $filename, string $output_subdir, bool $corrupt = false)
{
$relativeDir = dirname(substr($path, strlen(CronUploaderConfig::get_dir()) + 7));
if($relativeDir==".") {
if ($relativeDir==".") {
$relativeDir = "";
}
@ -424,10 +426,11 @@ class CronUploader extends Extension
private const PARTIAL_DOWNLOAD_EXTENSIONS = ['crdownload','part'];
private function is_skippable_file(string $path) {
private function is_skippable_file(string $path)
{
$info = pathinfo($path);
if(in_array(strtolower($info['extension']),self::PARTIAL_DOWNLOAD_EXTENSIONS)) {
if (in_array(strtolower($info['extension']), self::PARTIAL_DOWNLOAD_EXTENSIONS)) {
return true;
}
@ -499,4 +502,3 @@ class CronUploader extends Extension
$page->set_data(implode("\r\n", $this->output_buffer));
}
}

View File

@ -2,9 +2,15 @@
class CronUploaderTheme extends Themelet
{
public function display_documentation(bool $running, array $queue_dirinfo, array $uploaded_dirinfo, array $failed_dirinfo,
string $cron_cmd, string $cron_url, ?array $log_entries)
{
public function display_documentation(
bool $running,
array $queue_dirinfo,
array $uploaded_dirinfo,
array $failed_dirinfo,
string $cron_cmd,
string $cron_url,
?array $log_entries
) {
global $page;
@ -81,9 +87,9 @@ class CronUploaderTheme extends Themelet
$page->add_block($block_install);
$page->add_block($block_usage);
if(!empty($log_entries)) {
if (!empty($log_entries)) {
$log_html = "<table class='log'>";
foreach($log_entries as $entry) {
foreach ($log_entries as $entry) {
$log_html .= "<tr><th>{$entry["date_sent"]}</th><td>{$entry["message"]}</td></tr>";
}
$log_html .= "</table>";
@ -111,13 +117,13 @@ class CronUploaderTheme extends Themelet
$html .= "<tr><td colspan='2'><input type='submit' value='Re-stage files to queue' /></td></tr>";
$html .= "</table></form>";
$html .= make_form(make_link("admin/cron_uploader_clear_queue"), "POST",false,"","return confirm('Are you sure you want to delete everything in the queue folder?');")
$html .= make_form(make_link("admin/cron_uploader_clear_queue"), "POST", false, "", "return confirm('Are you sure you want to delete everything in the queue folder?');")
."<table class='form'><tr><td>"
."<input type='submit' value='Clear queue folder'></td></tr></table></form>";
$html .= make_form(make_link("admin/cron_uploader_clear_uploaded"), "POST",false,"","return confirm('Are you sure you want to delete everything in the uploaded folder?');")
$html .= make_form(make_link("admin/cron_uploader_clear_uploaded"), "POST", false, "", "return confirm('Are you sure you want to delete everything in the uploaded folder?');")
."<table class='form'><tr><td>"
."<input type='submit' value='Clear uploaded folder'></td></tr></table></form>";
$html .= make_form(make_link("admin/cron_uploader_clear_failed"), "POST",false,"","return confirm('Are you sure you want to delete everything in the failed folder?');")
$html .= make_form(make_link("admin/cron_uploader_clear_failed"), "POST", false, "", "return confirm('Are you sure you want to delete everything in the failed folder?');")
."<table class='form'><tr><td>"
."<input type='submit' value='Clear failed folder'></td></tr></table></form>";
$html .= "</table>\n";

View File

@ -90,8 +90,10 @@ class DanbooruApi extends Extension
} elseif (isset($_GET['name'])) {
$namelist = explode(",", $_GET['name']);
foreach ($namelist as $name) {
$sqlresult = $database->get_all($database->scoreql_to_sql(
"SELECT id,tag,count FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(?)"),
$sqlresult = $database->get_all(
$database->scoreql_to_sql(
"SELECT id,tag,count FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(?)"
),
[$name]
);
foreach ($sqlresult as $row) {

View File

@ -66,6 +66,6 @@ class DowntimeTheme extends Themelet
</body>
</html>
EOD
);
);
}
}

View File

@ -242,7 +242,7 @@ class Favorites extends Extension
{
global $database;
if ($do_set) {
if(!$database->exists("select 1 from user_favorites where image_id=:image_id and user_id=:user_id",["image_id"=>$image_id, "user_id"=>$user_id])) {
if (!$database->exists("select 1 from user_favorites where image_id=:image_id and user_id=:user_id", ["image_id"=>$image_id, "user_id"=>$user_id])) {
$database->Execute(
"INSERT INTO user_favorites(image_id, user_id, created_at) VALUES(:image_id, :user_id, NOW())",
["image_id"=>$image_id, "user_id"=>$user_id]

View File

@ -37,7 +37,7 @@ class HelpPages extends Extension
private function get_pages(): array
{
if($this->pages==null) {
if ($this->pages==null) {
$e = new HelpPageListBuildingEvent();
send_event($e);
$this->pages = $e->pages;
@ -52,8 +52,6 @@ class HelpPages extends Extension
$pages = $this->get_pages();
if ($event->page_matches("help")) {
if ($event->count_args() == 0) {
$name = array_key_first($pages);
$page->set_mode(PageMode::REDIRECT);
@ -63,7 +61,7 @@ class HelpPages extends Extension
$page->set_mode(PageMode::PAGE);
$name = $event->get_arg(0);
$title = $name;
if(array_key_exists($name, $pages)) {
if (array_key_exists($name, $pages)) {
$title = $pages[$name];
} else {
return;
@ -97,10 +95,10 @@ class HelpPages extends Extension
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
if($event->parent=="help") {
if ($event->parent=="help") {
$pages = $this->get_pages();
foreach ($pages as $key=>$value) {
$event->add_nav_link("help_".$key, new Link('help/'.$key),$value);
$event->add_nav_link("help_".$key, new Link('help/'.$key), $value);
}
}
}

View File

@ -22,7 +22,7 @@ class HomeTheme extends Themelet
</body>
</html>
EOD
);
);
}
public function build_body(string $sitename, string $main_links, string $main_text, string $contact_link, $num_comma, string $counter_text)

View File

@ -6,5 +6,4 @@ abstract class MediaConfig
const CONVERT_PATH = "media_convert_path";
const VERSION = "ext_media_version";
const MEM_LIMIT = 'media_mem_limit';
}

View File

@ -14,14 +14,19 @@ class MediaResizeEvent extends Event
public $ignore_aspect_ratio;
public $allow_upscale;
public function __construct(String $engine, string $input_path, string $input_type, string $output_path,
int $target_width, int $target_height,
public function __construct(
String $engine,
string $input_path,
string $input_type,
string $output_path,
int $target_width,
int $target_height,
bool $ignore_aspect_ratio = false,
string $target_format = null,
int $target_quality = 80,
bool $minimize = false,
bool $allow_upscale = true)
{
bool $allow_upscale = true
) {
assert(in_array($engine, MediaEngine::ALL));
$this->engine = $engine;
$this->input_path = $input_path;
@ -54,5 +59,4 @@ class MediaCheckPropertiesEvent extends Event
$this->file_name = $file_name;
$this->ext = $ext;
}
}

View File

@ -265,7 +265,7 @@ class Media extends Extension
$matches = [];
if (preg_match(self::CONTENT_SEARCH_TERM_REGEX, $event->term, $matches)) {
$field = $matches[1];
if($field==="unknown") {
if ($field==="unknown") {
$event->add_querylet(new Querylet($database->scoreql_to_sql("video IS NULL OR audio IS NULL OR image IS NULL")));
} else {
$event->add_querylet(new Querylet($database->scoreql_to_sql("$field = SCORE_BOOL_Y")));
@ -1039,7 +1039,7 @@ class Media extends Extension
"ALTER TABLE images ADD COLUMN image SCORE_BOOL NULL"
));
switch($database->get_driver_name()) {
switch ($database->get_driver_name()) {
case DatabaseDriver::PGSQL:
case DatabaseDriver::SQLITE:
$database->execute('CREATE INDEX images_image_idx ON images(image) WHERE image IS NOT NULL');

View File

@ -55,7 +55,7 @@ class NotATag extends Extension
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
if($event->parent==="tags") {
if ($event->parent==="tags") {
if ($user->can(Permissions::BAN_IMAGE)) {
$event->add_nav_link("untags", new Link('untag/list/1'), "UnTags");
}

View File

@ -341,7 +341,7 @@ class Ratings extends Extension
$old = $_POST["rating_old"];
$new = $_POST["rating_new"];
if($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING)) {
$database->execute("UPDATE images SET rating = :new WHERE rating = :old", ["new"=>$new, "old"=>$old ]);
}

View File

@ -63,9 +63,9 @@ class Trash extends Extension
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
if($event->parent=="posts") {
if($user->can(Permissions::VIEW_TRASH)) {
$event->add_nav_link("posts_trash", new Link('/post/list/in%3Atrash/1'), "Trash",null, 60);
if ($event->parent=="posts") {
if ($user->can(Permissions::VIEW_TRASH)) {
$event->add_nav_link("posts_trash", new Link('/post/list/in%3Atrash/1'), "Trash", null, 60);
}
}
}

View File

@ -35,6 +35,9 @@ class DataUploadEvent extends Event
assert(is_array($metadata["tags"]));
assert(is_string($metadata["source"]) || is_null($metadata["source"]));
// DB limits to 64 char filenames
$metadata['filename'] = substr($metadata['filename'], 0, 63);
$this->metadata = $metadata;
$this->set_tmpname($tmpname);

View File

@ -25,7 +25,7 @@ class CustomHomeTheme extends HomeTheme
</body>
</html>
EOD
);
);
}
public function build_body(string $sitename, string $main_links, string $main_text, string $contact_link, $num_comma, string $counter_text)