cronDatabase.php 2.44 KB
Newer Older
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
1
2
3
4
<?php

// Cronjob Skript fuer Aktionen ausserhalb der DB
// z.B. Mailbenachrichtigung, Loeschen verwaister Ablagen etc.
5
// schachi 2016-06-28
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
6
7
8
9

  // stelle sicher das dieses Skript in einem Subdir liegt, normalerweise im Ordner cronjobs, sonst gibt es kausale Problem :-(
  chdir(__DIR__);
  chdir('../');
10
  //print_r (realpath(__DIR__).' '.getcwd()."\n");
11
  if (!is_readable('conf.ini') ) trigger_error ('Error loading config file from here '.getcwd()."\n");
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
12
13
14

  $conf = array();
  $conf = parse_ini_file('conf.ini', true);
15
  //print_r ($conf);
16
  if ( !isset( $conf['_database'] ) ) trigger_error ('Error loading config file. No database specified.');
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
17
18
  $dbinfo =& $conf['_database'];
  if ( !is_array( $dbinfo ) || !isset($dbinfo['host']) || !isset( $dbinfo['user'] ) || !isset( $dbinfo['password'] ) || !isset( $dbinfo['name'] ) ) {
19
    trigger_error ('Error loading config file.  The database information was not entered correctly.');
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
20
  }
21
  $db = mysqli_connect($dbinfo['host'], $dbinfo['user'], $dbinfo['password'], $dbinfo['name'] );
22
  if ( !$db ) trigger_error ('Failed to connect to MySQL database: '.mysqli_connect_error($db)."\n");
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
23

24
25
26
27
28
29
30
31
32
33
34
35
36
  $debug = 0;  // 1 = ausgabe und keine Mail an User
  $mailto = 0; // 0 = keine Mail an Debugger
  //$mailto = 'schachi@mpi-magdeburg.mpg.de';

  // url
  $url = 'http';
  if ( isset($conf['_own']['ssl'])) {
    if ($conf['_own']['ssl'] == 1) $url = 'https';
  }
  $path = (basename(realpath('./')));
  $host = shell_exec("hostname -f | tr -d '\n'");
  $url  = $url.'://'.$host.'/'.$path;
  if ($debug) print_r($url."\n");
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
37
38
39
40
41


  // loesche alle <table>__history, welche nicht erwuenscht sind
  // Xataface hat nur einen globalen Schalter ON/OFF fuer history, aber wer braucht denn alle histories?
  $sql = "SELECT reiter FROM view_reiter WHERE reiter IN (SELECT CONCAT(lst.reiter, '__history') AS table_his FROM list_reiter AS lst LEFT JOIN view_reiter AS vReit ON lst.reiter = vReit.reiter WHERE lst.history = '0' AND vReit.table_type = 'BASE TABLE' AND lst.reiter NOT LIKE '%__history') AND table_type = 'BASE TABLE';";
42
  $result = mysqli_query($db, $sql) OR trigger_error ('Query failed: '.mysqli_error($db)."\n");
43
  $count = mysqli_num_rows($result);
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
44
  if ( $count >= 1 ) {
45
    while($row = mysqli_fetch_assoc($result)) {
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
46
      $table = $row['reiter'];
47
48
      $sql = "DROP TABLE IF EXISTS $table;";
      if ($debug) echo "$sql\n";
49
      mysqli_query($db, $sql) OR trigger_error ('Query failed: '.mysqli_error($db)."\n");
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
50
51
52
    }
  }

53
  mysqli_close($db);
Gerd Schachtschneider's avatar
initial  
Gerd Schachtschneider committed
54
55

?>