The new tutorial "
How to update tables to follow XOOPS' new naming scheme" is good. thank you Mamba.
I just have some opinions to make it complete and more standard and understandable.
you suggest such a name for the update file:
$modversion['onUpdate'] = 'include/update_function.php';
i prefer to change it to:
$modversion['onUpdate'] = 'include/module.php';
because:
while the name of this file can be anything but it is not used just for update. you can use it for onUninstall and onInstall too. so it would be good to standardize it in all modules in one file, i suggest include/module.php
IMO it would be more understandable and will do the job with one file instead of several files for each update/install/uninstall
Also there is one small issue in your way in using update function.
every time the user click on update your script will execute and the user will see nothing but this. for example in news:
xoops_module_update_news executed successfully.
but he should just see it in the first time. and if the module is the recent one the module developer should state that.
also you can explain the naming structure of the fucntion more.
e.g.:
$func['onUpdate'] = 'xoops_module_update_' . $dirname . (&$module, $oldversion = null)
$func['onInstall'] = 'xoops_module_install_' . $dirname . (&$module)
$func['onUninstall'] = 'xoops_module_uninstall_' . $dirname . (&$module)
$dirname: is your module dirname.
$module: is the handler for your module. so you have all your module information ready in the function.
$oldversion: is the old version in user database. it is very useful when you want to upgrade your module and need to know what is the user version of the module in database. It will get the old version in the installed old module in user database and you will choose what to do next.
see an example from newbb as an historical module:
function xoops_module_update_newbb(&$module, $oldversion = null)
{
load_functions("config");
mod_clearConfg($module->getVar("dirname", "n"));
$newbbConfig = newbb_load_config();
//$oldversion = $module->getVar('version');
//$oldconfig = $module->getVar('hasconfig');
// NewBB 1.0 -- no config
//if (empty($oldconfig)) {
if ($oldversion == 100) {
include_once __DIR__ . "/module.v100.php";
xoops_module_update_newbb_v100($module);
}
// NewBB 2.* and CBB 1.*
// change group permission name
// change forum moderators
if ($oldversion < 220) {
include_once __DIR__ . "/module.v220.php";
xoops_module_update_newbb_v220($module);
}
if ($oldversion < 230) {
$GLOBALS['xoopsDB']->queryFromFile(XOOPS_ROOT_PATH."/modules/".$module->getVar("dirname", "n")."/sql/upgrade_230.sql");
//$module->setErrors("bb_moderates table inserted");
}
if ($oldversion < 304) {
$GLOBALS['xoopsDB']->queryFromFile(XOOPS_ROOT_PATH."/modules/".$module->getVar("dirname", "n")."/sql/mysql.304.sql");
}
if ($oldversion < 400) {
$GLOBALS['xoopsDB']->queryFromFile(XOOPS_ROOT_PATH."/modules/".$module->getVar("dirname", "n")."/sql/mysql.400.sql");
include __DIR__ . "/module.v400.php";
xoops_module_update_newbb_v400($module);
}
if ($oldversion < 403) {
$sql = " ALTER TABLE ".$GLOBALS['xoopsDB']->prefix("bb_posts").
" CHANGE `poster_ip` `poster_ip` varchar(15) NOT NULL default '0.0.0.0'";
$GLOBALS['xoopsDB']->queryF( $sql );
}
if ($oldversion < 431) {
$GLOBALS['xoopsDB']->queryFromFile(XOOPS_ROOT_PATH."/modules/".$module->getVar("dirname", "n")."/sql/mysql.430.sql");
}
also there are 3 less important functions for developers just for your information:
xoops_module_pre_update_
xoops_module_pre_install_
xoops_module_pre_uninstall_
which run before the main functions.
anyway back to the top, to solve the issue when user try to update a module several times. you can write the function like this:
function xoops_module_update_animal(&$module, $oldversion = null) {
if ($oldversion == round( $module->getInfo( "version" ) * 100, 2 )) {
echo "You have the latest " . $module->getInfo("name") . " module (". $module->getInfo("dirname") . $module->getInfo("version") . ") and update is not neccasary";
return true;
}
It would be a MUST if you have important updates in your module that should run just one time or updates have performance impacts. e.g.: alter database with queries.
The above can be copied/pasted to any module in the head of its update function.
Also it show the usage of $module and $oldversion to module developers.
side note about version:
as you can see xoops core can just accept 2 decimal points to play such a games i described above with versions.
so the above for news 1.68 and news 1.687 and news 1.688 and ... would be the same eg: if ($oldversion == 168)
so module developer should care:
IF your module need an update you must at least change the second decimal point of version.
e.g.:
news 1.67 to news 1.68 => update will be run
news 1.68 => news 1.683 => no update
I hope the above be a complete tutorial for all update/install/uninstall functions.
also i hope you understand me