<?php

require_once "config.php";
require_once WWW_DIR . "/lib/adminpage.php";
require_once WWW_DIR . "/lib/tvinfo.php";
$page = new AdminPage();
$tvinfo = new TvInfo();
$id = 0;
// set the current action
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'view';
switch ($action) {
    case 'update':
        $tvinfo->refresh($_GET["id"]);
        if (isset($_GET['from']) && !empty($_GET['from'])) {
            header("Location:" . $_GET['from']);
            exit;
        }
        header("Location:" . WWW_TOP . "/tvinfo-edit.php?id=" . $_GET["id"]);
        exit;
    case 'submit':
        if ($_POST["id"] == "") {
            $imgbytes = "";
            if ($_FILES['imagedata']['size'] > 0) {
                $fileName = $_FILES['imagedata']['name'];
                $tmpName = $_FILES['imagedata']['tmp_name'];
                $fileSize = $_FILES['imagedata']['size'];
                $fileType = $_FILES['imagedata']['type'];
                //
                // check the uploaded file is actually an image.
                //
                $file_info = getimagesize($tmpName);
$t = new TvInfo(true);
$tvdb = new TheTVDB(true);
//
// all entries with a blank description
/*
$rows = $db->query("select ID, releasetitle from tvinfo
                            where description is null
                            and ID in (select distinct tvinfoID from releases)
                            order by ID desc");
*/
//
// all entries with no image or description
$rows = $db->query("select ID, releasetitle from tvinfo\n                            where (cover=0 or coalesce(description,'') = '')\n                            and ID in (select distinct tvinfoID from releases)\n                            and localonly = 0\n                            order by releasetitle desc");
echo "Updating " . count($rows) . " entries \n";
foreach ($rows as $row) {
    $t->refresh($row["ID"]);
    echo "Refreshing " . $row["releasetitle"] . "\n";
}
/*
// process every tv release sat waiting with -1
$s = New Sites;
$site = $s->get();
$t->processTvReleases($site, 1000000);
*/
/*
//rescan every tvinfo without a tvdbID and link if match >= 90%
$rows = $db->query("select ID, releasetitle from tvinfo where tvdbID is null and localonly = 0");
foreach ($rows as $row) {
    $seriesinfo = $tvdb->lookupSeriesID($row["releasetitle"]);
    if($seriesinfo && $seriesinfo["id"] > 0) {
        $perc = $t->compare($row['releasetitle'], $seriesinfo["name"]);