summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorAlessandro Cosentino <cosenal@gmail.com>2012-08-10 10:42:21 -0400
committerAlessandro Cosentino <cosenal@gmail.com>2012-08-10 10:42:21 -0400
commit837438ab0d9a2f65983514778510550b7ecc83ec (patch)
treea540b558686e95683fc824f1bd1d879f3648eb4f /lib
parentcc594d1cadffcba368957f1f9dc1851c851723c1 (diff)
parent03b7bb8c06e1202276128e226043b70ab2425f43 (diff)
Merge branch 'newsapp_cron' into newsapp
Diffstat (limited to 'lib')
-rw-r--r--lib/backgroundjob.php91
-rw-r--r--lib/feedmapper.php17
2 files changed, 104 insertions, 4 deletions
diff --git a/lib/backgroundjob.php b/lib/backgroundjob.php
new file mode 100644
index 000000000..1c12cbc4f
--- /dev/null
+++ b/lib/backgroundjob.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['id'] == $b['id'] ){
+ return 0;
+ }
+ elseif( $a['id'] < $b['id'] ){
+ return -1;
+ }
+ else{
+ return 1;
+ }
+ }
+
+ static public function run(){
+ if( \OC::$CLI ){
+ self::cliStep();
+ }
+ else{
+ self::webStep();
+ }
+ }
+
+ static private function cliStep(){
+ $feedmapper = new \OC_News_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 \OC_News_FeedMapper();
+ $feeds = $feedmapper->findAll();
+ usort( $feeds, array( 'OCA\News\Backgroundjob', 'sortFeeds' ));
+
+ $done = false;
+ foreach( $feeds as $feed ){
+ if( $feed['id'] > $lastid ){
+ // set lastid BEFORE updating feed!
+ \OCP\Config::setAppValue('news', 'backgroundjob_lastid',$feed['id']);
+ $done = true;
+ self::updateFeed( $feedmapper, $feed );
+ }
+ }
+
+ if( !$done ){
+ \OCP\Config::setAppValue('news', 'backgroundjob_lastid',0);
+ }
+ }
+
+ static private function updateFeed( $feedmapper, $feed ){
+ $newfeed = null;
+ $newfeed = \OC_News_Utils::fetch( $feed['url'] );
+ if( $newfeed !== null ){
+ $feedmapper = new \OC_News_FeedMapper();
+ $newfeedid = $feedmapper->save($newfeed, $feed['folderid'] );
+ }
+ }
+}
diff --git a/lib/feedmapper.php b/lib/feedmapper.php
index c5f01f8d2..a8292cd39 100644
--- a/lib/feedmapper.php
+++ b/lib/feedmapper.php
@@ -39,15 +39,24 @@ class OC_News_FeedMapper {
* @returns
*/
public function findAll(){
- $stmt = OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE user_id = ?');
- $result = $stmt->execute(array($this->userid));
+ $query = 'SELECT * FROM ' . self::tableName;
+ $params = array();
+ if( $this->userid ){
+ $query = $query.' WHERE user_id = ?';
+ $params[] = $this->userid;
+ }
+
+ $stmt = OCP\DB::prepare( $query );
+ $result = $stmt->execute( $params );
$feeds = array();
while ($row = $result->fetchRow()) {
$url = $row['url'];
$id = $row['id'];
$folderid = $row['folder_id'];
- $feeds[] = array("url" => $url, "id" => $id, "folderid" => $folderid);
+ $userid = $row['user_id'];
+ $feeds[] = array("url" => $url, "id" => $id, "folderid" => $folderid, 'userid' => $userid );
}
+
return $feeds;
}
@@ -230,4 +239,4 @@ class OC_News_FeedMapper {
return true;
}
-} \ No newline at end of file
+}