nnscripts = $nnscripts; // Set the database variable $this->db = $db; } /** * 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 < NOW() - INTERVAL %d HOUR", LIMIT) : '' ) ); $result = $this->db->queryDirect( $sql ); $this->nnscripts->display( ( $result ? "Done" : "Error" ) . PHP_EOL ); } /** * Remove parts where the binary is missing * * @return void */ private function removePartsWithMissingBinary() { // Remove parts with missing binary $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", ( ( defined('LIMIT') && is_int(LIMIT) && 0 < LIMIT ) ? sprintf("AND p.dateadded < NOW() - INTERVAL %d HOUR", LIMIT) : '' ) ); $result = $this->db->query( $sql ); if( is_array( $result ) && 0 < count( $result ) ) { $this->nnscripts->display( sprintf( "Removing %d parts with missing binaries: ", count( $result ) ) ); // Build the total ID list $ids = array(); $counter = 0; foreach( $result AS $row ) { $ids[] = $row['ID']; $counter++; if( 100 === $counter ) { $this->nnscripts->display( "." ); $sql = sprintf( "DELETE FROM parts WHERE ID IN (%s)", implode(',', $ids) ); $this->db->queryDirect( $sql ); $ids = array(); $counter = 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 ); } } } try { // Init $scriptName = 'Remove parts which are not linked to a binary'; $scriptVersion = '0.1'; // 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; }