[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Copyright (C) 2005-2010 Alfresco Software Limited. 4 * 5 * This file is part of Alfresco 6 * 7 * Alfresco is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * Alfresco is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 require_once $CFG->libdir.'/alfresco/Service/Store.php'; 22 require_once $CFG->libdir.'/alfresco/Service/Node.php'; 23 require_once $CFG->libdir.'/alfresco/Service/WebService/WebServiceFactory.php'; 24 25 class Session extends BaseObject 26 { 27 public $authenticationService; 28 public $repositoryService; 29 public $contentService; 30 31 private $_repository; 32 private $_ticket; 33 private $_stores; 34 private $_namespaceMap; 35 36 private $nodeCache; 37 private $idCount = 0; 38 39 /** 40 * Constructor 41 * 42 * @param userName the user name 43 * @param ticket the currenlty authenticated users ticket 44 */ 45 public function __construct($repository, $ticket) 46 { 47 $this->nodeCache = array(); 48 49 $this->_repository = $repository; 50 $this->_ticket = $ticket; 51 52 $this->repositoryService = WebServiceFactory::getRepositoryService($this->_repository->connectionUrl, $this->_ticket); 53 $this->contentService = WebServiceFactory::getContentService($this->_repository->connectionUrl, $this->_ticket); 54 } 55 56 /** 57 * Creates a new store in the current respository 58 * 59 * @param $address the address of the new store 60 * @param $scheme the scheme of the new store, default value of 'workspace' 61 * @return Store the new store 62 */ 63 public function createStore($address, $scheme="workspace") 64 { 65 // Create the store 66 $result = $this->repositoryService->createStore(array( 67 "scheme" => $scheme, 68 "address" => $address)); 69 $store = new Store($this, $result->createStoreReturn->address, $result->createStoreReturn->scheme); 70 71 // Add to the cached list if its been populated 72 if (isset($this->_stores) == true) 73 { 74 $this->_stores[] = $store; 75 } 76 77 // Return the newly created store 78 return $store; 79 } 80 81 /** 82 * Get the store 83 * 84 * @param $address the address of the store 85 * @param $scheme the scheme of the store. The default it 'workspace' 86 * @return Store the store 87 */ 88 public function getStore($address, $scheme="workspace") 89 { 90 return new Store($this, $address, $scheme); 91 } 92 93 /** 94 * Get the store from it string representation (eg: workspace://SpacesStore) 95 * 96 * @param $value the stores string representation 97 * @return Store the store 98 */ 99 public function getStoreFromString($value) 100 { 101 list($scheme, $address) = explode("://", $value); 102 return new Store($this, $address, $scheme); 103 } 104 105 public function getNode($store, $id) 106 { 107 $node = $this->getNodeImpl($store, $id); 108 if ($node == null) 109 { 110 $node = new Node($this, $store, $id); 111 $this->addNode($node); 112 } 113 return $node; 114 } 115 116 public function getNodeFromString($value) 117 { 118 // TODO 119 throw new Exception("getNode($value) not yet implemented"); 120 } 121 122 /** 123 * Adds a new node to the session. 124 */ 125 public function addNode($node) 126 { 127 $this->nodeCache[$node->__toString()] = $node; 128 } 129 130 private function getNodeImpl($store, $id) 131 { 132 $result = null; 133 $nodeRef = $store->scheme . "://" . $store->address . "/" . $id; 134 if (array_key_exists($nodeRef, $this->nodeCache) == true) 135 { 136 $result = $this->nodeCache[$nodeRef]; 137 } 138 return $result; 139 } 140 141 /** 142 * Commits all unsaved changes to the repository 143 */ 144 public function save($debug=false) 145 { 146 // Build the update statements from the node cache 147 $statements = array(); 148 foreach ($this->nodeCache as $node) 149 { 150 $node->onBeforeSave($statements); 151 } 152 153 if ($debug == true) 154 { 155 var_dump($statements); 156 echo ("<br><br>"); 157 } 158 159 if (count($statements) > 0) 160 { 161 // Make the web service call 162 $result = $this->repositoryService->update(array("statements" => $statements)); 163 //var_dump($result); 164 165 // Update the state of the updated nodes 166 foreach ($this->nodeCache as $node) 167 { 168 $node->onAfterSave($this->getIdMap($result)); 169 } 170 } 171 } 172 173 /** 174 * Clears the current session by emptying the node cache. 175 * 176 * WARNING: all unsaved changes will be lost when clearing the session. 177 */ 178 public function clear() 179 { 180 // Clear the node cache 181 $this->nodeCache = array(); 182 } 183 184 private function getIdMap($result) 185 { 186 $return = array(); 187 $statements = $result->updateReturn; 188 if (is_array($statements) == true) 189 { 190 foreach ($statements as $statement) 191 { 192 if ($statement->statement == "create") 193 { 194 $id = $statement->sourceId; 195 $uuid = $statement->destination->uuid; 196 $return[$id] = $uuid; 197 } 198 } 199 } 200 else 201 { 202 if ($statements->statement == "create") 203 { 204 $id = $statements->sourceId; 205 $uuid = $statements->destination->uuid; 206 $return[$id] = $uuid; 207 } 208 } 209 return $return; 210 } 211 212 public function query($store, $query, $language='lucene') 213 { 214 // TODO need to support paged queries 215 $result = $this->repositoryService->query(array( 216 "store" => $store->__toArray(), 217 "query" => array( 218 "language" => $language, 219 "statement" => $query), 220 "includeMetaData" => false)); 221 222 // TODO for now do nothing with the score and the returned data 223 $resultSet = $result->queryReturn->resultSet; 224 return $this->resultSetToNodes($this, $store, $resultSet); 225 } 226 227 public function getTicket() 228 { 229 return $this->_ticket; 230 } 231 232 public function getRepository() 233 { 234 return $this->_repository; 235 } 236 237 public function getNamespaceMap() 238 { 239 if ($this->_namespaceMap == null) 240 { 241 $this->_namespaceMap = new NamespaceMap(); 242 } 243 return $this->_namespaceMap; 244 } 245 246 public function getStores() 247 { 248 if (isset ($this->_stores) == false) 249 { 250 $this->_stores = array (); 251 $results = $this->repositoryService->getStores(); 252 253 foreach ($results->getStoresReturn as $result) 254 { 255 $this->_stores[] = new Store($this, $result->address, $result->scheme); 256 } 257 } 258 259 return $this->_stores; 260 } 261 262 /** Want these methods to be package scope some how! **/ 263 264 public function nextSessionId() 265 { 266 $sessionId = "session".$this->_ticket.$this->idCount; 267 $this->idCount ++; 268 return $sessionId; 269 } 270 } 271 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |