1
redheadedrod
Preloads, what are they and how can I use them?

Could someone explain to me what preloads are and how do we use them?

I never did quite catch the grasp of what they are. I noticed in the Database connector that my initial 5 minute inspection of the file showed that the logger variable no longer appeared to be addressed and instead there was a preload reference.

I can duplicate what the 2.6 database file had I think pretty seamlessly but I am trying to understand what a preload is and what it is used for.

I understand partially the concept with protector using preloads but I am thinking those are different. My understanding of protector is that it would be loaded prior to some code so it could reside in memory then it could actually access some items before they were passed to the rest of the code. But I never quite understood what the preloads were in the core or how they worked.

Thanks to anyone who can help me understand them better. They are much more used with 2.6 from what my initial impression is.
Attending College working towards Bachelors in Software Engineering and Network Security.

2
noo-b
Re: Preloads, what are they and how can I use them?
  • 2013/3/11 10:52

  • noo-b

  • Just can't stay away

  • Posts: 456

  • Since: 2007/10/23


I Love Xoops

3
Mamba
Re: Preloads, what are they and how can I use them?
  • 2013/3/11 10:57

  • Mamba

  • Moderator

  • Posts: 11409

  • Since: 2004/4/23


Support XOOPS => DONATE
Use 2.5.11 | Docs | Modules | Bugs

4
irmtfan
Re: Preloads, what are they and how can I use them?
  • 2013/3/11 11:33

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


actually they are simpler than you can expect.
the concept of having preloads is "Loading some functions prior to loading the module itself"

These functions can be load in some defined places in Core.

The most current defined places are:
/xoops255/class/database/databasefactory.php (1 hit)
    
Line 60:                 $xoopsPreload->triggerEvent('core.class.database.databasefactory.connection', array(&$class));
  /
xoops255/class/smarty/xoops_plugins/function.xoInboxCount.php (1 hit)
    
Line 17:         $xoopsPreload->triggerEvent('core.class.smarty.xoops_plugins.xoinboxcount', array($pm_handler));
  /
xoops255/class/theme_blocks.php (1 hit)
    
Line 119:         $xoopsPreload->triggerEvent('core.class.theme_blocks.retrieveBlocks', array(&$this, &$template, &$block_arr));
  /
xoops255/class/xoopsform/formdhtmltextarea.php (1 hit)
    
Line 202:         $xoopsPreload->triggerEvent('core.class.xoopsform.formdhtmltextarea.codeicon', array(&$code));
  /
xoops255/edituser.php (1 hit)
    
Line 22$xoopsPreload->triggerEvent('core.edituser.start');
  /
xoops255/footer.php (2 hits)
    
Line 22$xoopsPreload->triggerEvent('core.footer.start');
    
Line 72$xoopsPreload->triggerEvent('core.footer.end');
  /
xoops255/header.php (5 hits)
    
Line 25$xoopsPreload->triggerEvent('core.header.start');
    
Line 62:     $xoopsPreload->triggerEvent('core.header.addmeta');
    
Line 91:     $xoopsPreload->triggerEvent('core.header.checkcache');
    
Line 93:         $xoopsPreload->triggerEvent('core.header.cacheend');
    
Line 109$xoopsPreload->triggerEvent('core.header.end');
  /
xoops255/include/common.php (3 hits)
    
Line 51$xoopsPreload->triggerEvent('core.include.common.start');
    
Line 167$xoopsPreload->triggerEvent('core.include.common.language');
    
Line 337$xoopsPreload->triggerEvent('core.include.common.end');
  /
xoops255/include/functions.php (1 hit)
    
Line 563:     $xoopsPreload->triggerEvent('core.include.functions.redirectheader', array($url$time$message$addredirect$allowExternalLink));
  /
xoops255/index.php (1 hit)
    
Line 25$xoopsPreload->triggerEvent('core.index.start');
   /
xoops255/lostpass.php (1 hit)
    
Line 22$xoopsPreload->triggerEvent('core.lostpass.start');
  /
xoops255/modules/system/blocks/system_blocks.php (1 hit)
    
Line 185:     $xoopsPreload->triggerEvent('system.blocks.system_blocks.usershow', array(&$pm_handler));
  /
xoops255/modules/system/class/gui.php (1 hit)
    
Line 90:         $xoopsPreload->triggerEvent('system.class.gui.header');
  /
xoops255/pmlite.php (1 hit)
    
Line 21$xoopsPreload->triggerEvent('core.pmlite.start');
  /
xoops255/readpmsg.php (1 hit)
    
Line 21$xoopsPreload->triggerEvent('core.readpmsg.start');
  /
xoops255/register.php (1 hit)
    
Line 27$xoopsPreload->triggerEvent('core.register.start');
  /
xoops255/user.php (1 hit)
    
Line 27$xoopsPreload->triggerEvent('core.user.start');
  /
xoops255/userinfo.php (1 hit)
    
Line 27$xoopsPreload->triggerEvent('core.userinfo.start');
  /
xoops255/viewpmsg.php (1 hit)
    
Line 21$xoopsPreload->triggerEvent('core.viewpmsg.start');


to use a defined preload in your module you just need to do this:
1- create YOUR_MODULE/preloads/core.php file

2- add the name of preload by removing all dots, add a "event" and uppercase of the first letters for all.

eg: core.footer.start => eventCoreFooterStart
// to log main part of modules
    
function eventCoreFooterStart($args)
    {
        include 
dirname(dirname(__FILE__)) . '/include/log.php';
    }


You can define preloads in some places of Core without hardcoding.
one of the amazing places is defining a new xoops_smarty function for your module.

for an example please look at:
xoops255\class\smarty\xoops_plugins\function.xoInboxCount.php:


function smarty_function_xoInboxCount($params, &$smarty)
{
    global 
$xoopsUser;

    if (!isset(
$xoopsUser) || !is_object($xoopsUser)) {
        return;
    }
    
$time time();
    if (isset(
$_SESSION['xoops_inbox_count']) && @$_SESSION['xoops_inbox_count_expire'] > $time) {
        
$count intval($_SESSION['xoops_inbox_count']);
    } else {
        
$pm_handler =& xoops_gethandler('privmessage');

        
$xoopsPreload =& XoopsPreload::getInstance();
        
$xoopsPreload->triggerEvent('core.class.smarty.xoops_plugins.xoinboxcount', array($pm_handler));

        
$criteria = new CriteriaCompo(new Criteria('read_msg'0));
        
$criteria->add(new Criteria('to_userid'$xoopsUser->getVar('uid')));
        
$count intval($pm_handler->getCount($criteria));
        
$_SESSION['xoops_inbox_count'] = $count;
        
$_SESSION['xoops_inbox_count_expire'] = $time 60;
    }
    if (!@empty( 
$params['assign'])) {
        
$smarty->assign$params['assign'], $count );
    } else {
        echo 
$count;
    }
}

?>


IMO the above is all you should know as a module developer.

5
redheadedrod
Re: Preloads, what are they and how can I use them?

Thanks for the information guys, I will go back through them as I can and if they don't make sense ask questions.

Won't really affect me for a couple months yet but it would also be nice to hear more information about the plugin system and how modules can easily share information between them that is new to 2.6. When I get to rewriting some modules I want to make full use of things such as these preloads, plugins and whatever else is available. But in another thread of course.. ;)

Attending College working towards Bachelors in Software Engineering and Network Security.

6
irmtfan
Re: Preloads, what are they and how can I use them?
  • 2013/3/13 4:06

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


You should know preloads are a last resort for module developers.
preloads invented because previously you sometimes need to hack core (e.g.: Protector module) to make your module works.
So you should try any other solution like a hidden block in the whole website. If you could not solve your issue and you need to hack core file then you can use preloads.

Any module using preloads can be consider as a core module and should be tested more than regular modules.

7
redheadedrod
Re: Preloads, what are they and how can I use them?

Ok, I did look through the threads and there was no clear examples of a preload and how they are used. Maybe I am just being dense with this subject and I am sure once the light goes on it will make a lot of sense but that bulb is pretty dim at the moment.

Don't know that a full understanding will be necessary to be able to finish up the PDO stuff but would be nice to know what I am tweaking.
Attending College working towards Bachelors in Software Engineering and Network Security.

8
irmtfan
Re: Preloads, what are they and how can I use them?
  • 2013/3/17 2:19

  • irmtfan

  • Module Developer

  • Posts: 3419

  • Since: 2003/12/7


I think my example of preloads usage is clear enough.

I forgot to mention defining the class.

sop the whole instruction would be this:
1- create YOUR_MODULE/preloads/core.php file

2- add the class and the related function name of preload by removing all dots, add a "event" and uppercase of the first letters for all.

eg: core.footer.start => eventCoreFooterStart

defined('XOOPS_ROOT_PATH') or die('Restricted access');
class 
UserlogCorePreload extends XoopsPreloadItem
{
    
// to log main part of modules
    
function eventCoreFooterStart($args)
    {
        include 
dirname(dirname(__FILE__)) . '/include/log.php';
    }


The class name for any module would be {$Modulename}CorePreload

9
zyspec
Re: Preloads, what are they and how can I use them?
  • 2013/3/17 3:05

  • zyspec

  • Module Developer

  • Posts: 1095

  • Since: 2004/9/21


irmtfan,

A preload can also be used instead of creating a cron job. For example if a module needs to check the status of some event or activity a preload can accomplish this so that it can all be contained within XOOPS instead of forcing inexperienced site administrators to learn about cron jobs.

You are right that other methods could be used - hacking a theme, hacking the core, or a hidden block but using a preload is a cleaner solution in my opinion that requires no action by the site administrator. You are also correct stating that the preload must be tested very carefully. Improper use of a preload could potentially bring down the whole site, not just the module using the preload.

10
redheadedrod
Re: Preloads, what are they and how can I use them?

Ok let's start a little sooner in the food chain here...

Doesn't make sense to me how to use it if I don't know what actually happens.

Lets start from the aspect that I have no clue what a preload is. Not even a hint...

From the links and comments here things are not really clear but with the comment zyspec gave I am beginning to wonder if I might not be getting a better hint about what they might be.

If I had to guess today what a preload is I would guess it is a function that is run at some point in a page load but prior to accessing any modules or block contents thus it happens every time a page loads. (From zyspec's comment this would make sense.)

From the structure mentioned by irmtfan and his comments plus the links I am guessing that the structure of preloads are similar to delegates in C#. In other words the preloads are sorted into a queue that would be run one at a time in the order placed into the queue.

So if I am right things would make better sense BUT at what point in the system do these preloads get run? Will I still have access to all of the normal xoops variables? Obviously if this is the case you really don't want to do a large query if you use it in a module and the comments about including it in a module etc make more sense as well.

Also, is there a specific order for these preloads? Core preloads first then modules I assume?

Thanks... Hopefully I am on the right track to clarity with this.

Rodney
Attending College working towards Bachelors in Software Engineering and Network Security.

Login

Who's Online

213 user(s) are online (134 user(s) are browsing Support Forums)


Members: 0


Guests: 213


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits