source: trunk/plugins/event_tracer/main.inc.php @ 3275

Last change on this file since 3275 was 3275, checked in by ddtddt, 15 years ago

merge 3274

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
RevLine 
[2302]1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3049]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2342]6// | Copyright(C) 2003-2008 Piwigo team    http://phpwebgallery.net |
[2302]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/*
[1580]25Plugin Name: Event tracer
[3275]26Version: 2.0.2
[1580]27Description: For developers. Shows all calls to trigger_event.
[3275]28Plugin URI: http://piwigo.org/ext/extension_view.php?eid=288
[2342]29Author: Piwigo team
30Author URI: http://piwigo.org
[1580]31*/
[2302]32
[1580]33if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
34
35class EventTracer
36{
37  var $my_config;
[2773]38  var $trigger_counts = array();
[2497]39
[1731]40  function EventTracer()
[1580]41  {
42  }
43
[2254]44  function get_config_file_dir()
45  {
46    global $conf;
47    return $conf['local_data_dir'].'/plugins/';
48  }
49
50  function get_config_file_name()
51  {
52    return basename(dirname(__FILE__)).'.dat';
53  }
54
[1580]55  function load_config()
56  {
[2254]57    $x = @file_get_contents( $this->get_config_file_dir().$this->get_config_file_name() );
[1580]58    if ($x!==false)
59    {
60      $c = unserialize($x);
61      // do some more tests here
62      $this->my_config = $c;
63    }
64    if ( !isset($this->my_config)
65        or empty($this->my_config['filters']) )
66    {
67      $this->my_config['filters'] = array( '.*' );
68      $this->my_config['show_args'] = false;
[2773]69      $this->my_config['show_registered'] = true;
[1580]70      $this->save_config();
71    }
72  }
73
74  function save_config()
75  {
[2254]76    $dir = $this->get_config_file_dir();
[2497]77    @mkgetdir($dir);
[2254]78    $file = fopen( $dir.$this->get_config_file_name(), 'w' );
[1580]79    fwrite($file, serialize($this->my_config) );
80    fclose( $file );
81  }
82
[2773]83  function on_page_tail()
84  {
85    if (1 || @$this->my_config['show_registered'])
86    {
87      global $debug, $pwg_event_handlers;
88      $out = '';
89      foreach ($pwg_event_handlers as $event => $prio_array)
90      {
91        $out .= $event.' '.intval(@$this->trigger_counts[$event])." calls\n";
92        foreach ($prio_array as $prio => $handlers)
93        {
94          foreach ($handlers as $handler)
95          {
96            $out .= "\t$prio ";
97            if ( is_array($handler['function']) )
98            {
99              if ( is_string($handler['function'][0]) )
100                $out .= $handler['function'][0].'::';
101              else
102                $out .= @get_class($handler['function'][0]).'->';
103              $out .= $handler['function'][1];
104            }
105            else
106             $out .= $handler['function'];
107            $out .= "\n";
108          }
109        }
110        $out .= "\n";
111      }
112      $debug .= '<pre>'.$out.'</pre>';
113    }
114  }
115
[3136]116  function on_trigger($event_info)
[1590]117  {
[3136]118    if ($event_info['type']!='post_event')
119      @$this->trigger_counts[$event_info['event']]++;
120
[1590]121    foreach( $this->my_config['filters'] as $filter)
[1580]122    {
[1590]123      if ( preg_match( '/'.$filter.'/', $event_info['event'] ) )
[1580]124      {
[1590]125        if ($this->my_config['show_args'])
[1580]126        {
[1590]127          $s = '<pre>';
128          $s .= htmlspecialchars( var_export( $event_info['data'], true ) );
129          $s .= '</pre>';
[1580]130        }
[1590]131        else
132          $s = '';
[3136]133        pwg_debug($event_info['type'].' "'.$event_info['event'].'" '.($this->trigger_counts[$event_info['event']]).' calls '.($s) );
[1590]134        break;
[1580]135      }
136    }
137  }
138
[1705]139  function plugin_admin_menu($menu)
[1580]140  {
[1705]141    array_push($menu,
142        array(
143          'NAME' => 'Event Tracer',
[1731]144          'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/tracer_admin.php')
[1705]145        )
146      );
147    return $menu;
[1580]148  }
149}
150
[1731]151$obj = new EventTracer();
[1705]152$obj->load_config();
[1580]153
[1705]154add_event_handler('get_admin_plugin_menu_links', array(&$obj, 'plugin_admin_menu') );
[2773]155add_event_handler('loc_begin_page_tail', array(&$obj, 'on_page_tail') );
[3136]156add_event_handler('trigger', array(&$obj, 'on_trigger') );
[1705]157set_plugin_data($plugin['id'], $obj);
[2254]158?>
Note: See TracBrowser for help on using the repository browser.