name_to_id

This commit is contained in:
Shish 2019-11-11 16:31:14 +00:00
parent 247cfcbd77
commit 6486bb95da
6 changed files with 15 additions and 28 deletions

View file

@ -734,7 +734,7 @@ class Image
$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)))"
),
),
["id"=>$this->id, "tag"=>$tag]
);
} else {

View file

@ -103,6 +103,16 @@ class User
return is_null($row) ? null : new User($row);
}
public static function name_to_id(string $name): int
{
$u = User::by_name($name);
if (is_null($u)) {
throw SCoreException("Can't find any user named " . html_escape($name));
} else {
return $u->id;
}
}
public static function by_name_and_pass(string $name, string $pass): ?User
{
$my_user = User::by_name($name);

View file

@ -347,13 +347,7 @@ class CommentList extends Extension
$comments = $matches[2];
$event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM comments GROUP BY image_id HAVING count(image_id) $cmp $comments)"));
} elseif (preg_match("/^commented_by[=|:](.*)$/i", $event->term, $matches)) {
$my_user = User::by_name($matches[1]);
if (!is_null($my_user)) {
$user_id = $my_user->id;
} else {
$user_id = -1;
}
$user_id = User::name_to_id($matches[1]);
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM comments WHERE owner_id = $user_id)"));
} elseif (preg_match("/^commented_by_userno[=|:]([0-9]+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[1]);

View file

@ -120,13 +120,7 @@ class Favorites extends Extension
$favorites = $matches[2];
$event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE favorites $cmp $favorites)"));
} elseif (preg_match("/^favorited_by[=|:](.*)$/i", $event->term, $matches)) {
$my_user = User::by_name($matches[1]);
if (!is_null($my_user)) {
$user_id = $my_user->id;
} else {
$user_id = -1;
}
$user_id = User::name_to_id($matches[1]);
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM user_favorites WHERE user_id = $user_id)"));
} elseif (preg_match("/^favorited_by_userno[=|:](\d+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[1]);

View file

@ -194,13 +194,7 @@ class Notes extends Extension
$notes = $matches[2];
$event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)"));
} elseif (preg_match("/^notes_by[=|:](.*)$/i", $event->term, $matches)) {
$my_user = User::by_name($matches[1]);
if (!is_null($my_user)) {
$user_id = $my_user->id;
} else {
$user_id = -1;
}
$user_id = User::name_to_id($matches[1]);
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)"));
} elseif (preg_match("/^(notes_by_userno|notes_by_user_id)[=|:](\d+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[2]);

View file

@ -296,12 +296,7 @@ class UserPage extends Extension
$matches = [];
if (preg_match("/^(?:poster|user)[=|:](.*)$/i", $event->term, $matches)) {
$duser = User::by_name($matches[1]);
if (!is_null($duser)) {
$user_id = $duser->id;
} else {
$user_id = -1;
}
$user_id = User::name_to_id($matches[1]);
$event->add_querylet(new Querylet("images.owner_id = $user_id"));
} elseif (preg_match("/^(?:poster|user)_id[=|:]([0-9]+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[1]);