source: trunk/bin/init_codebase_tables.cli.php

Last change on this file was 757, checked in by anonymous, 2 years ago

Fix depreciated notices

  • Property svn:executable set to *
File size: 6.4 KB
Line 
1#!/usr/bin/env php
2<?php
3/*
4* The Strangecode Codebase - a general application development framework for PHP
5* For details visit the project site: <http://trac.strangecode.com/>
6* Copyright © 2015 Strangecode, LLC
7*
8* This program is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by
10* the Free Software Foundation, either version 3 of the License, or
11* (at your option) any later version.
12*
13* This program is distributed in the hope that it will be useful,
14* but WITHOUT ANY WARRANTY; without even the implied warranty of
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16* GNU General Public License for more details.
17*
18* You should have received a copy of the GNU General Public License
19* along with this program.  If not, see <http://www.gnu.org/licenses/>.
20*/
21
22/*
23* Initialize tables managed by Codebase classes.
24*
25* @author   Quinn Comendant <quinn@strangecode.com>
26* @version  2.0
27* @since    05 Mar 2016 19:12:09
28*/
29
30/********************************************************************
31* MAIN
32********************************************************************/
33
34// Process command line options.
35$opt = getopt('fhc:');
36
37// Get name of class to initialize (or 'all' for all).
38$class = strtolower(end($_SERVER['argv']));
39
40// Give them a fighting chance. Show the help message.
41if ($_SERVER['argc'] <= 1 || isset($opt['h'])) {
42    help();
43    exit(1);
44}
45
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';
51
52if ('all' == $class) {
53    initClassDB('acl');
54    initClassDB('auth');
55    initClassDB('lock');
56    initClassDB('prefs');
57    initClassDB('version');
58    initClassDB('session');
59} else {
60    initClassDB($class);
61}
62
63$app->stop();
64die;
65
66/********************************************************************
67* FUNCTIONS
68********************************************************************/
69
70/*
71*
72*
73* @access   public
74* @param
75* @return
76* @author   Quinn Comendant <quinn@strangecode.com>
77* @version  1.0
78* @since    12 Jul 2015 23:16:02
79*/
80function help()
81{
82    global $cli_executed;
83    ?>
84This script initializes DB tables managed by Codebase classes. If database schema is changed a log message will display.
85
86Usage: <?php echo $cli_executed; ?> [OPTIONS] CLASSNAME
87
88OPTIONS
89
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.
93
94CLASSNAME is one of:
95
96    all         Special case: create tables for all classes.
97    acl         Create tables for ACL
98    auth        Create tables for Auth_SQL
99    lock        Create tables for Lock
100    prefs       Create tables for Prefs
101    version     Create tables for Version
102    session     Create tables for DBSessionHandler
103<?php
104}
105
106/*
107*
108*
109* @access   public
110* @param
111* @return
112* @author   Quinn Comendant <quinn@strangecode.com>
113* @version  1.0
114* @since    12 Jul 2015 23:25:35
115*/
116function initClassDB($class)
117{
118    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__);
123
124    switch ($class) {
125        case 'acl':
126            // ACL!
127            require_once CODEBASE_PATH . '/lib/ACL.inc.php';
128            $acl =& ACL::getInstance();
129            $acl->setParam(array('create_table' => true));
130            $acl->initDB(isset($opt['f']));
131            break;
132
133        case 'auth':
134            // Auth_SQL!
135            require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php';
136            $auth = new Auth_SQL('codebase');
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            }
142            $auth->initDB(isset($opt['f']));
143            break;
144
145        case 'lock':
146            // Lock!
147            require_once CODEBASE_PATH . '/lib/Lock.inc.php';
148            require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php';
149            $lock =& Lock::getInstance(new Auth_SQL('codebase'));
150            $lock->setParam(array('create_table' => true));
151            $lock->initDB(isset($opt['f']));
152            break;
153
154        case 'prefs':
155            // Prefs!
156            require_once CODEBASE_PATH . '/lib/Prefs.inc.php';
157            $prefs = new Prefs('codebase');
158            $prefs->setParam(array('create_table' => true));
159            $prefs->initDB(isset($opt['f']));
160            break;
161
162        case 'version':
163            // Version!
164            require_once CODEBASE_PATH . '/lib/Version.inc.php';
165            require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php';
166            $version = Version::getInstance(new Auth_SQL('codebase'));
167            $version->setParam(array('create_table' => true));
168            $version->initDB(isset($opt['f']));
169            break;
170
171        case 'session':
172            // DBSessionHandler!
173
174            if (version_compare(PHP_VERSION, '7.0.0', '<')) {
175                // We need to hack the app to allow sessions to run in a CLI.
176                $app->stop();
177                $app->cli = false;
178                $app->setParam(array('enable_session' => true));
179                $app->start();
180            }
181
182            if (!isset($opt['c'])) {
183                // User didn't provide custom config. Use sane defaults.
184                require_once CODEBASE_PATH . '/lib/DBSessionHandler.inc.php';
185                // Creating a session here causes a “Session save handler cannot be changed after headers have already been sent” warning. This can be ignored.
186                $db_session = new DBSessionHandler($app->db, array(
187                    'db_table' => 'session_tbl',
188                    'create_table' => true,
189                ));
190                $db_session->initDB(isset($opt['f']));
191            } else if (true === $app->getParam('enable_db_session_handler') && true === $app->getParam('enable_db') && isset($app->db_session)) {
192                // Only init if db_session is enabled in config.
193                $app->db_session->initDB(isset($opt['f']));
194            }
195            break;
196
197        default:
198            echo "The class $class is not setup to initClassDB().\n";
199            exit(1);
200    }
201}
Note: See TracBrowser for help on using the repository browser.