$row->nid)); $file = db_fetch_array(db_query("SELECT * FROM {files} WHERE nid=%d limit 1", $node->nid)); $filepath = file_create_path($file['filepath']); $filename = basename($filepath); if (!$file['fid'] || !file_exists($filepath)) { $errors[] = $node; drupal_set_message( t('%node was NOT be upgraded because the file "%filepath" could not be found.', array('%node' => l($node->title, 'node/'. $node->nid), '%filepath' => theme('placeholder', $filepath))), 'error' ); } else { $audio = db_fetch_array(db_query("SELECT * FROM {audio_node_metadata} WHERE fid=%d", $file['fid'])); $id3 = audio_46to47_read_id3tags($filepath); $fileinfo = $id3['fileinfo']; var_dump($audio); var_dump($fileinfo); // audio files table db_query("INSERT INTO {audio_file} (vid, origname, filename, filepath, filemime, filesize) VALUES (%d, '%s', '%s', '%s', '%s', '%s')", $node->nid, $filename, $filename, $filepath, $file['filemime'], filesize($filepath)); // audio table db_queryd("INSERT INTO {audio} (nid, vid, title_format, play_count, downloadable, fileformat, sample_rate, channel_mode, bitrate, bitrate_mode, playtime) VALUES (%d, %d, '%s', %d, %d, '%s', %d, '%s', %f, '%s', '%s')", $node->nid, $node->vid, $node->title, $audio['playcount'], $audio['downloadable'], $fileinfo['fileformat'], $fileinfo['sample_rate'], $fileinfo['channel_mode'], $fileinfo['bitrate'], $fileinfo['bitrate_mode'], $fileinfo['playtime']); foreach(array('artist', 'title', 'album', 'year', 'track', 'genre', 'comment') as $tag) { db_query("INSERT INTO {audio_metadata} (vid, tag, value, clean) VALUES (%d, '%s', '%s', '%s')", $node->nid, $tag, $audio[$tag], audio_46to47_clean_tag($audio[$tag])); } drupal_set_message(t('%node was upgraded successfully.', array('%node' => l($node->title, 'node/'. $node->nid)))); } } if ($errors) { drupal_set_message(t('There were errors upgrading the audio module.'), 'error'); } else { drupal_set_message(t('The audio module was upgraded successfully. You can now remove the audio_node_metadata table.')); } } /** * Uses ID3 tags to get information about an audio file... * Returns one masive array (or false) if the file is not found. * * @param $filepath * string full path to audio file to examine * @return * array with two sub arrays keyed to 'tags' and 'fileinfo'. */ function audio_46to47_read_id3tags($filepath) { $getid3_path = variable_get('audio_getid3_path', drupal_get_path('module', 'audio') .'/getid3/getid3/'); require_once($getid3_path .'getid3.php'); $getID3 = new getID3; $getID3->encoding = 'UTF-8'; $getID3->encoding_id3v1 = 'ISO-8859-1'; $getID3->option_tags_html = FALSE; // Analyze file $info = $getID3->analyze($filepath); // Copies data from all subarrays of [tags] into [comments] so metadata is // all available in one location for all tag formats metainformation is // always available under [tags] even if this is not called. getid3_lib::CopyTagsToComments($info); // warnings if (!empty($info['warning']) && variable_get('audio_getid3_show_warnings', FALSE)) { $warning = t('While reading the ID3 tags, the following warnings were encountered:'); $warning .= ''; drupal_set_message($warning, 'error'); } // report errors and then exit if (isset($info['error'])) { $error = t("The following errors where encountered while reading the file's ID3 tags: "); $error .= ''; var_dump($error); return FALSE; } // copy the id3 tags $ret['tags'] = array(); foreach ((array) $info['comments'] as $key => $value ) { $ret['tags'][$key] = $value[0]; } // now copy file info $ret['fileinfo'] = array( 'fileformat' => $info['fileformat'], 'mimetype' => $info['mime_type'], 'playtime' => $info['playtime_string'], 'bitrate' => $info['audio']['bitrate'], 'bitrate_mode' => $info['audio']['bitrate_mode'], 'channel_mode' => $info['audio']['channelmode'], 'sample_rate' => $info['audio']['sample_rate'], ); return $ret; } /** * Take a tag and force it to be a-z 0-9 _ - * @param $string * ID3 tag value * @return * cleaned up tag value for URL or database */ function audio_46to47_clean_tag($string) { // if we've got characters besides 0-9 A-Z a-z hyphen or underscore, replace them if (preg_match('/[^-\w]/', $string)) { // remove accents $string = strtr($string, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy'); // convert to equivalent chars $string = strtr($string, array('Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u')); // remove anything else that's not alphanumeric and replace it with an underscore $string = preg_replace('/[^-\w]+/', '_', $string); } // wack any leading or trailing underscores $string = trim($string, '_'); // finally, make it to lower case return strtolower($string); }