From 04f02b49c5d524345bc916aad6e247c714177662 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 5 Jul 2009 17:38:49 -0700 Subject: Add task logging functionality. When a task runs, it creates a log that is stored in the persistant cache for 30 days. On the admin_maintenance page there is a new link for completed tasks "browse log". Clicking this will open a dialog box that has the the contents of the log displayed. The user can then view the log and close the dialog, or press the save button to download the log to their local machine. --- modules/gallery/models/task.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'modules/gallery/models/task.php') diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php index 9e3ae5c6..2e77a7a6 100644 --- a/modules/gallery/models/task.php +++ b/modules/gallery/models/task.php @@ -40,7 +40,31 @@ class Task_Model extends ORM { return parent::save(); } + public function delete() { + Cache::instance()->delete($this->_cache_key()); + return parent::save(); + } + public function owner() { return user::lookup($this->owner_id); } + + public function log($msg) { + $key = $this->_cache_key(); + $log = Cache::instance()->get($key); + + // Save for 30 days. + $log .= !empty($log) ? "\n" : ""; + Cache::instance()->set($key, "$log{$msg}", + array("task", "log", "import"), 2592000); + } + + public function get_task_log() { + $log_data = Cache::instance()->get($this->_cache_key()); + return $log_data !== null ? $log_data : false; + } + + private function _cache_key() { + return md5("$this->id; $this->name; $this->callback"); + } } \ No newline at end of file -- cgit v1.2.3 From 493fb27825327e14168c3c0b133c338a53457e0e Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 6 Jul 2009 22:20:04 -0700 Subject: Cleanup remove task processing. Change Task_Model::delete to call parent::delete instead of parent::save. :-) Also change admin_maintenance_controller to delete via looping over all of the completed tasks and delete individually, so we can delete the associated cache entries at the same time. --- modules/gallery/controllers/admin_maintenance.php | 9 ++++++++- modules/gallery/models/task.php | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'modules/gallery/models/task.php') diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php index a65bd770..37cc5222 100644 --- a/modules/gallery/controllers/admin_maintenance.php +++ b/modules/gallery/controllers/admin_maintenance.php @@ -161,7 +161,14 @@ class Admin_Maintenance_Controller extends Admin_Controller { public function remove_finished_tasks() { access::verify_csrf(); - Database::instance()->delete("tasks", array("done" => 1)); + + // Do it the long way so we can call delete and remove the cache. + $finished = ORM::factory("task") + ->where(array("done" => 1)) + ->find_all(); + foreach ($finished as $task) { + task::remove($task->id); + } message::success(t("All finished tasks removed")); url::redirect("admin/maintenance"); } diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php index 2e77a7a6..012b88cf 100644 --- a/modules/gallery/models/task.php +++ b/modules/gallery/models/task.php @@ -42,7 +42,7 @@ class Task_Model extends ORM { public function delete() { Cache::instance()->delete($this->_cache_key()); - return parent::save(); + return parent::delete(); } public function owner() { -- cgit v1.2.3 From b0cd0a52be1d58e451daa52950deb7d5257e1162 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 7 Jul 2009 12:10:50 -0700 Subject: Allow the task::log method to take an array of lines and change g2_import.php helper to use this approach. --- modules/g2_import/helpers/g2_import.php | 36 +++++++++++++++------------------ modules/gallery/models/task.php | 4 ++++ 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'modules/gallery/models/task.php') diff --git a/modules/g2_import/helpers/g2_import.php b/modules/g2_import/helpers/g2_import.php index 0d72c139..f9a15869 100644 --- a/modules/g2_import/helpers/g2_import.php +++ b/modules/g2_import/helpers/g2_import.php @@ -376,8 +376,8 @@ class g2_import_Core { } $album->save(); - $message = t("Album '%name' imported.", array("name" => $album->name)); - $message .= self::import_keywords_as_tags($g2_album->getKeywords(), $album); + $message[] = t("Album '%name' imported.", array("name" => $album->name)); + $message[] = self::import_keywords_as_tags($g2_album->getKeywords(), $album); self::set_map($g2_album_id, $album->id); // @todo import album highlights @@ -460,12 +460,12 @@ class g2_import_Core { $corrupt = 1; } - $message = ""; + $message = array(); switch ($g2_type) { case "GalleryPhotoItem": if (!in_array($g2_item->getMimeType(), array("image/jpeg", "image/gif", "image/png"))) { Kohana::log("alert", "$g2_path is an unsupported image type; using a placeholder gif"); - $message = t("'%path' is an unsupported image type, using a placeholder", + $message[] = t("'%path' is an unsupported image type, using a placeholder", array("path" => $g2_path)); $g2_path = MODPATH . "g2_import/data/broken-image.gif"; $corrupt = 1; @@ -480,12 +480,11 @@ class g2_import_Core { self::_decode_html_special_chars($g2_item->getTitle()), self::_decode_html_special_chars(self::extract_description($g2_item)), self::map($g2_item->getOwnerId())); - $message .= (strlen($message) ? "\n" : "") . - t("Imported photo: '%title'", array("title" => p::purify($item->title))); + $message[].= t("Imported photo: '%title'", array("title" => p::purify($item->title))); } catch (Exception $e) { Kohana::log( "alert", "Corrupt image $g2_path\n" . $e->__toString()); - $message .= (strlen($message) ? "\n" : "") . t("Corrupt image '%path'\n$exception", + $message[] = t("Corrupt image '%path'\n$exception", array("path" => $g2_path,"exception" => $e->__toString())); $corrupt = 1; } @@ -502,17 +501,16 @@ class g2_import_Core { self::_decode_html_special_chars($g2_item->getTitle()), self::_decode_html_special_chars(self::extract_description($g2_item)), self::map($g2_item->getOwnerId())); - $message .= (strlen($message) ? "\n" : "") . - t("Imported movie: '%title'", array("title" => p::purify($item->title))); + $message[] = t("Imported movie: '%title'", array("title" => p::purify($item->title))); } catch (Exception $e) { Kohana::log("alert", "Corrupt movie $g2_path\n" . $e->__toString()); - $message .= (strlen($message) ? "\n" : "") . t("Corrupt movie '%path'\n$exception", - array("path" => $g2_path,"exception" => $e->__toString())); + $message[] = t("Corrupt movie '%path'\n$exception", + array("path" => $g2_path,"exception" => $e->__toString())); $corrupt = 1; } } else { Kohana::log("alert", "$g2_path is an unsupported movie type"); - $message .= t("'%path' is an unsupported movie type", array("path" => $g2_path)); + $message[] = t("'%path' is an unsupported movie type", array("path" => $g2_path)); $corrupt = 1; } @@ -524,16 +522,14 @@ class g2_import_Core { } if (!empty($item)) { - $message .= (strlen($message) ? "\n" : "") . - self::import_keywords_as_tags($g2_item->getKeywords(), $item); + $message[] = self::import_keywords_as_tags($g2_item->getKeywords(), $item); } if (isset($item)) { self::set_map($g2_item_id, $item->id); $item->view_count = g2(GalleryCoreApi::fetchItemViewCount($g2_item_id)); $item->save(); - $message .= (strlen($message) ? "\n" : "") . - t("View count updated: %count", array("count" => $item->view_count)); + $message[] = t("View count updated: %count", array("count" => $item->view_count)); } if ($corrupt) { @@ -544,21 +540,21 @@ class g2_import_Core { $g2_item_url = str_replace('&g2_GALLERYSID=TMP_SESSION_ID_DI_NOISSES_PMT', '', $g2_item_url); if (!empty($item)) { - $warning = + $message[] = t("%title from Gallery 2 could not be processed; " . "(imported as %title)", array("g2_url" => $g2_item_url, "g3_url" => $item->url(), "title" => $g2_item->getTitle())); } else { - $warning = + $message[] = t("%title from Gallery 2 could not be processed", array("g2_url" => $g2_item_url, "title" => $g2_item->getTitle())); } - $message .= (strlen($message) ? "\n" : "") . $warning; } self::$current_g2_item = null; + return $message; } /** @@ -661,7 +657,7 @@ class g2_import_Core { } } return strlen($tags) ? t("Added '%keywords' to '%title'", - array("tags" => $tags, "title" => p::purify($item->title))) : ""; + array("keywords" => $tags, "title" => p::purify($item->title))) : ""; } /** diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php index 012b88cf..55bb8e21 100644 --- a/modules/gallery/models/task.php +++ b/modules/gallery/models/task.php @@ -53,6 +53,10 @@ class Task_Model extends ORM { $key = $this->_cache_key(); $log = Cache::instance()->get($key); + if (is_array($msg)) { + $msg = implode("\n", $msg); + } + // Save for 30 days. $log .= !empty($log) ? "\n" : ""; Cache::instance()->set($key, "$log{$msg}", -- cgit v1.2.3 From 5d9e334fe6d7c7c1cf6d39a12684e215e797b4b3 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Tue, 7 Jul 2009 12:49:21 -0700 Subject: Added a wee bit of phpDoc. Changed the name of the method get_task_log to get_log Changed the default name of the file when the log is downloaded to gallery3_task_log.txt --- modules/gallery/controllers/admin_maintenance.php | 4 ++-- modules/gallery/models/task.php | 14 +++++++++++++- modules/gallery/views/admin_maintenance.html.php | 2 +- modules/gallery/views/admin_maintenance_show_log.html.php | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) (limited to 'modules/gallery/models/task.php') diff --git a/modules/gallery/controllers/admin_maintenance.php b/modules/gallery/controllers/admin_maintenance.php index 37cc5222..543961a1 100644 --- a/modules/gallery/controllers/admin_maintenance.php +++ b/modules/gallery/controllers/admin_maintenance.php @@ -119,8 +119,8 @@ class Admin_Maintenance_Controller extends Admin_Controller { } header("Content-Type: application/text"); - header("Content-Disposition: filename=g2_import.txt"); - print $task->get_task_log(); + header("Content-Disposition: filename=gallery3_task_log.txt"); + print $task->get_log(); } /** diff --git a/modules/gallery/models/task.php b/modules/gallery/models/task.php index 55bb8e21..b7e255a2 100644 --- a/modules/gallery/models/task.php +++ b/modules/gallery/models/task.php @@ -49,6 +49,10 @@ class Task_Model extends ORM { return user::lookup($this->owner_id); } + /** + * Log a message to the task log. + * @params $msg mixed a string or array of strings + */ public function log($msg) { $key = $this->_cache_key(); $log = Cache::instance()->get($key); @@ -63,11 +67,19 @@ class Task_Model extends ORM { array("task", "log", "import"), 2592000); } - public function get_task_log() { + /** + * Retrieve the cached log information for this task. + * @returns the log data or null if there is no log data + */ + public function get_log() { $log_data = Cache::instance()->get($this->_cache_key()); return $log_data !== null ? $log_data : false; } + /** + * Build the task cache key + * @returns the key to use in access the cache + */ private function _cache_key() { return md5("$this->id; $this->name; $this->callback"); } diff --git a/modules/gallery/views/admin_maintenance.html.php b/modules/gallery/views/admin_maintenance.html.php index f50c95dd..eecc045c 100644 --- a/modules/gallery/views/admin_maintenance.html.php +++ b/modules/gallery/views/admin_maintenance.html.php @@ -164,7 +164,7 @@ id?csrf=$csrf") ?>"> - get_task_log()): ?> + get_log()): ?> id?csrf=$csrf") ?>"> diff --git a/modules/gallery/views/admin_maintenance_show_log.html.php b/modules/gallery/views/admin_maintenance_show_log.html.php index afd988bb..9d850986 100644 --- a/modules/gallery/views/admin_maintenance_show_log.html.php +++ b/modules/gallery/views/admin_maintenance_show_log.html.php @@ -12,7 +12,7 @@ appendTo('body').submit().remove();

name ?>

-
get_task_log()) ?>
+
get_log()) ?>
-- cgit v1.2.3