Index: modules/statistics/statistics.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.module,v
retrieving revision 1.340
diff -u -p -r1.340 statistics.module
--- modules/statistics/statistics.module	14 Nov 2010 21:04:45 -0000	1.340
+++ modules/statistics/statistics.module	14 Feb 2011 06:38:35 -0000
@@ -296,11 +296,13 @@ function statistics_title_list($dbfield,
  *   - timestamp: timestamp of when that node was last viewed.
  */
 function statistics_get($nid) {
+  $statistics = &drupal_static(__FUNCTION__, array());
 
-  if ($nid > 0) {
-    // Retrieve an array with both totalcount and daycount.
-    return db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchAssoc();
+  if (!isset($statistics[$nid])) {
+    $statistics[$nid] = db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchAssoc();
   }
+
+  return $statistics[$nid];
 }
 
 /**
Index: modules/statistics/statistics.tokens.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.tokens.inc,v
retrieving revision 1.4
diff -u -p -r1.4 statistics.tokens.inc
--- modules/statistics/statistics.tokens.inc	20 Apr 2010 09:48:06 -0000	1.4
+++ modules/statistics/statistics.tokens.inc	14 Feb 2011 06:38:35 -0000
@@ -33,30 +33,35 @@ function statistics_token_info() {
  * Implements hook_tokens().
  */
 function statistics_tokens($type, $tokens, array $data = array(), array $options = array()) {
-  $url_options = array('absolute' => TRUE);
+  $language_code = isset($options['language']->language) ? $options['language']->language : NULL;
   $replacements = array();
 
   if ($type == 'node' & !empty($data['node'])) {
     $node = $data['node'];
 
     foreach ($tokens as $name => $original) {
-      if ($name == 'total-count') {
-        $statistics = statistics_get($node->nid);
-        $replacements[$original] = $statistics['totalcount'];
-      }
-      elseif ($name == 'day-count') {
-        $statistics = statistics_get($node->nid);
-        $replacements[$original] = $statistics['daycount'];
-      }
-      elseif ($name == 'last-view') {
-        $statistics = statistics_get($node->nid);
-        $replacements[$original] = format_date($statistics['timestamp']);
+      switch ($name) {
+        case 'total-count':
+          $statistics = statistics_get($node->nid);
+          $replacements[$original] = $statistics ? $statistics['totalcount'] : 0;
+          break;
+        case 'day-count':
+          $statistics = statistics_get($node->nid);
+          $replacements[$original] = $statistics ? $statistics['daycount'] : 0;
+          break;
+        case 'last-view':
+          $statistics = statistics_get($node->nid);
+          if (!empty($statistics['timestamp'])) {
+            $replacements[$original] = format_date($statistics['timestamp'], 'medium', '', NULL, $language_code);
+          }
+          break;
       }
     }
 
-    if ($created_tokens = token_find_with_prefix($tokens, 'last-view')) {
-      $statistics = statistics_get($node->nid);
-      $replacements += token_generate('date', $created_tokens, array('date' => $statistics['timestamp']), $options);
+    if (($last_view_tokens = token_find_with_prefix($tokens, 'last-view')) && $statistics = statistics_get($node->nid)) {
+      if (!empty($statistics['timestamp'])) {
+        $replacements += token_generate('date', $last_view_tokens, array('date' => $statistics['timestamp']), $options);
+      }
     }
   }
 
Index: modules/statistics/statistics.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.test,v
retrieving revision 1.25
diff -u -p -r1.25 statistics.test
--- modules/statistics/statistics.test	4 Feb 2011 18:42:22 -0000	1.25
+++ modules/statistics/statistics.test	14 Feb 2011 06:38:35 -0000
@@ -418,12 +418,8 @@ class StatisticsTokenReplaceTestCase ext
   function testStatisticsTokenReplacement() {
     global $language;
 
-    // Create user and node.
-    $user = $this->drupalCreateUser(array('create page content'));
-    $this->drupalLogin($user);
-    $node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $user->uid));
-
-    // Hit the node.
+    // Create and visit a node.
+    $node = $this->drupalCreateNode();
     $this->drupalGet('node/' . $node->nid);
     $statistics = statistics_get($node->nid);
 
@@ -441,5 +437,20 @@ class StatisticsTokenReplaceTestCase ext
       $output = token_replace($input, array('node' => $node), array('language' => $language));
       $this->assertFalse(strcmp($output, $expected), t('Statistics token %token replaced.', array('%token' => $input)));
     }
+
+    // Create a new node but do not visit it.
+    $unvisited_node = $this->drupalCreateNode();
+
+    // Generate and test tokens for the node that has not yet been viewed.
+    $tests = array();
+    $tests['[node:total-count]'] = 0;
+    $tests['[node:day-count]'] = 0;
+    $tests['[node:last-view]'] = '';
+    $tests['[node:last-view:short]'] = '';
+
+    foreach ($tests as $input => $expected) {
+      $output = token_replace($input, array('node' => $unvisited_node), array('language' => $language, 'clear' => TRUE));
+      $this->assertFalse(strcmp($output, $expected), t('Statistics token %token replaced.', array('%token' => $input)));
+    }
   }
 }
