Bug #9261: Allow SystemTasks to be skipped
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 7 Oct 2010 06:38:20 +0000 (00:38 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 7 Oct 2010 06:45:41 +0000 (00:45 -0600)
If a system task requires auth, skip it until the user is authenticated.

For bin/run_task, if a task is skipped output a warning message.
Currently, no UI exists for allowing authentication from the command
line.

framework/LoginTasks/lib/Horde/LoginTasks/SystemTask.php
framework/LoginTasks/lib/Horde/LoginTasks/Tasklist.php
framework/LoginTasks/package.xml
horde/bin/run_task
imp/lib/LoginTasks/SystemTask/UpgradeFromImp4.php
imp/lib/LoginTasks/SystemTask/UpgradeFromImp4Auth.php [new file with mode: 0644]

index 44458e3..a4ceb3a 100644 (file)
@@ -34,4 +34,15 @@ abstract class Horde_LoginTasks_SystemTask
      */
     abstract public function execute();
 
+    /**
+     * Skip the current task?  If true, will not run on this access but
+     * will attempt to run on the next access.
+     *
+     * @return boolean  Skip the current task?
+     */
+    public function skip()
+    {
+        return false;
+    }
+
 }
index f06564e..303c13f 100644 (file)
@@ -43,6 +43,15 @@ class Horde_LoginTasks_Tasklist
     protected $_tasks = array();
 
     /**
+     * THe list of system tasks to run during this login.
+     *
+     * @see $_tasks
+     *
+     * @var array
+     */
+    protected $_stasks = array();
+
+    /**
      * Current task location pointer.
      *
      * @var integer
@@ -56,14 +65,18 @@ class Horde_LoginTasks_Tasklist
      */
     public function addTask($task)
     {
-        switch ($task->priority) {
-        case Horde_LoginTasks::PRIORITY_HIGH:
-            array_unshift($this->_tasks, $task);
-            break;
-
-        case Horde_LoginTasks::PRIORITY_NORMAL:
-            $this->_tasks[] = $task;
-            break;
+        if ($task instanceof Horde_LoginTasks_SystemTask) {
+            $this->_stasks[] = $task;
+        } else {
+            switch ($task->priority) {
+            case Horde_LoginTasks::PRIORITY_HIGH:
+                array_unshift($this->_tasks, $task);
+                break;
+
+            case Horde_LoginTasks::PRIORITY_NORMAL:
+                $this->_tasks[] = $task;
+                break;
+            }
         }
     }
 
@@ -78,6 +91,14 @@ class Horde_LoginTasks_Tasklist
     {
         $tmp = array();
 
+        /* Always loop through system tasks first. */
+        foreach ($this->_stasks as $key => $val) {
+            if (!$val->skip()) {
+                $tmp[] = $val;
+                unset($this->_stasks[$key]);
+            }
+        }
+
         reset($this->_tasks);
         while (list($k, $v) = each($this->_tasks)) {
             if ($v->needsDisplay() && ($k >= $this->_ptr)) {
@@ -130,7 +151,8 @@ class Horde_LoginTasks_Tasklist
      */
     public function isDone()
     {
-        return ($this->_ptr == count($this->_tasks));
+        return (empty($this->_stasks) &&
+                ($this->_ptr == count($this->_tasks)));
     }
 
 }
index 93c618f..e82feef 100644 (file)
@@ -21,7 +21,8 @@
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Removed horde/Core and horde/Util dependency.
+ <notes>* Add ability to dynamically skip system tasks.
+ * Removed horde/Core and horde/Util dependency.
  * Added system tasks.
  * Added ONCE interval.
  * Renamed Maintenance:: -&gt; Horde_LoginTasks::.
index e6719cf..c94e9cc 100755 (executable)
@@ -71,8 +71,10 @@ if (!class_exists($class)) {
     }
 }
 
-
 $ob = new $class();
-$ob->execute();
-
-$cli->message('Completed task.', 'cli.success');
+if (($ob instanceof Horde_LoginTasks_SystemTask) && $ob->skip()) {
+    $cli->message('System task was skipped.', 'cli.warning');
+} else {
+    $ob->execute();
+    $cli->message('Completed task.', 'cli.success');
+}
index fdf02c0..e79da1a 100644 (file)
@@ -27,7 +27,6 @@ class IMP_LoginTasks_SystemTask_UpgradeFromImp4 extends Horde_LoginTasks_SystemT
     public function execute()
     {
         $this->_upgradeAbookPrefs();
-        $this->_upgradeExpireImapCache();
         $this->_upgradeForwardPrefs();
         $this->_upgradeLoginTasksPrefs();
         $this->_upgradeSortPrefs();
@@ -68,23 +67,6 @@ class IMP_LoginTasks_SystemTask_UpgradeFromImp4 extends Horde_LoginTasks_SystemT
     }
 
     /**
-     * Expire existing IMAP cache.
-     */
-    protected function _upgradeExpireImapCache()
-    {
-        try {
-            $ob = $GLOBALS['injector']->getInstance('IMP_Injector_Factory_Imap')->create()->ob;
-            $ob->login();
-
-            $mboxes = $ob->listMailboxes('*', Horde_Imap_Client::MBOX_ALL, array('flat' => true));
-
-            foreach ($mboxes as $val) {
-                $ob->cache->deleteMailbox($val);
-            }
-        } catch (Exception $e) {}
-    }
-
-    /**
      * Upgrade to the new forward preferences.
      */
     protected function _upgradeForwardPrefs()
diff --git a/imp/lib/LoginTasks/SystemTask/UpgradeFromImp4Auth.php b/imp/lib/LoginTasks/SystemTask/UpgradeFromImp4Auth.php
new file mode 100644 (file)
index 0000000..4c6920e
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Login system task for automated upgrade tasks.
+ * These tasks DO require IMP authentication.
+ *
+ * Copyright 2009-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author   Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/gpl.html GPL
+ * @package  IMP
+ */
+class IMP_LoginTasks_SystemTask_UpgradeFromImp4Auth extends Horde_LoginTasks_SystemTask
+{
+    /**
+     */
+    public $interval = Horde_LoginTasks::ONCE;
+
+    /**
+     */
+    public function execute()
+    {
+        $this->_upgradeExpireImapCache();
+    }
+
+    /**
+     */
+    public function skip()
+    {
+        /* Skip task until we are authenticated. */
+        return !$GLOBALS['registry']->isAuthenticated(array('app' => 'imp'));
+    }
+
+    /**
+     * Expire existing IMAP cache.
+     */
+    protected function _upgradeExpireImapCache()
+    {
+        try {
+            $ob = $GLOBALS['injector']->getInstance('IMP_Injector_Factory_Imap')->create()->ob;
+
+            if ($cache = $ob->getCache()) {
+                $ob->login();
+
+                $mboxes = $ob->listMailboxes('*', Horde_Imap_Client::MBOX_ALL, array('flat' => true));
+
+                foreach ($mboxes as $val) {
+                    $ob->cache->deleteMailbox($val);
+                }
+            }
+        } catch (Exception $e) {}
+    }
+
+}