Add total, new, purged counts to output. Output json if from browser.
This commit is contained in:
		
							parent
							
								
									0d74e59634
								
							
						
					
					
						commit
						8b7ccda1b1
					
				| 
						 | 
				
			
			@ -5,8 +5,14 @@ require dirname(__FILE__) . "/../variables.php";
 | 
			
		|||
$dbbooks = array();
 | 
			
		||||
$parsedbooks = array();
 | 
			
		||||
 | 
			
		||||
$cli = ( php_sapi_name() != "cli" ) ? false : true;
 | 
			
		||||
 | 
			
		||||
$count_total = 0;
 | 
			
		||||
$count_new = 0;
 | 
			
		||||
$count_purged = 0;
 | 
			
		||||
 | 
			
		||||
function walkBooks($path = BOOKDIR) {
 | 
			
		||||
   global $globaldbh, $parsedbooks;
 | 
			
		||||
   global $globaldbh, $parsedbooks, $count_new, $count_total;
 | 
			
		||||
   $contents = glob($path . "/*");
 | 
			
		||||
   foreach ( $contents as $item ) {
 | 
			
		||||
      if ( is_dir($item) ) {
 | 
			
		||||
| 
						 | 
				
			
			@ -16,12 +22,17 @@ function walkBooks($path = BOOKDIR) {
 | 
			
		|||
         $dbfile = basename($item);
 | 
			
		||||
         $dbmtime = date("Y-m-d H:i:s", filemtime($item));
 | 
			
		||||
         $parsedbooks[] = $dbpath . (($path == BOOKDIR) ? "" : "/") . $dbfile;
 | 
			
		||||
         $query = "INSERT INTO books (path, filename, mtime) VALUES(:path, :filename, :mtime) ON DUPLICATE KEY UPDATE id=id";
 | 
			
		||||
         $query = "INSERT INTO books (path, filename, mtime) VALUES(:path, :filename, :mtime)";
 | 
			
		||||
         $sth = $globaldbh->prepare($query);
 | 
			
		||||
         $sth->bindValue(":path", $dbpath, PDO::PARAM_STR);
 | 
			
		||||
         $sth->bindValue(":filename", $dbfile, PDO::PARAM_STR);
 | 
			
		||||
         $sth->bindValue(":mtime", $dbmtime, PDO::PARAM_STR);
 | 
			
		||||
         $sth->execute();
 | 
			
		||||
         try {
 | 
			
		||||
            $sth->execute();
 | 
			
		||||
            $count_new++;
 | 
			
		||||
         } catch (PDOException $e) {
 | 
			
		||||
         }
 | 
			
		||||
         $count_total++;
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -40,7 +51,22 @@ foreach ( $dbbooks as $dbbook ) {
 | 
			
		|||
   if ( !is_file(BOOKDIR . $dbbook) ) {
 | 
			
		||||
      $sth->bindValue(":target", $dbbook, PDO::PARAM_STR);
 | 
			
		||||
      $sth->execute();
 | 
			
		||||
      $count_purged++;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if ( $cli ) {
 | 
			
		||||
   echo "New Books: {$count_new}\n";
 | 
			
		||||
   echo "Purged Books: {$count_purged}\n";
 | 
			
		||||
   echo "Total Books: {$count_total}\n";
 | 
			
		||||
} else {
 | 
			
		||||
   $data = array();
 | 
			
		||||
   $data["new"] = $count_new;
 | 
			
		||||
   $data["purged"] = $count_purged;
 | 
			
		||||
   $data["total"] = $count_total;
 | 
			
		||||
   header('Content-Type: application/json');
 | 
			
		||||
   echo json_encode($data);
 | 
			
		||||
   exit();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// vim: set sw=3 ts=3:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user