[r354]: / kohaBar-daemon / trunk / kohabard.php  Maximize  Restore  History

Download this file

176 lines (141 with data), 5.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/php
<?php
include_once('MySQLConnectionFactory.class.php');
//Begin Main
$debug = false;
//Get Command Line Arguments
while ( current( $argv ) ) {
switch ( current( $argv ) ) {
case '-h':
printUsage( $argv );
die();
break;
case '-p':
$file = next( $argv );
break;
case '-f':
$file = next( $argv);
break;
case '-t':
$sleepTime = next( $argv );
break;
case '-d':
$debug = true;
break;
default:
next( $argv );
break;
}
}
#$debug = true;
if ( ! $file ) {
printUsage( $argv );
die( "ERROR: No file specified, use -p to specify a file to save the xml to.\n" );
}
if ( ! $sleepTime ) {
printUsage( $argv );
die( "ERROR: No time specified, use -t to specify a time inverval in seconds for the xml file to be updated.\n" );
}
if ( $debug ) {
print "\nDebug Mode On\n";
}
//Open DB Connection
$dbh = MySQLConnectionFactory::create();
//Just get the branch codes once at daemon startup
$branchCodes = getBranchCodes( $dbh );
//Daemonize
chdir('/') or die("Could Not chdir to /");
#ob_start(); //Buffer output
//Begin looping
while ( 1 ) {
$day = date( 'd' );
$month = date( 'm' );
$year = date( 'Y' );
$checkoutsToday = array();
$checkoutsMonthToDate = array();
$checkoutsYearToDate = array();
foreach ( $branchCodes as $thisBranch ) {
$today = $year . '-' . $month . '-' . $day;
$sql = "SELECT COUNT(*) as checkouts FROM statistics
WHERE ( DATE( statistics.datetime ) >= DATE( '$today')
AND ( statistics.type = 'issue' OR statistics.type = 'renew' )
AND statistics.branch = '$thisBranch')";
$results = mysql_query( $sql ) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
$row = mysql_fetch_assoc( $results );
$checkoutsToday[ $thisBranch ] = $row['checkouts'];
if ( $debug ) { echo "\n Checkouts for Today SQL: \n$sql \n" . $row['checkouts']; }
$today = $year . '-' . $month . '-' . '01';
$sql = "SELECT COUNT(*) as checkouts FROM statistics
WHERE ( DATE( statistics.datetime ) >= DATE( '$today' )
AND ( statistics.type = 'issue' OR statistics.type = 'renew' )
AND statistics.branch = '$thisBranch')";
$results = mysql_query( $sql ) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
$row = mysql_fetch_assoc( $results );
$checkoutsMonthToDate[ $thisBranch ] = $row['checkouts'];
if ( $debug ) { echo "\n Checkouts for Month SQL: \n$sql \n" . $row['checkouts']; }
$today = $year . '-' . '01' . '-' . '01';
$sql = "SELECT COUNT(*) as checkouts FROM statistics
WHERE ( DATE( statistics.datetime ) >= DATE( '$today' )
AND ( statistics.type = 'issue' OR statistics.type = 'renew' )
AND statistics.branch = '$thisBranch')";
$results = mysql_query( $sql ) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $sql . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());
$row = mysql_fetch_assoc( $results );
$checkoutsYearToDate[ $thisBranch ] = $row['checkouts'];
if ( $debug ) { echo "\n Checkouts for Year SQL: \n$sql \n" . $row['checkouts']; }
}
//Create the XML document
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "kohaStats" );
$doc->appendChild( $r );
foreach( $branchCodes as $thisBranch ) {
$b = $doc->createElement( $thisBranch );
$checkoutsTodayNode = $doc->createElement( "checkoutsToday" );
$checkoutsTodayNode->appendChild(
$doc->createTextNode( $checkoutsToday[ $thisBranch ] )
);
$b->appendChild( $checkoutsTodayNode );
$checkoutsMonthToDateNode = $doc->createElement( "checkoutsMonthToDate" );
$checkoutsMonthToDateNode->appendChild(
$doc->createTextNode( $checkoutsMonthToDate[ $thisBranch ] )
);
$b->appendChild( $checkoutsMonthToDateNode );
$checkoutsYearToDateNode = $doc->createElement( "checkoutsYearToDate" );
$checkoutsYearToDateNode->appendChild(
$doc->createTextNode( $checkoutsYearToDate[ $thisBranch ] )
);
$b->appendChild( $checkoutsYearToDateNode );
$r->appendChild( $b );
}
//Save to XML to file
$fh = fopen( $file, 'w' );
fwrite( $fh, $doc->saveXML() );
fclose( $fh );
ob_clean(); //Erase the output buffer
sleep( $sleepTime ); //Wait another x seconds before starting over again.
}
//End Main
function getBranchCodes( $dbh ) {
$branchCodes = array();
$sql = 'SELECT branchcode FROM branches';
$results = mysql_query( $sql );
while ( $row = mysql_fetch_assoc( $results ) ) {
$branchCodes[] = $row['branchcode'];
}
return $branchCodes;
}
# Message about this program and how to use it
function printUsage( $arguments ) {
$usage = "
This program generates an xml file of statistics from the Koha ILS for the kohabar firefox extension.
usage: $arguments[0] [-htdp]
-h : This (help) message
-t time : Amount of time in seconds between updates
-p path : The file to which the xml output will be saved
-f path : Same as using -p
-d : Print debugging messages to stderr
example: kohabard.pl -t 120 -p /usr/local/koha/intranet/htdocs/
\n";
fwrite( STDERR, $usage );
}
?>