OSDN Git Service

MERGE: リビジョン1991。Adminクラスにおける<%skinfile%>の引数を修正
[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                 $data = array();
68                 $manager->notify('ActionLogCleared', $data);
69
70                 return DB::execute($query) !== FALSE;
71         }
72
73         /**
74           * (Static) Method to trim the action log (from over 500 back to 250 entries)
75           */
76         function trimLog() {
77                 static $checked = 0;
78
79                 // only check once per run
80                 if ($checked) return;
81
82                 // trim
83                 $checked = 1;
84
85                 $query = sprintf('SELECT COUNT(*) AS result FROM %s', sql_table('actionlog'));
86                 $iTotal = DB::getValue($query);
87
88                 // if size > 500, drop back to about 250
89                 $iMaxSize = 500;
90                 $iDropSize = 250;
91                 if ($iTotal > $iMaxSize) {
92                         $query = sprintf('SELECT timestamp as result FROM %s ORDER BY timestamp DESC LIMIT %d,1',
93                                 sql_table('actionlog'), intval($iDropSize));
94                         $tsChop = DB::getValue($query);
95                         $query = sprintf("DELETE FROM %s WHERE timestamp < '%s'", sql_table('actionlog'), $tsChop);
96                         DB::execute($query);
97                 }
98
99         }
100
101 }
102
103 ?>