OSDN Git Service

MERGE: リビジョン1873〜1893。skinnable-masterのマージ
[nucleus-jp/nucleus-next.git] / nucleus / libs / ACTIONLOG.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2009 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  */
12 /**
13  * Actionlog class for Nucleus
14  *
15  * @license http://nucleuscms.org/license.txt GNU General Public License
16  * @copyright Copyright (C) 2002-2009 The Nucleus Group
17  * @version $Id: ACTIONLOG.php 1822 2012-05-04 13:47:22Z sakamocchi $
18  */
19 define('ERROR',1);              // only errors
20 define('WARNING',2);    // errors and warnings
21 define('INFO',3);               // info, errors and warnings
22 define('DEBUG',4);              // everything
23 $CONF['LogLevel'] = INFO;
24
25 class ActionLog
26 {
27         /**
28          * ActionLog::add()
29          * Method to add a message to the action log
30          * 
31          * @static
32          * @param       Integer $level  log level
33          * @param       String  $message        log message
34          * @return      
35          * 
36          */
37         function add($level, $message)
38         {
39                 global $member, $CONF;
40                 
41                 if ( $CONF['LogLevel'] < $level )
42                 {
43                         return;
44                 }
45                 
46                 if ( $member && $member->isLoggedIn() )
47                 {
48                         $message = "[" . $member->getDisplayName() . "] " . $message;
49                 }
50                 
51                 $query = "INSERT INTO %s (timestamp, message) VALUES (%s, %s)";
52                 $query = sprintf($query, sql_table('actionlog'), DB::formatDateTime(), DB::quoteValue($message));
53                 DB::execute($query);
54                 
55                 self::trimLog();
56                 return;
57         }
58         
59         /**
60           * (Static) Method to clear the whole action log
61           */
62         function clear() {
63                 global $manager;
64
65                 $query = sprintf('DELETE FROM %s', sql_table('actionlog'));
66
67                 $manager->notify('ActionLogCleared',array());
68
69                 return DB::execute($query) !== FALSE;
70         }
71
72         /**
73           * (Static) Method to trim the action log (from over 500 back to 250 entries)
74           */
75         function trimLog() {
76                 static $checked = 0;
77
78                 // only check once per run
79                 if ($checked) return;
80
81                 // trim
82                 $checked = 1;
83
84                 $query = sprintf('SELECT COUNT(*) AS result FROM %s', sql_table('actionlog'));
85                 $iTotal = DB::getValue($query);
86
87                 // if size > 500, drop back to about 250
88                 $iMaxSize = 500;
89                 $iDropSize = 250;
90                 if ($iTotal > $iMaxSize) {
91                         $query = sprintf('SELECT timestamp as result FROM %s ORDER BY timestamp DESC LIMIT %d,1',
92                                 sql_table('actionlog'), intval($iDropSize));
93                         $tsChop = DB::getValue($query);
94                         $query = sprintf("DELETE FROM %s WHERE timestamp < '%s'", sql_table('actionlog'), $tsChop);
95                         DB::execute($query);
96                 }
97
98         }
99
100 }
101
102 ?>