Ticket #8374: scan-0-23.diff

File scan-0-23.diff, 4.4 KB (added by Dave Miller <mythtv@…>, 14 years ago)

patch against 0.23 branch

  • scan.php

     
    2525            AND hostname  = ?
    2626        ', hostname);
    2727
    28 // First delete any videos that do not exist anymore
     28// Scan for new videos first, so we can relocate any that moved before they get deleted
     29    foreach ($video_dirs as $path) {
     30        $files = array();
     31        $qpath = escapeshellarg($path);
     32        exec("find -L $qpath -name '.*' -prune -o -type f -print", $files, $retval);
     33        foreach ($files as $file) {
     34            if ( in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $Known_Exts) === FALSE )
     35                continue;
     36        // Check readable status
     37            if (!is_readable($file))
     38                continue;
     39        // Strip the directory off of the filename
     40            $fullpath = $file;
     41            $file = preg_replace('#^'.preg_quote($path).'/*#', '', $file);
     42        // Already exists at this location?
     43            $exists = $db->query_col('
     44                SELECT  COUNT(1)
     45                FROM    videometadata
     46                WHERE   filename = ?',
     47                $file);
     48            if ($exists != 0) {
     49                continue;
     50            }
     51        // Already exists at some other location? (file got moved?)
     52            $hashes = array();
     53            $qpath = escapeshellarg($fullpath);
     54            exec("mythhash $qpath", $hashes, $retval);
     55            $hash = $hashes[0]; // should only be one line anyway
     56            if ($hash != '') {
     57                $id = $db->query_col('SELECT intid FROM videometadata WHERE hash = ?', $hash);
     58                // If it already exists, just move it
     59                if ($id != 0) {
     60                    $db->query('UPDATE videometadata SET filename = ? WHERE intid = ?', $file, $id);
     61                    continue;
     62                }
     63            }
     64        // Add to the database
     65            $filename   = basename($file);
     66            $title      = filenametotitle($filename);
     67            $db->query('INSERT INTO videometadata ( title, filename, host, showlevel, browse, hash )
     68                                           VALUES (     ?,        ?,     ?,        1,      ?,    ? )',
     69                       strlen($title) > 0 ? $title : $filename,
     70                       $file,
     71                       hostname,
     72                       setting('VideoNewBrowsable', hostname),
     73                       $hash
     74                       );
     75        }
     76    }
     77
     78// Now that we've matched any orphans with their records in the database, we
     79// can delete any remaining videos from the db that do not exist anymore
    2980    $sh = $db->query('SELECT videometadata.intid,
    3081                             videometadata.filename,
    3182                             videometadata.coverfile
     
    66117        }
    67118    }
    68119
    69 // Now scan for any new ones
    70     foreach ($video_dirs as $path) {
    71         $files = array();
    72         exec("find -L $path -name '.*' -prune -o -type f -print", $files, $retval);
    73         foreach ($files as $file) {
    74             if ( in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $Known_Exts) === FALSE )
    75                 continue;
    76         // Check readable status
    77             if (!is_readable($file))
    78                 continue;
    79         // Strip the directory off of the filename
    80             $file = preg_replace('#^'.preg_quote($path).'/*#', '', $file);
    81         // Already exists?
    82             $exists = $db->query_col('
    83                 SELECT  COUNT(1)
    84                 FROM    videometadata
    85                 WHERE   filename = ?',
    86                 $file);
    87             if ($exists != 0) {
    88                 continue;
    89             }
    90         // Add to the database
    91             $filename   = basename($file);
    92             $title      = filenametotitle($filename);
    93             $db->query('INSERT INTO videometadata ( title, filename, host, showlevel, browse )
    94                                            VALUES (     ?,        ?,     ?,        1,      ? )',
    95                        strlen($title) > 0 ? $title : $filename,
    96                        $file,
    97                        hostname,
    98                        setting('VideoNewBrowsable', hostname)
    99                        );
    100         }
    101     }
    102 
    103120// Converts the filename to the title and cleans it up a little
    104121// This is a direct port of the C++ code from mythvideo
    105122    function filenametotitle($filename) {