From 99d84c933003d6ecc5a7fe2d81f87cb0e1de9017 Mon Sep 17 00:00:00 2001
From: Dries Buytaert The pager uses
", stripslashes($text))) : stripslashes($text)) : message_na();
+ if ($text) {
+ $text = stripslashes($text);
+
+ if (strip_tags($text, "") == $text) {
+ $text = nl2br($text);
+ }
+ }
+ else {
+ $text = message_na();
+ }
+
+ return $text;
}
function check_file($filename) {
@@ -640,6 +639,34 @@ function form_weight($title = NULL, $name = "weight", $value = 0, $delta = 10, $
return form_select($title, $name, $value, $weights, $description, $extra);
}
+/**
+ * Parse an URL; this function must be follow the changes of a clean url implementation
+ *
+ * @param string $url optional, url to parse; default to request_uri()
+ * @return array $result associative array:
+ * script => index/node/module/admin
+ * query => GET variables
+ *
+ */
+function drupal_parse_url($url = NULL) {
+ global $PHP_SELF;
+ static $cache;
+
+ if ($url == NULL) {
+ $url = $PHP_SELF ."?". getenv("QUERY_STRING");
+ }
+
+ if (!$cache[$url]) {
+ $parts = parse_url($url);
+ preg_match("/(\w+?)\.php/", $parts["path"], $found);
+ $cache[$url]["script"] = $found[1];
+ parse_str($parts["query"], $cache[$url]["query"]);
+ $cache[$url]["anchor"] = $parts["fragment"];
+ }
+
+ return $cache[$url];
+}
+
/**
* Build an URL; use this functions when you must write an URL
* for example in a form or a redirect.
@@ -649,9 +676,20 @@ function form_weight($title = NULL, $name = "weight", $value = 0, $delta = 10, $
* @param $anchor optional, anchor name
*/
function drupal_url($args = array(), $script = "node", $anchor = 0) {
+ static $search, $replace;
+
+ if (!$search) {
+ /*
+ According to RFC 1738 [3] the special characters "$-_.+!*'()," and the
+ reserved characters "/:@#?&=" can be used unencoded within an URL
+ */
+ $search = array("%24", "%2B", "%21", "%2A", "%27", "%28", "%29", "%2C", "%2F", "%3A", "%40", "%23", "%3F", "%26", "%3D");
+ $replace = array("$", "+", "!", "*", "'", "(", ")", ",", "/", ":", "@", "#", "?", "&", "=");
+ }
+
$t = array();
foreach ($args as $key => $value) {
- $t[] = "$key=". urlencode($value);
+ $t[] = "$key=". str_replace($search, $replace, urlencode($value));
}
if (count($t)) {
return "$script.php?". implode("&", $t) . ($anchor ? "#$anchor" : "");
diff --git a/includes/pager.inc b/includes/pager.inc
index a7cb48784..8f93a5a54 100644
--- a/includes/pager.inc
+++ b/includes/pager.inc
@@ -5,7 +5,7 @@ function pager_help() {
?>
Implementation note: making queries pagable
LIMIT
-based queries to fetch only the records required to render a certain page. However, it has to learn the total number of records returned by the query to (among others) compute the number of pages (= number of all records / number of records per page). This is done by inserting COUNT(*)
in the original query, ie. by rewriting the original query SELECT nid, type FROM node WHERE status = '1' ORDER BY static DESC, created DESC
to read SELECT COUNT(*) FROM node WHERE status = '1' ORDER BY static DESC, created DESC
Rewriting the query is accomplished using a regular expression; preg_replace("/SELECT.*FROM/i", "SELECT COUNT(*) FROM", $query)
.
Unfortunately, the call to preg_replace()
does not work as intended for queries that already have a COUNT()
clause; the original COUNT()
will be removed from the query, possibly making the remainder of the query fail (eg. when the use of HAVING
or ORDER
depends on the value returned by COUNT()
). In practice, for queries to be db_query_pager()
-able, they shold be reformulated not to use COUNT()
.
Unfortunately, the call to preg_replace()
does not work as intended for queries that already have a COUNT()
clause; the original COUNT()
will be removed from the query, possibly making the remainder of the query fail (eg. when the use of HAVING
or ORDER
depends on the value returned by COUNT()
). In practice, for queries to be pager_query()
-able, they shold be reformulated not to use COUNT()
.
". pager_first(($tags[0] ? $tags[0] : t("first page")), $limit, $element) ." | "; - $output .= "". pager_previous(($tags[1] ? $tags[1] : t("previous page")), $limit, $element) ." | "; - $output .= "". pager_list($limit, $element, ($tags[2] ? $tags[2] : 9 )) ." | "; - $output .= "". pager_next(($tags[3] ? $tags[3] : t("next page")), $limit, $element) ." | "; - $output .= "". pager_last(($tags[4] ? $tags[4] : t("last page")), $limit, $element) ." | "; + $output .= "". pager_first(($tags[0] ? $tags[0] : t("first page")), $limit, $element, $attributes) ." | "; + $output .= "". pager_previous(($tags[1] ? $tags[1] : t("previous page")), $limit, $element, 1, $attributes) ." | "; + $output .= "". pager_list($limit, $element, ($tags[2] ? $tags[2] : 9 ), "", $attributes) ." | "; + $output .= "". pager_next(($tags[3] ? $tags[3] : t("next page")), $limit, $element, 1, $attributes) ." | "; + $output .= "". pager_last(($tags[4] ? $tags[4] : t("last page")), $limit, $element, $attributes) ." | "; $output .= "
@@ -272,57 +269,33 @@ function pager_list($limit, $element = 0, $quantity = 5, $text = "") { * * @return resource MySQL query result */ -function db_query_pager($query, $limit = 10, $element = 0) { - global $from, $from_array, $db_type, $pager_total; +function pager_query($query, $limit = 10, $element = 0) { + global $from, $pager_from_array, $db_type, $pager_total; // count the total number of records in this query: - $array = db_fetch_array(db_query(preg_replace("/SELECT.*FROM/i", "SELECT COUNT(*) FROM", $query))); - if ($array) { - $pager_total[$element] = array_pop($array); - } - else { - $pager_total[$element] = 0; - } + $pager_total[$element] = db_result(db_query(preg_replace("/SELECT.*FROM/i", "SELECT COUNT(*) FROM", $query))); // convert comma separated $from to an array, used by other functions: - $from_array = explode(",", $from); + $pager_from_array = explode(",", $from); - if ((int)$from_array[$element]) { - if ($db_type == "mysql") { - // MySQL formatted limit query with offset: - $limit_query = $query . " LIMIT " . (int)$from_array[$element] . ", $limit"; - } - else { - // pear formatted limit query with offset: - $limit_query = $query . " LIMIT $limit OFFSET " . (int)$from_array[$element]; - } - } - else { - // standard limit query without offset: - $limit_query = $query . " LIMIT $limit"; - } - return db_query($limit_query); + return db_query_range($query, (int)$pager_from_array[$element], (int)$limit); } -function pager_link($from_new) { - $from_list = @implode(",", $from_new); - if ("$from_list" == "0") { - // single pager at zero, so remove the $from - return preg_replace(array("/from=*[^&]*/", "/[&]$/", "/[?]$/"), "", request_uri()); - } - if (preg_match("/from=/", request_uri())) { - // replace existing from= - return preg_replace("/from=*[^&]*/", "from=$from_list", request_uri()); - } - if (preg_match("/[?]/", request_uri())) { - // append &from= - $href = request_uri() . "&from=$from_list"; +function pager_link($from_new, $attributes = array()) { + $url = drupal_parse_url(); + + if (count($from_new) == 0 || (count($from_new) == 1 && $from_new[0] == 0)) { + unset($url["query"]["from"]); } else { - // append ?from= - $href = request_uri() . "?from=$from_list"; + $url["query"]["from"] = implode(",", $from_new); } - return $href; + + if (count($attributes)) { + $url["query"] = array_merge($url["query"], $attributes); + } + + return drupal_url($url["query"], $url["script"]); } function pager_load_array($value, $element, $old_array) { diff --git a/modules/blog.module b/modules/blog.module index 14cf276b6..14863d550 100644 --- a/modules/blog.module +++ b/modules/blog.module @@ -157,7 +157,7 @@ function blog_page_user($uid = 0) { $links[] = lm(t("view recent blog entries"), array("mod" => "blog", "op" => "view"), "", array("title" => t("View all recent blog entries."))); $theme->box(t("%username's blog", array ("%username" => $account->name)), "". $theme->links($links). " "); - $result = db_query_pager("SELECT nid FROM node WHERE type = 'blog' AND uid = '$account->uid' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); + $result = pager_query("SELECT nid FROM node WHERE type = 'blog' AND uid = '$account->uid' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); while ($node = db_fetch_object($result)) { node_view(node_load(array("nid" => $node->nid)), 1); } @@ -168,7 +168,7 @@ function blog_page_user($uid = 0) { function blog_page_last() { global $user, $theme; - $result = db_query_pager("SELECT nid FROM node WHERE type = 'blog' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); + $result = pager_query("SELECT nid FROM node WHERE type = 'blog' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); while ($node = db_fetch_object($result)) { $output = node_view(node_load(array("nid" => $node->nid)), 1); diff --git a/modules/blog/blog.module b/modules/blog/blog.module index 14cf276b6..14863d550 100644 --- a/modules/blog/blog.module +++ b/modules/blog/blog.module @@ -157,7 +157,7 @@ function blog_page_user($uid = 0) { $links[] = lm(t("view recent blog entries"), array("mod" => "blog", "op" => "view"), "", array("title" => t("View all recent blog entries."))); $theme->box(t("%username's blog", array ("%username" => $account->name)), "". $theme->links($links). " "); - $result = db_query_pager("SELECT nid FROM node WHERE type = 'blog' AND uid = '$account->uid' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); + $result = pager_query("SELECT nid FROM node WHERE type = 'blog' AND uid = '$account->uid' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); while ($node = db_fetch_object($result)) { node_view(node_load(array("nid" => $node->nid)), 1); } @@ -168,7 +168,7 @@ function blog_page_user($uid = 0) { function blog_page_last() { global $user, $theme; - $result = db_query_pager("SELECT nid FROM node WHERE type = 'blog' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); + $result = pager_query("SELECT nid FROM node WHERE type = 'blog' AND status = 1 ORDER BY nid DESC", $user->nodes ? $user->nodes : variable_get("default_nodes_main", 10)); while ($node = db_fetch_object($result)) { $output = node_view(node_load(array("nid" => $node->nid)), 1); diff --git a/modules/node.module b/modules/node.module index 5c05c6a76..587c94662 100644 --- a/modules/node.module +++ b/modules/node.module @@ -399,16 +399,6 @@ function node_filter_line($text) { ** mark-up and use of line breaks and paragraphs. */ - /* - ** When block tags are used, regular line breaks get stripped. - ** However, when only text formatting tags are used, Drupal will - ** insert linebreaks. - */ - - if (strip_tags($text, "") != $text) { - $text = ereg_replace("[\r\n]", "", $text); - } - /* ** Replace some common "artifacts": */ @@ -549,7 +539,7 @@ function node_admin_nodes() { $queries = array(array("ORDER BY n.created DESC", "new nodes"), array("ORDER BY n.changed DESC", "updated nodes"), array("WHERE n.status = 1 AND n.moderate = 0 ORDER BY n.nid DESC", "published nodes"), array("WHERE n.status = 0 AND n.moderate = 0 ORDER BY n.nid DESC", "non-published nodes"), array("WHERE n.status = 1 AND n.moderate = 1 ORDER BY n.nid DESC", "pending nodes"), array("WHERE n.status = 1 AND n.promote = 1 ORDER BY n.nid DESC", "promoted nodes")); - $result = db_query_pager("SELECT n.*, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid ". $queries[$query ? $query : 1][0], 50); + $result = pager_query("SELECT n.*, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid ". $queries[$query ? $query : 1][0], 50); foreach ($queries as $key => $value) { $links[] = la($value[1], array("mod" => "node", "op" => "nodes", "query" => $key)); @@ -1348,14 +1338,14 @@ function node_page() { if ($or) { // this is an OR of terms - $result = db_query_pager("SELECT DISTINCT(n.nid), type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); + $result = pager_query("SELECT DISTINCT(n.nid), type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); } else if ($and) { // this is an AND - $result = db_query_pager("SELECT n.nid, n.type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' GROUP BY n.nid, n.type, n.status, n.static, n.created HAVING COUNT(n.nid) = ".count($terms)." ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); + $result = pager_query("SELECT n.nid, n.type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' GROUP BY n.nid, n.type, n.status, n.static, n.created HAVING COUNT(n.nid) = ".count($terms)." ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); } else { - $result = db_query_pager("SELECT nid, type FROM node WHERE ". ($id ? "nid = '$id'" : "promote = '1'") ." AND status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); + $result = pager_query("SELECT nid, type FROM node WHERE ". ($id ? "nid = '$id'" : "promote = '1'") ." AND status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); } diff --git a/modules/node/node.module b/modules/node/node.module index 5c05c6a76..587c94662 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -399,16 +399,6 @@ function node_filter_line($text) { ** mark-up and use of line breaks and paragraphs. */ - /* - ** When block tags are used, regular line breaks get stripped. - ** However, when only text formatting tags are used, Drupal will - ** insert linebreaks. - */ - - if (strip_tags($text, "
") != $text) { - $text = ereg_replace("[\r\n]", "", $text); - } - /* ** Replace some common "artifacts": */ @@ -549,7 +539,7 @@ function node_admin_nodes() { $queries = array(array("ORDER BY n.created DESC", "new nodes"), array("ORDER BY n.changed DESC", "updated nodes"), array("WHERE n.status = 1 AND n.moderate = 0 ORDER BY n.nid DESC", "published nodes"), array("WHERE n.status = 0 AND n.moderate = 0 ORDER BY n.nid DESC", "non-published nodes"), array("WHERE n.status = 1 AND n.moderate = 1 ORDER BY n.nid DESC", "pending nodes"), array("WHERE n.status = 1 AND n.promote = 1 ORDER BY n.nid DESC", "promoted nodes")); - $result = db_query_pager("SELECT n.*, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid ". $queries[$query ? $query : 1][0], 50); + $result = pager_query("SELECT n.*, u.name, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid ". $queries[$query ? $query : 1][0], 50); foreach ($queries as $key => $value) { $links[] = la($value[1], array("mod" => "node", "op" => "nodes", "query" => $key)); @@ -1348,14 +1338,14 @@ function node_page() { if ($or) { // this is an OR of terms - $result = db_query_pager("SELECT DISTINCT(n.nid), type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); + $result = pager_query("SELECT DISTINCT(n.nid), type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); } else if ($and) { // this is an AND - $result = db_query_pager("SELECT n.nid, n.type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' GROUP BY n.nid, n.type, n.status, n.static, n.created HAVING COUNT(n.nid) = ".count($terms)." ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); + $result = pager_query("SELECT n.nid, n.type FROM node n LEFT JOIN term_node r ON n.nid = r.nid WHERE tid IN (".implode(",", $terms).") AND ". ($id ? "nid = '$id' AND " : "") ."status = '1' GROUP BY n.nid, n.type, n.status, n.static, n.created HAVING COUNT(n.nid) = ".count($terms)." ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); } else { - $result = db_query_pager("SELECT nid, type FROM node WHERE ". ($id ? "nid = '$id'" : "promote = '1'") ." AND status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); + $result = pager_query("SELECT nid, type FROM node WHERE ". ($id ? "nid = '$id'" : "promote = '1'") ." AND status = '1' ORDER BY static DESC, created DESC", ($user->nodes ? $user->nodes : variable_get("default_nodes_main", 10))); } -- cgit v1.2.3