PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/manager/cacheextender/files/cache_sync.wrapper.class.php

https://github.com/good-web-master/modx.evo.custom
PHP | 133 lines | 113 code | 5 blank | 15 comment | 37 complexity | b078a57458d96e10223b24d6f90b4942 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /* CacheExtender Wrapper for MODx Evo by thebat053
  3. Array $d['url'] => id
  4. Array $a[id] => array(id, alias, path, parent, array(child1, child2, child3));
  5. [0] [1] [2] [3] [4]
  6. */
  7. abstract class documentCacheBase implements Iterator, Countable {
  8. protected $cache;
  9. protected $cacheLen;
  10. protected $pointer = 0;
  11. protected $dataStart;
  12. protected $records;
  13. protected $curRecord;
  14. protected $recordLen;
  15. public function rewind() {$this->pointer = $this->dataStart;$this->curRecord = 0;}
  16. public function key() {return $this->bin2dec(substr($this->cache, 4 + $this->curRecord*8, 4),4);}
  17. public function next() {$this->pointer += $this->recordLen;$this->curRecord++;}
  18. public function valid() {return($this->pointer >= $this->dataStart && $this->pointer < $this->cacheLen);}
  19. public function count() {return $this->records;}
  20. protected function bin2dec(&$str, $len){$shift = 0;$result = 0;for($i=0; $i<$len; ++$i){$result |= (@ord($str[$i])<<$shift);$shift += 8;}return $result;}
  21. protected function getRecord($key){if(!isset($key))return false;if(!$offset = $this->findOffset((int)$key)) return false; $len = $this->bin2dec(substr($this->cache, $offset , 3),3); return substr($this->cache, $offset + 3, $len);}
  22. protected function getRecordCurrent(){$len = $this->bin2dec(substr($this->cache, $this->pointer , 3),3); $this->recordLen = (int)$len + 3; return substr($this->cache, $this->pointer + 3, $len);}
  23. protected function findOffset($key){$left = 1; $right = $this->records; while ($left <= $right){ $middle = (int)(($left+$right)/2); $ckey = (int)$this->bin2dec(substr($this->cache,$middle*8 - 4,4),4); if(($left == $right) && ($ckey != $key)) return false; else if($ckey == $key) return $this->bin2dec(substr($this->cache, $middle*8,4),4) + $this->dataStart; else if($key < $ckey) $right = $middle - 1; else if($key > $ckey) $left = $middle + 1;} return false;}
  24. }
  25. //AliasListingClass
  26. //call: aliasListing[documentId]
  27. //returns: array('id' => documentId, 'alias' => documentAlias, 'path' => documentPath, 'parent' => documentParent), analog $a array
  28. class AliasListingClass extends documentCacheBase implements ArrayAccess {
  29. public function __construct($cache){$this->cache = $cache;$this->records = $this->bin2dec(substr($cache,0,4),4);$this->dataStart = $this->records * 8 + 4;$this->cacheLen = strlen($cache);}
  30. public function offsetExists($offset){return $this->findOffset((int)$offset);}
  31. public function offsetGet($offset){return $this->parseOutput($this->getRecord((int)$offset));}
  32. public function offsetSet($key, $value){echo("Cache content can't be modified!");return false;}
  33. public function offsetUnset($key){echo("Cache content can't be modified!");return false;}
  34. public function current(){return $this->parseOutput($this->getRecordCurrent());}
  35. private function parseOutput($item){
  36. if(!$item)
  37. return false;
  38. $tmp = unserialize($item);
  39. return array('id' => $tmp[0], 'alias' => $tmp[1], 'path' => $tmp[2], 'parent' => $tmp[3]);
  40. }
  41. function __destruct() {unset($this->cache);}
  42. }
  43. //DocumentMapCacheClass
  44. //call: documentMap_cache[documentId]
  45. //returns: array(child1, child2, child3 ..... childN), analog function getChildIds(..) documentMap_cache array
  46. class DocumentMapCacheClass extends documentCacheBase implements ArrayAccess {
  47. public function __construct($cache){$this->cache = $cache;$this->records = $this->bin2dec(substr($cache,0,4),4);$this->dataStart = $this->records * 8 + 4;$this->cacheLen = strlen($cache);}
  48. public function offsetExists($offset){return $this->findOffset((int)$offset);}
  49. public function offsetGet($offset){return $this->parseOutput($this->getRecord((int)$offset));}
  50. public function offsetSet($key, $value){echo("Cache content can't be modified!");return false;}
  51. public function offsetUnset($key){echo("Cache content can't be modified!");return false;}
  52. public function current(){return $this->parseOutput($this->getRecordCurrent());}
  53. private function parseOutput($item){
  54. if(!$item)
  55. return false;
  56. $tmp = unserialize($item);
  57. return $tmp[4];
  58. }
  59. function __destruct() {unset($this->cache);}
  60. }
  61. //DocumentMapClass
  62. //call: documentMap[anyid], may foreach
  63. //returns: array(parent => documentId) analog $m array
  64. class DocumentMapClass extends documentCacheBase implements ArrayAccess {
  65. public function __construct($cache){$this->cache = $cache;$this->records = $this->bin2dec(substr($cache,0,4),4);$this->dataStart = $this->records * 8 + 4;$this->cacheLen = strlen($cache);}
  66. public function offsetExists($offset){return $this->findOffset((int)$offset);}
  67. public function offsetGet($offset){return $this->parseOutput($this->getRecord((int)$offset));}
  68. public function offsetSet($key, $value){echo("Cache content can't be modified!");return false;}
  69. public function offsetUnset($key){echo("Cache content can't be modified!");return false;}
  70. public function current(){return $this->parseOutput($this->getRecordCurrent());}
  71. private function parseOutput($item){
  72. if(!$item)
  73. return false;
  74. $tmp = unserialize($item);
  75. return array($tmp[3] => $tmp[0]); //parent => id
  76. }
  77. function __destruct() {unset($this->cache);}
  78. }
  79. class DocumentListingClass implements ArrayAccess, Iterator, Countable {
  80. private $cache;
  81. private $cacheLen;
  82. private $pointer = 0;
  83. private $dataStart;
  84. private $records;
  85. private $curRecord;
  86. private $recordLen;
  87. public function __construct($cache){$this->cache = $cache;$this->records = $this->bin2dec(substr($cache,0,4),4);$this->dataStart = $this->records * 4 + 4;$this->cacheLen = strlen($cache);}
  88. public function rewind() {$this->pointer = $this->dataStart; $this->curRecord = 0;}
  89. public function key() {$offset = $this->bin2dec(substr($this->cache, 4 + $this->curRecord*4, 4),4); $len = $this->bin2dec(substr($this->cache, $this->dataStart + $offset,2),2); return substr($this->cache, $this->dataStart + $offset + 2,$len);}
  90. public function next() {$this->pointer += $this->recordLen; $this->curRecord++;}
  91. public function valid() {return($this->pointer >= $this->dataStart && $this->pointer < $this->cacheLen);}
  92. public function count() {return $this->records;}
  93. public function offsetExists($offset){return $this->findOffset($offset);}
  94. public function offsetGet($offset){return $this->parseOutput($this->getRecord($offset));}
  95. public function offsetSet($key, $value){echo("Cache content can't be modified!");return false;}
  96. public function offsetUnset($key){echo("Cache content can't be modified!");return false;}
  97. public function current(){return $this->parseOutput($this->getRecordCurrent());}
  98. protected function bin2dec(&$str, $len){$shift = 0;$result = 0;for($i=0; $i<$len; ++$i){$result |= (@ord($str[$i])<<$shift);$shift += 8;}return $result;}
  99. protected function getRecord($key){if(!$key) return false; if(!$offset = $this->findOffset($key)) return false; $len = $this->bin2dec(substr($this->cache, $offset , 2),2); return (int)$this->bin2dec(substr($this->cache, $offset + $len + 2, 4), 4);}
  100. protected function getRecordCurrent(){$len = $this->bin2dec(substr($this->cache, $this->pointer , 2),2); $this->recordLen = (int)$len + 2 + 4; return $this->bin2dec(substr($this->cache, $this->pointer + 2 + $len, 4),4);}
  101. protected function findOffset($key){$key = (string)$key; $left = 1; $right = $this->records; while ($left <= $right){$middle = (int)(($left+$right)/2); $idxoffs = $this->bin2dec(substr($this->cache,$middle * 4 ,4),4); $len = $this->bin2dec(substr($this->cache, $this->dataStart + $idxoffs, 2),2); $ckey = substr($this->cache, $this->dataStart + $idxoffs + 2, $len); if(($left == $right) && ((string)$ckey != (string)$key)) return false; else if((string)$ckey == (string)$key) return $idxoffs + $this->dataStart; else if((string)$key < (string)$ckey) $right = $middle - 1; else if((string)$key > (string)$ckey) $left = $middle + 1;} return false;}
  102. // $strict not supported at this time
  103. public function array_search($value, &$obj = null, $strict = false){ if(is_null($value)) return null; $value = (int)$value; foreach($this as $item) if($item == $value) return $this->key(); return false;}
  104. public function array_key_exists($key, &$obj = null){return $this->offsetExists($key);}
  105. public function array_values(&$obj){$result = array(); foreach($this as $item) array_push($result, $item); return $result;}
  106. public function array_keys(&$obj = null, $search_value = null, $strict = false){$result = array(); foreach($this as $item){ if(!$search_value) array_push($result, $this->key()); else if($this->key() == $search_value) array_push($result, $this->key());} return $result;}
  107. public function array_push(&$obj, $value){echo("Cache content can't be modified!");return false;}
  108. public function array_shift(&$obj){echo("Cache content can't be modified!");return false;}
  109. public function array_pop(&$obj, $offset, $length = 0, $preserve_keys = false){echo("Cache content can't be modified!");return false;}
  110. public function in_array($value, &$obj = null){return ($this->array_search($value) !== false);}
  111. private function parseOutput($item){
  112. if(!$item) return false;
  113. return (int)$item;
  114. }
  115. function __destruct() {unset($this->cache);}
  116. }
  117. if($cacheMode == 'part')
  118. $this->documentListing = &$d;
  119. else {
  120. $cacheFileUrl = file_get_contents($cacheFileNameUrl);
  121. $this->documentListing = new DocumentListingClass($cacheFileUrl);
  122. }
  123. $cacheFile = file_get_contents($cacheFileName);
  124. $this->aliasListing = new AliasListingClass($cacheFile);
  125. $this->documentMap = new DocumentMapClass($cacheFile);
  126. $this->documentMap_cache = new DocumentMapCacheClass($cacheFile);
  127. ?>