<?php
set_time_limit(0);
error_reporting(0);
include 'inc/mysqli.php';
include 'inc/functions.php';
/*
if($_GET['hash'] != 'NG479KLRYEmw340FWP'){
	exit('Доступ ограничен!');
}
*/
$data  = $db->getAll("SELECT * FROM `pars` WHERE `status` = 1 ORDER BY RAND()");

foreach($data as $file){
	
	$file['title'] = str_replace('&#039;', '\'',$file['title']);
	$file['artist'] = str_replace('&#039;', '\'',$file['artist']);
	$file['title'] = str_replace('&#39;', '\'',$file['title']);
	$file['artist'] = str_replace('&#39;', '\'',$file['artist']);
	$file['title'] = str_replace('&amp;', '&',$file['title']);
	$file['artist'] = str_replace('&amp;', '&',$file['artist']);
	
	$db->query("INSERT INTO `files` SET 
	`filename` = ?s, `title` = ?s, `artist` = ?s, `year` = ?s,
	`name_full` = ?s, `playtime_string` = ?s,
	`filesize` = ?s, `bitrate` = ?s, `store` = ?s,
	`host` = ?s, `ya_url` = ?s, `date_add` = ?s, `lang` = ?s,
	`first_letter_a` = ?s, `first_letter_t` = ?s, `genre` = ?s, `rating` = ?s, `golos` = ?i",

	$file['filename'], trim($file['title']), trim($file['artist']),
	$file['year'], trim($file['artist']).' - '.trim($file['title']),
	$file['playtime_string'], $file['filesize'],
	$file['bitrate'], $file['store'], $file['host'], $file['ya_url'], time(), 
	$file['lang'], $file['first_letter_a'], $file['first_letter_t'], $file['genre'], mt_rand(3,5).'.'.mt_rand(1,9), mt_rand(10,100));
	
	$id = $db->insertId();
	
	$data_a  = $db->getAll("SELECT `canonical_artist` FROM `files` WHERE `artist` = ?s LIMIT 1", $file['artist']);
	if(!empty($data_a[0]['canonical_artist'])){
		$db->query("UPDATE `files` SET `canonical_artist` = ?i WHERE `id` = ?i", $data_a[0]['canonical_artist'], $id);
	}else{
		$db->query("UPDATE `files` SET `canonical_artist` = ?i WHERE `id` = ?i", $id, $id);
	}
	
	$db->query("INSERT INTO `sitemap_url` SET `url` = ?s, `type` = 'file', `position` = 50, `changefreq` = 'hourly', `priority` = '1.0'", 
	'/song/'.$id.'/'.rus2eng(str2url($file['artist'].' '.$file['title'])).'/');
	
	$db->query("DELETE FROM `pars` WHERE `id` = ?i", $file['id']);
}



?>