OSDN Git Service

PKWK_READONLY prohibits editing
[pukiwiki/pukiwiki.git] / plugin / counter.inc.php
index 702a7b1..e4febe4 100644 (file)
@@ -1,42 +1,30 @@
 <?php
 /*
- * PukiWiki ¥«¥¦¥ó¥¿¡¼¥×¥é¥°¥¤¥ó
+ * PukiWiki counter plugin
+ * $Id: counter.inc.php,v 1.15 2004/12/04 06:54:34 henoheno Exp $
  *
- * CopyRight 2002 Y.MASUI GPL2
- * http://masui.net/pukiwiki/ masui@masui.net
- *
- * $Id: counter.inc.php,v 1.14 2004/07/31 03:09:20 henoheno Exp $
+ * (C) 2004 PukiWiki Developer Team
+ * (C) 2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
  */
 
-// counter file
-if (!defined('COUNTER_EXT'))
-{
-       define('COUNTER_EXT','.count');
-}
+// Counter file's suffix
+define('PLUGIN_COUNTER_SUFFIX', '.count');
 
 function plugin_counter_inline()
 {
        global $vars;
 
-       $arg = '';
-       if (func_num_args() > 0)
-       {
-               $args = func_get_args();
-               $arg = strtolower($args[0]);
-       }
-
-       $counter = plugin_counter_get_count($vars['page']);
-
-       switch ($arg)
-       {
-               case 'today':
-               case 'yesterday':
-                       $count = $counter[$arg];
-                       break;
-               default:
-                       $count = $counter['total'];
+       $arg = strtolower(array_shift(func_get_args()));
+       switch ($arg) {
+       case ''     : $arg = 'total'; /*FALLTHROUGH*/
+       case 'total': /*FALLTHROUGH*/
+       case 'today': /*FALLTHROUGH*/
+       case 'yesterday':
+               $counter = plugin_counter_get_count($vars['page']);
+               return $counter[$arg];
+       default:
+               return '&counter([total|today|yesterday]);';
        }
-       return $count;
 }
 
 function plugin_counter_convert()
@@ -44,11 +32,10 @@ function plugin_counter_convert()
        global $vars;
 
        $counter = plugin_counter_get_count($vars['page']);
-
        return <<<EOD
 <div class="counter">
-Counter: {$counter['total']},
-today: {$counter['today']},
+Counter:   {$counter['total']},
+today:     {$counter['today']},
 yesterday: {$counter['yesterday']}
 </div>
 EOD;
@@ -60,79 +47,57 @@ function plugin_counter_get_count($page)
        static $counters = array();
        static $default;
 
-       // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃÍ
-       if (!isset($default))
-       {
+       if (! isset($default))
                $default = array(
                        'total'     => 0,
                        'date'      => get_date('Y/m/d'),
                        'today'     => 0,
                        'yesterday' => 0,
-                       'ip'        => ''
-               );
-       }
-       if (!is_page($page))
-       {
-               return $default;
-       }
-       if (array_key_exists($page,$counters))
-       {
-               return $counters[$page];
-       }
+                       'ip'        => '');
+
+       if (! is_page($page)) return $default;
+       if (isset($counters[$page])) return $counters[$page];
 
-       // ¥«¥¦¥ó¥¿¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤò¥»¥Ã¥È
+       // Set default
        $counters[$page] = $default;
+       $modify = FALSE;
 
-       // ¥«¥¦¥ó¥¿¥Õ¥¡¥¤¥ë¤¬Â¸ºß¤¹¤ë¾ì¹ç¤ÏÆɤ߹þ¤à
-       $file = COUNTER_DIR.encode($page).COUNTER_EXT;
+       $file = COUNTER_DIR . encode($page) . PLUGIN_COUNTER_SUFFIX;
        $fp = fopen($file, file_exists($file) ? 'r+' : 'w+')
-               or die_message('counter.inc.php:cannot open '.$file);
+               or die_message('counter.inc.php: Cannot open ' . $file);
        set_file_buffer($fp, 0);
-       flock($fp,LOCK_EX);
+       flock($fp, LOCK_EX);
        rewind($fp);
-
-       foreach ($default as $key=>$val)
-       {
-               $counters[$page][$key] = rtrim(fgets($fp,256));
-               if (feof($fp)) { break; }
+       foreach ($default as $key=>$val) {
+               // Update
+               $counters[$page][$key] = rtrim(fgets($fp, 256));
+               if (feof($fp)) break;
        }
-       // ¥Õ¥¡¥¤¥ë¹¹¿·¤¬É¬Íפ«?
-       $modify = FALSE;
-
-       // ÆüÉÕ¤¬ÊѤï¤Ã¤¿
-       if ($counters[$page]['date'] != $default['date'])
-       {
+       if ($counters[$page]['date'] != $default['date']) {
+               // New day
                $modify = TRUE;
-               $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d',strtotime('yesterday',UTIME)));
+               $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d', strtotime('yesterday', UTIME)));
                $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
                $counters[$page]['date']      = $default['date'];
                $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
                $counters[$page]['today']     = 1;
                $counters[$page]['total']++;
-       }
-       // IP¥¢¥É¥ì¥¹¤¬°Û¤Ê¤ë
-       else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR'])
-       {
+
+       } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
+               // Not the same host
                $modify = TRUE;
                $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
                $counters[$page]['today']++;
                $counters[$page]['total']++;
        }
-
-       //¥Ú¡¼¥¸Æɤ߽Ф·»þ¤Î¤ß¥Õ¥¡¥¤¥ë¤ò¹¹¿·
-       if ($modify and $vars['cmd'] == 'read')
-       {
-               // ¥Õ¥¡¥¤¥ë¤ò´Ý¤á¤ë
+       // Modify
+       if ($modify && $vars['cmd'] == 'read') {
                rewind($fp);
-               ftruncate($fp,0);
-               // ½ñ¤­½Ð¤¹
+               ftruncate($fp, 0);
                foreach (array_keys($default) as $key)
-               {
-                       fputs($fp,$counters[$page][$key]."\n");
-               }
+                       fputs($fp, $counters[$page][$key] . "\n");
        }
-       // ¥Õ¥¡¥¤¥ë¤òÊĤ¸¤ë
-       flock($fp,LOCK_UN);
+       flock($fp, LOCK_UN);
        fclose($fp);
 
        return $counters[$page];