2 /////////////////////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////////////////////
9 // getid3.dirscan.php - tool for batch media file processing with getID3() //
11 /////////////////////////////////////////////////////////////////////////////////
13 // Directory Scanning and Caching CLI tool by Karl G. Holz <newaeonØmac*com> //
15 /////////////////////////////////////////////////////////////////////////////////
17 * This is a directory scanning and caching cli tool for getID3().
19 * use like so for the default sqlite3 database, which is hidden:
21 * cd <path you want to start scanning from>
22 * php <path to getid3 files>/getid3.dirscan.php
26 * php <path to getid3 files>/getid3.dirscan.php <dir to scan> <file ext in csv list>
28 * Supported Cache Types (this extension)
33 * -------------------------------------------------------------------
38 $database['database']='';
39 $database['username']='';
40 $database['password']='';
41 $database['table']='';
46 $database['table']='getid3_cache';
47 $database['hide']=true;
50 $dir = $_SERVER['PWD'];
51 $media = array('mp4', 'm4v', 'mov', 'mp3', 'm4a', 'jpg', 'png', 'gif');
54 * configure the database bellow
58 $database['table'] = 'getid3_cache';
59 $database['hide'] = true;
63 $database['host'] = '';
64 $database['database'] = '';
65 $database['username'] = '';
66 $database['password'] = '';
67 $database['table'] = '';
73 require_once(dirname(__FILE__).'/getid3.php');
75 * dirscan scans all directories for files that match your selected filetypes into the cache database
76 * this is useful for a lot of media files
86 * type_brace() * Might not work on Solaris and other non GNU systems *
88 * Configures a filetype list for use with glob searches,
89 * will match uppercase or lowercase extensions only, no mixing
90 * @param string $dir directory to use
91 * @param mixed cvs list of extentions or an array
92 * @return string or null if checks fail
94 private function type_brace($dir, $search=array()) {
95 $dir = str_replace(array('///', '//'), array('/', '/'), $dir);
99 if (!is_array($search)) {
100 $e = explode(',', $search);
101 } elseif (count($search) < 1) {
107 foreach ($e as $new) {
108 $ext[] = strtolower(trim($new));
109 $ext[] = strtoupper(trim($new));
111 $b = $dir.'/*.{'.implode(',', $ext).'}';
116 * this function will search 4 levels deep for directories
117 * will return null on failure
118 * @param string $root
119 * @return array return an array of dirs under root
120 * @todo figure out how to block tabo directories with ease
122 private function getDirs($root) {
123 switch ($root) { // return null on tabo directories, add as needed -> case {dir to block }: this is not perfect yet
138 default: // scan 4 directories deep
139 if (!is_dir($root)) {
142 $dirs = array_merge(glob($root.'/*', GLOB_ONLYDIR), glob($root.'/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*/*/*', GLOB_ONLYDIR), glob($root.'/*/*/*/*/*/*/*', GLOB_ONLYDIR));
145 if (count($dirs) < 1) {
146 $dirs = array($root);
152 * file_check() check the number of file that are found that match the brace search
154 * @param string $search
157 private function file_check($search) {
159 $s = glob($search, GLOB_BRACE);
160 foreach ($s as $file) {
161 $t[] = str_replace(array('///', '//'), array('/', '/'), $file);
170 return microtime(true);
171 // old method for PHP < 5
172 //$a = explode(' ', microtime());
173 //return (double) $a[0] + $a[1];
180 * @param type $match search type name extentions, can be an array or csv list
181 * @param type $cache caching extention, select one of sqlite3, mysql, dbm
182 * @param array $opt database options,
184 function scan_files($dir, $match, $cache='sqlite3', $opt=array('table'=>'getid3_cache', 'hide'=>true)) {
185 $Start = self::getTime();
186 switch ($cache) { // load the caching module
188 if (!class_exists('getID3_cached_sqlite3')) {
189 require_once(dirname(__FILE__)).'/extension.cache.sqlite3.php';
191 $id3 = new getID3_cached_sqlite3($opt['table'], $opt['hide']);
194 if (!class_exists('getID3_cached_mysql')) {
195 require_once(dirname(__FILE__)).'/extension.cache.mysql.php';
197 $id3 = new getID3_cached_mysql($opt['host'], $opt['database'], $opt['username'], $opt['password'], $opt['table']);
199 // I'll leave this for some one else
201 // if (!class_exists('getID3_cached_dbm')) {
202 // require_once(dirname(__FILE__)).'/extension.cache.dbm.php';
204 // die(' This has not be implemented, sorry for the inconvenience');
207 die(' You have selected an Invalid cache type, only "sqlite3" and "mysql" are valid'."\n");
210 $count = array('dir'=>0, 'file'=>0);
211 $dirs = self::getDirs($dir);
212 if ($dirs !== null) {
213 foreach ($dirs as $d) {
214 echo ' Scanning: '.$d."\n";
215 $search = self::type_brace($d, $match);
216 if ($search !== null) {
217 $files = self::file_check($search);
218 if ($files !== null) {
219 foreach ($files as $f) {
220 echo ' * Analyzing '.$f.' '."\n";
226 echo 'Failed to get files '."\n";
229 echo 'Failed to create match string '."\n";
232 echo '**************************************'."\n";
233 echo '* Finished Scanning your directories '."\n*\n";
234 echo '* Directories '.$count['dir']."\n";
235 echo '* Files '.$count['file']."\n";
236 $End = self::getTime();
237 $t = number_format(($End - $Start) / 60, 2);
238 echo '* Time taken to scan '.$dir.' '.$t.' min '."\n";
239 echo '**************************************'."\n";
241 echo ' failed to get directories '."\n";
246 if (PHP_SAPI === 'cli') {
247 if (count($argv) == 2) {
248 if (is_dir($argv[1])) {
251 if (count(explode(',', $argv[2])) > 0) {
255 echo ' * Starting to scan directory: '.$dir."\n";
256 echo ' * Using default media types: '.implode(',', $media)."\n";
257 dirscan::scan_files($dir, $media, $cache, $database);