Module Quickloader for CMSMS 1.3

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
pb
Forum Members
Forum Members
Posts: 27
Joined: Sun Jun 01, 2008 8:41 pm

Module Quickloader for CMSMS 1.3

Post by pb »

This is my Module Quickloader - only some changes in class.moduleloader.inc.php and all runs faster, sometimes over 10%.

Works for me and i hope for you:

Code: Select all

<?php
# CMS - CMS Made Simple
# (c)2004-6 by Ted Kulp (ted@cmsmadesimple.org)
# This project's homepage is: http://cmsmadesimple.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# BUT withOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA	02111-1307	USA
#
#$Id$

/**
 * Class to load modules
 *
 * @since		1.0
 * @package		CMS
 */

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'class.module.inc.php');

class ModuleLoader
{
	/**
	* Loads modules from the filesystem.  If loadall is true, then it will load all
	* modules whether they're installed, or active.  If it is false, then it will
	* only load modules which are installed and active.
	*/
	function LoadModules($loadall = false, $noadmin = false)
	{ 
		global $gCms,$CMS_VERSION;
		$db =& $gCms->GetDb();
		$cmsmodules = &$gCms->modules;

		$dir = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR."modules";
    $files=array();
    if (function_exists('scandir'))       // PHP 5 only  quick scan
      $files=scandir($dir);
    else
    {

      $handle = opendir($dir);
      while ( false !== ( $file = readdir($handle) ) ) $files[]=$file;
      closedir($handle);
    }  
    $allfiles=array();
    foreach ($files as $one)
      if ($one<>'.' and $one<>'..')
        if (@is_file("$dir/$one/$one.module.php"))
				  $allfiles[$one]="$dir/$one/$one.module.php";
		
    if ($loadall == true)
		{				 
      foreach ($allfiles as $key => $one)    
      {                      
					  include_once("$one");
					  if (class_exists($key))
					  {
              $newmodule =& new $key;
					    $name = $newmodule->GetName();
					    $cmsmodules[$name]['object'] =& $newmodule;
					    $cmsmodules[$name]['installed'] = false;
					    $cmsmodules[$name]['active'] = false;
				    }
				    else
					     unset($cmsmodules[$name]);
            
			}		  
		}
    
		#Figger out what modules are active and/or installed
		#Load them if loadall is false
		if (isset($db))
		{
			$query = '';
			if ($noadmin)
			$query = "SELECT * FROM ".cms_db_prefix()."modules WHERE admin_only = 0 and active = 1 and module_name <>'' ORDER BY module_name";
			else
			$query = "SELECT * FROM ".cms_db_prefix()."modules WHERE  module_name <>'' ORDER BY module_name";
			$result = &$db->Execute($query);
			while ($result && !$result->EOF)
			{								
					$modulename = $result->fields['module_name'];
										
						if ($loadall == true)
						{
							if (isset($cmsmodules[$modulename]))
							{
								$cmsmodules[$modulename]['installed'] = true;
								$cmsmodules[$modulename]['active'] = ($result->fields['active'] == 1?true:false);
							}
						}
						else
						{ 
							if ($result->fields['active'] == 1)
							{
								if (@isset($allfiles[$modulename]))
								{
									include_once($allfiles[$modulename]);
									$unsetit=true;
									if (class_exists($modulename))
									{ $unsetit=false; 
										$newmodule =& new $modulename;
										$name = $newmodule->GetName();
										$dbversion = $result->fields['version'];
										#Check to see if there is an update and wether or not we should perform it
										if (version_compare($dbversion, $newmodule->GetVersion()) == -1 && $newmodule->AllowAutoUpgrade() == TRUE)
										{
											$newmodule->Upgrade($dbversion, $newmodule->GetVersion());
											$query = "UPDATE ".cms_db_prefix()."modules SET version = ? WHERE module_name = ?";
											$db->Execute($query, array($newmodule->GetVersion(), $name));
											Events::SendEvent('Core', 'ModuleUpgraded', array('name' => $name, 'oldversion' => $dbversion, 'newversion' => $newmodule->GetVersion()));
											$dbversion = $newmodule->GetVersion();
										}

										#Check to see if version in db matches file version
										if ($dbversion == $newmodule->GetVersion() && version_compare($newmodule->MinimumCMSVersion(), $CMS_VERSION) != 1)
										{
											$cmsmodules[$name]['object'] =& $newmodule;
											$cmsmodules[$name]['installed'] = true;
											$cmsmodules[$name]['active'] = ($result->fields['active'] == 1?true:false);											
										}
										else
											$unsetit=true;
									}
									else //No point in doing anything with it
										$unsetit=true;
								}
								else
									$unsetit=true;
							}
							if ($unsetit) unset($cmsmodules[$modulename]);
						}
					
					$result->MoveNext();
				
			}
			
			if ($result) $result->Close();
		}
	}


}

?>
Last edited by pb on Mon Jun 09, 2008 11:56 am, edited 1 time in total.
Post Reply

Return to “Tips and Tricks”