| 1 | <?php
|
|---|
| 2 | /* $Id: Odin32DBUpdate.phtml,v 1.3 2001-09-06 03:01:42 bird Exp $
|
|---|
| 3 | *
|
|---|
| 4 | * Update script for the Odin32 API Database.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2001 knut st. osmundsen (kosmunds@csc.com)
|
|---|
| 7 | *
|
|---|
| 8 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 9 | *
|
|---|
| 10 | */
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | /*
|
|---|
| 14 | * Verify access.
|
|---|
| 15 | */
|
|---|
| 16 | if (!isset($sPasswd) || $sPasswd != "<somepassword>")
|
|---|
| 17 | {
|
|---|
| 18 | printf("...");
|
|---|
| 19 | }
|
|---|
| 20 | else
|
|---|
| 21 | {
|
|---|
| 22 | require "Odin32DBHelpers.php3";
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | /* Profiling */
|
|---|
| 26 | $sPageTimer = Odin32DBTimerStart("page timer");
|
|---|
| 27 | /* Profiling */
|
|---|
| 28 |
|
|---|
| 29 | /*
|
|---|
| 30 | * ARG!Manually load the shit since system is broken in safe_mode.
|
|---|
| 31 | */
|
|---|
| 32 | if (0)
|
|---|
| 33 | {
|
|---|
| 34 | /*
|
|---|
| 35 | * Load in the dump.
|
|---|
| 36 | */
|
|---|
| 37 | /*
|
|---|
| 38 | unzip -p e:\netlabs.org\ftp\anonymous\pub\odin\Daily\db\odin32db.zip | d:\mysql\bin\mysql.exe --host=127.0.0.1 --user=odin32api --password=<> Odin32API
|
|---|
| 39 | */
|
|---|
| 40 | /*$rc = system('unzip -p e:\\netlabs.org\\ftp\\anonymous\\pub\\odin\\Daily\\db\\odin32db.zip | d:\\mysql\\bin\\mysql.exe --host=127.0.0.1 --user=odin32api --password=<> Odin32API >> e:\\temp\\db.log 2>&1');
|
|---|
| 41 |
|
|---|
| 42 | printf("load rc=%d\n", $rc);*/
|
|---|
| 43 | }
|
|---|
| 44 | else
|
|---|
| 45 | {
|
|---|
| 46 | /*
|
|---|
| 47 | * We're using DATA FILE stuff to speed up loading.
|
|---|
| 48 | */
|
|---|
| 49 | /* Connect to database */
|
|---|
| 50 | require "Odin32DBConnect.php3";
|
|---|
| 51 |
|
|---|
| 52 | $sDir = 'e:/netlabs.org/ftp/anonymous/pub/odin/Daily/db';
|
|---|
| 53 |
|
|---|
| 54 | $hDir = opendir($sDir);
|
|---|
| 55 | while ($sEntry = readdir($hDir))
|
|---|
| 56 | {
|
|---|
| 57 | $sFile = $sDir.'/'.$sEntry;
|
|---|
| 58 | if (!is_dir($sFile))
|
|---|
| 59 | {
|
|---|
| 60 | if (substr($sFile, -4) == '.sql')
|
|---|
| 61 | if (ProcessSqlFile($db, $sFile))
|
|---|
| 62 | unlink($sFile);
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 | closedir($hDir);
|
|---|
| 66 |
|
|---|
| 67 | $hDir = opendir($sDir);
|
|---|
| 68 | while ($sEntry = readdir($hDir))
|
|---|
| 69 | {
|
|---|
| 70 | $sFile = $sDir.'/'.$sEntry;
|
|---|
| 71 | if (!is_dir($sFile))
|
|---|
| 72 | {
|
|---|
| 73 | if (substr($sFile, -4) == '.txt')
|
|---|
| 74 | if (LoadSqlDataFile($db, $sFile))
|
|---|
| 75 | unlink($sFile);
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | closedir($hDir);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /* Profiling */
|
|---|
| 82 | Odin32DBTimerStop($sPageTimer);
|
|---|
| 83 | /* Profiling */
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Processes the ';' separated sql statements in a given file.
|
|---|
| 90 | * @returns Success indicator.
|
|---|
| 91 | * @param $sFile Name of the file.
|
|---|
| 92 | */
|
|---|
| 93 | function ProcessSqlFile($db, $sFile)
|
|---|
| 94 | {
|
|---|
| 95 | $fRc = false;
|
|---|
| 96 |
|
|---|
| 97 | echo "sql: ".$sFile."\r\n";
|
|---|
| 98 |
|
|---|
| 99 | $fh = fopen($sFile, "r");
|
|---|
| 100 | if ($fh)
|
|---|
| 101 | {
|
|---|
| 102 | $fRc = true;
|
|---|
| 103 |
|
|---|
| 104 | $sSql = '';
|
|---|
| 105 | while ($s = fgets($fh, 1024*1024))
|
|---|
| 106 | {
|
|---|
| 107 | flush();
|
|---|
| 108 | if (substr($s, 0, 1) == '#' || substr($s, 0, 2) == '--')
|
|---|
| 109 | {
|
|---|
| 110 | $sSql = '';
|
|---|
| 111 | continue;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | $sSql = $sSql.$s;
|
|---|
| 115 | if (strchr(substr($s, -3), ';')) /* check for end of statement (;). */
|
|---|
| 116 | {
|
|---|
| 117 | $sSql = trim($sSql);
|
|---|
| 118 | $sSql = substr($sSql, 0, strlen($sSql) - 1);
|
|---|
| 119 | flush();
|
|---|
| 120 | if (!($result = mysql_query($sSql, $db)))
|
|---|
| 121 | {
|
|---|
| 122 | Odin32DBSqlError($sSql);
|
|---|
| 123 | echo "sql: '".substr($sSql, 0, strlen($sSql) > 80 ? 80 : strlen($sSql))."'\r\n";
|
|---|
| 124 | flush();
|
|---|
| 125 | $fRc = false;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /* next */
|
|---|
| 129 | $sSql = '';
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | fclose($fh);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | return $fRc;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | /**
|
|---|
| 141 | * Loads data file using LOAD IN DATA
|
|---|
| 142 | * @returns Success indicator.
|
|---|
| 143 | * @param $sFile File to load.
|
|---|
| 144 | */
|
|---|
| 145 | function LoadSqlDataFile($db, $sFile)
|
|---|
| 146 | {
|
|---|
| 147 | $sTable = substr(strrchr(substr($sFile, 0, strlen($sFile) - 4), '/'), 1);
|
|---|
| 148 | $sSql = "LOAD DATA INFILE '".$sFile."' REPLACE INTO TABLE ".$sTable;
|
|---|
| 149 | echo "load: $sSql\r\n";
|
|---|
| 150 | if (!($result = mysql_query($sSql, $db)))
|
|---|
| 151 | {
|
|---|
| 152 | Odin32DBSqlError($sSql);
|
|---|
| 153 | echo "load: $sSql\r\n";
|
|---|
| 154 | flush();
|
|---|
| 155 | return false;
|
|---|
| 156 | }
|
|---|
| 157 | return true;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | ?>
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|