PageRenderTime 50ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 1ms

/localinc/api/command/crbrowser.php

http://github.com/symfony-cmf/phpcrbrowser
PHP | 309 lines | 169 code | 56 blank | 84 comment | 14 complexity | 760b5c15b2c5d79b6c0dd82d44a1ed21 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. class api_command_crbrowser extends api_command {
  3. public function __construct($attribs) {
  4. $this->path = $attribs['path'];
  5. parent::__construct($attribs);
  6. }
  7. /**
  8. * Enter description here...
  9. *
  10. * @return jr_cr_session
  11. */
  12. protected function getSession() {
  13. if (!$this->session) {
  14. $this->session = getJRSession();
  15. }
  16. return $this->session;
  17. }
  18. /**
  19. * Enter description here...
  20. *
  21. * @return jr_cr_node
  22. */
  23. protected function getRootNode() {
  24. $session = $this->getSession();
  25. return $session->getRootNode();
  26. }
  27. /**
  28. * Enter description here...
  29. *
  30. * @return jr_cr_node the requested node
  31. */
  32. protected function getNode() {
  33. $root = $this->getRootNode();
  34. $path = $_GET['fullpath'];
  35. if ($path == '/') {
  36. $node = $root;
  37. } else {
  38. $node = $root->getNode($_GET['fullpath']);
  39. }
  40. return $node;
  41. }
  42. public function index() {
  43. $mySession = $this->getSession();
  44. /*
  45. if ($_GET['q']) {
  46. $qm = $mySession->getWorkspace()->getQueryManager();
  47. $q = $qm->createQuery($_GET['q'], 'LUCENE');
  48. foreach ($q->execute()->getNodes() as $n) {
  49. print $n->getPath();
  50. print "<br>";
  51. }
  52. }
  53. $root = $mySession->getRootNode();
  54. $node = $root->getNode($this->path);
  55. if ($this->request->getVerb() == 'POST') {
  56. if (! empty($_POST['propname'])) {
  57. $node->setProperty($_POST['propname'], $_POST['propvalue'],\PHPCR\PropertyType);
  58. }
  59. if (! empty($_POST['nodename'])) {
  60. $node->addNode($_POST['nodename'], 'nt:unstructured');
  61. }
  62. $mySession->save();
  63. }
  64. $nodes = $node->getNodes();
  65. $xml = '<node>';
  66. $xml .= '<name>' . htmlspecialchars($node->getName()) . '</name>';
  67. $xml .= '<path>' . htmlspecialchars($node->getPath()) . '</path>';
  68. $xml .= '<subnodes>';
  69. foreach ($nodes as $n) {
  70. $xml .= '<node><name>' . htmlspecialchars($n->getName()) . '</name><path>' . htmlspecialchars($n->getPath()) . '</path></node>';
  71. }
  72. $xml .= '</subnodes>';
  73. $xml .= '<properties>';
  74. foreach ($node->getProperties() as $p) {
  75. try {
  76. $val = $p->getString();
  77. if (strlen($val) > 200) {
  78. $val = base64_encode($val);
  79. }
  80. } catch (Exception $e) {
  81. $val = "EXCEPTION";
  82. }
  83. $xml .= '<property><name>' . htmlspecialchars($p->getName()) . '</name><value>' . htmlspecialchars($val) . '</value></property>';
  84. }
  85. $xml .= '</properties>';
  86. $xml .= '</node>';
  87. $d = new DOMDocument();
  88. $d->loadXML($xml);
  89. //$mySession->save();
  90. //$mySession->optimize();
  91. */
  92. $this->data[] = new api_model_dom($d);
  93. }
  94. function tree() {
  95. $node = $this->getNode();
  96. $nodes = $node->getNodes();
  97. $data = array("ResultSet" => array("Result" => array()));
  98. foreach ($nodes as $node) {
  99. $f = array("label" => $node->getName(), "fullpath" => $node->getPath());
  100. if (! ($node->hasNodes())) {
  101. $f['isLeaf'] = true;
  102. }
  103. $data["ResultSet"]["Result"][] = $f;
  104. }
  105. $this->data = json_encode($data);
  106. }
  107. function properties() {
  108. $node = $this->getNode();
  109. $props = $node->getProperties();
  110. $data = array("ResultSet" => array("Result" => array()));
  111. foreach ($props as $prop) {
  112. try {
  113. if ($prop->getType() == \PHPCR\PropertyType::BINARY) {
  114. $mt = $node->getProperty("jcr:mimeType");
  115. if ($mt && $mt->getString() == 'text/html') {
  116. $v = $prop->getString();
  117. } else {
  118. $v = " ";
  119. }
  120. } else {
  121. $v = $prop->getString();
  122. }
  123. } catch (Exception $e) {
  124. $v = '';
  125. try {
  126. if (method_exists($prop, 'getValues')) {
  127. foreach ($prop->getValues() as $val) {
  128. $v .= "[" . $val->getString(). "] ";
  129. }
  130. }
  131. } catch (Exception $e) {
  132. $v = "__EXCEPTION2__";
  133. }
  134. }
  135. $f = array("name" => $prop->getName(), "value" => $v, "type" => $prop->getType());
  136. $data["ResultSet"]["Result"][] = $f;
  137. }
  138. $this->data = json_encode($data);
  139. }
  140. function versions() {
  141. $node = $this->getNode();
  142. $data = array("ResultSet" => array("Result" => array()));
  143. $sess = $this->getSession();
  144. $vm = $sess->getWorkspace()->getVersionManager();
  145. $foo = $vm->getVersionHistory($node->getPath());
  146. foreach ($foo->getAllVersions() as $n) {
  147. $f = array("name" => $n->getName(), "date" => $n->getProperty("jcr:created")->getString());
  148. //if ($n->hasNode($n->hasProperty("jcr:content/jcr:data")) {
  149. try {
  150. $f['size'] = $n->getProperty("jcr:frozenNode/jcr:content/jcr:data")->getLength();
  151. //}
  152. } catch (Exception $e) {
  153. }
  154. $data["ResultSet"]["Result"][] = $f;
  155. }
  156. $f['name'] = 'base';
  157. // $f['date'] = $node->getBaseVersion()->getName();
  158. $f['size'] = '';
  159. $data["ResultSet"]["Result"][] = $f;
  160. $this->data = json_encode($data);
  161. }
  162. function nodetypes() {
  163. $sess = $this->getSession();
  164. $ntm = $sess->getWorkspace()->getNodeTypeManager();
  165. $all = $ntm->getAllNodeTypes();
  166. $data = array("ResultSet" => array("Result" => array()));
  167. foreach ($all as $nt) {
  168. $f = array('name' => $nt->getName(), "isMixin" => $nt->isMixin(), "primaryItemName" => $nt->getPrimaryItemName());
  169. $data["ResultSet"]["Result"][] = $f;
  170. }
  171. $data["ResultSet"]["Result"][] = $f;
  172. $this->data = json_encode($data);
  173. }
  174. function restore() {
  175. $sess = $this->getSession();
  176. $node = $this->getNode();
  177. //if (! $node->isCheckedOut()) {
  178. $vm = $sess->getWorkspace()->getVersionManager();
  179. $vm->checkout($node->getPath());
  180. //}
  181. $vm->restore(true,$_GET['version'], $node->getPath());
  182. $sess->save();
  183. $vm->checkin($node->getPath());
  184. $vm->checkout($node->getPath());
  185. $this->data = json_encode(true);
  186. /*if (is_object($foo)) {
  187. error_log($foo->getRootVersion()->getPath());
  188. }*/
  189. }
  190. function addnode() {
  191. $sess = $this->getSession();
  192. $node = $this->getNode();
  193. if (! $node->isCheckedOut()) {
  194. $vm = $sess->getWorkspace()->getVersionManager();
  195. $vm->checkout($node->getPath());
  196. }
  197. error_log($_POST['name']. " : " .$_POST['type']);
  198. $node->addNode($_POST['name'],$_POST['type']);
  199. $sess->save();
  200. $this->data = json_encode(true);
  201. }
  202. function removenode() {
  203. $sess = $this->getSession();
  204. $node = $this->getNode();
  205. $node->remove();
  206. $sess->save();
  207. $this->data = json_encode(true);
  208. }
  209. function setproperty() {
  210. $sess = $this->getSession();
  211. $node = $this->getNode();
  212. /*if (! $node->isCheckedOut()) {
  213. $node->checkout();
  214. }*/
  215. $vm = $sess->getWorkspace()->getVersionManager();
  216. $vm->checkout($node->getPath());
  217. error_log("set property " . $node->getPath() . " " . $_GET['name'] ." = " . $_GET['value']);
  218. $node->setProperty($_GET['name'],$_GET['value']);
  219. $sess->save();
  220. $this->data = json_encode(true);
  221. }
  222. function createversion() {
  223. $sess = $this->getSession();
  224. $node = $this->getNode();
  225. //if (! $node->isCheckedOut()) {
  226. $vm = $sess->getWorkspace()->getVersionManager();
  227. $vm->checkout($node->getPath());
  228. //}
  229. $node->addMixin('mix:versionable');
  230. $sess->save();
  231. $vm->checkin($node->getPath());
  232. error_log("new version " .$node->getPath());
  233. $vm->checkout($node->getPath());
  234. $this->data = json_encode(true);
  235. }
  236. function delcache() {
  237. $sess = $this->getSession();
  238. $sess->cache->clean();
  239. error_log("cleaned cache");
  240. $this->data = json_encode(true);
  241. }
  242. public function getData() {
  243. return $this->data;
  244. }
  245. }