Actions

Source Code of This Project

/lib/task/pwpReceiveNewsTask.class.php

<?php
/*
 * This file is part of the pwp package.
 * (c) 2009-2010 Victor Rad' <victor.v.rad[at]gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

class pwpReceiveNewsTask extends sfBaseTask
{
    protected function configure()
    {
        // // add your own arguments here
        // $this->addArguments(array(
        //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
        // ));

        $this->addOptions(array(
                new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
                new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
                new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
                // add your own options here
        ));

        $this->namespace        = 'pwp';
        $this->name             = 'receive-news';
        $this->briefDescription = 'Receives news through RSS and Atom';
        $this->detailedDescription = <<<EOF
The [pwp:receive-news|INFO] task does things.
Call it with:

  [php symfony pwp:receive-news|INFO]
EOF;
    }

    /**
     * Receives news through RSS and Atom
     *
     * @param <type> $arguments
     * @param <type> $options
     */
    protected function execute($arguments = array(), $options = array())
    {
        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();

        $providers = DFactory::get('NewsProviders')->getTable()->findByStatus('active');

        foreach ($providers as $provider) {
            $newsCount = 0;
            $providerNews = simplexml_load_file($provider->news_link);
            if (!empty($providerNews->channel)) {
                foreach ($providerNews->channel->item as $item) {
                    $hash = md5($item->title . substr($item->description, 0, 20));
                    if(!count(Doctrine::getTable('News')->findByHash($hash))) {
                        $news = new News();
                        $news->news_providers_id = $provider->id;
                        $news->title = (string) $item->title;
                        $news->description = strip_tags($item->description);
                        $news->hash = $hash;
                        $news->link = (string) $item->link;
                        $news->created_at = date('Y-m-d H:i:s', strtotime((string) $item->pubDate));
                        $news->save();
                        ++$newsCount;
                    }
                }
            }
            $this->logSection('tasks', sprintf('From %s received %d news', $provider->name , $newsCount));
        }
    }
}