Changeset 573 for trunk/bin


Ignore:
Timestamp:
Feb 27, 2017 2:13:19 PM (7 years ago)
Author:
anonymous
Message:

Refactoring of init_codebase_tables.cli.php to support external _config.inc.php

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/init_codebase_tables.cli.php

    r560 r573  
    2424*
    2525* @author   Quinn Comendant <quinn@strangecode.com>
    26 * @version  1.0
    27 * @since    12 Jul 2015 22:44:58
     26* @version  2.0
     27* @since    05 Mar 2016 19:12:09
    2828*/
    29 
    30 /********************************************************************
    31 * CONFIG
    32 ********************************************************************/
    33 
    34 require_once dirname(__FILE__) . '/_config.inc.php';
    35 require_once CODEBASE_PATH . '/lib/ACL.inc.php';
    36 require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php';
    37 require_once CODEBASE_PATH . '/lib/Lock.inc.php';
    38 require_once CODEBASE_PATH . '/lib/Prefs.inc.php';
    39 require_once CODEBASE_PATH . '/lib/Version.inc.php';
    4029
    4130/********************************************************************
     
    4433
    4534// Process command line options.
    46 $opt = getopt('fh');
     35$opt = getopt('fhc:');
    4736
    48 // Get class name.
     37// Get name of class to initialize (or 'all' for all).
    4938$class = strtolower(end($_SERVER['argv']));
    5039
     
    5544}
    5645
    57 $app->logMsg(sprintf('Running initDB on %s codebase tables in db %s', $class, $db->getParam('db_name')), LOG_INFO, __FILE__, __LINE__);
     46if (isset($opt['c']) && is_file($opt['c'])) {
     47    require_once $opt['c'];
     48    $app->logMsg(sprintf('Loaded custom config file: %s', $opt['c']), LOG_INFO, __FILE__, __LINE__);
     49}
     50require_once dirname(__FILE__) . '/_config.inc.php';
    5851
    5952if ('all' == $class) {
     
    6356    initClassDB('prefs');
    6457    initClassDB('version');
     58    initClassDB('session');
    6559} else {
    6660    initClassDB($class);
    6761}
    6862
     63$app->stop();
     64die;
    6965
    7066/********************************************************************
     
    9288OPTIONS
    9389
    94     -f  Force recreation of tables, if they already exist.
    95     -h  Show this help message.
     90    -c FILE Path to include custom site _config.inc.php file.
     91    -f      Force recreation of tables, if they already exist. WARNING: this will delete existing records.
     92    -h      Show this help message.
    9693
    9794CLASSNAME is one of:
     
    103100    prefs       Create tables for Prefs
    104101    version     Create tables for Version
     102    session     Create tables for DBSessionHandler
    105103<?php
    106104}
     
    119117{
    120118    global $opt;
     119    $app =& App::getInstance();
     120    $db =& DB::getInstance();
     121
     122    $app->logMsg(sprintf('Running %s->initDB() on database %s', $class, $db->getParam('db_name')), LOG_INFO, __FILE__, __LINE__);
    121123
    122124    switch ($class) {
    123125        case 'acl':
    124126            // ACL!
     127            require_once CODEBASE_PATH . '/lib/ACL.inc.php';
    125128            $acl =& ACL::getInstance();
    126129            $acl->setParam(array('create_table' => true));
     
    130133        case 'auth':
    131134            // Auth_SQL!
     135            require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php';
    132136            $auth = new Auth_SQL('codebase');
    133             $auth->setParam(array(
    134                 'create_table' => true,
    135                 'abuse_detection' => true,
    136             ));
     137            $auth->setParam(array('create_table' => true));
     138            if (!isset($opt['c'])) {
     139                // User didn't provide custom config. Use sane defaults.
     140                $auth->setParam(array('abuse_detection' => true));
     141            }
    137142            $auth->initDB(isset($opt['f']));
    138143            break;
     
    140145        case 'lock':
    141146            // Lock!
     147            require_once CODEBASE_PATH . '/lib/Lock.inc.php';
    142148            $lock =& Lock::getInstance(new Auth_SQL('codebase'));
    143149            $lock->setParam(array('create_table' => true));
     
    147153        case 'prefs':
    148154            // Prefs!
     155            require_once CODEBASE_PATH . '/lib/Prefs.inc.php';
    149156            $prefs = new Prefs('codebase');
    150157            $prefs->setParam(array('create_table' => true));
     
    154161        case 'version':
    155162            // Version!
     163            require_once CODEBASE_PATH . '/lib/Version.inc.php';
    156164            $version = Version::getInstance(new Auth_SQL('codebase'));
    157165            $version->setParam(array('create_table' => true));
     
    159167            break;
    160168
     169        case 'session':
     170            // DBSessionHandler!
     171
     172            // We need to hack the app to allow sessions to run in a CLI.
     173            $app->stop();
     174            $app->cli = false;
     175            $app->setParam(array('enable_session' => true));
     176            $app->start();
     177
     178            if (!isset($opt['c'])) {
     179                // User didn't provide custom config. Use sane defaults.
     180                require_once CODEBASE_PATH . '/lib/DBSessionHandler.inc.php';
     181                $db_session = new DBSessionHandler($app->db, array(
     182                    'db_table' => 'session_tbl',
     183                    'create_table' => true,
     184                ));
     185                $db_session->initDB(isset($opt['f']));
     186            } else if (true === $app->getParam('enable_db_session_handler') && true === $app->getParam('enable_db') && isset($app->db_session)) {
     187                // Only init if db_session is enabled in config.
     188                $app->db_session->initDB(isset($opt['f']));
     189            }
     190            break;
     191
    161192        default:
    162             echo "The class $class is not setup to initClassDB.\n";
     193            echo "The class $class is not setup to initClassDB().\n";
    163194            exit(1);
    164195    }
Note: See TracChangeset for help on using the changeset viewer.