splitting up huge functions in ext/comment
This commit is contained in:
parent
6a94ae4aaf
commit
5a8df90fd9
@ -76,7 +76,11 @@ class Comment {
|
|||||||
*/
|
*/
|
||||||
public static function count_comments_by_user($user) {
|
public static function count_comments_by_user($user) {
|
||||||
global $database;
|
global $database;
|
||||||
return $database->get_one("SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id", array("owner_id"=>$user->id));
|
return $database->get_one("
|
||||||
|
SELECT COUNT(*) AS count
|
||||||
|
FROM comments
|
||||||
|
WHERE owner_id=:owner_id
|
||||||
|
", array("owner_id"=>$user->id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,6 +93,9 @@ class Comment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class CommentList extends Extension {
|
class CommentList extends Extension {
|
||||||
|
/** @var CommentListTheme $theme */
|
||||||
|
var $theme;
|
||||||
|
|
||||||
public function onInitExt(InitExtEvent $event) {
|
public function onInitExt(InitExtEvent $event) {
|
||||||
global $config, $database;
|
global $config, $database;
|
||||||
$config->set_default_int('comment_window', 5);
|
$config->set_default_int('comment_window', 5);
|
||||||
@ -147,9 +154,27 @@ class CommentList extends Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onPageRequest(PageRequestEvent $event) {
|
public function onPageRequest(PageRequestEvent $event) {
|
||||||
global $page, $user, $database;
|
|
||||||
if($event->page_matches("comment")) {
|
if($event->page_matches("comment")) {
|
||||||
if($event->get_arg(0) === "add") {
|
if($event->get_arg(0) === "add") {
|
||||||
|
$this->onPageRequest_add();
|
||||||
|
}
|
||||||
|
else if($event->get_arg(0) === "delete") {
|
||||||
|
$this->onPageRequest_delete($event);
|
||||||
|
}
|
||||||
|
else if($event->get_arg(0) === "bulk_delete") {
|
||||||
|
$this->onPageRequest_bulk_delete();
|
||||||
|
}
|
||||||
|
else if($event->get_arg(0) === "list") {
|
||||||
|
$this->onPageRequest_list($event);
|
||||||
|
}
|
||||||
|
else if($event->get_arg(0) === "beta-search") {
|
||||||
|
$this->onPageRequest_beta_search($event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onPageRequest_add() {
|
||||||
|
global $user, $page;
|
||||||
if (isset($_POST['image_id']) && isset($_POST['comment'])) {
|
if (isset($_POST['image_id']) && isset($_POST['comment'])) {
|
||||||
try {
|
try {
|
||||||
$i_iid = int_escape($_POST['image_id']);
|
$i_iid = int_escape($_POST['image_id']);
|
||||||
@ -157,13 +182,14 @@ class CommentList extends Extension {
|
|||||||
send_event($cpe);
|
send_event($cpe);
|
||||||
$page->set_mode("redirect");
|
$page->set_mode("redirect");
|
||||||
$page->set_redirect(make_link("post/view/$i_iid#comment_on_$i_iid"));
|
$page->set_redirect(make_link("post/view/$i_iid#comment_on_$i_iid"));
|
||||||
}
|
} catch (CommentPostingException $ex) {
|
||||||
catch(CommentPostingException $ex) {
|
|
||||||
$this->theme->display_error(403, "Comment Blocked", $ex->getMessage());
|
$this->theme->display_error(403, "Comment Blocked", $ex->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if($event->get_arg(0) === "delete") {
|
|
||||||
|
private function onPageRequest_delete(PageRequestEvent $event) {
|
||||||
|
global $user, $page;
|
||||||
if ($user->can("delete_comment")) {
|
if ($user->can("delete_comment")) {
|
||||||
// FIXME: post, not args
|
// FIXME: post, not args
|
||||||
if ($event->count_args() === 3) {
|
if ($event->count_args() === 3) {
|
||||||
@ -172,40 +198,45 @@ class CommentList extends Extension {
|
|||||||
$page->set_mode("redirect");
|
$page->set_mode("redirect");
|
||||||
if (!empty($_SERVER['HTTP_REFERER'])) {
|
if (!empty($_SERVER['HTTP_REFERER'])) {
|
||||||
$page->set_redirect($_SERVER['HTTP_REFERER']);
|
$page->set_redirect($_SERVER['HTTP_REFERER']);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$page->set_redirect(make_link("post/view/" . $event->get_arg(2)));
|
$page->set_redirect(make_link("post/view/" . $event->get_arg(2)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$this->theme->display_permission_denied();
|
$this->theme->display_permission_denied();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if($event->get_arg(0) === "bulk_delete") {
|
|
||||||
|
private function onPageRequest_bulk_delete() {
|
||||||
|
global $user, $database, $page;
|
||||||
if ($user->can("delete_comment") && !empty($_POST["ip"])) {
|
if ($user->can("delete_comment") && !empty($_POST["ip"])) {
|
||||||
$ip = $_POST['ip'];
|
$ip = $_POST['ip'];
|
||||||
|
|
||||||
$cids = $database->get_col("SELECT id FROM comments WHERE owner_ip=:ip", array("ip"=>$ip));
|
$comment_ids = $database->get_col("
|
||||||
$num = count($cids);
|
SELECT id
|
||||||
|
FROM comments
|
||||||
|
WHERE owner_ip=:ip
|
||||||
|
", array("ip" => $ip));
|
||||||
|
$num = count($comment_ids);
|
||||||
log_warning("comment", "Deleting $num comments from $ip");
|
log_warning("comment", "Deleting $num comments from $ip");
|
||||||
foreach($cids as $cid) {
|
foreach($comment_ids as $cid) {
|
||||||
send_event(new CommentDeletionEvent($cid));
|
send_event(new CommentDeletionEvent($cid));
|
||||||
}
|
}
|
||||||
flash_message("Deleted $num comments");
|
flash_message("Deleted $num comments");
|
||||||
|
|
||||||
$page->set_mode("redirect");
|
$page->set_mode("redirect");
|
||||||
$page->set_redirect(make_link("admin"));
|
$page->set_redirect(make_link("admin"));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$this->theme->display_permission_denied();
|
$this->theme->display_permission_denied();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if($event->get_arg(0) === "list") {
|
|
||||||
|
private function onPageRequest_list(PageRequestEvent $event) {
|
||||||
$page_num = int_escape($event->get_arg(1));
|
$page_num = int_escape($event->get_arg(1));
|
||||||
$this->build_page($page_num);
|
$this->build_page($page_num);
|
||||||
}
|
}
|
||||||
else if($event->get_arg(0) === "beta-search") {
|
|
||||||
|
private function onPageRequest_beta_search(PageRequestEvent $event) {
|
||||||
$search = $event->get_arg(1);
|
$search = $event->get_arg(1);
|
||||||
$page_num = int_escape($event->get_arg(2));
|
$page_num = int_escape($event->get_arg(2));
|
||||||
$duser = User::by_name($search);
|
$duser = User::by_name($search);
|
||||||
@ -216,8 +247,6 @@ class CommentList extends Extension {
|
|||||||
$comments = $this->get_user_comments($duser->id, $com_per_page, ($page_num - 1) * $com_per_page);
|
$comments = $this->get_user_comments($duser->id, $com_per_page, ($page_num - 1) * $com_per_page);
|
||||||
$this->theme->display_all_user_comments($comments, $page_num, $total_pages, $duser);
|
$this->theme->display_all_user_comments($comments, $page_num, $total_pages, $duser);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onAdminBuilding(AdminBuildingEvent $event) {
|
public function onAdminBuilding(AdminBuildingEvent $event) {
|
||||||
$this->theme->display_admin_block();
|
$this->theme->display_admin_block();
|
||||||
@ -264,7 +293,10 @@ class CommentList extends Extension {
|
|||||||
|
|
||||||
public function onCommentDeletion(CommentDeletionEvent $event) {
|
public function onCommentDeletion(CommentDeletionEvent $event) {
|
||||||
global $database;
|
global $database;
|
||||||
$database->Execute("DELETE FROM comments WHERE id=:comment_id", array("comment_id"=>$event->comment_id));
|
$database->Execute("
|
||||||
|
DELETE FROM comments
|
||||||
|
WHERE id=:comment_id
|
||||||
|
", array("comment_id"=>$event->comment_id));
|
||||||
log_info("comment", "Deleting Comment #{$event->comment_id}");
|
log_info("comment", "Deleting Comment #{$event->comment_id}");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,38 +361,31 @@ class CommentList extends Extension {
|
|||||||
$database->cache->set("comment_pages", $total_pages, 600);
|
$database->cache->set("comment_pages", $total_pages, 600);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(is_null($current_page) || $current_page <= 0) {
|
|
||||||
$current_page = 1;
|
|
||||||
}
|
|
||||||
$current_page = $this->sanity_check_pagenumber($current_page, $total_pages);
|
$current_page = $this->sanity_check_pagenumber($current_page, $total_pages);
|
||||||
|
|
||||||
$threads_per_page = 10;
|
$threads_per_page = 10;
|
||||||
$start = $threads_per_page * ($current_page - 1);
|
$start = $threads_per_page * ($current_page - 1);
|
||||||
|
|
||||||
$get_threads = "
|
$result = $database->Execute("
|
||||||
SELECT image_id,MAX(posted) AS latest
|
SELECT image_id,MAX(posted) AS latest
|
||||||
FROM comments
|
FROM comments
|
||||||
$where
|
$where
|
||||||
GROUP BY image_id
|
GROUP BY image_id
|
||||||
ORDER BY latest DESC
|
ORDER BY latest DESC
|
||||||
LIMIT :limit OFFSET :offset
|
LIMIT :limit OFFSET :offset
|
||||||
";
|
", array("limit"=>$threads_per_page, "offset"=>$start));
|
||||||
$result = $database->Execute($get_threads, array("limit"=>$threads_per_page, "offset"=>$start));
|
|
||||||
|
|
||||||
if(ext_is_live("Ratings")) {
|
$user_ratings = ext_is_live("Ratings") ? Ratings::get_user_privs($user) : "";
|
||||||
$user_ratings = Ratings::get_user_privs($user);
|
|
||||||
} else {
|
|
||||||
$user_ratings = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
$images = array();
|
$images = array();
|
||||||
while($row = $result->fetch()) {
|
while($row = $result->fetch()) {
|
||||||
$image = Image::by_id($row["image_id"]);
|
$image = Image::by_id($row["image_id"]);
|
||||||
if(ext_is_live("Ratings") && !is_null($image)) {
|
if(
|
||||||
if(strpos($user_ratings, $image->rating) === FALSE) {
|
ext_is_live("Ratings") && !is_null($image) &&
|
||||||
|
strpos($user_ratings, $image->rating) === FALSE
|
||||||
|
) {
|
||||||
$image = null; // this is "clever", I may live to regret it
|
$image = null; // this is "clever", I may live to regret it
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if(!is_null($image)) {
|
if(!is_null($image)) {
|
||||||
$comments = $this->get_comments($image->id);
|
$comments = $this->get_comments($image->id);
|
||||||
$images[] = array($image, $comments);
|
$images[] = array($image, $comments);
|
||||||
@ -372,13 +397,27 @@ class CommentList extends Extension {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
// get comments {{{
|
// get comments {{{
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @param array $args
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function get_generic_comments($query, $args) {
|
||||||
|
global $database;
|
||||||
|
$rows = $database->get_all($query, $args);
|
||||||
|
$comments = array();
|
||||||
|
foreach($rows as $row) {
|
||||||
|
$comments[] = new Comment($row);
|
||||||
|
}
|
||||||
|
return $comments;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $count
|
* @param int $count
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function get_recent_comments($count) {
|
private function get_recent_comments($count) {
|
||||||
global $database;
|
return $this->get_generic_comments("
|
||||||
$rows = $database->get_all("
|
|
||||||
SELECT
|
SELECT
|
||||||
users.id as user_id, users.name as user_name, users.email as user_email, users.class as user_class,
|
users.id as user_id, users.name as user_name, users.email as user_email, users.class as user_class,
|
||||||
comments.comment as comment, comments.id as comment_id,
|
comments.comment as comment, comments.id as comment_id,
|
||||||
@ -389,11 +428,6 @@ class CommentList extends Extension {
|
|||||||
ORDER BY comments.id DESC
|
ORDER BY comments.id DESC
|
||||||
LIMIT :limit
|
LIMIT :limit
|
||||||
", array("limit"=>$count));
|
", array("limit"=>$count));
|
||||||
$comments = array();
|
|
||||||
foreach($rows as $row) {
|
|
||||||
$comments[] = new Comment($row);
|
|
||||||
}
|
|
||||||
return $comments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -403,8 +437,7 @@ class CommentList extends Extension {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function get_user_comments(/*int*/ $user_id, /*int*/ $count, /*int*/ $offset=0) {
|
private function get_user_comments(/*int*/ $user_id, /*int*/ $count, /*int*/ $offset=0) {
|
||||||
global $database;
|
return $this->get_generic_comments("
|
||||||
$rows = $database->get_all("
|
|
||||||
SELECT
|
SELECT
|
||||||
users.id as user_id, users.name as user_name, users.email as user_email, users.class as user_class,
|
users.id as user_id, users.name as user_name, users.email as user_email, users.class as user_class,
|
||||||
comments.comment as comment, comments.id as comment_id,
|
comments.comment as comment, comments.id as comment_id,
|
||||||
@ -416,11 +449,6 @@ class CommentList extends Extension {
|
|||||||
ORDER BY comments.id DESC
|
ORDER BY comments.id DESC
|
||||||
LIMIT :limit OFFSET :offset
|
LIMIT :limit OFFSET :offset
|
||||||
", array("user_id"=>$user_id, "offset"=>$offset, "limit"=>$count));
|
", array("user_id"=>$user_id, "offset"=>$offset, "limit"=>$count));
|
||||||
$comments = array();
|
|
||||||
foreach($rows as $row) {
|
|
||||||
$comments[] = new Comment($row);
|
|
||||||
}
|
|
||||||
return $comments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -428,9 +456,7 @@ class CommentList extends Extension {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function get_comments(/*int*/ $image_id) {
|
private function get_comments(/*int*/ $image_id) {
|
||||||
global $database;
|
return $this->get_generic_comments("
|
||||||
$i_image_id = int_escape($image_id);
|
|
||||||
$rows = $database->get_all("
|
|
||||||
SELECT
|
SELECT
|
||||||
users.id as user_id, users.name as user_name, users.email as user_email, users.class as user_class,
|
users.id as user_id, users.name as user_name, users.email as user_email, users.class as user_class,
|
||||||
comments.comment as comment, comments.id as comment_id,
|
comments.comment as comment, comments.id as comment_id,
|
||||||
@ -440,12 +466,7 @@ class CommentList extends Extension {
|
|||||||
LEFT JOIN users ON comments.owner_id=users.id
|
LEFT JOIN users ON comments.owner_id=users.id
|
||||||
WHERE comments.image_id=:image_id
|
WHERE comments.image_id=:image_id
|
||||||
ORDER BY comments.id ASC
|
ORDER BY comments.id ASC
|
||||||
", array("image_id"=>$i_image_id));
|
", array("image_id"=>$image_id));
|
||||||
$comments = array();
|
|
||||||
foreach($rows as $row) {
|
|
||||||
$comments[] = new Comment($row);
|
|
||||||
}
|
|
||||||
return $comments;
|
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
@ -463,9 +484,11 @@ class CommentList extends Extension {
|
|||||||
else $window_sql = "interval '$window minute'";
|
else $window_sql = "interval '$window minute'";
|
||||||
|
|
||||||
// window doesn't work as an SQL param because it's inside quotes >_<
|
// window doesn't work as an SQL param because it's inside quotes >_<
|
||||||
$result = $database->get_all("SELECT * FROM comments WHERE owner_ip = :remote_ip ".
|
$result = $database->get_all("
|
||||||
"AND posted > now() - $window_sql",
|
SELECT *
|
||||||
Array("remote_ip"=>$_SERVER['REMOTE_ADDR']));
|
FROM comments
|
||||||
|
WHERE owner_ip = :remote_ip AND posted > now() - $window_sql
|
||||||
|
", array("remote_ip"=>$_SERVER['REMOTE_ADDR']));
|
||||||
|
|
||||||
return (count($result) >= $max);
|
return (count($result) >= $max);
|
||||||
}
|
}
|
||||||
@ -537,7 +560,11 @@ class CommentList extends Extension {
|
|||||||
*/
|
*/
|
||||||
private function is_dupe(/*int*/ $image_id, /*string*/ $comment) {
|
private function is_dupe(/*int*/ $image_id, /*string*/ $comment) {
|
||||||
global $database;
|
global $database;
|
||||||
return ($database->get_row("SELECT * FROM comments WHERE image_id=:image_id AND comment=:comment", array("image_id"=>$image_id, "comment"=>$comment)));
|
return $database->get_row("
|
||||||
|
SELECT *
|
||||||
|
FROM comments
|
||||||
|
WHERE image_id=:image_id AND comment=:comment
|
||||||
|
", array("image_id"=>$image_id, "comment"=>$comment));
|
||||||
}
|
}
|
||||||
// do some checks
|
// do some checks
|
||||||
|
|
||||||
@ -547,15 +574,12 @@ class CommentList extends Extension {
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
private function sanity_check_pagenumber(/*int*/ $pagenum, /*int*/ $maxpage) {
|
private function sanity_check_pagenumber(/*int*/ $pagenum, /*int*/ $maxpage) {
|
||||||
if (!is_numeric($pagenum)){
|
if(!is_numeric($pagenum) || $pagenum <= 0) {
|
||||||
$pagenum = 1;
|
$pagenum = 1;
|
||||||
}
|
}
|
||||||
if($pagenum > $maxpage) {
|
if($pagenum > $maxpage) {
|
||||||
$pagenum = $maxpage;
|
$pagenum = $maxpage;
|
||||||
}
|
}
|
||||||
if ($pagenum<=0){
|
|
||||||
$pagenum=1;
|
|
||||||
}
|
|
||||||
return $pagenum;
|
return $pagenum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user