diff options
-rw-r--r-- | Zotlabs/Extend/Hook.php | 76 | ||||
-rwxr-xr-x | boot.php | 4 | ||||
-rwxr-xr-x | include/plugin.php | 26 | ||||
-rw-r--r-- | install/schema_mysql.sql | 4 | ||||
-rw-r--r-- | install/schema_postgres.sql | 2 | ||||
-rw-r--r-- | install/update.php | 15 |
6 files changed, 110 insertions, 17 deletions
diff --git a/Zotlabs/Extend/Hook.php b/Zotlabs/Extend/Hook.php new file mode 100644 index 000000000..836b29db8 --- /dev/null +++ b/Zotlabs/Extend/Hook.php @@ -0,0 +1,76 @@ +<?php + +namespace Zotlabs\Extend; + + +class Hook { + + static public function register($hook,$file,$function,$version = 1,$priority = 0) { + $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' and priority = %d and hook_version = %d LIMIT 1", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + if($r) + return true; + + $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`, `hook_version`) VALUES ( '%s', '%s', '%s', %d, %d )", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + + return $r; + } + + static public function unregister($hook,$file,$function,$version = 1,$priority = 0) { + $r = q("DELETE FROM hook WHERE hook = '%s' AND `file` = '%s' AND `function` = '%s' and priority = %d and hook_version = %d", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + + return $r; + } + + + /** + * @brief Inserts a hook into a page request. + * + * Insert a short-lived hook into the running page request. + * Hooks are normally persistent so that they can be called + * across asynchronous processes such as delivery and poll + * processes. + * + * insert_hook lets you attach a hook callback immediately + * which will not persist beyond the life of this page request + * or the current process. + * + * @param string $hook + * name of hook to attach callback + * @param string $fn + * function name of callback handler + * @param int $version + * hook interface version, 0 uses two callback params, 1 uses one callback param + * @param int $priority + * currently not implemented in this function, would require the hook array to be resorted + */ + + static public function insert($hook, $fn, $version = 0, $priority = 0) { + + if(! is_array(App::$hooks)) + App::$hooks = array(); + + if(! array_key_exists($hook, App::$hooks)) + App::$hooks[$hook] = array(); + + App::$hooks[$hook][] = array('', $fn, $priority, $version); + } + +}
\ No newline at end of file @@ -47,10 +47,10 @@ require_once('include/account.php'); define ( 'PLATFORM_NAME', 'hubzilla' ); define ( 'RED_VERSION', trim(file_get_contents('version.inc'))); -define ( 'STD_VERSION', '1.4.1' ); +define ( 'STD_VERSION', '1.4.2' ); define ( 'ZOT_REVISION', 1 ); -define ( 'DB_UPDATE_VERSION', 1165 ); +define ( 'DB_UPDATE_VERSION', 1166 ); /** diff --git a/include/plugin.php b/include/plugin.php index 8dceb8fb1..29f987e78 100755 --- a/include/plugin.php +++ b/include/plugin.php @@ -185,7 +185,7 @@ function register_hook($hook, $file, $function, $priority = 0) { dbesc($file), dbesc($function) ); - if(count($r)) + if($r) return true; $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`) VALUES ( '%s', '%s', '%s', '%s' )", @@ -226,9 +226,8 @@ function unregister_hook($hook, $file, $function) { function load_hooks() { - $a = get_app(); -// if(! is_array(App::$hooks)) - App::$hooks = array(); + + App::$hooks = array(); $r = q("SELECT * FROM hook WHERE true ORDER BY priority DESC"); if($r) { @@ -236,10 +235,10 @@ function load_hooks() { if(! array_key_exists($rr['hook'],App::$hooks)) App::$hooks[$rr['hook']] = array(); - App::$hooks[$rr['hook']][] = array($rr['file'],$rr['function']); + App::$hooks[$rr['hook']][] = array($rr['file'],$rr['function'],$rr['priority'],$rr['hook_version']); } } -//logger('hooks: ' . print_r(App::$hooks,true)); + //logger('hooks: ' . print_r(App::$hooks,true)); } /** @@ -259,15 +258,15 @@ function load_hooks() { * @param string $fn * function name of callback handler */ -function insert_hook($hook, $fn) { - $a = get_app(); +function insert_hook($hook, $fn, $version = 0, $priority = 0) { + if(! is_array(App::$hooks)) App::$hooks = array(); if(! array_key_exists($hook, App::$hooks)) App::$hooks[$hook] = array(); - App::$hooks[$hook][] = array('', $fn); + App::$hooks[$hook][] = array('', $fn, $priority, $version); } /** @@ -280,8 +279,7 @@ function insert_hook($hook, $fn) { * @param string|array &$data to transmit to the callback handler */ function call_hooks($name, &$data = null) { - $a = get_app(); - + $a = 0; if((is_array(App::$hooks)) && (array_key_exists($name, App::$hooks))) { foreach(App::$hooks[$name] as $hook) { if($hook[0]) @@ -289,9 +287,11 @@ function call_hooks($name, &$data = null) { if(function_exists($hook[1])) { $func = $hook[1]; - $func($a, $data); + if($hook[3]) + $func($data); + else + $func($a, $data); } else { - q("DELETE FROM hook WHERE hook = '%s' AND file = '%s' AND function = '%s'", dbesc($name), dbesc($hook[0]), diff --git a/install/schema_mysql.sql b/install/schema_mysql.sql index 01cf97674..c36bfaa57 100644 --- a/install/schema_mysql.sql +++ b/install/schema_mysql.sql @@ -517,8 +517,10 @@ CREATE TABLE IF NOT EXISTS `hook` ( `file` char(255) NOT NULL DEFAULT '', `function` char(255) NOT NULL DEFAULT '', `priority` int(11) unsigned NOT NULL DEFAULT '0', + `hook_version` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), - KEY `hook` (`hook`) + KEY `hook` (`hook`), + KEY `hook_version` (`hook_version`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `hubloc` ( diff --git a/install/schema_postgres.sql b/install/schema_postgres.sql index a7cd5875c..d4bb54b1e 100644 --- a/install/schema_postgres.sql +++ b/install/schema_postgres.sql @@ -512,10 +512,12 @@ CREATE TABLE "hook" ( "file" text NOT NULL, "function" text NOT NULL, "priority" bigint NOT NULL DEFAULT '0', + "hook_version" smallint NOT NULL DEFAULT '0', PRIMARY KEY ("id") ); create index "hook_idx" on hook ("hook"); +create index "hook_version_idx" on hook ("hook_version"); CREATE TABLE "hubloc" ( "hubloc_id" serial NOT NULL, "hubloc_guid" text NOT NULL DEFAULT '', diff --git a/install/update.php b/install/update.php index bfd01494f..2dc4a6db3 100644 --- a/install/update.php +++ b/install/update.php @@ -1,6 +1,6 @@ <?php -define( 'UPDATE_VERSION' , 1165 ); +define( 'UPDATE_VERSION' , 1166 ); /** * @@ -2058,3 +2058,16 @@ function update_r1164() { return UPDATE_FAILED; } +function update_r1165() { + + $r1 = q("alter table hook add hook_version int not null default '0' "); + + if(ACTIVE_DBTYPE == DBTYPE_POSTGRES) + $r2 = q("create index \"hook_version_idx\" on hook (\"hook_version\") "); + else + $r2 = q("alter table hook add index ( hook_version ) "); + if($r1 && $r2) + return UPDATE_SUCCESS; + return UPDATE_FAILED; +} + |