PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/dbtech/vbactivity/includes/class_cache.php

https://gitlab.com/elasa/vb-elasa.ir
PHP | 439 lines | 224 code | 61 blank | 154 comment | 27 complexity | 0cb651dd1307330bc41e5d6b4fb2f5b0 MD5 | raw file
  1. <?php
  2. /*======================================================================*\
  3. || #################################################################### ||
  4. || # ---------------------------------------------------------------- # ||
  5. || # Copyright ©2013 Fillip Hannisdal AKA Revan/NeoRevan/Belazor # ||
  6. || # All Rights Reserved. # ||
  7. || # This file may not be redistributed in whole or significant part. # ||
  8. || # ---------------------------------------------------------------- # ||
  9. || # You are not allowed to use this on your server unless the files # ||
  10. || # you downloaded were done so with permission. # ||
  11. || # ---------------------------------------------------------------- # ||
  12. || #################################################################### ||
  13. \*======================================================================*/
  14. // #############################################################################
  15. // cache functionality class
  16. /**
  17. * Class that handles keeping the database cache up to date.
  18. */
  19. class VBACTIVITY_CACHE
  20. {
  21. /**
  22. * The vBulletin registry object
  23. *
  24. * @private vB_Registry
  25. */
  26. private static $vbulletin = NULL;
  27. /**
  28. * The prefix for the mod we are working with
  29. *
  30. * @public string
  31. */
  32. public static $prefix = 'dbtech_vbactivity_';
  33. /**
  34. * Array of cache fields
  35. *
  36. * @public array
  37. */
  38. public static $cachefields = array();
  39. /**
  40. * Array of items to fetch
  41. *
  42. * @protected array
  43. */
  44. protected static $queryfields = array();
  45. /**
  46. * Array of items to NOT fetch
  47. *
  48. * @protected array
  49. */
  50. protected static $exclude = array();
  51. /**
  52. * Array of items to NOT fetch
  53. *
  54. * @protected array
  55. */
  56. protected static $_tables = array(
  57. 'achievement' => '
  58. LEFT JOIN $dbtech_vbactivity_category AS category ON (category.categoryid = dbtech_vbactivity_achievement.categoryid)
  59. ORDER BY category.displayorder, dbtech_vbactivity_achievement.displayorder ASC
  60. ',
  61. 'category' => 'ORDER BY `displayorder` ASC',
  62. 'condition' => '',
  63. 'conditionbridge' => '',
  64. 'contest' => 'ORDER BY `end` DESC',
  65. 'contesttype' => 'ORDER BY `title` ASC',
  66. 'medal' => '
  67. LEFT JOIN $dbtech_vbactivity_category AS category ON (category.categoryid = dbtech_vbactivity_medal.categoryid)
  68. ORDER BY category.displayorder, dbtech_vbactivity_medal.displayorder ASC
  69. ',
  70. 'type' => '',
  71. );
  72. /**
  73. * Array of items to NOT fetch
  74. *
  75. * @protected array
  76. */
  77. protected static $_idColumns = array();
  78. /**
  79. * Initialises the database caching by setting the cache
  80. * list and begins verification of the data.
  81. *
  82. * @param vB_Registry Registry object
  83. * @param string Prefix
  84. * @param array (Optional) List of all cached arrays
  85. * @param array (Optional) List of values to not fetch
  86. *
  87. * @return none Nothing
  88. */
  89. public static function init($vbulletin, $cachefields = array(), $exclude = array())
  90. {
  91. // Check if the vBulletin Registry is an object
  92. if (is_object($vbulletin))
  93. {
  94. // Yep, all good
  95. self::$vbulletin =& $vbulletin;
  96. }
  97. else
  98. {
  99. // Something went wrong here I think
  100. trigger_error(__CLASS__ . "::Registry object is not an object", E_USER_ERROR);
  101. }
  102. // Set exclude
  103. self::$exclude = $exclude;
  104. // Make sure this is unique
  105. $cachefields = array_unique($cachefields);
  106. if (count($cachefields) > 0)
  107. {
  108. foreach ($cachefields as $key => $title)
  109. {
  110. if (strpos($title, self::$prefix) === false)
  111. {
  112. // Get rid of the non-relevant fields
  113. unset($cachefields[$key]);
  114. }
  115. }
  116. // Set the cleaned cachefields variable
  117. self::$cachefields = $cachefields;
  118. }
  119. if (count(self::$cachefields) == 0)
  120. {
  121. // We don't need this stuff
  122. return;
  123. // Something went wrong here I think
  124. //trigger_error("DBTech_Framework_Cache::Cachefields has no elements.", E_USER_ERROR);
  125. }
  126. // Check for valid info
  127. self::_checkDatastore();
  128. if (count(self::$queryfields) > 0)
  129. {
  130. // We need to re-query - prepare the string
  131. $itemlist = "'" . implode("','", self::$queryfields) . "'";
  132. if ($itemlist != "''")
  133. {
  134. // Do fetch from the database
  135. self::$vbulletin->datastore->do_db_fetch($itemlist);
  136. }
  137. }
  138. // Set the cache fields
  139. self::_set();
  140. }
  141. /**
  142. * Builds the cache in case the datastore has been cleaned out.
  143. *
  144. * @param string Database table we are working with
  145. * @param string (Optional) Any additional clauses to the query
  146. */
  147. public static function build($type)
  148. {
  149. // Premove the prefix
  150. $dbtype = self::$prefix . $type;
  151. // Initialise the some arrays so we can add to them quicker
  152. $data = array();
  153. if (!is_object(VBACTIVITY::$db))
  154. {
  155. // Ensure this doesn't error on upgrades
  156. return false;
  157. }
  158. VBACTIVITY::$db->hideErrors();
  159. $data = VBACTIVITY::$db->fetchAllKeyed('
  160. SELECT :dbtype.*
  161. FROM $:dbtype AS :dbtype
  162. :clauses
  163. ', (isset(self::$_idColumns[$type]) ? self::$_idColumns[$type] : $type . 'id'), array(
  164. ':dbtype' => $dbtype,
  165. ':clauses' => isset(self::$_tables[$type]) ? self::$_tables[$type] : ''
  166. ));
  167. foreach ($data as &$cols)
  168. {
  169. foreach ($cols as $key => $value)
  170. {
  171. // Loop through the query result and build the array
  172. $cols[$key] = addslashes($value);
  173. }
  174. }
  175. VBACTIVITY::$db->showErrors();
  176. if (!is_array($data))
  177. {
  178. // Ensure this is an array
  179. $data = array();
  180. }
  181. // Finally update the datastore with the new value
  182. build_datastore($dbtype, serialize($data), 1);
  183. // Premove the prefix
  184. $field_short = substr($dbtype, strlen(self::$prefix));
  185. // Strip the slashes
  186. self::$vbulletin->input->stripslashes_deep($data);
  187. // Set the data
  188. VBACTIVITY::$cache[$field_short] = $data;
  189. foreach ((array)VBACTIVITY::$cache[$field_short] as $id => $arr)
  190. {
  191. foreach ((array)VBACTIVITY::$unserialize[$field_short] as $key)
  192. {
  193. // Do unserialize
  194. VBACTIVITY::$cache[$field_short][$id][$key] = @unserialize(stripslashes($arr[$key]));
  195. }
  196. }
  197. }
  198. /**
  199. * Builds the cache for all available tables.
  200. */
  201. public static function buildAll()
  202. {
  203. foreach (self::$_tables as $type => $clauses)
  204. {
  205. // Build this cache
  206. self::build($type);
  207. }
  208. }
  209. /**
  210. * Reads from the vB Optimise cache
  211. *
  212. * @param string Referencing the vB Optimise option varname
  213. * @param string The cache key to read from
  214. */
  215. public static function read($cacheType, $key)
  216. {
  217. if (!self::_canCache($cacheType))
  218. {
  219. // We can't cache this
  220. return -1;
  221. }
  222. // Fetch the vBO data
  223. $_data = vb_optimise::$cache->get('dbtech.vbactivity.' . $key);
  224. if (is_array($_data) AND TIMENOW < $_data['time'])
  225. {
  226. $i = 2;
  227. /*DBTECH_PRO_START*/
  228. $i++;
  229. /*DBTECH_PRO_END*/
  230. // We saved some queries
  231. vb_optimise::stat($i);
  232. vb_optimise::report('Fetched dbtech.vbactivity.' . $key . ' from cache successfully.');
  233. return $_data['cache'];
  234. }
  235. return false;
  236. }
  237. /**
  238. * Writes to the vB Optimise cache
  239. *
  240. * @param string Database table we are working with
  241. * @param string (Optional) Any additional clauses to the query
  242. */
  243. public static function write($data, $cacheType, $key)
  244. {
  245. if (!self::_canCache($cacheType))
  246. {
  247. // We can't cache this
  248. return false;
  249. }
  250. // By default, we want to "null out" the cache
  251. $_data = false;
  252. if ($data !== false)
  253. {
  254. // Write the vBO data
  255. $_data = array(
  256. 'time' => TIMENOW + (self::$vbulletin->options['vbo_cache_vbactivity' . $cacheType] * 3600),
  257. 'cache' => $data,
  258. );
  259. }
  260. // Write the cache
  261. vb_optimise::$cache->set('dbtech.vbactivity.' . $key, $_data);
  262. vb_optimise::report('Cached dbtech.vbactivity.' . $key . ' successfully.');
  263. return true;
  264. }
  265. /**
  266. * Writes to the vB Optimise cache
  267. */
  268. public static function flush()
  269. {
  270. if (!self::_canCache())
  271. {
  272. // We can't cache this
  273. return false;
  274. }
  275. // Flush the cache
  276. vb_optimise::$cache->flush();
  277. return true;
  278. }
  279. /**
  280. * Checks whether or not datastore items are present,
  281. * and schedules for re-query if needed.
  282. */
  283. private static function _checkDatastore()
  284. {
  285. foreach (self::$cachefields as $title)
  286. {
  287. if (strpos($title, self::$prefix) === false)
  288. {
  289. // We don't care.
  290. continue;
  291. }
  292. // Check if the value is set
  293. if (!isset(self::$vbulletin->$title))
  294. {
  295. if (in_array($title, self::$exclude))
  296. {
  297. // Skip this
  298. self::$vbulletin->$title = self::$exclude[$title];
  299. }
  300. else
  301. {
  302. // It wasn't :(
  303. self::$queryfields[] = $title;
  304. // Build datastore
  305. self::build(substr($title, strlen(self::$prefix)));
  306. }
  307. }
  308. }
  309. }
  310. /**
  311. * Sets the specified cache field after making sure all slashes
  312. * are stripped again
  313. */
  314. private static function _set()
  315. {
  316. foreach (self::$cachefields as $field)
  317. {
  318. // Premove the prefix
  319. $field_short = substr($field, strlen(self::$prefix));
  320. // Fetch the data from the vB array
  321. $data = self::$vbulletin->$field;
  322. if (is_array($data))
  323. {
  324. // Strip the slashes
  325. self::$vbulletin->input->stripslashes_deep($data);
  326. // Unset from the vbulletin array to save memory
  327. unset(self::$vbulletin->$field);
  328. }
  329. else if (!in_array($field, self::$exclude))
  330. {
  331. // Ensure this is an array
  332. $data = array();
  333. }
  334. // Set the data
  335. VBACTIVITY::$cache[$field_short] = $data;
  336. }
  337. }
  338. /**
  339. * Tests whether we can cache something
  340. *
  341. * @param string Original message
  342. * @param string Overriding
  343. */
  344. protected static function _canCache($cacheType = '')
  345. {
  346. if (!class_exists('vb_optimise'))
  347. {
  348. // We don't have vBO installed
  349. return false;
  350. }
  351. if (!isset(self::$vbulletin->options['vbo_online']))
  352. {
  353. // vBO is turned off
  354. return false;
  355. }
  356. if (!is_object(vb_optimise::$cache))
  357. {
  358. // Not a valid cache object
  359. return false;
  360. }
  361. if (!$cacheType)
  362. {
  363. // This will be used for the flush
  364. return true;
  365. }
  366. if (!isset(self::$vbulletin->options['vbo_cache_vbactivity' . $cacheType]))
  367. {
  368. // vBO's version is too old
  369. return false;
  370. }
  371. if (!self::$vbulletin->options['vbo_cache_vbactivity' . $cacheType])
  372. {
  373. // The cache time has been turned off
  374. return false;
  375. }
  376. return true;
  377. }
  378. }