source: extensions/event_tracer/main.inc.php

Last change on this file was 32720, checked in by ddtddt, 2 years ago

[event_tracer] update for piwigo 12

  • Property svn:eol-style set to LF
File size: 5.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 Piwigo team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/*
25Plugin Name: Event tracer
26Version: auto
27Description: For developers. Shows all calls to trigger_change.
28Plugin URI: http://piwigo.org/ext/extension_view.php?eid=288
29Author: Piwigo team
30Author URI: http://piwigo.org
31Has Settings: true
32*/
33
34if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
35
36class EventTracer
37{
38  var $my_config;
39  var $trigger_counts = array();
40
41  function EventTracer()
42  {
43  }
44
45  function get_config_file_dir()
46  {
47    global $conf;
48    return PHPWG_ROOT_PATH.$conf['data_location'].'plugins/';
49  }
50
51  function get_config_file_name()
52  {
53    return basename(dirname(__FILE__)).'.dat';
54  }
55
56  function load_config()
57  {
58    $x = @file_get_contents( $this->get_config_file_dir().$this->get_config_file_name() );
59    if ($x!==false)
60    {
61      $c = unserialize($x);
62      // do some more tests here
63      $this->my_config = $c;
64    }
65    if ( !isset($this->my_config)
66        or empty($this->my_config['filters']) )
67    {
68      $this->my_config['filters'] = array( '.*' );
69      $this->my_config['show_registered'] = true;
70      $this->save_config();
71    }
72  }
73
74  function save_config()
75  {
76    $dir = $this->get_config_file_dir();
77    @mkgetdir($dir);
78    $file = fopen( $dir.$this->get_config_file_name(), 'w' );
79    fwrite($file, serialize($this->my_config) );
80    fclose( $file );
81  }
82
83  function on_page_tail()
84  {
85    global $debug;
86    if (@$this->my_config['show_registered'])
87    {
88      global $pwg_event_handlers;
89      $out = '';
90      foreach ($pwg_event_handlers as $event => $prio_array)
91      {
92        $out .= $event.' '.intval(@$this->trigger_counts[$event])." calls\n";
93        foreach ($prio_array as $prio => $handlers)
94        {
95          foreach ($handlers as $handler)
96          {
97            $out .= "\t$prio ";
98            if ( is_array($handler['function']) )
99            {
100              if ( is_string($handler['function'][0]) )
101                $out .= $handler['function'][0].'::';
102              else
103                $out .= @get_class($handler['function'][0]).'->';
104              $out .= $handler['function'][1];
105            }
106            else
107            {
108              if ( !is_scalar($handler['function']) && get_class($handler['function'])=='Closure')
109                $out .= 'Closure';
110              else
111                $out .= $handler['function'];
112            }
113            $out .= "\n";
114          }
115        }
116        $out .= "\n";
117      }
118      $debug .= '<pre>'.$out.'</pre>';
119    }
120    if (@$this->my_config['show_included_files'])
121    {
122      $debug .= "<pre><em>Included files</em>\n".var_export( get_included_files(), true ).'</pre>';
123    }
124  }
125
126  function on_trigger($event_info)
127  {
128    if ($event_info['type']!='post_event')
129      @$this->trigger_counts[$event_info['event']]++;
130
131    foreach( $this->my_config['filters'] as $filter)
132    {
133      if ( preg_match( '/'.$filter.'/', $event_info['event'] ) )
134      {
135        if (@$this->my_config['show_args'])
136        {
137          $s = '<pre>';
138          $s .= htmlspecialchars( var_export( $event_info['data'], true ) );
139          $s .= '</pre>';
140        }
141        else
142          $s = '';
143        pwg_debug($event_info['type'].' "'.$event_info['event'].'" '.($this->trigger_counts[$event_info['event']]).' calls '.($s) );
144        break;
145      }
146    }
147  }
148}
149
150$obj = new EventTracer();
151$obj->load_config();
152
153
154add_event_handler('loc_begin_page_tail', array(&$obj, 'on_page_tail') );
155add_event_handler('trigger', array(&$obj, 'on_trigger') );
156set_plugin_data($plugin['id'], $obj);
157?>
Note: See TracBrowser for help on using the repository browser.