<input type="Submit" name="newfeeds" value="Start new feeds manager"><br><br>
</form>


<?php 
if (isset($_POST['newfeeds'])) {
    error_reporting(5);
    // set arrays for speical words and lifestyle words
    specialwords();
    lifestylearray();
    //  query to find top 20% of scorers for each lifestyle?  Do as a function
    scalelist();
    //  put new urls out for RSS feed collection and first go at scoring.
    foreach ($newinputurl as $newfurl) {
        echo $newfurl;
        //  activates  monkeychow to add url to feedreader
        fof_add_feed('http://' . $newfurl);
        //echo 'afteradd';
        // split text into individual words in a table
        blogpostwords();
        if ($lastchow && $firstitem) {
            // allocate votes/scoring based on wikipedia lifestyle definitions
            blostposttopfifty();
            //  sum votes to an indiviudal to see if they qualify for a lifestyle inclusion
            lifestylelightstatsi();
        }
        // closes if
    }
    // closes foreach
}
// closes if
function newdailyitems($todayfeeds, $firstupdateid)
{
    global $firstchow;
    global $lastchow;
    $fedalready = $firstupdateid - 1;
    foreach ($todayfeeds as $tfid => $turl) {
        unset($count);
        unset($curfeed);
        //   this function actives monekchow rss reader to see if new feed exist for this blog url
        $count = fof_update_feed($turl);
        $curfeed = $tfid;
        //  do scoring on a per blog post basis/ has any new blog posts been add for this feed?
        $db->query = "SELECT * FROM " . RSSDATA . ".items WHERE " . RSSDATA . ".items.feed_id = {$tfid} AND " . RSSDATA . ".items.id > {$fedalready} ORDER BY " . RSSDATA . ".items.id ASC LIMIT 1";
        //echo $db->query;
        $resultnewitemsfirst = mysql_query($db->query) or die(mysql_error());
        if (mysql_num_rows($resultnewitemsfirst) == 1) {
            $rowfirst = mysql_fetch_object($resultnewitemsfirst);
            $firstchow = $rowfirst->id;
            //echo $firstchow;
            //  find out id of last blog post added for this blog
            $db->query = "SELECT * FROM " . RSSDATA . ".items WHERE " . RSSDATA . ".items.feed_id = {$tfid} AND " . RSSDATA . ".items.id > {$firstupdateid} ORDER BY " . RSSDATA . ".items.id DESC LIMIT 1";
            //echo $db->query;
            $resultchowlast = mysql_query($db->query) or die(mysql_error());
            $rowsecond = mysql_fetch_object($resultchowlast);
            $lastchow = $rowsecond->id;
            //echo $lastchow;
        }
        //  closes if no new posts then skip
        if ($firstchow && $lastchow) {
            // create a list of posts for this individual
            $db->query = "SELECT * FROM " . RSSDATA . ".items WHERE " . RSSDATA . ".items.id BETWEEN {$firstchow}  AND {$lastchow} ";
            //echo $db->query;
            $resultitems = mysql_query($db->query) or die("Error in query: {$db->query}. " . mysql_error());
            if (mysql_num_rows($resultitems) > 0) {
                while ($row = mysql_fetch_object($resultitems)) {
                    //unset($rowfour->content);
                    $itemid = $row->id;
                    //echo $itemid;
                    $row->content = html_entity_decode($row->content);
                    //echo $row->content;
                    $rowtwo->content = strip_tags($row->content);
                    $remove = array("'", "-", ",", "(", ")", "?", ".", "&rsquo;", "&ldquo;", "&rsquo;", "&rdquo;", ":", "@", "!", "#", "^", "%", "/", "|", '\'', "+", "=", "{", "}", "[", "]", '"', ";", "<", ">", "_", "~", "<br />", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");
                    $rowthree->content = str_replace($remove, " ", $rowtwo->content);
                    $rowfour->content = trim($rowthree->content);
                    // need to add regex to remove word starting with /  that databases don't like   add at  insert stage see below
                    $wlen = strlen($rowfour->content);
                    //echo $wlen;
                    $rowthreespace->content = str_replace($remove, " ", $rowtwo->content);
                    $rowfourspace->content = trim($rowthreespace->content);
                    $rowfive->content = explode(" ", $rowthree->content);
                    $postcontent = $rowfive->content;
                    //print_r($postcontent);
                    postwords($wlen, $postcontent, $itemid);
                }
                //  closes while loop containing list of blog posts
            }
            // closes if a post ready for word prep.
            // allocate votes/scoring based on wikipedia lifestyle definitions
            blostposttopfifty($firstchow, $lastchow);
            unset($firstchow);
            unset($lastchow);
        }
        // if there has been new post for this blog url
    }
    //closes foreach for each feed that needs split into single words and scored
}
function addnewurl()
{
    //  put new urls out for RSS feed collection and first go at scoring.
    foreach ($newinputfeeds as $keyn => $newfurl) {
        echo $newfurl;
        //  activates  monkeychow to add url to feedreader
        fof_add_feed($newfurl);
        //echo 'afteradd';
        // split text into individual words in a table
        blogpostwords();
        if ($lastchow && $firstitem) {
            // allocate votes/scoring based on wikipedia lifestyle definitions
            blostposttopfifty();
            //  sum votes to an indiviudal to see if they qualify for a lifestyle inclusion
            lifestylelightstatsi();
        }
        // closes if
    }
    // closes foreach
}