<?php
	
	//LOAD MOVIE LENS DATA SET 10M
	
	//------- DB CONFIGURATION -------- 
	//*****************************************************************************************
	$host = "your host name";
	$user = "your database user name";
	$psw = "your database user password";
	$db = "your database name";
	//*****************************************************************************************	
	
	$conexion = mysql_connect($host, $user, $psw);
	mysql_select_db($db, $conexion);
	$genres = array ("unknown", "Action", "Adventure", "Animation", "Children", "Comedy", "Crime", "Documentary", "Drama", "Fantasy", "Film-Noir", "Horror", "Musical", "Mystery", "Romance", "Sci-Fi", "Thriller", "War", "Western");
	
	load_genres_movielens($genres);
	load_users();
	load_movies();
	load_movies_genres();
	load_tags();
	load_ratings();
	load_actors();
	load_movies_actors();
	load_directors();
	load_movies_directors();
	load_writers();
	load_movies_writers();
	load_movies_extra();
	
	mysql_close($conexion);				
	
	//----------- FUNCTIONS ----------- 
	//*****************************************************************************************
	function load_genres_movielens($genres) {
	
		echo "Loading genres...\n";
			
		foreach ($genres as $genre) {			
			mysql_query("INSERT INTO genres (genre) VALUES ('".$genre."')");
		}
		
		echo "Genres loaded. Done!\n";
	
	}
	//*****************************************************************************************
	function load_users() {
		
		echo "Loading users...\n";
		
		for ($i = 0; $i < 71567; $i++) { // 71567 es el numero de usuarios que usa el dataset 10M, no se tiene ninguna otra info del user
			mysql_query("INSERT INTO users (id) VALUES ('".$i."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
		}
		echo "\n".$i." users loaded. Done!\n";					
	}			
	//*****************************************************************************************
	function load_movies(){
		
		echo "Loading movies...\n";
		
		$i = 0;	
		
		$lines = file('movies.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);
			$datos[1] = str_replace("'", "\'", $datos[1]);
		
			$title = substr($datos[1], 0, -6); 
			$year = substr($datos[1], -5, -1);				

			mysql_query("INSERT INTO movies (id, title, year) VALUES ('".$datos[0]."', '".$title."', '".$year."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/10681 movies loaded. Done!\n";				
	}
	//*****************************************************************************************
	function load_movies_genres(){
	
		echo "Loading movies_genres...\n";
		
		$i = 0;		
		
		$handle = @fopen("movies.dat", "r");
		if ($handle) {
			while (($buffer = fgets($handle, 4096)) !== false) {
				$datos = explode("::", $buffer); 
				
				$genres = explode("|", $datos[2]);
				
				foreach ($genres as $genre_num => $genre) {
					
					$genre = str_replace("'", "\'", $genre);
					$genre = trim($genre);
					
					$sql = "SELECT id FROM genres WHERE genre = '".$genre."'";						
					$result = mysql_query($sql);
					$row = @mysql_fetch_array($result);
					
					mysql_query("INSERT INTO movies_genres (movie_id, genre_id) VALUES ('".$datos[0]."', '".$row['id']."')");						
				
				}
				
				if ($i % 1000 == 0) {
					echo ".";
				}
				$i++;					
			}
			if (!feof($handle)) {
				echo "Error: unexpected fgets() fail\n";
			}
			fclose($handle);
		}
		echo "\n".$i."/10681 movies_genres loaded. Done!\n";			
	}
	//*****************************************************************************************
	function load_tags(){
		
		echo "Loading tags...\n";
		
		$i = 0;
	
		$lines = file('tags.dat');
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line); 
			$tag = str_replace("'", "\'", $datos[2]);

			$sql = "SELECT id FROM tag WHERE tag = '".$tag."'";						
			$result = mysql_query($sql);
			$cant_rows = mysql_num_rows($result);
			
			if ($cant_rows > 0) {
				$row = @mysql_fetch_array($result);
				$tag_id = $row['id'];				
			} else {
				mysql_query("INSERT INTO tag (tag) VALUES ('".$tag."')");
				$tag_id = mysql_insert_id();
			}
			$date = @date("Y-m-d", $datos[3]);
			mysql_query("INSERT INTO tags (user_id, movie_id, tag_id, timestamp) VALUES ('".$datos[0]."', '".$datos[1]."', '".$tag_id."', '".$date."')");
			
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;					
		}
		echo "\n".$i."/95580 tags loaded. Done!\n";				
	}
	//*****************************************************************************************
	function load_ratings(){
		
		echo "Loading ratings";
		
		$i = 0;				
		
		$handle = @fopen("ratings.dat", "r");
		if ($handle) {
			while (($buffer = fgets($handle, 4096)) !== false) {
				$datos = explode("::", $buffer); 
				$date = @date("Y-m-d", $datos[3]);
				
				mysql_query("INSERT INTO ratings (user_id, movie_id, rating, timestamp) VALUES ('".$datos[0]."', '".$datos[1]."', '".$datos[2]."', '".$date."')");
			
				if ($i % 1000 == 0) {
					echo ".";
				}
				$i++;					
			}
			if (!feof($handle)) {
				echo "Error: unexpected fgets() fail\n";
			}
			fclose($handle);
		}
		echo "\n".$i."/10000054 ratings loaded. Done!\n";
		
	}
	//*****************************************************************************************
	function load_actors(){
		
		echo "Loading actors...\n";
		
		$i = 0;	
		
		$lines = file('actors.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);			

			mysql_query("INSERT INTO actors (id, name) VALUES ('".$datos[0]."', '".$datos[1]."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/57658 actors loaded. Done!\n";				
	}
	//*****************************************************************************************
	function load_movies_actors(){
		
		echo "Loading movies_actors...\n";
		
		$i = 0;	
		
		$lines = file('movies_actors.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);			

			mysql_query("INSERT INTO movies_actors (movie_id, actor_id) VALUES ('".$datos[0]."', '".$datos[1]."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/28577 movies_actors loaded. Done!\n";			
	}
	//*****************************************************************************************
	function load_directors(){
		
		echo "Loading directors...\n";
		
		$i = 0;	
		
		$lines = file('directors.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);			

			mysql_query("INSERT INTO directors (id, name) VALUES ('".$datos[0]."', '".$datos[1]."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/4005 directors loaded. Done!\n";				
	}
	//*****************************************************************************************
	function load_movies_directors(){
		
		echo "Loading movies_directors...\n";
		
		$i = 0;	
		
		$lines = file('movies_directors.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);			

			mysql_query("INSERT INTO movies_directors (movie_id, director_id) VALUES ('".$datos[0]."', '".$datos[1]."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/9934 movies_directors loaded. Done!\n";				
	}
	//*****************************************************************************************
	function load_writers(){
		
		echo "Loading writers...\n";
		
		$i = 0;	
		
		$lines = file('writers.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);			

			mysql_query("INSERT INTO writers (id, name) VALUES ('".$datos[0]."', '".$datos[1]."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/7951 writers loaded. Done!\n";				
	}
	//*****************************************************************************************
	function load_movies_writers(){
		
		echo "Loading movies_writers...\n";
		
		$i = 0;	
		
		$lines = file('movies_writers.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);			

			mysql_query("INSERT INTO movies_writers (movie_id, writer_id) VALUES ('".$datos[0]."', '".$datos[1]."')");
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/15063 movies_writers loaded. Done!\n";				
	}
	//*****************************************************************************************
	function load_movies_extra(){
		
		echo "Loading movies_extra...\n";
		
		$i = 0;	
		
		$lines = file('movies_extra.dat');				
		foreach ($lines as $line_num => $line) { 
			$datos = explode("::", $line);		
			
			mysql_query("UPDATE movies SET imdb_url = ".$datos[1].", rated_imdb = ".$datos[2].", rating_imdb = ".$datos[3].", rating_count_imdb = ".$datos[4].", imdb_id = ".$datos[5]." WHERE id = ".$datos[0]);
			if ($i % 1000 == 0) {
				echo ".";
			}
			$i++;
		}
		echo "\n".$i."/10681 movies_extra loaded. Done!\n";				
	}
	//*****************************************************************************************
?>
