diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-03-23 14:37:49 +0100 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-03-23 14:37:49 +0100 |
commit | 7837e71f70749f400fc075e9b27b2c92d5928ea9 (patch) | |
tree | 195a2f6da60aea10cb08682f03c7dabd913450a8 /backgroundjob/task.php | |
parent | 53248304dd317e7af3fbc6de09c47f4fb2427530 (diff) |
added index template
Diffstat (limited to 'backgroundjob/task.php')
-rw-r--r-- | backgroundjob/task.php | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/backgroundjob/task.php b/backgroundjob/task.php new file mode 100644 index 000000000..098ff8393 --- /dev/null +++ b/backgroundjob/task.php @@ -0,0 +1,91 @@ +<?php +/** +* ownCloud - News app +* +* @author Jakob Sack +* @copyright 2012 Jakob Sack owncloud@jakobsack.de +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. +* +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace OCA\News; + +/** + * This class maps a feed to an entry in the feeds table of the database. + */ +class Backgroundjob { + static public function sortFeeds( $a, $b ) { + if( $a->getId() == $b->getId() ) { + return 0; + } + elseif( $a->getId() < $b->getId() ) { + return -1; + } + else{ + return 1; + } + } + + static public function run() { + if( \OC::$CLI ) { + self::cliStep(); + } + else{ + self::webStep(); + } + } + + static private function cliStep() { + $feedmapper = new FeedMapper(); + + // Iterate over all feeds + $feeds = $feedmapper->findAll(); + foreach( $feeds as $feed ) { + self::updateFeed( $feedmapper, $feed ); + } + } + + static private function webStep() { + // Iterate over all users + $lastid = \OCP\Config::getAppValue('news', 'backgroundjob_lastid',0); + + $feedmapper = new FeedMapper(); + $feeds = $feedmapper->findAll(); + usort( $feeds, array( 'OCA\News\Backgroundjob', 'sortFeeds' )); + + $done = false; + foreach( $feeds as $feed ) { + if( $feed->getId() > $lastid ) { + // set lastid BEFORE updating feed! + \OCP\Config::setAppValue('news', 'backgroundjob_lastid',$feed->getId()); + $done = true; + self::updateFeed( $feedmapper, $feed ); + } + } + + if( !$done ) { + \OCP\Config::setAppValue('news', 'backgroundjob_lastid',0); + } + } + + static private function updateFeed( $feedmapper, $feed ) { + $newfeed = null; + $newfeed = Utils::fetch( $feed->getUrl() ); + if( $newfeed !== null ) { + $feedmapper = new FeedMapper(); + $newfeedid = $feedmapper->save($newfeed, $feed->getFolderId() ); + } + } +} |