Back to index

d-push  2.0
hierarchycache.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   hierarchycache.php
00004 * Project   :   Z-Push
00005 * Descr     :   HierarchyCache implementation
00006 *
00007 * Created   :   18.08.2011
00008 *
00009 * Copyright 2007 - 2011 Zarafa Deutschland GmbH
00010 *
00011 * This program is free software: you can redistribute it and/or modify
00012 * it under the terms of the GNU Affero General Public License, version 3,
00013 * as published by the Free Software Foundation with the following additional
00014 * term according to sec. 7:
00015 *
00016 * According to sec. 7 of the GNU Affero General Public License, version 3,
00017 * the terms of the AGPL are supplemented with the following terms:
00018 *
00019 * "Zarafa" is a registered trademark of Zarafa B.V.
00020 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00021 * The licensing of the Program under the AGPL does not imply a trademark license.
00022 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00023 *
00024 * However, if you propagate an unmodified version of the Program you are
00025 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00026 * Furthermore you may use our trademarks where it is necessary to indicate
00027 * the intended purpose of a product or service provided you use it in accordance
00028 * with honest practices in industrial or commercial matters.
00029 * If you want to propagate modified versions of the Program under the name "Z-Push",
00030 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00031 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00032 *
00033 * This program is distributed in the hope that it will be useful,
00034 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00036 * GNU Affero General Public License for more details.
00037 *
00038 * You should have received a copy of the GNU Affero General Public License
00039 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00040 *
00041 * Consult LICENSE file for details
00042 ************************************************/
00043 
00044 
00045 class HierarchyCache {
00046     private $changed = false;
00047     protected $cacheById;
00048     private $cacheByIdOld;
00049 
00056     public function HierarchyCache() {
00057         $this->cacheById = array();
00058         $this->cacheByIdOld = $this->cacheById;
00059         $this->changed = true;
00060     }
00061 
00068     public function IsStateChanged() {
00069         return $this->changed;
00070     }
00071 
00078     public function CopyOldState() {
00079         $this->cacheByIdOld = $this->cacheById;
00080         return true;
00081     }
00082 
00093     public function GetFolder($serverid, $oldState = false) {
00094         if (!$oldState && array_key_exists($serverid, $this->cacheById)) {
00095             return $this->cacheById[$serverid];
00096         }
00097         else if ($oldState && array_key_exists($serverid, $this->cacheByIdOld)) {
00098             return $this->cacheByIdOld[$serverid];
00099         }
00100         return false;
00101     }
00102 
00111     public function AddFolder($folder) {
00112         ZLog::Write(LOGLEVEL_DEBUG, "HierarchyCache: AddFolder() serverid: {$folder->serverid} displayname: {$folder->displayname}");
00113 
00114         // on update the $folder does most of the times not contain a type
00115         // we copy the value in this case to the new $folder object
00116         if (isset($this->cacheById[$folder->serverid]) && (!isset($folder->type) || $folder->type == false) && isset($this->cacheById[$folder->serverid]->type)) {
00117             $folder->type = $this->cacheById[$folder->serverid]->type;
00118             ZLog::Write(LOGLEVEL_DEBUG, sprintf("HierarchyCache: AddFolder() is an update: used type '%s' from old object", $folder->type));
00119         }
00120 
00121         // add/update
00122         $this->cacheById[$folder->serverid] = $folder;
00123         $this->changed = true;
00124 
00125         return true;
00126     }
00127 
00136     public function DelFolder($serverid) {
00137         $ftype = $this->GetFolder($serverid);
00138 
00139         ZLog::Write(LOGLEVEL_DEBUG, sprintf("HierarchyCache: DelFolder() serverid: '%s' - type: '%s'", $serverid, $ftype->type));
00140         unset($this->cacheById[$serverid]);
00141         $this->changed = true;
00142         return true;
00143     }
00144 
00153     public function ImportFolders($folders) {
00154         if (!is_array($folders))
00155             return false;
00156 
00157         $this->cacheById = array();
00158 
00159         foreach ($folders as $folder) {
00160             if (!isset($folder->type))
00161                 continue;
00162             $this->AddFolder($folder);
00163         }
00164         return true;
00165     }
00166 
00175     public function ExportFolders($oldstate = false) {
00176         if ($oldstate === false)
00177             return $this->cacheById;
00178         else
00179             return $this->cacheByIdOld;
00180     }
00181 
00188     public function GetDeletedFolders() {
00189         // diffing the OldCacheById with CacheById we know if folders were deleted
00190         return array_diff_key($this->cacheByIdOld, $this->cacheById);
00191     }
00192 
00199     public function GetStat() {
00200         return sprintf("HierarchyCache is %s - Cached objects: %d", ((isset($this->cacheById))?"up":"down"), ((isset($this->cacheById))?count($this->cacheById):"0"));
00201     }
00202 
00210     public function __sleep() {
00211         return array("cacheById");
00212     }
00213 
00214 }
00215 
00216 ?>