<?php

/* Classes and functions for database abstraction */

class announcements {
    public static function getList() {
        global $db;
        return $db->query('SELECT * FROM announcement')->fetchAll();
    }
    
    public static function getById($id) {
        global $db;
        $stmt = $db->prepare('SELECT * FROM announcement WHERE id = :id');
        if ($stmt->execute(array(':id' => $id)))
            return $stmt->fetch(PDO::FETCH_ASSOC);
        else
            return false;
    }
    
    public static function getLatest() {
        global $db;
        return $db->query('SELECT * FROM announcement ORDER BY publication_date DESC LIMIT 1')->fetch(PDO::FETCH_ASSOC);
    }
}

class feeds {
    public static function getList() {
        global $db;
        return $db->query('SELECT * FROM feed')->fetchAll(PDO::FETCH_ASSOC);
    }
    
    public static function getRefreshList() {
        global $db;
        return $db->query('SELECT * FROM feed WHERE auto_refresh = TRUE AND next_refresh <= now()')->fetchAll(PDO::FETCH_ASSOC);
    }
    
    public static function getBySlug($slug) {
        global $db;
        $stmt = $db->prepare('SELECT * FROM feed WHERE slug = :slug');
        if ($stmt->execute(array(':slug' => $slug)))
            return $stmt->fetch(PDO::FETCH_ASSOC);
        else
            return false;
    }
    
    public static function updateNextRefresh($id) {
        global $db;
        
        $stmt = $db->prepare('
            UPDATE feed
            SET next_refresh = next_refresh + (
                SELECT concat(refresh_interval, \' seconds\')
                FROM feed
                WHERE id = :id
            )::interval
            WHERE id = :id'
        );
        $stmt->bindValue(':id', $id);
        $stmt->execute();
    }
    
    public static function create($slug, $uri,
                               $auto_refresh, $refresh_interval, $next_refresh,
                               $expire, $expire_date,
                               $password, $creation_ip, $creation_date
    ) {
        global $db;
        
        $stmt = $db->prepare('
            INSERT INTO feed(
                slug, uri,
                auto_refresh, refresh_interval, next_refresh,
                expire, expire_date,
                password, creation_ip, creation_date
            ) VALUES (
                :slug, :uri,
                :auto_refresh, :refresh_interval, :next_refresh,
                :expire, :expire_date,
                :password, :creation_ip, :creation_date
            )'
        );
        $stmt->bindValue(':slug', $slug);
        $stmt->bindValue(':uri', $uri);
        $stmt->bindValue(':auto_refresh', $auto_refresh);
        $stmt->bindValue(':refresh_interval', $refresh_interval ? $refresh_interval : null);
        $stmt->bindValue(':next_refresh', $next_refresh);
        $stmt->bindValue(':expire', $expire);
        $stmt->bindValue(':expire_date', $expire_date ? $expire_date: null);
        $stmt->bindValue(':password', $password ? $password : null);
        $stmt->bindValue(':creation_ip', $creation_ip);
        $stmt->bindValue(':creation_date', $creation_date);
        return $stmt->execute();
    }
}

class feedItems {
    public static function getLatest($feedId) {
        global $db;
        $stmt = $db->prepare('SELECT * FROM feeditem WHERE feed = :feedid ORDER BY timestamp DESC LIMIT 1');
        if ($stmt->execute(array(':feedid' => $feedId)))
            return $stmt->fetch(PDO::FETCH_ASSOC);
        else
            return false;
    }
    
    public static function getAll($feedId) {
        global $db;
        $stmt = $db->prepare('SELECT * FROM feeditem WHERE feed = :feedid ORDER BY timestamp DESC');
        if ($stmt->execute(array(':feedid' => $feedId)))
            return $stmt->fetchAll(PDO::FETCH_ASSOC);
        else
            return false;
    }
    
    public static function newItem($feedId, $html) {
        $html = mb_convert_encoding($html, 'UTF-8', 'UTF-8,ISO-8859-1,Windows-1251,GB2312,SJIS,Windows-1251');
        
        $latest = feedItems::getLatest($feedId);
        if ($latest)
            $diff = xdiff_string_diff($latest['html'], $html);
        else
            $diff = xdiff_string_diff('', $html);
        
        if (empty($diff))
            return 1;
        
        global $db;
        
        $stmt = $db->prepare('INSERT INTO feeditem VALUES (:feedid, \'now\', :html, :diff)');
        $stmt->bindValue(':feedid', $feedId);
        $stmt->bindValue(':html', $html);
        $stmt->bindValue(':diff', $diff);
        
        $stmt->execute();
        
        return 0;
    }
	
	public static function deleteOldItems($keepCount) {
		global $db;
		
		return 1;
	}
}