]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/james-heinrich/getid3/demos/getid3.demo.dirscan.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / james-heinrich / getid3 / demos / getid3.demo.dirscan.php
1 <?php
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 /////////////////////////////////////////////////////////////////////////////////
8 ///                                                                            //
9 // getid3.dirscan.php - tool for batch media file processing with getID3()     //
10 //                                                                            ///
11 /////////////////////////////////////////////////////////////////////////////////
12 ///                                                                            //
13 //  Directory Scanning and Caching CLI tool by Karl G. Holz <newaeonØmac*com>  //
14 //                                                                            ///
15 /////////////////////////////////////////////////////////////////////////////////
16 /**
17 * This is a directory scanning and caching cli tool for getID3().
18 *
19 * use like so for the default sqlite3 database, which is hidden:
20 *
21 * cd <path you want to start scanning from>
22 * php <path to getid3 files>/getid3.dirscan.php
23 *
24 * or
25 *
26 * php <path to getid3 files>/getid3.dirscan.php <dir to scan> <file ext in csv list>
27 *
28 * Supported Cache Types    (this extension)
29 *
30 *   SQL Databases:
31 *
32 *   cache_type
33 *   -------------------------------------------------------------------
34 *    mysql
35
36 $cache='mysql';
37 $database['host']='';
38 $database['database']='';
39 $database['username']='';
40 $database['password']='';
41 $database['table']='';
42
43 *    sqlite3
44
45 $cache='sqlite3';
46 $database['table']='getid3_cache';
47 $database['hide']=true;
48
49 */
50 $dir      = $_SERVER['PWD'];
51 $media    = array('mp4', 'm4v', 'mov', 'mp3', 'm4a', 'jpg', 'png', 'gif');
52 $database = array();
53 /**
54 * configure the database bellow
55 */
56 // sqlite3
57 $cache             = 'sqlite3';
58 $database['table'] = 'getid3_cache';
59 $database['hide']  = true;
60 /**
61  * mysql
62 $cache                = 'mysql';
63 $database['host']     = '';
64 $database['database'] = '';
65 $database['username'] = '';
66 $database['password'] = '';
67 $database['table']    = '';
68 */
69
70 /**
71 * id3 tags class file
72 */
73 require_once(dirname(__FILE__).'/getid3.php');
74 /**
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
77 *
78 *
79 * @package dirscan
80 * @author Karl Holz
81 *
82 */
83
84 class dirscan {
85         /**
86         * type_brace()  * Might not work on Solaris and other non GNU systems *
87         *
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
93         */
94         private function type_brace($dir, $search=array()) {
95                 $dir = str_replace(array('///', '//'), array('/', '/'), $dir);
96                 if (!is_dir($dir)) {
97                         return null;
98                 }
99                 if (!is_array($search)) {
100                         $e = explode(',', $search);
101                 } elseif (count($search) < 1) {
102                         return null;
103                 } else {
104                         $e = $search;
105                 }
106                 $ext = array();
107                 foreach ($e as $new) {
108                         $ext[] = strtolower(trim($new));
109                         $ext[] = strtoupper(trim($new));
110                 }
111                 $b = $dir.'/*.{'.implode(',', $ext).'}';
112                 return $b;
113         }
114
115         /**
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
121         */
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
124                         case '/':
125                         case '/var':
126                         case '/etc':
127                         case '/home':
128                         case '/usr':
129                         case '/root':
130                         case '/private/etc':
131                         case '/private/var':
132                         case '/etc/apache2':
133                         case '/home':
134                         case '/tmp':
135                         case '/var/log':
136                                 return null;
137                                 break;
138                         default: // scan 4 directories deep
139                                 if (!is_dir($root)) {
140                                 return null;
141                                 }
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));
143                                 break;
144                 }
145                 if (count($dirs) < 1) {
146                         $dirs = array($root);
147                 }
148                 return $dirs;
149         }
150
151         /**
152         *  file_check() check the number of file that are found that match the brace search
153         *
154         * @param string $search
155         * @return mixed
156         */
157         private function file_check($search) {
158                 $t = array();
159                 $s = glob($search, GLOB_BRACE);
160                 foreach ($s as $file) {
161                         $t[] = str_replace(array('///', '//'), array('/', '/'), $file);
162                 }
163                 if (count($t) > 0) {
164                         return $t;
165                 }
166                 return null;
167         }
168
169         function getTime() {
170                 return microtime(true);
171                 // old method for PHP < 5
172                 //$a = explode(' ', microtime());
173                 //return (double) $a[0] + $a[1];
174         }
175
176
177         /**
178         *
179         * @param type $dir
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,
183         */
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
187                         case 'sqlite3':
188                                 if (!class_exists('getID3_cached_sqlite3')) {
189                                         require_once(dirname(__FILE__)).'/extension.cache.sqlite3.php';
190                                 }
191                                 $id3 = new getID3_cached_sqlite3($opt['table'], $opt['hide']);
192                                 break;
193                         case 'mysql':
194                                 if (!class_exists('getID3_cached_mysql')) {
195                                         require_once(dirname(__FILE__)).'/extension.cache.mysql.php';
196                                 }
197                                 $id3 = new getID3_cached_mysql($opt['host'], $opt['database'], $opt['username'], $opt['password'], $opt['table']);
198                                 break;
199                 // I'll leave this for some one else
200                         //case 'dbm':
201                         //      if (!class_exists('getID3_cached_dbm')) {
202                         //              require_once(dirname(__FILE__)).'/extension.cache.dbm.php';
203                         //      }
204                         //      die(' This has not be implemented, sorry for the inconvenience');
205                         //      break;
206                         default:
207                                 die(' You have selected an Invalid cache type, only "sqlite3" and "mysql" are valid'."\n");
208                                 break;
209                 }
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";
221                                                         $id3->analyze($f);
222                                                         $count['file']++;
223                                                 }
224                                                 $count['dir']++;
225                                         } else {
226                                                 echo 'Failed to get files '."\n";
227                                         }
228                                 } else {
229                                         echo 'Failed to create match string '."\n";
230                                 }
231                         }
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";
240                 } else {
241                         echo ' failed to get directories '."\n";
242                 }
243         }
244 }
245
246 if (PHP_SAPI === 'cli') {
247         if (count($argv) == 2) {
248                 if (is_dir($argv[1])) {
249                         $dir = $argv[1];
250                 }
251                 if (count(explode(',', $argv[2])) > 0) {
252                         $media = $arg[2];
253                 }
254         }
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);
258 }