- Patch 4859: new drupal_unpack() consolidates duplicate code and makes it

easy to show avatars next to nodes and comments.  Patch by Moshe.  As a
  showcase, maybe Xtemplate should have an option to enable/disable avatars?
4.4.x
Dries Buytaert 2004-01-13 19:25:37 +00:00
parent 75685d8c3d
commit eee72bfa95
8 changed files with 37 additions and 29 deletions

View File

@ -161,6 +161,22 @@ function bootstrap_hooks() {
return array('init', 'exit'); return array('init', 'exit');
} }
/*
** Unserializes and appends elements from a serialized string
** $obj is the object to which we shall append
** $field is the element whose value is a serialized string
*/
function drupal_unpack($obj, $field = 'data') {
if ($obj->$field && $data = unserialize($obj->$field)) {
foreach ($data as $key => $value) {
if (!isset($obj->$key)) {
$obj->$key = $value;
}
}
}
return $obj;
}
function referer_uri() { function referer_uri() {
if (isset($_SERVER["HTTP_REFERER"])) { if (isset($_SERVER["HTTP_REFERER"])) {
return check_url($_SERVER["HTTP_REFERER"]); return check_url($_SERVER["HTTP_REFERER"]);

View File

@ -25,13 +25,7 @@ function sess_read($key) {
} }
$user = db_fetch_object($result); $user = db_fetch_object($result);
if ($user->data && $data = unserialize($user->data)) { $user = drupal_unpack($user);
foreach ($data as $key => $value) {
if (!isset($user->$key)) {
$user->$key = $value;
}
}
}
return !empty($user->session) ? $user->session : ''; return !empty($user->session) ? $user->session : '';
} }

View File

@ -187,7 +187,7 @@ function comment_edit($cid) {
global $user; global $user;
$comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $cid)); $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $cid));
$comment = drupal_unpack($comment);
if (comment_access("edit", $comment)) { if (comment_access("edit", $comment)) {
return comment_preview(object2array($comment)); return comment_preview(object2array($comment));
} }
@ -205,6 +205,7 @@ function comment_reply($pid, $nid) {
if ($pid) { if ($pid) {
$comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $pid)); $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $pid));
$comment = drupal_unpack($comment);
$output .= theme("comment_view", $comment); $output .= theme("comment_view", $comment);
} }
else if (user_access("access content")) { else if (user_access("access content")) {
@ -259,6 +260,7 @@ function comment_preview($edit) {
if ($edit["pid"]) { if ($edit["pid"]) {
$comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $edit["pid"])); $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $edit["pid"]));
$comment = drupal_unpack($comment);
$output .= theme("comment_view", $comment); $output .= theme("comment_view", $comment);
} }
else { else {
@ -678,7 +680,6 @@ function comment_render($node, $cid = 0) {
*/ */
$result = pager_query($query, $comments_per_page, 0, "SELECT COUNT(*) FROM {comments} WHERE nid = '". check_query($nid) ."'"); $result = pager_query($query, $comments_per_page, 0, "SELECT COUNT(*) FROM {comments} WHERE nid = '". check_query($nid) ."'");
if (db_num_rows($result) && (variable_get("comment_controls", 0) == 0 || variable_get("comment_controls", 0) == 2)) { if (db_num_rows($result) && (variable_get("comment_controls", 0) == 0 || variable_get("comment_controls", 0) == 2)) {
$output .= "<form method=\"post\" action=\"". url("comment") ."\"><div>\n"; $output .= "<form method=\"post\" action=\"". url("comment") ."\"><div>\n";
$output .= theme("comment_controls", $threshold, $mode, $order, $comments_per_page); $output .= theme("comment_controls", $threshold, $mode, $order, $comments_per_page);
@ -690,6 +691,7 @@ function comment_render($node, $cid = 0) {
$output .= form_hidden("nid", $nid); $output .= form_hidden("nid", $nid);
while ($comment = db_fetch_object($result)) { while ($comment = db_fetch_object($result)) {
$comment = drupal_unpack($comment);
$comment->depth = count(explode(".", $comment->thread)) - 1; $comment->depth = count(explode(".", $comment->thread)) - 1;
if ($mode == 1) { if ($mode == 1) {
@ -899,6 +901,7 @@ function comment_admin_edit($id) {
$result = db_query("SELECT c.*, u.name, u.uid FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $id); $result = db_query("SELECT c.*, u.name, u.uid FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $id);
$comment = db_fetch_object($result); $comment = db_fetch_object($result);
$comment = drupal_unpack($comment);
if ($comment) { if ($comment) {
$form .= form_item(t("Author"), format_name($comment)); $form .= form_item(t("Author"), format_name($comment));

View File

@ -187,7 +187,7 @@ function comment_edit($cid) {
global $user; global $user;
$comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $cid)); $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $cid));
$comment = drupal_unpack($comment);
if (comment_access("edit", $comment)) { if (comment_access("edit", $comment)) {
return comment_preview(object2array($comment)); return comment_preview(object2array($comment));
} }
@ -205,6 +205,7 @@ function comment_reply($pid, $nid) {
if ($pid) { if ($pid) {
$comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $pid)); $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $pid));
$comment = drupal_unpack($comment);
$output .= theme("comment_view", $comment); $output .= theme("comment_view", $comment);
} }
else if (user_access("access content")) { else if (user_access("access content")) {
@ -259,6 +260,7 @@ function comment_preview($edit) {
if ($edit["pid"]) { if ($edit["pid"]) {
$comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $edit["pid"])); $comment = db_fetch_object(db_query("SELECT c.*, u.uid, u.name, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = 0", $edit["pid"]));
$comment = drupal_unpack($comment);
$output .= theme("comment_view", $comment); $output .= theme("comment_view", $comment);
} }
else { else {
@ -678,7 +680,6 @@ function comment_render($node, $cid = 0) {
*/ */
$result = pager_query($query, $comments_per_page, 0, "SELECT COUNT(*) FROM {comments} WHERE nid = '". check_query($nid) ."'"); $result = pager_query($query, $comments_per_page, 0, "SELECT COUNT(*) FROM {comments} WHERE nid = '". check_query($nid) ."'");
if (db_num_rows($result) && (variable_get("comment_controls", 0) == 0 || variable_get("comment_controls", 0) == 2)) { if (db_num_rows($result) && (variable_get("comment_controls", 0) == 0 || variable_get("comment_controls", 0) == 2)) {
$output .= "<form method=\"post\" action=\"". url("comment") ."\"><div>\n"; $output .= "<form method=\"post\" action=\"". url("comment") ."\"><div>\n";
$output .= theme("comment_controls", $threshold, $mode, $order, $comments_per_page); $output .= theme("comment_controls", $threshold, $mode, $order, $comments_per_page);
@ -690,6 +691,7 @@ function comment_render($node, $cid = 0) {
$output .= form_hidden("nid", $nid); $output .= form_hidden("nid", $nid);
while ($comment = db_fetch_object($result)) { while ($comment = db_fetch_object($result)) {
$comment = drupal_unpack($comment);
$comment->depth = count(explode(".", $comment->thread)) - 1; $comment->depth = count(explode(".", $comment->thread)) - 1;
if ($mode == 1) { if ($mode == 1) {
@ -899,6 +901,7 @@ function comment_admin_edit($id) {
$result = db_query("SELECT c.*, u.name, u.uid FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $id); $result = db_query("SELECT c.*, u.name, u.uid FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status != 2", $id);
$comment = db_fetch_object($result); $comment = db_fetch_object($result);
$comment = drupal_unpack($comment);
if ($comment) { if ($comment) {
$form .= form_item(t("Author"), format_name($comment)); $form .= form_item(t("Author"), format_name($comment));

View File

@ -263,10 +263,11 @@ function node_load($conditions, $revision = -1) {
** Retrieve the node: ** Retrieve the node:
*/ */
$node = db_fetch_object(db_query("SELECT n.*, u.uid, u.name FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE ". implode(" AND ", $cond))); $node = db_fetch_object(db_query("SELECT n.*, u.uid, u.name, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE ". implode(" AND ", $cond)));
$node = drupal_unpack($node);
/* /*
** Unserialize the revisions field: ** Unserialize the revisions and user data fields:
*/ */
if ($node->revisions) { if ($node->revisions) {

View File

@ -263,10 +263,11 @@ function node_load($conditions, $revision = -1) {
** Retrieve the node: ** Retrieve the node:
*/ */
$node = db_fetch_object(db_query("SELECT n.*, u.uid, u.name FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE ". implode(" AND ", $cond))); $node = db_fetch_object(db_query("SELECT n.*, u.uid, u.name, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE ". implode(" AND ", $cond)));
$node = drupal_unpack($node);
/* /*
** Unserialize the revisions field: ** Unserialize the revisions and user data fields:
*/ */
if ($node->revisions) { if ($node->revisions) {

View File

@ -40,13 +40,8 @@ function user_load($array = array()) {
$result = db_query_range("SELECT u.*, r.name AS role FROM {role} r INNER JOIN {users} u ON r.rid = u.rid WHERE $query u.status < 3", 0, 1); $result = db_query_range("SELECT u.*, r.name AS role FROM {role} r INNER JOIN {users} u ON r.rid = u.rid WHERE $query u.status < 3", 0, 1);
$user = db_fetch_object($result); $user = db_fetch_object($result);
if ($user->data && $data = unserialize($user->data)) { $user = drupal_unpack($user);
foreach ($data as $key => $value) {
if (!isset($user->$key)) {
$user->$key = $value;
}
}
}
user_module_invoke("load", $array, $user); user_module_invoke("load", $array, $user);

View File

@ -40,13 +40,8 @@ function user_load($array = array()) {
$result = db_query_range("SELECT u.*, r.name AS role FROM {role} r INNER JOIN {users} u ON r.rid = u.rid WHERE $query u.status < 3", 0, 1); $result = db_query_range("SELECT u.*, r.name AS role FROM {role} r INNER JOIN {users} u ON r.rid = u.rid WHERE $query u.status < 3", 0, 1);
$user = db_fetch_object($result); $user = db_fetch_object($result);
if ($user->data && $data = unserialize($user->data)) { $user = drupal_unpack($user);
foreach ($data as $key => $value) {
if (!isset($user->$key)) {
$user->$key = $value;
}
}
}
user_module_invoke("load", $array, $user); user_module_invoke("load", $array, $user);