Changeset 153


Ignore:
Timestamp:
Jun 7, 2006 8:41:19 PM (18 years ago)
Author:
scdev
Message:

Q - decided to use standard instantiation for Prefs and Cache instead of singleton methods.

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/module_maker/skel/admin.php

    r152 r153  
    3434
    3535// Configure the prefs object.
    36 $prefs =& Prefs::getInstance('%NAME_PLURAL%');
    37 $prefs->setParam(array('persistent' => false));
     36$tmp_prefs = new Prefs('%NAME_PLURAL%');
     37$tmp_prefs->setParam(array('persistent' => false));
    3838
    3939// Configure the cache object.
    40 $cache =& Cache::getInstance('%NAME_PLURAL%');
     40$cache = new Cache('%NAME_PLURAL%');
    4141$cache->setParam(array('enable' => true));
    4242
     
    255255{
    256256    global $lock;
     257    global $cache;
    257258    $db =& DB::getInstance();
    258     $cache =& Cache::getInstance('%NAME_PLURAL%');
    259259   
    260260    $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $id);
     
    290290{
    291291    global $auth;
     292    global $cache;
    292293    $db =& DB::getInstance();
    293     $cache =& Cache::getInstance('%NAME_PLURAL%');
    294294   
    295295    // Remove any stale cached list data.
     
    311311{
    312312    global $auth, $lock;
     313    global $cache;
    313314    $app =& App::getInstance();
    314     $cache =& Cache::getInstance('%NAME_PLURAL%');
    315315   
    316316    $lock->select('%DB_TBL%', '%PRIMARY_KEY%', $frm['%PRIMARY_KEY%']);
     
    338338    global $page;
    339339    global $so;
     340    global $tmp_prefs;
     341    global $cache;
    340342    $db =& DB::getInstance();   
    341     $prefs =& Prefs::getInstance('%NAME_PLURAL%');
    342     $cache =& Cache::getInstance('%NAME_PLURAL%');
    343343   
    344344    $where_clause = '';
     
    390390    // without knowing the hash.
    391391    $cache_hash = md5($sql . '|' . $page->total_items);
    392     if ($prefs->get('cache_hash') != $cache_hash) {
     392    if ($tmp_prefs->get('cache_hash') != $cache_hash) {
    393393        $cache->delete('list');
    394         $prefs->set('cache_hash', $cache_hash);
     394        $tmp_prefs->set('cache_hash', $cache_hash);
    395395    }
    396396
     
    416416function updateRank($ranks)
    417417{
     418    global $cache;
    418419    $db =& DB::getInstance();
    419     $cache =& Cache::getInstance('%NAME_PLURAL%');
    420420   
    421421    if (!is_array($ranks)) {
  • trunk/lib/Cache.inc.php

    r152 r153  
    131131        }
    132132
    133         $key = md5($key);
     133        $keyhash = md5($key);
    134134        $var = serialize($var);
    135135        $var_len = strlen($var);
     
    146146
    147147        // Remove any value already stored under this key.
    148         unset($_SESSION[$this->_ns][$key]);
     148        unset($_SESSION[$this->_ns][$keyhash]);
    149149
    150150        // Continue to prune the cache if its size is greater than stack_size_limit, but keep at least min_items.
     
    154154
    155155        // Save this value under the specified key.
    156         $_SESSION[$this->_ns][$key] =& $var;
     156        $_SESSION[$this->_ns][$keyhash] =& $var;
    157157
    158158        if ($var_len >= 1024000) {
     
    174174    function get($key)
    175175    {
     176        $app =& App::getInstance();
     177       
    176178        if (true !== $this->getParam('enabled')) {
    177             return false;
    178         }
    179 
    180         $key = md5($key);
    181         if (isset($_SESSION[$this->_ns][$key])) {
     179            $app->logMsg(sprintf('Cache not enabled, not getting data.', null), LOG_DEBUG, __FILE__, __LINE__);
     180            return false;
     181        }
     182
     183        $keyhash = md5($key);
     184        if (isset($_SESSION[$this->_ns][$keyhash])) {
     185            $app->logMsg(sprintf('Retreiving %s from cache.', $key), LOG_DEBUG, __FILE__, __LINE__);
    182186            // Move the accessed cached datum to the top of the stack. Maybe somebody knows a better way to do this?
    183             $tmp =& $_SESSION[$this->_ns][$key];
    184             unset($_SESSION[$this->_ns][$key]);
    185             $_SESSION[$this->_ns][$key] =& $tmp;
     187            $tmp =& $_SESSION[$this->_ns][$keyhash];
     188            unset($_SESSION[$this->_ns][$keyhash]);
     189            $_SESSION[$this->_ns][$keyhash] =& $tmp;
    186190            // Return the unserialized datum.
    187             return unserialize($_SESSION[$this->_ns][$key]);
     191            return unserialize($_SESSION[$this->_ns][$keyhash]);
    188192        } else {
    189193            return false;
     
    203207        }
    204208
    205         $key = md5($key);
    206         return array_key_exists($key, $_SESSION[$this->_ns]);
     209        $keyhash = md5($key);
     210        return array_key_exists($keyhash, $_SESSION[$this->_ns]);
    207211    }
    208212
     
    215219    function delete($key)
    216220    {
    217         $key = md5($key);
    218         unset($_SESSION[$this->_ns][$key]);
     221        $keyhash = md5($key);
     222        unset($_SESSION[$this->_ns][$keyhash]);
    219223    }
    220224   
  • trunk/lib/PageNumbers.inc.php

    r152 r153  
    6565        // in special cases like using a /my/page/# scheme.
    6666        $this->url_base = $_SERVER['PHP_SELF'] . '?page_number=';
     67       
     68        $this->prefs = new Prefs();
     69        $this->prefs->setParam(array('persistent' => false));
    6770    }
    6871
     
    7275    function setPerPage($per_page, $default=25, $save_value=true)
    7376    {
    74         $prefs =& Prefs::getInstance($_SERVER['PHP_SELF']);
    75         $prefs->setParam(array('persistent' => false));
    76    
    7777        // (1) By provided argument, if valid.
    7878        // (2) By saved preference, if available.
     
    8282            $this->_per_page = $per_page;
    8383            if ($save_value) {
    84                 $prefs->set('items_per_page', $this->_per_page);
    85             }
    86         } else if ($save_value && $prefs->exists('items_per_page')) {
    87             $this->_per_page = (int)$prefs->get('items_per_page');
     84                $this->prefs->set('items_per_page', $this->_per_page);
     85            }
     86        } else if ($save_value && $this->prefs->exists('items_per_page')) {
     87            $this->_per_page = (int)$this->prefs->get('items_per_page');
    8888        } else if (is_numeric($default) && $default > 0) {
    8989            $this->_per_page = $default;
     
    9797    function setPageNumber($page_number, $save_value=true)
    9898    {
    99         $prefs =& Prefs::getInstance($_SERVER['PHP_SELF']);
    100         $prefs->setParam(array('persistent' => false));
    101    
    10299        // (1) By provided argument, if valid.
    103100        // (2) By saved preference, if available.
     
    111108            }
    112109            if ($save_value) {
    113                 $prefs->set('page_number', $this->current_page);
    114             }
    115         } else if ($save_value && $prefs->exists('page_number')) {
    116             $this->current_page = (int)$prefs->get('page_number');
     110                $this->prefs->set('page_number', $this->current_page);
     111            }
     112        } else if ($save_value && $this->prefs->exists('page_number')) {
     113            $this->current_page = (int)$this->prefs->get('page_number');
    117114        }
    118115        $this->set_page_number_initialized = true;
  • trunk/lib/Prefs.inc.php

    r152 r153  
    1414// Load preferences for the user's session.
    1515require_once 'codebase/lib/Prefs.inc.php';
    16 $prefs =& Prefs::getInstance('my-namespace');
     16$prefs = new Prefs('my-namespace');
    1717$prefs->setParam(array(
    1818    'persistent' => $auth->isLoggedIn(),
     
    7474            $this->setParam(array('create_table' => $app->getParam('db_create_tables')));
    7575        }
    76     }
    77 
    78     /**
    79      * This method enforces the singleton pattern for this class.
    80      *
    81      * @return  object  Reference to the global Prefs object.
    82      * @access  public
    83      * @static
    84      */
    85     function &getInstance($namespace='')
    86     {
    87         static $instances = array();
    88 
    89         if (!array_key_exists($namespace, $instances)) {
    90             $instances[$namespace] = new Prefs($namespace);
    91         }
    92 
    93         return $instances[$namespace];
    9476    }
    9577
  • trunk/lib/SortOrder.inc.php

    r152 r153  
    3939        $this->default_sort = $default_sort;
    4040        $this->default_order = $default_order;
     41       
     42        $this->prefs = new Prefs();
     43        $this->prefs->setParam(array('persistent' => false));
    4144    }
    4245
     
    6972    function setDefault($default_sort = '', $default_order = '')
    7073    {
    71         $prefs =& Prefs::getInstance($_SERVER['PHP_SELF']);
    72         $prefs->setParam(array('persistent' => false));
    73 
    7474        // Which column to sort by?
    7575        // (1) By GET or POST specification, if available.
     
    7979        if (!empty($new_sort_by)) {
    8080            $this->sort_by = $new_sort_by;
    81             $prefs->set('sort_by', $this->sort_by);
    82         } else if ($prefs->exists('sort_by')) {
    83             $this->sort_by = $prefs->get('sort_by');
     81            $this->prefs->set('sort_by', $this->sort_by);
     82        } else if ($this->prefs->exists('sort_by')) {
     83            $this->sort_by = $this->prefs->get('sort_by');
    8484        } else {
    8585            $this->sort_by = $default_sort;
     
    9393        if (!empty($new_order)) {
    9494            $this->order = $new_order;
    95             $prefs->set('sort_order', $this->order);
    96         } else if ($prefs->exists('sort_order')) {
    97             $this->order = $prefs->get('sort_order');
     95            $this->prefs->set('sort_order', $this->order);
     96        } else if ($this->prefs->exists('sort_order')) {
     97            $this->order = $this->prefs->get('sort_order');
    9898        } else {
    9999            $this->order = $default_order;
     
    112112    function set($sort = null, $order = null)
    113113    {
    114         $prefs =& Prefs::getInstance($_SERVER['PHP_SELF']);
    115         $prefs->setParam(array('persistent' => false));
    116 
    117114        // Set new sort value.
    118115        if (isset($sort)) {
    119116            $this->sort_by = $sort;
    120             $prefs->set('sort_by', $this->sort_by);
     117            $this->prefs->set('sort_by', $this->sort_by);
    121118        }
    122119
     
    124121        if (isset($order)) {
    125122            $this->order = $order;
    126             $prefs->set('sort_order', $this->order);
     123            $this->prefs->set('sort_order', $this->order);
    127124        }
    128125    }
  • trunk/services/admins.php

    r152 r153  
    3131
    3232// Configure the prefs object.
    33 $prefs =& Prefs::getInstance('admins');
    34 $prefs->setParam(array('persistent' => false));
     33$tmp_prefs = new Prefs('admins');
     34$tmp_prefs->setParam(array('persistent' => false));
    3535
    3636// Configure the cache object.
    37 $cache =& Cache::getInstance('admins');
     37$cache = new Cache('admins');
    3838$cache->setParam(array('enable' => true));
    3939
     
    322322function deleteRecord($id)
    323323{
    324     global $auth, $lock;
     324    global $auth;
     325    global $lock;
     326    global $cache;
    325327    $app =& App::getInstance();
    326328    $db =& DB::getInstance();
    327     $cache =& Cache::getInstance('admins');
    328329   
    329330    $lock->select('admin_tbl', 'admin_id', $id);
     
    371372{
    372373    global $auth;
     374    global $cache;
    373375    $app =& App::getInstance();
    374376    $db =& DB::getInstance();
    375     $cache =& Cache::getInstance('admins');
    376377   
    377378    // Remove any stale cached list data.
     
    414415function updateRecord($frm)
    415416{
    416     global $auth, $lock;
     417    global $auth;
     418    global $lock;
     419    global $cache;
    417420    $app =& App::getInstance();
    418421    $db =& DB::getInstance();
    419     $cache =& Cache::getInstance('admins');
    420422   
    421423    $lock->select('admin_tbl', 'admin_id', $frm['admin_id']);
     
    460462    global $page;
    461463    global $so;
     464    global $tmp_prefs;
     465    global $cache;
    462466    $db =& DB::getInstance();
    463     $prefs =& Prefs::getInstance('admins');
    464     $cache =& Cache::getInstance('admins');
    465467   
    466468    $where_clause = '';
     
    514516    // without knowing the hash.
    515517    $cache_hash = md5($sql . '|' . $page->total_items);
    516     if ($prefs->get('cache_hash') != $cache_hash) {
     518    if ($tmp_prefs->get('cache_hash') != $cache_hash) {
    517519        $cache->delete('list');
    518         $prefs->set('cache_hash', $cache_hash);
     520        $tmp_prefs->set('cache_hash', $cache_hash);
    519521    }
    520522
  • trunk/services/logs.php

    r152 r153  
    3737$no_download_files = '/^$/';
    3838
    39 
    40 // Set the defaults and catch incoming settings.
    41 $prefs =& Prefs::getInstance('admin_logs');
    42 $prefs->setDefaults(array(
     39// Configure the prefs object.
     40$tmp_prefs = new Prefs('admin_logs');
     41$tmp_prefs->setParam(array('persistent' => false));
     42$tmp_prefs->setDefaults(array(
    4343    'log_file' => $app->getParam('log_filename')
    4444));
    45 $prefs->set('log_file', getFormData('log'));
     45$tmp_prefs->set('log_file', getFormData('log'));
    4646
    4747// Titles and navigation header.
    48 $nav->addPage(sprintf(_("Viewing log: <em>%s</em>"), $prefs->get('log_file')), '/admin/logs.php');
     48$nav->addPage(sprintf(_("Viewing log: <em>%s</em>"), $tmp_prefs->get('log_file')), '/admin/logs.php');
    4949
    5050/********************************************************************
     
    5959case 'delete' :
    6060//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
    61     deleteLog($prefs->get('log_file'));
    62     $prefs->set('log_file', $app->getParam('log_filename'));
     61    deleteLog($tmp_prefs->get('log_file'));
     62    $tmp_prefs->set('log_file', $app->getParam('log_filename'));
    6363    if ($app->validBoomerangURL('app_log')) {
    6464        // Display boomerang page.
     
    7171case 'clear' :
    7272//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
    73     clearLog($prefs->get('log_file'));
     73    clearLog($tmp_prefs->get('log_file'));
    7474    if ($app->validBoomerangURL('app_log')) {
    7575        // Display boomerang page.
     
    8282case 'archive' :
    8383//     $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET);
    84     if (archiveLog($prefs->get('log_file'))) {
     84    if (archiveLog($tmp_prefs->get('log_file'))) {
    8585        // Now flush current log.
    8686        $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear');
     
    100100case 'download' :
    101101    header('Content-Type: application/octet-stream');
    102     header(sprintf('Content-Disposition: attachment; filename=%s.txt', $prefs->get('log_file')));
    103     printLog($prefs->get('log_file'));
     102    header(sprintf('Content-Disposition: attachment; filename=%s.txt', $tmp_prefs->get('log_file')));
     103    printLog($tmp_prefs->get('log_file'));
    104104    die;
    105105    break;
    106106
    107107default :
    108     $list =& getLog($prefs->get('log_file'), getFormData('search_query'));
     108    $list =& getLog($tmp_prefs->get('log_file'), getFormData('search_query'));
    109109    $main_template = 'log_list.ihtml';
    110110    break;
     
    128128include 'header.ihtml';
    129129if ('output' == $main_template) {
    130     printLog($prefs->get('log_file'));
     130    printLog($tmp_prefs->get('log_file'));
    131131} else {
    132132    include 'codebase/services/templates/' . $main_template;
  • trunk/services/templates/log_list.ihtml

    r152 r153  
    1010        <tr class="commandtext">
    1111            <td>
    12                 <?php if ($l['filename'] == $prefs->get('log_file')) { ?>
     12                <?php if ($l['filename'] == $tmp_prefs->get('log_file')) { ?>
    1313                    <span class="commanditem"><strong><?php echo sprintf(_("%s"), $l['filename']); ?></strong></span>
    1414                <?php } else { ?>
Note: See TracChangeset for help on using the changeset viewer.