Changeset 252

Show
Ignore:
Timestamp:
08/06/06 12:04:18 (2 years ago)
Author:
michiel
Message:

new plugin system.
It's all object oriented now.

I'll write documentation later today.

Location:
trunk
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/common/mvblog.php

    r247 r252  
    9797         */ 
    9898        public $webroot    = ""; 
     99        public $plugman; 
    99100        /* }}} */ 
    100101 
     
    143144                /* populate the categories array */ 
    144145                $this->_get_categories(); 
     146 
     147                /* plugin handling */ 
     148                require_once("plugins.php"); 
     149                $plugman = new MvBlog_pluginmgr("plugins/", $this); 
     150                #$plugman->activate_plugin("acronyms"); 
     151                $plugman->set_active_plugins(array("acronyms")); 
     152                $this->plugman = $plugman; 
    145153        } 
    146154        /* }}} */ 
     
    175183        /* _init_db {{{ */ 
    176184        protected function _init_db() { 
    177                 require("DB.php"); 
    178                 require("hosts.php"); 
     185                require_once("DB.php"); 
     186                require_once("hosts.php"); 
    179187 
    180188                $options = array( 
     
    523531                                                <?php } ?> 
    524532                                                        <?php 
    525                                                         $text = $this->run_plugins($this->strip_invalid_xml(stripslashes($row["body"])), "text_output"); 
     533                                                        $text = $this->strip_invalid_xml(stripslashes($row["body"])); 
     534                                                        $text = $this->plugman->run_hooks("text_output", $text); 
    526535                                                        if ($this->limit_text($text, 4000)) { 
    527536                                                                echo $text; 
     
    12361245        /** 
    12371246         * load all the plugins 
     1247         * obsoleted by MvBlog_pluginmgr 
    12381248         */ 
    12391249        public function load_plugins() { 
    1240                 /* this should become a setting */ 
    1241                 $plugins = array(); 
    1242                 $plugin_dir = "plugins/"; 
    1243                 if (is_dir($plugin_dir)) { 
    1244                         $plug_fd = opendir($plugin_dir); 
    1245                         while (false !== ($fp = readdir($plug_fd))) { 
    1246                                 if (!preg_match("/^\./", $fp)) { 
    1247                                         require_once($plugin_dir.$fp); 
    1248                                 } 
    1249                         } 
    1250                 } 
     1250                return true; 
    12511251        } 
    12521252        /* }}} */ 
     
    12541254        /** 
    12551255         * register the plugin in the global plugin array 
     1256         * obsoleted by MvBlog_pluginmgr 
    12561257         * 
    12571258         * @param string $name the plugin name 
     
    12601261         */ 
    12611262        public function register_plugin($name, $type) { 
    1262                 $this->plugins[$type][] = $name; 
     1263                return true; 
    12631264        } 
    12641265        /* }}} */ 
  • trunk/plugins/acronyms.php

    r83 r252  
    11<?php 
    2 register_plugin("acroreplace", "text_output"); 
     2Class acronyms extends MvBlog_plugin implements MvBlog_pluginiface { 
     3        /* variables */ 
     4        public $name    = "Acronyms"; 
     5        public $author  = "Michiel van Baak"; 
     6        public $license = "GPL"; 
     7        public $website = "http://www.mvblog.org"; 
    38 
    4 function plugin_acroreplace($text) { 
    5         /* get all acronyms from database */ 
    6         $res =& $GLOBALS["db"]->query("SELECT acronym,description FROM acronyms"); 
    7         if (PEAR::isError($res)) { 
    8                 die($res->getMessage()); 
     9        private $_mvblog; 
     10        private $_acronyms; 
     11 
     12        /* methods */ 
     13        public function __construct(&$mvblog) { 
     14                $this->addHook("text_output", "acroreplace"); 
     15                $this->_mvblog =& $mvblog; 
    916        } 
    10         while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { 
    11                 $acronyms[$row["acronym"]] = $row["description"]; 
     17 
     18        public function activate() { 
     19                /* get all acronyms from database */ 
     20                $res =& $this->_mvblog->db->query("SELECT acronym,description FROM acronyms"); 
     21                if (PEAR::isError($res)) { 
     22                        die($res->getMessage()); 
     23                } 
     24                while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) { 
     25                        $this->_acronyms[$row["acronym"]] = $row["description"]; 
     26                } 
    1227        } 
    13         foreach ($acronyms as $acronym => $description) { 
    14                 //replace all the supplied acronyms 
    15                 $text = preg_replace("|(?!<[^<>]*?)(?<![?./&])\b$acronym\b(?!:)(?![^<>]*?>)|imsU","<acronym class=\"blog_acro\" title=\"$description\">$acronym</acronym>" , $text); 
    16                 //allow user to overwrite acronym replacing by putting the term between $ signs 
    17                 $text = preg_replace("|[$]<acronym class=\"blog_acro\" title=\"$description\">$acronym</acronym>[$]|imsU" , "$acronym" , $text); 
     28 
     29        public function deactivate() { 
     30                unset($this->_acronyms); 
    1831        } 
    19         return trim($text); 
     32 
     33        public function acroreplace($text) { 
     34                foreach ($this->_acronyms as $acronym => $description) { 
     35                        //replace all the supplied acronyms 
     36                        $text = preg_replace("|(?!<[^<>]*?)(?<![?./&])\b$acronym\b(?!:)(?![^<>]*?>)|imsU","<acronym class=\"blog_acro\" title=\"$description\">$acronym</acronym>" , $text); 
     37                        //allow user to overwrite acronym replacing by putting the term between $ signs 
     38                        $text = preg_replace("|[$]<acronym class=\"blog_acro\" title=\"$description\">$acronym</acronym>[$]|imsU" , "$acronym" , $text); 
     39                } 
     40                return trim($text); 
     41        } 
    2042} 
    2143?> 
  • trunk/plugins/test.php

    r126 r252  
    11<?php 
    2 /* 
    3  * skeleton plugin. 
    4  */ 
    5 register_plugin("test", "text_output"); 
    6 if (function_exists("register_admin_plugin")) { 
    7         register_admin_plugin("test", "text_output"); 
    8 } 
    9 /* function to run when this plugin kicks in */ 
    10 /* name is plugin_pluginname */ 
    11 function plugin_test($data) { 
    12         $output = str_ireplace("michiel", "The MvBlog Projectleader", $data); 
    13         return $output; 
    14 } 
     2Class test extends MvBlog_plugin implements MvBlog_pluginiface { 
     3        /* variables */ 
     4        public $name    = "test"; 
     5        public $author  = "Michiel van Baak"; 
     6        public $license = "GPL"; 
     7        public $website = "http://www.mvblog.org"; 
    158 
    16 /* admin functions */ 
    17 /* edit, save and overview are required */ 
    18 function plugin_test_edit() { 
    19         /* code to show a screen to edit settings etc */ 
    20 } 
     9        private $_mvblog; 
    2110 
    22 function plugin_test_save() { 
    23         /* code to save changes */ 
    24 } 
     11        /* methods */ 
     12        public function __construct(&$mvblog) { 
     13                $this->addHook("text_output", "test"); 
     14                $this->_mvblog =& $mvblog; 
     15        } 
    2516 
    26 function plugin_test_overview() { 
    27         /* code to show screen with items or description of plugin or whatever */ 
     17        public function activate() { 
     18                /* get data, do whatever is needed on plugin activation */ 
     19        } 
     20 
     21        public function deactivate() { 
     22        } 
     23 
     24        public function test($text) { 
     25                $output = str_ireplace("michiel", "The MvBlog Projectleader", $text); 
     26                $output = str_ireplace("mafkees", "The MvBlog Projectleader", $output); 
     27                return $output; 
     28        } 
    2829} 
    2930?>