/**
  * @covers \PressBooks\Utility\scandir_by_date
  */
 public function test_scandir_by_date()
 {
     $files = \PressBooks\Utility\scandir_by_date(__DIR__);
     $this->assertTrue(is_array($files));
     $this->assertContains(basename(__FILE__), $files);
     $this->assertNotContains('.htaccess', $files);
 }
/**
 * Scan the export directory, return latest of each file type
 * 
 * @return array 
 */
function latest_exports()
{
    $suffix = array('._3.epub', '.epub', '.pdf', '.mobi', '.hpub', '.icml', '.html', '.xml');
    $dir = \PressBooks\Export\Export::getExportFolder();
    $files = array();
    // group by extension, sort by date newest first
    foreach (\PressBooks\Utility\scandir_by_date($dir) as $file) {
        $ext = strstr($file, '.');
        $files[$ext][] = $file;
    }
    // get only one of the latest of each type
    $latest = array();
    foreach ($suffix as $value) {
        if (array_key_exists($value, $files)) {
            $latest[$value] = $files[$value][0];
        }
    }
    // @TODO filter these results against user prefs
    return $latest;
}
 /**
  * Create $this->outputPath
  *
  * @return bool
  */
 function convert()
 {
     // Get most recent Epub file
     $input_folder = static::getExportFolder();
     $input_path = false;
     $files = \PressBooks\Utility\scandir_by_date($input_folder);
     foreach ($files as $file) {
         if (preg_match('/\\.epub/i', $file)) {
             $input_path = $input_folder . $file;
             break;
         }
     }
     if (!$input_path) {
         $this->logError("Could not convert to MOBI because no EPUB file was found in {$input_folder}");
         return false;
     }
     // Convert
     $filename = $this->timestampedFileName('.mobi');
     $this->outputPath = $filename;
     $command = PB_KINDLEGEN_COMMAND . ' ' . escapeshellcmd($input_path) . ' -locale en -o ' . escapeshellcmd(basename($this->outputPath)) . ' 2>&1';
     $output = array();
     $return_var = 0;
     exec($command, $output, $return_var);
     // Check build results
     $last_line = array_filter($output);
     $last_line = strtolower(end($last_line));
     if (false !== strpos($last_line, 'mobi file built successfully')) {
         // Ok!
         return true;
     } elseif (false !== strpos($last_line, 'mobi file built with warnings')) {
         // Built, but has warnings
         $this->hasWarnings = true;
         $this->logError(implode("\n", $output));
         return true;
     } else {
         // Error
         $this->logError(implode("\n", $output));
         return false;
     }
 }
/**
 * Scan the export directory, return latest of each file type
 * 
 * @return array 
 */
function latest_exports() {
	$suffix = array(
	    '._3.epub',
	    '.epub',
	    '.pdf',
	    '.mobi',
	    '.hpub',
	    '.icml',
	    '.html',
	    '.xml',
	    '._vanilla.xml',
	    '._oss.pdf',
	);

	$dir = \PressBooks\Export\Export::getExportFolder();

	$files = array();

	// group by extension, sort by date newest first 
	foreach ( \PressBooks\Utility\scandir_by_date( $dir ) as $file ) {
		// only interested in the part of filename starting with the timestamp
		preg_match( '/-\d{10,11}(.*)/', $file, $matches );
		// grab the first captured parenthisized subpattern
		$ext = $matches[1];
		$files[$ext][] = $file;
	}

	// get only one of the latest of each type
	$latest = array();

	foreach ( $suffix as $value ) {
		if ( array_key_exists( $value, $files ) ) {
			$latest[$value] = $files[$value][0];
		}
	}
	// @TODO filter these results against user prefs

	return $latest;
}