LiveUser
[ class tree: LiveUser ] [ index: LiveUser ] [ all elements ]

Source for file demodata.php

Documentation is available at demodata.php

  1. <?php
  2. /**
  3.  * This script will populate the database with the
  4.  * necessary data to run the example.
  5.  *
  6.  *
  7.  * Syntax:
  8.  * DefineGenerator [options]
  9.  * ...where [options] can be:
  10.  * -h --help : Shows this list of options
  11.  *
  12.  * -d --dsn (required): Defines the PEAR::DB DSN to connect to the database.
  13.  * Example: --dsn=mysql://user:passwd@hostname/databasename
  14.  * or -d "mysql://user:passwd@hostname/databasename"
  15.  *
  16.  * -c --create (optional): Defines if the database needs to be created or not.
  17.  * Example: --create=1 or -c "1"
  18.  *
  19.  * -f --file (required): input file containing the structure and
  20.  *                       data in MDB2_Schema format.
  21.  * Example: --file=/path/to/output/file.xml
  22.  *
  23.  * Example usage: php demodata.php -d mysql://root:@localhost/liveuser_test_example5 -f
  24.  * example5/demodata.xml
  25.  *
  26.  * Alternativly you can also call the script from the web using GET
  27.  * demodata.php?dsn=mysql://root:@localhost/liveuser_test_example5&file=example5/demodata.xml&create=1
  28.  *
  29.  * PHP version 4 and 5
  30.  *
  31.  * LICENSE: This library is free software; you can redistribute it and/or
  32.  * modify it under the terms of the GNU Lesser General Public
  33.  * License as published by the Free Software Foundation; either
  34.  * version 2.1 of the License, or (at your option) any later version.
  35.  *
  36.  * This library is distributed in the hope that it will be useful,
  37.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  39.  * Lesser General Public License for more details.
  40.  *
  41.  * You should have received a copy of the GNU Lesser General Public
  42.  * License along with this library; if not, write to the Free Software
  43.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  44.  * MA  02111-1307  USA
  45.  *
  46.  *
  47.  * @category  authentication
  48.  * @package   LiveUser
  49.  * @author    Lukas Smith <smith@pooteeweet.org>
  50.  * @author    Arnaud Limbourg <arnaud@limbourg.com>
  51.  * @copyright 2002-2006 Markus Wolff
  52.  * @license   http://www.gnu.org/licenses/lgpl.txt
  53.  * @version   CVS: $Id: demodata.php,v 1.16 2006/05/23 14:32:59 lsmith Exp $
  54.  * @link      http://pear.php.net/LiveUser
  55.  */
  56.  
  57. require_once 'MDB2/Schema.php';
  58.  
  59. $dsn $file '';
  60. $create = false;
  61.  
  62. if (array_key_exists('REQUEST_METHOD'$_SERVER&& $_SERVER['REQUEST_METHOD'== 'GET'{
  63.     echo '<pre>';
  64.     if (array_key_exists('help'$_GET)) {
  65.         printHelp();
  66.     }
  67.     if (array_key_exists('file'$_GET)) {
  68.         $file $_GET['file'];
  69.     }
  70.     if (array_key_exists('dsn'$_GET)) {
  71.         $dsn $_GET['dsn'];
  72.     }
  73.     if (array_key_exists('create'$_GET)) {
  74.         $create = (bool)$_GET['create'];
  75.     }
  76. else {
  77.     require_once 'Console/Getopt.php';
  78.     $argv = Console_Getopt::readPHPArgv();
  79.  
  80.     $shortoptions "h?d:f:c:";
  81.     $longoptions = array('file=''dsn=''create=');
  82.  
  83.     $con = new Console_Getopt;
  84.     $args $con->readPHPArgv();
  85.     array_shift($args);
  86.     $options $con->getopt($args$shortoptions$longoptions);
  87.  
  88.     if (PEAR::isError($options)) {
  89.         printHelp($options);
  90.     }
  91.  
  92.     $options $options[0];
  93.     foreach ($options as $opt{
  94.         switch ($opt[0]{
  95.         case 'c':
  96.         case '--create':
  97.             $create = (bool)$opt[1];
  98.             break;
  99.         case 'd':
  100.         case '--dsn':
  101.             $dsn $opt[1];
  102.             break;
  103.         case 'f':
  104.         case '--file':
  105.             $file $opt[1];
  106.             break;
  107.         case 'h':
  108.         case '--help':
  109.             printHelp();
  110.             break;
  111.         }
  112.     }
  113. }
  114.  
  115. /******************************************************************
  116. Begin sanity checks on arguments
  117. ******************************************************************/
  118. if ($dsn == '' || $file == ''{
  119.     printHelp();
  120. }
  121.  
  122. if (!file_exists($file)) {
  123.     print "The file $file does not exist\n";
  124.     exit();
  125. }
  126. /******************************************************************
  127. End sanity checks on arguments
  128. ******************************************************************/
  129.  
  130. print "\n";
  131.  
  132. $options = array(
  133. #    'portability' => (MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_EMPTY_TO_NULL),
  134. #   'seqcol_name' = >'id', // uncomment this line if you want to use DB as the backend
  135. );
  136. $dsn = MDB2::parseDSN($dsn);
  137. $database $dsn['database'];
  138. unset($dsn['database']);
  139. $manager =MDB2_Schema::factory($dsn$options);
  140.  
  141. if (PEAR::isError($manager)) {
  142.    print "I could not connect to the database\n";
  143.    print "  " $manager->getMessage()  "\n";
  144.    print "  " $manager->getUserInfo("\n";
  145.    exit();
  146. }
  147.  
  148. $variables = array(
  149.     'database' => $database,
  150.     'create' => (int)$create,
  151. );
  152. $res $manager->updateDatabase($filefalse$variables);
  153.  
  154. if (PEAR::isError($res)) {
  155.     print "I could not populate the database, see error below\n";
  156.     print "  " $res->getMessage()  "\n";
  157.     print "  " $res->getUserInfo("\n";
  158. else {
  159.     print "Database populated successfully\n";
  160. }
  161.  
  162. /**
  163.  * printHelp()
  164.  *
  165.  * @return void 
  166.  * @desc Prints out a list of commandline options
  167.  */
  168. function printHelp()
  169. {
  170. echo ('
  171. Syntax:
  172. DefineGenerator [options]
  173.  
  174. ...where [options] can be:
  175. -h --help : Shows this list of options
  176.  
  177. -d --dsn (required) : Defines the PEAR::DB DSN to connect to the database.
  178. Example: --dsn=mysql://user:passwd@hostname/databasename
  179.  
  180. -c --create (optional): Defines if the database needs to be created or not.
  181. Example: --create=1 or -c "1"
  182.  
  183. -f --file (required) : input file containing the structure and
  184. data in MDB2_Schema format. Example: --file=/path/to/output/file.xml
  185.  
  186. Example usage: Make sure the database exists beforehand
  187.  
  188. php demodata.php -d mysql://root:@localhost/liveuser_test_exampleX -f exampleX/demodata.xml
  189.  
  190. Alternativly you can also call the script from the web using GET
  191.  
  192. demodata.php?dsn=mysql://root:@localhost/liveuser_test_exampleX&file=exampleX/demodata.xml&create=1
  193. ');
  194. exit;
  195. }
  196. ?>

Documentation generated on Mon, 28 Jan 2008 03:30:13 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.