This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/numeric_score/main.php

436 lines
17 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
2023-02-04 18:00:23 +00:00
use GQLA\Type;
use GQLA\Field;
use GQLA\Mutation;
#[Type(name: "NumericScoreVote")]
class NumericScoreVote
{
public int $image_id;
public int $user_id;
#[Field]
public int $score;
#[Field]
public function post(): Image
{
return Image::by_id_ex($this->image_id);
2023-02-04 18:00:23 +00:00
}
#[Field]
public function user(): User
{
return User::by_id($this->user_id);
}
#[Field(extends: "Post")]
public static function score(Image $post): int
{
global $database;
if ($post['score'] ?? null) {
return $post['score'];
2023-02-04 18:00:23 +00:00
}
return $database->get_one(
"SELECT sum(score) FROM numeric_score_votes WHERE image_id=:image_id",
2023-11-11 21:49:12 +00:00
['image_id' => $post->id]
2023-02-04 18:00:23 +00:00
) ?? 0;
}
/**
* @return NumericScoreVote[]
*/
2023-02-04 18:00:23 +00:00
#[Field(extends: "Post", type: "[NumericScoreVote!]!")]
public static function votes(Image $post): array
{
global $database;
$rows = $database->get_all(
"SELECT * FROM numeric_score_votes WHERE image_id=:image_id",
2023-11-11 21:49:12 +00:00
['image_id' => $post->id]
2023-02-04 18:00:23 +00:00
);
$votes = [];
foreach ($rows as $row) {
$nsv = new NumericScoreVote();
$nsv->image_id = $row["image_id"];
$nsv->user_id = $row["user_id"];
$nsv->score = $row["score"];
$votes[] = $nsv;
}
return $votes;
}
2023-02-13 22:44:08 +00:00
#[Field(extends: "Post", type: "Int!")]
public static function my_vote(Image $post): int
{
global $database, $user;
return $database->get_one(
"SELECT score FROM numeric_score_votes WHERE image_id=:image_id AND user_id=:user_id",
2023-11-11 21:49:12 +00:00
['image_id' => $post->id, "user_id" => $user->id]
2023-02-13 22:44:08 +00:00
) ?? 0;
}
2023-02-04 18:00:23 +00:00
#[Mutation]
public static function create_vote(int $post_id, int $score): bool
{
global $user;
2023-02-24 05:32:23 +00:00
if ($user->can(Permissions::CREATE_VOTE)) {
2023-02-04 18:00:23 +00:00
assert($score == 0 || $score == -1 || $score == 1);
send_event(new NumericScoreSetEvent($post_id, $user, $score));
return true;
}
return false;
}
}
class NumericScoreSetEvent extends Event
{
public int $image_id;
public User $user;
public int $score;
public function __construct(int $image_id, User $user, int $score)
{
2020-01-26 13:19:35 +00:00
parent::__construct();
$this->image_id = $image_id;
$this->user = $user;
$this->score = $score;
}
}
class NumericScore extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var NumericScoreTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function onInitExt(InitExtEvent $event): void
{
Image::$prop_types["numeric_score"] = ImagePropType::INT;
}
public function onDisplayingImage(DisplayingImageEvent $event): void
{
global $user;
2023-02-24 05:32:23 +00:00
if ($user->can(Permissions::CREATE_VOTE)) {
$this->theme->get_voter($event->image);
}
}
public function onUserPageBuilding(UserPageBuildingEvent $event): void
{
global $user;
2019-07-09 14:10:21 +00:00
if ($user->can(Permissions::EDIT_OTHER_VOTE)) {
$this->theme->get_nuller($event->display_user);
}
$n_up = Search::count_images(["upvoted_by={$event->display_user->name}"]);
$link_up = search_link(["upvoted_by={$event->display_user->name}"]);
$n_down = Search::count_images(["downvoted_by={$event->display_user->name}"]);
2023-12-24 13:46:59 +00:00
$link_down = search_link(["downvoted_by={$event->display_user->name}"]);
$event->add_part("<a href='$link_up'>$n_up Upvotes</a> / <a href='$link_down'>$n_down Downvotes</a>");
}
public function onPageRequest(PageRequestEvent $event): void
{
global $config, $database, $user, $page;
2024-02-11 11:34:09 +00:00
if ($event->page_matches("numeric_score_votes/{image_id}")) {
$image_id = $event->get_iarg('image_id');
$x = $database->get_all(
2020-01-26 13:19:35 +00:00
"SELECT users.name as username, user_id, score
FROM numeric_score_votes
JOIN users ON numeric_score_votes.user_id=users.id
2019-11-27 11:22:46 +00:00
WHERE image_id=:image_id",
2023-11-11 21:49:12 +00:00
['image_id' => $image_id]
);
$html = "<table style='width: 100%;'>";
foreach ($x as $vote) {
$html .= "<tr><td>";
$html .= "<a href='".make_link("user/{$vote['username']}")."'>{$vote['username']}</a>";
$html .= "</td><td width='10'>";
$html .= $vote['score'];
$html .= "</td></tr>";
}
die($html);
} elseif ($event->page_matches("numeric_score_vote", method: "POST", permission: Permissions::CREATE_VOTE)) {
$image_id = int_escape($event->req_POST("image_id"));
$score = int_escape($event->req_POST("vote"));
if (($score == -1 || $score == 0 || $score == 1) && $image_id > 0) {
send_event(new NumericScoreSetEvent($image_id, $user, $score));
}
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/view/$image_id"));
} elseif ($event->page_matches("numeric_score/remove_votes_on", method: "POST", permission: Permissions::EDIT_OTHER_VOTE)) {
$image_id = int_escape($event->req_POST("image_id"));
$database->execute(
"DELETE FROM numeric_score_votes WHERE image_id=:image_id",
['image_id' => $image_id]
);
$database->execute(
"UPDATE images SET numeric_score=0 WHERE id=:id",
['id' => $image_id]
);
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/view/$image_id"));
} elseif ($event->page_matches("numeric_score/remove_votes_by", method: "POST", permission: Permissions::EDIT_OTHER_VOTE)) {
$this->delete_votes_by(int_escape($event->req_POST('user_id')));
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link());
} elseif ($event->page_matches("popular_by_day") || $event->page_matches("popular_by_month") || $event->page_matches("popular_by_year")) {
//FIXME: popular_by isn't linked from anywhere
list($day, $month, $year) = [date("d"), date("m"), date("Y")];
2024-02-09 16:36:57 +00:00
if ($event->get_GET('day')) {
$D = (int) $event->get_GET('day');
$day = clamp($D, 1, 31);
}
2024-02-09 16:36:57 +00:00
if ($event->get_GET('month')) {
$M = (int) $event->get_GET('month');
$month = clamp($M, 1, 12);
}
2024-02-09 16:36:57 +00:00
if ($event->get_GET('year')) {
$Y = (int) $event->get_GET('year');
$year = clamp($Y, 1970, 2100);
}
$totaldate = $year."/".$month."/".$day;
2024-08-31 16:05:18 +00:00
if ($database->get_driver_id() === DatabaseDriverID::SQLITE) {
$sql = "SELECT id FROM images WHERE strftime('%Y', posted) = cast(:year as text)";
$month = str_pad(strval($month), 2, "0", STR_PAD_LEFT);
$day = str_pad(strval($day), 2, "0", STR_PAD_LEFT);
} else {
$sql = "SELECT id FROM images WHERE EXTRACT(YEAR FROM posted) = :year";
}
2019-08-16 14:18:14 +00:00
$args = ["limit" => $config->get_int(IndexConfig::IMAGES), "year" => $year];
if ($event->page_matches("popular_by_day")) {
2024-08-31 16:05:18 +00:00
if ($database->get_driver_id() === DatabaseDriverID::SQLITE) {
$sql .= " AND strftime('%m', posted) = cast(:month as text) AND strftime('%d', posted) = cast(:day as text)";
} else {
$sql .= " AND EXTRACT(MONTH FROM posted) = :month AND EXTRACT(DAY FROM posted) = :day";
}
$args = array_merge($args, ["month" => $month, "day" => $day]);
$current = date("F jS, Y", \Safe\strtotime($totaldate));
2024-01-20 20:48:47 +00:00
$name = "day";
$fmt = "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m\\&\\d\\a\\y\\=d";
} elseif ($event->page_matches("popular_by_month")) {
2024-08-31 16:05:18 +00:00
if ($database->get_driver_id() === DatabaseDriverID::SQLITE) {
$sql .= " AND strftime('%m', posted) = cast(:month as text)";
} else {
$sql .= " AND EXTRACT(MONTH FROM posted) = :month";
}
$args = array_merge($args, ["month" => $month]);
// PHP's -1 month and +1 month functionality break when modifying dates that are on the 31st of the month.
// See Example #3 on https://www.php.net/manual/en/datetime.modify.php
// To get around this, set the day to 1 when doing month work.
$totaldate = $year."/".$month."/01";
$current = date("F Y", \Safe\strtotime($totaldate));
2024-01-20 20:48:47 +00:00
$name = "month";
$fmt = "\\y\\e\\a\\r\\=Y\\&\\m\\o\\n\\t\\h\\=m";
} elseif ($event->page_matches("popular_by_year")) {
2024-01-20 20:48:47 +00:00
$current = "$year";
$name = "year";
$fmt = "\\y\\e\\a\\r\=Y";
} else {
// this should never happen due to the fact that the page event is already matched against earlier.
2023-01-11 11:15:26 +00:00
throw new \UnexpectedValueException("Error: Invalid page event.");
}
$sql .= " AND NOT numeric_score=0 ORDER BY numeric_score DESC LIMIT :limit OFFSET 0";
//filter images by score != 0 + date > limit to max images on one page > order from highest to lowest score
$ids = $database->get_col($sql, $args);
$images = Search::get_images($ids);
2024-01-20 20:48:47 +00:00
$this->theme->view_popular($images, $totaldate, $current, $name, $fmt);
}
}
public function onNumericScoreSet(NumericScoreSetEvent $event): void
{
global $user;
2020-10-26 15:16:58 +00:00
log_debug("numeric_score", "Rated >>{$event->image_id} as {$event->score}", "Rated Post");
$this->add_vote($event->image_id, $user->id, $event->score);
}
public function onImageDeletion(ImageDeletionEvent $event): void
{
global $database;
$database->execute("DELETE FROM numeric_score_votes WHERE image_id=:id", ["id" => $event->image->id]);
}
public function onUserDeletion(UserDeletionEvent $event): void
{
$this->delete_votes_by($event->id);
}
public function delete_votes_by(int $user_id): void
{
global $database;
2023-11-11 21:49:12 +00:00
$image_ids = $database->get_col("SELECT image_id FROM numeric_score_votes WHERE user_id=:user_id", ['user_id' => $user_id]);
if (count($image_ids) == 0) {
return;
}
// vote recounting is pretty heavy, and often hits statement timeouts
// if you try to recount all the images in one go
foreach (array_chunk($image_ids, 20) as $chunk) {
$id_list = implode(",", $chunk);
$database->execute(
2019-11-27 11:22:46 +00:00
"DELETE FROM numeric_score_votes WHERE user_id=:user_id AND image_id IN (".$id_list.")",
2023-11-11 21:49:12 +00:00
['user_id' => $user_id]
);
$database->execute("
2012-03-29 18:18:00 +00:00
UPDATE images
SET numeric_score=COALESCE(
(
SELECT SUM(score)
FROM numeric_score_votes
WHERE image_id=images.id
),
0
)
WHERE images.id IN (".$id_list.")");
}
}
public function onParseLinkTemplate(ParseLinkTemplateEvent $event): void
{
$event->replace('$score', (string)$event->image['numeric_score']);
}
public function onHelpPageBuilding(HelpPageBuildingEvent $event): void
{
2023-11-11 21:49:12 +00:00
if ($event->key === HelpPages::SEARCH) {
$block = new Block();
$block->header = "Numeric Score";
$block->body = $this->theme->get_help_html();
$event->add_block($block);
}
}
public function onSearchTermParse(SearchTermParseEvent $event): void
{
2020-01-26 16:38:26 +00:00
if (is_null($event->term)) {
return;
}
2020-01-26 13:19:35 +00:00
$matches = [];
if (preg_match("/^score([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(-?\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$score = $matches[2];
$event->add_querylet(new Querylet("numeric_score $cmp $score"));
} elseif (preg_match("/^upvoted_by[=|:](.*)$/i", $event->term, $matches)) {
$duser = User::by_name($matches[1]);
$event->add_querylet(new Querylet(
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)",
2023-11-11 21:49:12 +00:00
["ns_user_id" => $duser->id]
));
} elseif (preg_match("/^downvoted_by[=|:](.*)$/i", $event->term, $matches)) {
$duser = User::by_name($matches[1]);
$event->add_querylet(new Querylet(
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)",
2023-11-11 21:49:12 +00:00
["ns_user_id" => $duser->id]
));
} elseif (preg_match("/^upvoted_by_id[=|:](\d+)$/i", $event->term, $matches)) {
$iid = int_escape($matches[1]);
$event->add_querylet(new Querylet(
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=1)",
2023-11-11 21:49:12 +00:00
["ns_user_id" => $iid]
));
} elseif (preg_match("/^downvoted_by_id[=|:](\d+)$/i", $event->term, $matches)) {
$iid = int_escape($matches[1]);
$event->add_querylet(new Querylet(
"images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=:ns_user_id AND score=-1)",
2023-11-11 21:49:12 +00:00
["ns_user_id" => $iid]
));
} elseif (preg_match("/^order[=|:](?:numeric_)?(score)(?:_(desc|asc))?$/i", $event->term, $matches)) {
$default_order_for_column = "DESC";
$sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column;
$event->order = "images.numeric_score $sort";
}
}
public function onTagTermCheck(TagTermCheckEvent $event): void
2020-01-29 20:22:50 +00:00
{
if (preg_match("/^vote[=|:](up|down|remove)$/i", $event->term)) {
$event->metatag = true;
}
}
public function onTagTermParse(TagTermParseEvent $event): void
{
$matches = [];
2020-01-29 20:22:50 +00:00
if (preg_match("/^vote[=|:](up|down|remove)$/", $event->term, $matches)) {
global $user;
$score = ($matches[1] == "up" ? 1 : ($matches[1] == "down" ? -1 : 0));
2023-02-24 05:32:23 +00:00
if ($user->can(Permissions::CREATE_VOTE)) {
2020-01-29 20:22:50 +00:00
send_event(new NumericScoreSetEvent($event->image_id, $user, $score));
}
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
{
2023-11-11 21:49:12 +00:00
if ($event->parent == "posts") {
$event->add_nav_link("numeric_score_day", new Link('popular_by_day'), "Popular by Day");
$event->add_nav_link("numeric_score_month", new Link('popular_by_month'), "Popular by Month");
$event->add_nav_link("numeric_score_year", new Link('popular_by_year'), "Popular by Year");
}
}
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void
{
global $database;
2019-11-03 19:49:52 +00:00
if ($this->get_version("ext_numeric_score_version") < 1) {
$database->execute("ALTER TABLE images ADD COLUMN numeric_score INTEGER NOT NULL DEFAULT 0");
$database->execute("CREATE INDEX images__numeric_score ON images(numeric_score)");
$database->create_table("numeric_score_votes", "
image_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
2009-01-22 12:05:55 +00:00
score INTEGER NOT NULL,
UNIQUE(image_id, user_id),
FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
");
$database->execute("CREATE INDEX numeric_score_votes_image_id_idx ON numeric_score_votes(image_id)", []);
2019-11-03 19:49:52 +00:00
$this->set_version("ext_numeric_score_version", 1);
}
2019-11-03 19:49:52 +00:00
if ($this->get_version("ext_numeric_score_version") < 2) {
$database->execute("CREATE INDEX numeric_score_votes__user_votes ON numeric_score_votes(user_id, score)");
2019-11-03 19:49:52 +00:00
$this->set_version("ext_numeric_score_version", 2);
}
}
private function add_vote(int $image_id, int $user_id, int $score): void
{
global $database;
$database->execute(
"DELETE FROM numeric_score_votes WHERE image_id=:imageid AND user_id=:userid",
["imageid" => $image_id, "userid" => $user_id]
);
if ($score != 0) {
$database->execute(
"INSERT INTO numeric_score_votes(image_id, user_id, score) VALUES(:imageid, :userid, :score)",
["imageid" => $image_id, "userid" => $user_id, "score" => $score]
);
}
2020-10-25 21:34:52 +00:00
$database->execute(
"UPDATE images SET numeric_score=(
2012-03-30 16:48:03 +00:00
COALESCE(
(SELECT SUM(score) FROM numeric_score_votes WHERE image_id=:imageid),
0
)
) WHERE id=:id",
["imageid" => $image_id, "id" => $image_id]
);
}
}