[7589] | 1 | <?php |
---|
| 2 | /*********************************************** |
---|
| 3 | * File : hierarchycache.php |
---|
| 4 | * Project : Z-Push |
---|
| 5 | * Descr : HierarchyCache implementation |
---|
| 6 | * |
---|
| 7 | * Created : 18.08.2011 |
---|
| 8 | * |
---|
| 9 | * Copyright 2007 - 2012 Zarafa Deutschland GmbH |
---|
| 10 | * |
---|
| 11 | * This program is free software: you can redistribute it and/or modify |
---|
| 12 | * it under the terms of the GNU Affero General Public License, version 3, |
---|
| 13 | * as published by the Free Software Foundation with the following additional |
---|
| 14 | * term according to sec. 7: |
---|
| 15 | * |
---|
| 16 | * According to sec. 7 of the GNU Affero General Public License, version 3, |
---|
| 17 | * the terms of the AGPL are supplemented with the following terms: |
---|
| 18 | * |
---|
| 19 | * "Zarafa" is a registered trademark of Zarafa B.V. |
---|
| 20 | * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH |
---|
| 21 | * The licensing of the Program under the AGPL does not imply a trademark license. |
---|
| 22 | * Therefore any rights, title and interest in our trademarks remain entirely with us. |
---|
| 23 | * |
---|
| 24 | * However, if you propagate an unmodified version of the Program you are |
---|
| 25 | * allowed to use the term "Z-Push" to indicate that you distribute the Program. |
---|
| 26 | * Furthermore you may use our trademarks where it is necessary to indicate |
---|
| 27 | * the intended purpose of a product or service provided you use it in accordance |
---|
| 28 | * with honest practices in industrial or commercial matters. |
---|
| 29 | * If you want to propagate modified versions of the Program under the name "Z-Push", |
---|
| 30 | * you may only do so if you have a written permission by Zarafa Deutschland GmbH |
---|
| 31 | * (to acquire a permission please contact Zarafa at trademark@zarafa.com). |
---|
| 32 | * |
---|
| 33 | * This program is distributed in the hope that it will be useful, |
---|
| 34 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 35 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 36 | * GNU Affero General Public License for more details. |
---|
| 37 | * |
---|
| 38 | * You should have received a copy of the GNU Affero General Public License |
---|
| 39 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 40 | * |
---|
| 41 | * Consult LICENSE file for details |
---|
| 42 | ************************************************/ |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | class HierarchyCache { |
---|
| 46 | private $changed = false; |
---|
| 47 | protected $cacheById; |
---|
| 48 | private $cacheByIdOld; |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Constructor of the HierarchyCache |
---|
| 52 | * |
---|
| 53 | * @access public |
---|
| 54 | * @return |
---|
| 55 | */ |
---|
| 56 | public function HierarchyCache() { |
---|
| 57 | $this->cacheById = array(); |
---|
| 58 | $this->cacheByIdOld = $this->cacheById; |
---|
| 59 | $this->changed = true; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Indicates if the cache was changed |
---|
| 64 | * |
---|
| 65 | * @access public |
---|
| 66 | * @return boolean |
---|
| 67 | */ |
---|
| 68 | public function IsStateChanged() { |
---|
| 69 | return $this->changed; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Copy current CacheById to memory |
---|
| 74 | * |
---|
| 75 | * @access public |
---|
| 76 | * @return boolean |
---|
| 77 | */ |
---|
| 78 | public function CopyOldState() { |
---|
| 79 | $this->cacheByIdOld = $this->cacheById; |
---|
| 80 | return true; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Returns the SyncFolder object for a folder id |
---|
| 85 | * If $oldstate is set, then the data from the previous state is returned |
---|
| 86 | * |
---|
| 87 | * @param string $serverid |
---|
| 88 | * @param boolean $oldstate (optional) by default false |
---|
| 89 | * |
---|
| 90 | * @access public |
---|
| 91 | * @return SyncObject/boolean false if not found |
---|
| 92 | */ |
---|
| 93 | public function GetFolder($serverid, $oldState = false) { |
---|
| 94 | if (!$oldState && array_key_exists($serverid, $this->cacheById)) { |
---|
| 95 | return $this->cacheById[$serverid]; |
---|
| 96 | } |
---|
| 97 | else if ($oldState && array_key_exists($serverid, $this->cacheByIdOld)) { |
---|
| 98 | return $this->cacheByIdOld[$serverid]; |
---|
| 99 | } |
---|
| 100 | return false; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Adds a folder to the HierarchyCache |
---|
| 105 | * |
---|
| 106 | * @param SyncObject $folder |
---|
| 107 | * |
---|
| 108 | * @access public |
---|
| 109 | * @return boolean |
---|
| 110 | */ |
---|
| 111 | public function AddFolder($folder) { |
---|
| 112 | ZLog::Write(LOGLEVEL_DEBUG, "HierarchyCache: AddFolder() serverid: {$folder->serverid} displayname: {$folder->displayname}"); |
---|
| 113 | |
---|
| 114 | // on update the $folder does most of the times not contain a type |
---|
| 115 | // we copy the value in this case to the new $folder object |
---|
| 116 | if (isset($this->cacheById[$folder->serverid]) && (!isset($folder->type) || $folder->type == false) && isset($this->cacheById[$folder->serverid]->type)) { |
---|
| 117 | $folder->type = $this->cacheById[$folder->serverid]->type; |
---|
| 118 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("HierarchyCache: AddFolder() is an update: used type '%s' from old object", $folder->type)); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | // add/update |
---|
| 122 | $this->cacheById[$folder->serverid] = $folder; |
---|
| 123 | $this->changed = true; |
---|
| 124 | |
---|
| 125 | return true; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * Removes a folder to the HierarchyCache |
---|
| 130 | * |
---|
| 131 | * @param string $serverid id of folder to be removed |
---|
| 132 | * |
---|
| 133 | * @access public |
---|
| 134 | * @return boolean |
---|
| 135 | */ |
---|
| 136 | public function DelFolder($serverid) { |
---|
| 137 | $ftype = $this->GetFolder($serverid); |
---|
| 138 | |
---|
| 139 | ZLog::Write(LOGLEVEL_DEBUG, sprintf("HierarchyCache: DelFolder() serverid: '%s' - type: '%s'", $serverid, $ftype->type)); |
---|
| 140 | unset($this->cacheById[$serverid]); |
---|
| 141 | $this->changed = true; |
---|
| 142 | return true; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * Imports a folder array to the HierarchyCache |
---|
| 147 | * |
---|
| 148 | * @param array $folders folders to the HierarchyCache |
---|
| 149 | * |
---|
| 150 | * @access public |
---|
| 151 | * @return boolean |
---|
| 152 | */ |
---|
| 153 | public function ImportFolders($folders) { |
---|
| 154 | if (!is_array($folders)) |
---|
| 155 | return false; |
---|
| 156 | |
---|
| 157 | $this->cacheById = array(); |
---|
| 158 | |
---|
| 159 | foreach ($folders as $folder) { |
---|
| 160 | if (!isset($folder->type)) |
---|
| 161 | continue; |
---|
| 162 | $this->AddFolder($folder); |
---|
| 163 | } |
---|
| 164 | return true; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Exports all folders from the HierarchyCache |
---|
| 169 | * |
---|
| 170 | * @param boolean $oldstate (optional) by default false |
---|
| 171 | * |
---|
| 172 | * @access public |
---|
| 173 | * @return array |
---|
| 174 | */ |
---|
| 175 | public function ExportFolders($oldstate = false) { |
---|
| 176 | if ($oldstate === false) |
---|
| 177 | return $this->cacheById; |
---|
| 178 | else |
---|
| 179 | return $this->cacheByIdOld; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * Returns all folder objects which were deleted in this operation |
---|
| 184 | * |
---|
| 185 | * @access public |
---|
| 186 | * @return array with SyncFolder objects |
---|
| 187 | */ |
---|
| 188 | public function GetDeletedFolders() { |
---|
| 189 | // diffing the OldCacheById with CacheById we know if folders were deleted |
---|
| 190 | return array_diff_key($this->cacheByIdOld, $this->cacheById); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * Returns some statistics about the HierarchyCache |
---|
| 195 | * |
---|
| 196 | * @access public |
---|
| 197 | * @return string |
---|
| 198 | */ |
---|
| 199 | public function GetStat() { |
---|
| 200 | return sprintf("HierarchyCache is %s - Cached objects: %d", ((isset($this->cacheById))?"up":"down"), ((isset($this->cacheById))?count($this->cacheById):"0")); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Returns objects which should be persistent |
---|
| 205 | * called before serialization |
---|
| 206 | * |
---|
| 207 | * @access public |
---|
| 208 | * @return array |
---|
| 209 | */ |
---|
| 210 | public function __sleep() { |
---|
| 211 | return array("cacheById"); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | ?> |
---|