nnscripts = $nnscripts; // Set the database variable $this->db = $db; // Add the "now" date $this->now = new DateTime(); if( defined('LIMIT') && is_int(LIMIT) && 0 < LIMIT ) { // substract the limit hours $this->now->sub( new DateInterval('PT'. LIMIT .'H') ); } } /** * Remove all the parts which are not linked to a binary * * @return void */ public function cleanup() { // Remove old parts without binaries $this->removePartsWithoutBinary(); // Remove parts where the binary is missing $this->removePartsWithMissingBinary(); } /** * Remove parts without a binary linked * * @return void */ private function removePartsWithoutBinary() { $this->nnscripts->display( "Removing old part records which are not linked to a binary: " ); // Build the sql $sql = sprintf( "DELETE FROM parts WHERE parts.binaryID = 0 %s", ( ( defined('LIMIT') && is_int(LIMIT) && 0 < LIMIT ) ? sprintf("AND parts.dateadded < '%s'", $this->now->format('Y-m-d H:i:s')) : '' ) ); $result = $this->db->queryDirect( $sql ); $this->nnscripts->display( ( $result ? "Done" : "Error" ) . PHP_EOL ); } /** * Get the total count of parts to remove * * @return int */ protected function getTotalPartsToRemove() { // Init $ret = 0; // Get the total to remove $sql = sprintf("SELECT count(*) AS total FROM parts AS p LEFT JOIN binaries AS b ON (b.ID = p.binaryID) WHERE b.ID IS NULL %s", ( ( defined('LIMIT') && is_int(LIMIT) && 0 < LIMIT ) ? sprintf("AND p.dateadded < '%s'", $this->now->format('Y-m-d H:i:s')) : '' ) ); $result = $this->db->query( $sql ); if( is_array($result) && 1 === count($result) ) { $ret = $result[0]['total']; } // Return return $ret; } /** * Remove parts where the binary is missing * * @return void */ private function removePartsWithMissingBinary() { // The query limit $queryLimit = ( defined('QUERYLIMIT') && is_int(QUERYLIMIT) && 0 < QUERYLIMIT ? QUERYLIMIT : 10000 ); // Get the total parts to remove $total = $this->getTotalPartsToRemove(); if( 0 < $total ) { $line = sprintf( "Removing %s parts with missing binaries", number_format($total, 0, '.', '') ); if( $total > $queryLimit ) $line .= sprintf( ' (in groups of %d parts)', $queryLimit ); $line .= ': '; $this->nnscripts->display( $line ); // Set the lastID $lastId = null; $ids = array(); // Loop until there are no parts to remove while( 0 < $total ) { // Build the query $sql = sprintf("SELECT DISTINCT p.ID FROM parts AS p LEFT JOIN binaries AS b ON (b.ID = p.binaryID) WHERE b.ID IS NULL %s %s ORDER BY p.ID ASC LIMIT %d", ( null !== $lastId ? sprintf('AND p.ID > %d', $lastId) : '' ), ( ( defined('LIMIT') && is_int(LIMIT) && 0 < LIMIT ) ? sprintf("AND p.dateadded < '%s'", $this->now->format('Y-m-d H:i:s')) : '' ), $queryLimit ); $result = $this->db->query( $sql ); if( is_array( $result ) && 0 < count( $result ) ) { // Build the total ID list $ids = array(); $counter = 0; foreach( $result AS $row ) { $ids[] = $lastId = $row['ID']; $counter++; if( $queryLimit === $counter ) { $this->nnscripts->display( "." ); $sql = sprintf( "DELETE FROM parts WHERE ID IN (%s)", implode(',', $ids) ); $this->db->queryDirect( $sql ); $ids = array(); $counter = 0; } } // Cleanup unset( $result ); // Lower the total $total -= $queryLimit; } else { // Query without result? $total = 0; } } // And remove the rest if( 0 < count($ids) ) { $this->nnscripts->display( "." ); $sql = sprintf( "DELETE FROM parts WHERE ID IN (%s)", implode(',', $ids) ); $this->db->queryDirect( $sql ); } $this->nnscripts->display( PHP_EOL ); } else { $this->nnscripts->display( 'No parts with missing binaries to remove.'. PHP_EOL ); } } } try { // Init $scriptName = 'Remove parts which are not linked to a binary'; $scriptVersion = '0.2'; // Load the NNscript class $nnscripts = new NNScripts( $scriptName, $scriptVersion ); // Display the header $nnscripts->displayHeader(); // Load the application part $db = new DB; if( !$db ) throw new Exception("Error loading database library"); // Load the categoryReleases class $cr = new removeParts( $nnscripts, $db ); $cr->cleanup(); } catch( Exception $e ) { echo $e->getMessage() . PHP_EOL; }