From a597354bcb48536f2c7633d53c78fa931b186c20 Mon Sep 17 00:00:00 2001 From: Steven Wittens Date: Tue, 31 May 2005 21:14:27 +0000 Subject: [PATCH] - Code cleanup: improve format_plural usage, add some missing placeholder/check calls, and introduce API for tags. --- includes/common.inc | 9 ++++++++- includes/theme.inc | 2 +- modules/blog.module | 13 ++++++++++--- modules/blog/blog.module | 13 ++++++++++--- modules/blogapi.module | 5 ++++- modules/blogapi/blogapi.module | 5 ++++- modules/forum.module | 9 ++++++--- modules/forum/forum.module | 9 ++++++--- modules/node.module | 7 +++++-- modules/node/node.module | 7 +++++-- modules/poll.module | 3 ++- modules/poll/poll.module | 3 ++- modules/statistics.module | 4 ++-- modules/statistics/statistics.module | 4 ++-- modules/taxonomy.module | 7 +++++-- modules/taxonomy/taxonomy.module | 7 +++++-- modules/throttle.module | 22 +++++++++++++++------- modules/throttle/throttle.module | 22 +++++++++++++++------- 18 files changed, 107 insertions(+), 44 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index b7210f6689c..bae9c25549c 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1613,7 +1613,6 @@ function drupal_attributes($attributes = array()) { foreach ($attributes as $key => $value) { $t[] = $key .'="'. check_plain($value) .'"'; } - return ' '. implode($t, ' '); } } @@ -1973,6 +1972,14 @@ if (version_compare(phpversion(), '5.0') < 0) { '); } +/** + * Add a tag to the page's HEAD. + */ +function drupal_add_link($attributes) { + drupal_set_html_head('\n"); +} + + /** * Add a JavaScript file to the output. * diff --git a/includes/theme.inc b/includes/theme.inc index 6f90399d76f..fa3a646aac4 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -501,7 +501,7 @@ function theme_links($links, $delimiter = ' | ') { function theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) { if (!$getsize || (file_exists($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) { $attributes = drupal_attributes($attributes); - return "\"$alt\""; + return ''. check_plain($alt) .''; } } diff --git a/modules/blog.module b/modules/blog.module index bbf4e4d220a..31d4689b549 100644 --- a/modules/blog.module +++ b/modules/blog.module @@ -148,7 +148,10 @@ function blog_page_user($uid) { $output .= theme('pager', NULL, variable_get('default_nodes_main', 10)); $output .= theme('xml_icon', url("blog/$account->uid/feed")); - drupal_set_html_head('uid/feed") .'" />'); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => t('RSS - %title', array('%title' => $title)), + 'href' => url("blog/$account->uid/feed"))); return $output; } else { @@ -172,7 +175,10 @@ function blog_page_last() { $output .= theme('pager', NULL, variable_get('default_nodes_main', 10)); $output .= theme('xml_icon', url('blog/feed')); - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => t('RSS - blogs'), + 'href' => url("blog/feed"))); return $output; } @@ -195,7 +201,8 @@ function blog_form(&$node) { if ($iid && $item = db_fetch_object(db_query('SELECT i.*, f.title as ftitle, f.link as flink FROM {aggregator_item} i, {aggregator_feed} f WHERE i.iid = %d AND i.fid = f.fid', $iid))) { $node->title = $item->title; - $node->body = "link\">$item->title - ". $item->description ." [flink\">$item->ftitle]\n"; + // Note: $item->description has been validated on aggregation. + $node->body = ''. check_plain($item->title) .' - '. $item->description .' ['. check_plain($item->ftitle) ."]\n"; } } diff --git a/modules/blog/blog.module b/modules/blog/blog.module index bbf4e4d220a..31d4689b549 100644 --- a/modules/blog/blog.module +++ b/modules/blog/blog.module @@ -148,7 +148,10 @@ function blog_page_user($uid) { $output .= theme('pager', NULL, variable_get('default_nodes_main', 10)); $output .= theme('xml_icon', url("blog/$account->uid/feed")); - drupal_set_html_head('uid/feed") .'" />'); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => t('RSS - %title', array('%title' => $title)), + 'href' => url("blog/$account->uid/feed"))); return $output; } else { @@ -172,7 +175,10 @@ function blog_page_last() { $output .= theme('pager', NULL, variable_get('default_nodes_main', 10)); $output .= theme('xml_icon', url('blog/feed')); - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => t('RSS - blogs'), + 'href' => url("blog/feed"))); return $output; } @@ -195,7 +201,8 @@ function blog_form(&$node) { if ($iid && $item = db_fetch_object(db_query('SELECT i.*, f.title as ftitle, f.link as flink FROM {aggregator_item} i, {aggregator_feed} f WHERE i.iid = %d AND i.fid = f.fid', $iid))) { $node->title = $item->title; - $node->body = "link\">$item->title - ". $item->description ." [flink\">$item->ftitle]\n"; + // Note: $item->description has been validated on aggregation. + $node->body = ''. check_plain($item->title) .' - '. $item->description .' ['. check_plain($item->ftitle) ."]\n"; } } diff --git a/modules/blogapi.module b/modules/blogapi.module index d836c27fd40..3c6172837c3 100644 --- a/modules/blogapi.module +++ b/modules/blogapi.module @@ -530,7 +530,10 @@ function blogapi_menu($may_cache) { $items = array(); if ($_GET['q'] == variable_get('site_frontpage', 'node')) { - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'EditURI', + 'type' => 'application/rsd+xml', + 'title' => t('RSD'), + 'href' => url('blogapi/rsd', NULL, NULL, TRUE))); } if ($may_cache) { diff --git a/modules/blogapi/blogapi.module b/modules/blogapi/blogapi.module index d836c27fd40..3c6172837c3 100644 --- a/modules/blogapi/blogapi.module +++ b/modules/blogapi/blogapi.module @@ -530,7 +530,10 @@ function blogapi_menu($may_cache) { $items = array(); if ($_GET['q'] == variable_get('site_frontpage', 'node')) { - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'EditURI', + 'type' => 'application/rsd+xml', + 'title' => t('RSD'), + 'href' => url('blogapi/rsd', NULL, NULL, TRUE))); } if ($may_cache) { diff --git a/modules/forum.module b/modules/forum.module index f117f6c8098..6bb116511f0 100644 --- a/modules/forum.module +++ b/modules/forum.module @@ -825,7 +825,10 @@ function theme_forum_display($forums, $topics, $parents, $tid, $sortby, $forum_p $output .= theme('forum_list', $forums, $parents, $tid); if ($tid && !in_array($tid, variable_get('forum_containers', array()))) { - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => 'RSS - '. $title, + 'href' => url('taxonomy/term/'. $tid .'/0/feed'))); $output .= theme('forum_topic_list', $tid, $topics, $sortby, $forum_per_page); $output .= theme('xml_icon', url("taxonomy/term/$tid/0/feed")); @@ -858,7 +861,7 @@ function theme_forum_list($forums, $parents, $tid) { $description .= '
'. l($forum->name, "forum/$forum->tid") ."
\n"; if ($forum->description) { - $description .= "
$forum->description
\n"; + $description .= '
'. check_plain($forum->description) ."
\n"; } $description .= "\n"; @@ -877,7 +880,7 @@ function theme_forum_list($forums, $parents, $tid) { $description .= '
'. l($forum->name, "forum/$forum->tid") ."
\n"; if ($forum->description) { - $description .= "
$forum->description
\n"; + $description .= '
'. check_plain($forum->description) ."
\n"; } $description .= "\n"; diff --git a/modules/forum/forum.module b/modules/forum/forum.module index f117f6c8098..6bb116511f0 100644 --- a/modules/forum/forum.module +++ b/modules/forum/forum.module @@ -825,7 +825,10 @@ function theme_forum_display($forums, $topics, $parents, $tid, $sortby, $forum_p $output .= theme('forum_list', $forums, $parents, $tid); if ($tid && !in_array($tid, variable_get('forum_containers', array()))) { - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => 'RSS - '. $title, + 'href' => url('taxonomy/term/'. $tid .'/0/feed'))); $output .= theme('forum_topic_list', $tid, $topics, $sortby, $forum_per_page); $output .= theme('xml_icon', url("taxonomy/term/$tid/0/feed")); @@ -858,7 +861,7 @@ function theme_forum_list($forums, $parents, $tid) { $description .= '
'. l($forum->name, "forum/$forum->tid") ."
\n"; if ($forum->description) { - $description .= "
$forum->description
\n"; + $description .= '
'. check_plain($forum->description) ."
\n"; } $description .= "\n"; @@ -877,7 +880,7 @@ function theme_forum_list($forums, $parents, $tid) { $description .= '
'. l($forum->name, "forum/$forum->tid") ."
\n"; if ($forum->description) { - $description .= "
$forum->description
\n"; + $description .= '
'. check_plain($forum->description) ."
\n"; } $description .= "\n"; diff --git a/modules/node.module b/modules/node.module index d03118ab623..15bbc9b2448 100644 --- a/modules/node.module +++ b/modules/node.module @@ -782,7 +782,7 @@ function node_admin_nodes() { foreach ($edit['nodes'] as $nid => $value) { if ($value) { $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid)); - $extra .= '
  • '. form_hidden('nodes]['. $nid, 1) . $title .'
  • '; + $extra .= '
  • '. form_hidden('nodes]['. $nid, 1) . check_plain($title) .'
  • '; } } $extra .= ''; @@ -1629,7 +1629,10 @@ function node_page_default() { $result = pager_query(db_rewrite_sql('SELECT n.nid, n.sticky, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), variable_get('default_nodes_main', 10)); if (db_num_rows($result)) { - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => t('RSS'), + 'href' => url('node/feed', NULL, NULL, TRUE))); $output = ''; while ($node = db_fetch_object($result)) { diff --git a/modules/node/node.module b/modules/node/node.module index d03118ab623..15bbc9b2448 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -782,7 +782,7 @@ function node_admin_nodes() { foreach ($edit['nodes'] as $nid => $value) { if ($value) { $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $nid)); - $extra .= '
  • '. form_hidden('nodes]['. $nid, 1) . $title .'
  • '; + $extra .= '
  • '. form_hidden('nodes]['. $nid, 1) . check_plain($title) .'
  • '; } } $extra .= ''; @@ -1629,7 +1629,10 @@ function node_page_default() { $result = pager_query(db_rewrite_sql('SELECT n.nid, n.sticky, n.created FROM {node} n WHERE n.promote = 1 AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), variable_get('default_nodes_main', 10)); if (db_num_rows($result)) { - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => t('RSS'), + 'href' => url('node/feed', NULL, NULL, TRUE))); $output = ''; while ($node = db_fetch_object($result)) { diff --git a/modules/poll.module b/modules/poll.module index a9139a2b8af..41214402339 100644 --- a/modules/poll.module +++ b/modules/poll.module @@ -95,11 +95,12 @@ function poll_validate(&$node) { if (isset($node->title)) { // Check for at least two options and validate amount of votes: $realchoices = 0; + // Renumber fields + $node->choice = array_values($node->choice); foreach ($node->choice as $i => $choice) { if ($choice['chtext'] != '') { $realchoices++; } - if ($choice['chvotes'] < 0) { form_set_error("choice][$i][chvotes", t('Negative values are not allowed.')); } diff --git a/modules/poll/poll.module b/modules/poll/poll.module index a9139a2b8af..41214402339 100644 --- a/modules/poll/poll.module +++ b/modules/poll/poll.module @@ -95,11 +95,12 @@ function poll_validate(&$node) { if (isset($node->title)) { // Check for at least two options and validate amount of votes: $realchoices = 0; + // Renumber fields + $node->choice = array_values($node->choice); foreach ($node->choice as $i => $choice) { if ($choice['chtext'] != '') { $realchoices++; } - if ($choice['chvotes'] < 0) { form_set_error("choice][$i][chvotes", t('Negative values are not allowed.')); } diff --git a/modules/statistics.module b/modules/statistics.module index 62db34e55e4..46e521b8021 100644 --- a/modules/statistics.module +++ b/modules/statistics.module @@ -144,11 +144,11 @@ function statistics_access_log($aid) { if ($access = db_fetch_object($result)) { $output = ''; $output .= ' "; - $output .= ' "; + $output .= ' '; $output .= ' "; $output .= ' '; $output .= ' '; - $output .= ' "; + $output .= ' '; $output .= '
    '. t('Page URL') ."". l(url($access->path, NULL, NULL, TRUE), $access->url) ."
    '. t('Page title') ."$access->title
    '. t('Page title') .''. check_plain($access->title) .'
    '. t('Referrer') ."". ($access->url ? l($access->url, $access->url) : '') ."
    '. t('Date') .''. format_date($access->timestamp, 'large') .'
    '. t('User') .''. format_name($access) .'
    '. t('Hostname') ."$access->hostname
    '. t('Hostname') .''. check_plain($access->hostname) .'
    '; return $output; } diff --git a/modules/statistics/statistics.module b/modules/statistics/statistics.module index 62db34e55e4..46e521b8021 100644 --- a/modules/statistics/statistics.module +++ b/modules/statistics/statistics.module @@ -144,11 +144,11 @@ function statistics_access_log($aid) { if ($access = db_fetch_object($result)) { $output = ''; $output .= ' "; - $output .= ' "; + $output .= ' '; $output .= ' "; $output .= ' '; $output .= ' '; - $output .= ' "; + $output .= ' '; $output .= '
    '. t('Page URL') ."". l(url($access->path, NULL, NULL, TRUE), $access->url) ."
    '. t('Page title') ."$access->title
    '. t('Page title') .''. check_plain($access->title) .'
    '. t('Referrer') ."". ($access->url ? l($access->url, $access->url) : '') ."
    '. t('Date') .''. format_date($access->timestamp, 'large') .'
    '. t('User') .''. format_name($access) .'
    '. t('Hostname') ."$access->hostname
    '. t('Hostname') .''. check_plain($access->hostname) .'
    '; return $output; } diff --git a/modules/taxonomy.module b/modules/taxonomy.module index 6d6b7fb479d..0662c0fca52 100644 --- a/modules/taxonomy.module +++ b/modules/taxonomy.module @@ -373,7 +373,7 @@ function taxonomy_overview() { $node_type = node_invoke($type, 'node_name'); $types[] = $node_type ? $node_type : $type; } - $rows[] = array(''.check_plain($vocabulary->name).'', implode(', ', $types), l(t('edit vocabulary'), "admin/taxonomy/edit/vocabulary/$vocabulary->vid"), l(t('add term'), "admin/taxonomy/add/term/$vocabulary->vid"), l(t('view terms'), "admin/taxonomy/$vocabulary->vid")); + $rows[] = array(''. check_plain($vocabulary->name) .'', implode(', ', $types), l(t('edit vocabulary'), "admin/taxonomy/edit/vocabulary/$vocabulary->vid"), l(t('add term'), "admin/taxonomy/add/term/$vocabulary->vid"), l(t('view terms'), "admin/taxonomy/$vocabulary->vid")); // Show terms if non-free. if (!$vocabulary->tags) { @@ -1082,7 +1082,10 @@ function taxonomy_term_page($str_tids = '', $depth = 0, $op = 'page') { $breadcrumbs = array_reverse($breadcrumbs); menu_set_location($breadcrumbs); - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => 'RSS - '. $title, + 'href' => url('taxonomy/term/'. $rss_tids .'/'. $depth .'/feed'))); $output = taxonomy_render_nodes(taxonomy_select_nodes($tids, $operator, $depth, TRUE)); $output .= theme('xml_icon', url("taxonomy/term/$rss_tids/$depth/feed")); diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index 6d6b7fb479d..0662c0fca52 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -373,7 +373,7 @@ function taxonomy_overview() { $node_type = node_invoke($type, 'node_name'); $types[] = $node_type ? $node_type : $type; } - $rows[] = array(''.check_plain($vocabulary->name).'', implode(', ', $types), l(t('edit vocabulary'), "admin/taxonomy/edit/vocabulary/$vocabulary->vid"), l(t('add term'), "admin/taxonomy/add/term/$vocabulary->vid"), l(t('view terms'), "admin/taxonomy/$vocabulary->vid")); + $rows[] = array(''. check_plain($vocabulary->name) .'', implode(', ', $types), l(t('edit vocabulary'), "admin/taxonomy/edit/vocabulary/$vocabulary->vid"), l(t('add term'), "admin/taxonomy/add/term/$vocabulary->vid"), l(t('view terms'), "admin/taxonomy/$vocabulary->vid")); // Show terms if non-free. if (!$vocabulary->tags) { @@ -1082,7 +1082,10 @@ function taxonomy_term_page($str_tids = '', $depth = 0, $op = 'page') { $breadcrumbs = array_reverse($breadcrumbs); menu_set_location($breadcrumbs); - drupal_set_html_head(''); + drupal_add_link(array('rel' => 'alternate', + 'type' => 'application/rss+xml', + 'title' => 'RSS - '. $title, + 'href' => url('taxonomy/term/'. $rss_tids .'/'. $depth .'/feed'))); $output = taxonomy_render_nodes(taxonomy_select_nodes($tids, $operator, $depth, TRUE)); $output .= theme('xml_icon', url("taxonomy/term/$rss_tids/$depth/feed")); diff --git a/modules/throttle.module b/modules/throttle.module index 946e83bd728..a2156924250 100644 --- a/modules/throttle.module +++ b/modules/throttle.module @@ -63,34 +63,42 @@ function throttle_exit() { } // update the throttle status + $message = ''; if ($max_users && $users > $max_users) { if (!$throttle) { variable_set('throttle_level', 1); - cache_clear_all(); - watchdog('throttle', t('Throttle: %users %user accessing site; throttle enabled.', array('%users' => "$users", '%user' => format_plural($users, 'user', 'users')))); + $message = format_plural($users, + '1 user accessing site; throttle enabled.', + '%count users accessing site; throttle enabled.'); } } elseif ($max_guests && $guests > $max_guests) { if (!$throttle) { variable_set('throttle_level', 1); - cache_clear_all(); - watchdog('throttle', t('Throttle: %guests %guest accessing site; throttle enabled.', array('%guests' => "$guests", '%guest' => format_plural($guests, 'guest', 'guests')))); + $message = format_plural($guests, + '1 guest accessing site; throttle enabled.', + '%count guests accessing site; throttle enabled.'); } } else { if ($throttle) { variable_set('throttle_level', 0); - cache_clear_all(); - watchdog('throttle', t('Throttle: %users %user, %guests %guest accessing site; throttle disabled.', array('%users' => "$users", '%user' => format_plural($users, 'user', 'users'), '%guests' => "$guests", '%guest' => format_plural($guests, 'guest', 'guests')))); + // Note: unorthodox format_plural() usage due to Gettext plural limitations. + $message = format_plural($users, '1 user', '%count users') .', '; + $message .= format_plural($guests, '1 guest accessing site; throttle disabled', '%count guests accessing site; throttle disabled'); } } + if ($message) { + cache_clear_all(); + watchdog('throttle', t('Throttle') .': '. $message); + } } } function _throttle_validate($value, $form) { if ($value != NULL) { if (!is_numeric($value) || $value < 0) { - form_set_error($form, t("'%value' is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => $value))); + form_set_error($form, t("'%value' is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => theme('placeholder', $value)))); } } } diff --git a/modules/throttle/throttle.module b/modules/throttle/throttle.module index 946e83bd728..a2156924250 100644 --- a/modules/throttle/throttle.module +++ b/modules/throttle/throttle.module @@ -63,34 +63,42 @@ function throttle_exit() { } // update the throttle status + $message = ''; if ($max_users && $users > $max_users) { if (!$throttle) { variable_set('throttle_level', 1); - cache_clear_all(); - watchdog('throttle', t('Throttle: %users %user accessing site; throttle enabled.', array('%users' => "$users", '%user' => format_plural($users, 'user', 'users')))); + $message = format_plural($users, + '1 user accessing site; throttle enabled.', + '%count users accessing site; throttle enabled.'); } } elseif ($max_guests && $guests > $max_guests) { if (!$throttle) { variable_set('throttle_level', 1); - cache_clear_all(); - watchdog('throttle', t('Throttle: %guests %guest accessing site; throttle enabled.', array('%guests' => "$guests", '%guest' => format_plural($guests, 'guest', 'guests')))); + $message = format_plural($guests, + '1 guest accessing site; throttle enabled.', + '%count guests accessing site; throttle enabled.'); } } else { if ($throttle) { variable_set('throttle_level', 0); - cache_clear_all(); - watchdog('throttle', t('Throttle: %users %user, %guests %guest accessing site; throttle disabled.', array('%users' => "$users", '%user' => format_plural($users, 'user', 'users'), '%guests' => "$guests", '%guest' => format_plural($guests, 'guest', 'guests')))); + // Note: unorthodox format_plural() usage due to Gettext plural limitations. + $message = format_plural($users, '1 user', '%count users') .', '; + $message .= format_plural($guests, '1 guest accessing site; throttle disabled', '%count guests accessing site; throttle disabled'); } } + if ($message) { + cache_clear_all(); + watchdog('throttle', t('Throttle') .': '. $message); + } } } function _throttle_validate($value, $form) { if ($value != NULL) { if (!is_numeric($value) || $value < 0) { - form_set_error($form, t("'%value' is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => $value))); + form_set_error($form, t("'%value' is not a valid auto-throttle setting. Please enter a positive numeric value.", array('%value' => theme('placeholder', $value)))); } } }