/wp-content/plugins/w3-total-cache/lib/W3/Db.php

https://gitlab.com/juanito.abelo/nlmobile · PHP · 496 lines · 240 code · 79 blank · 177 comment · 11 complexity · e369849152d13ab6c17410a886f6ad2e MD5 · raw file

  1. <?php
  2. /**
  3. * W3 Database object
  4. */
  5. if (!defined('ABSPATH')) {
  6. die();
  7. }
  8. if (!class_exists('W3_Db_Driver')) {
  9. require_once ABSPATH . 'wp-includes/wp-db.php';
  10. class W3_Db_Driver extends wpdb {
  11. }
  12. }
  13. //TODO: Added for backwards compatibility
  14. if(!class_exists('W3_Db')){
  15. /**
  16. * Class W3_Db
  17. * Database access mediator
  18. */
  19. class W3_Db extends W3_Db_Driver {
  20. /**
  21. * Returns onject instance. Called by WP engine
  22. *
  23. * @return W3_Db
  24. */
  25. static function instance() {
  26. static $instances = array();
  27. if (!isset($instances[0])) {
  28. $processors = array();
  29. $call_default_constructor = true;
  30. // no caching during activation
  31. $is_installing = (defined('WP_INSTALLING') && WP_INSTALLING);
  32. $config = w3_instance('W3_Config');
  33. if (!$is_installing && $config->get_boolean('dbcache.enabled')) {
  34. $processors[] = w3_instance('W3_DbCache');
  35. }
  36. if (w3_is_dbcluster()) {
  37. $processors[] = w3_instance('W3_Enterprise_DbCluster');
  38. }
  39. $processors[] = new W3_DbProcessor();
  40. $class = __CLASS__;
  41. $o = new $class($processors);
  42. $underlying_manager = new W3_DbCallUnderlying($o);
  43. foreach ($processors as $processor) {
  44. $processor->manager = $o;
  45. $processor->underlying_manager = $underlying_manager;
  46. }
  47. // initialize after processors configured
  48. $o->initialize();
  49. @$instances[0] = $o;
  50. }
  51. return $instances[0];
  52. }
  53. /*
  54. * @param boolean $call_default_constructor
  55. */
  56. function __construct($processors) {
  57. $this->processors = $processors;
  58. $this->processor = $processors[0];
  59. $this->processor_number = 0;
  60. }
  61. /**
  62. * Initializes object after processors configured. Called from instance() only
  63. */
  64. function initialize() {
  65. return $this->processor->initialize();
  66. }
  67. /**
  68. * Overriten logic of wp_db by processor.
  69. */
  70. function insert($table, $data, $format = null) {
  71. return $this->processor->insert($table, $data, $format);
  72. }
  73. /**
  74. * Overriten logic of wp_db by processor.
  75. */
  76. function query($query) {
  77. return $this->processor->query($query);
  78. }
  79. /**
  80. * Overriten logic of wp_db by processor.
  81. */
  82. function replace($table, $data, $format = null) {
  83. return $this->processor->replace($table, $data, $format);
  84. }
  85. /**
  86. * Overriten logic of wp_db by processor.
  87. */
  88. function update($table, $data, $where, $format = null, $where_format = null) {
  89. return $this->processor->update($table, $data, $where, $format, $where_format);
  90. }
  91. /**
  92. * Overriten logic of wp_db by processor.
  93. */
  94. function init_charset() {
  95. return $this->processor->init_charset();
  96. }
  97. /**
  98. * Overriten logic of wp_db by processor.
  99. */
  100. function set_charset($dbh, $charset = null, $collate = null) {
  101. return $this->processor->set_charset($dbh, $charset, $collate);
  102. }
  103. /**
  104. * Overriten logic of wp_db by processor.
  105. */
  106. function flush() {
  107. return $this->processor->flush();
  108. }
  109. /**
  110. * Overriten logic of wp_db by processor.
  111. */
  112. function check_database_version($dbh_or_table = false) {
  113. return $this->processor->check_database_version($dbh_or_table);
  114. }
  115. /**
  116. * Overriten logic of wp_db by processor.
  117. */
  118. function supports_collation( $dbh_or_table = false ) {
  119. return $this->processor->supports_collation($dbh_or_table);
  120. }
  121. /**
  122. * Overriten logic of wp_db by processor.
  123. */
  124. function has_cap( $db_cap, $dbh_or_table = false ) {
  125. return $this->processor->has_cap($db_cap, $dbh_or_table);
  126. }
  127. /**
  128. * Overriten logic of wp_db by processor.
  129. */
  130. function db_version( $dbh_or_table = false ) {
  131. return $this->processor->db_version($dbh_or_table);
  132. }
  133. /**
  134. * Default initialization method, calls wp_db apropriate method
  135. */
  136. function default_initialize() {
  137. parent::__construct(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
  138. }
  139. /**
  140. * Default implementation, calls wp_db apropriate method
  141. */
  142. function default_insert($table, $data, $format = null) {
  143. return parent::insert($table, $data, $format);
  144. }
  145. /**
  146. * Default implementation, calls wp_db apropriate method
  147. */
  148. function default_query($query) {
  149. return parent::query($query);
  150. }
  151. /**
  152. * Default implementation, calls wp_db apropriate method
  153. */
  154. function default_replace($table, $data, $format = null) {
  155. return parent::replace($table, $data, $format);
  156. }
  157. /**
  158. * Default implementation, calls wp_db apropriate method
  159. */
  160. function default_update($table, $data, $where, $format = null, $where_format = null) {
  161. return parent::update($table, $data, $where, $format, $where_format);
  162. }
  163. /**
  164. * Default implementation, calls wp_db apropriate method
  165. */
  166. function default_init_charset() {
  167. return parent::init_charset();
  168. }
  169. /**
  170. * Default implementation, calls wp_db apropriate method
  171. */
  172. function default_set_charset($dbh, $charset = null, $collate = null) {
  173. return parent::set_charset($dbh, $charset, $collate);
  174. }
  175. /**
  176. * Default implementation, calls wp_db apropriate method
  177. */
  178. function default_flush() {
  179. return parent::flush();
  180. }
  181. /**
  182. * Default implementation, calls wp_db apropriate method
  183. */
  184. function default_check_database_version($dbh_or_table = false) {
  185. return parent::check_database_version($dbh_or_table);
  186. }
  187. /**
  188. * Default implementation, calls wp_db apropriate method
  189. */
  190. function default_supports_collation( $dbh_or_table = false ) {
  191. return parent::supports_collation($dbh_or_table);
  192. }
  193. /**
  194. * Default implementation, calls wp_db apropriate method
  195. */
  196. function default_has_cap( $db_cap, $dbh_or_table = false ) {
  197. return parent::has_cap($db_cap, $dbh_or_table);
  198. }
  199. /**
  200. * Default implementation, calls wp_db apropriate method
  201. */
  202. function default_db_version( $dbh_or_table = false ) {
  203. return parent::db_version($dbh_or_table);
  204. }
  205. /**
  206. * Default implementation, calls wp_db apropriate method
  207. */
  208. function switch_active_processor($offset) {
  209. $new_processor_number = $this->processor_number + $offset;
  210. if ($new_processor_number <= 0) {
  211. $new_processor_number = 0;
  212. } else if ($new_processor_number >= count($this->processors)) {
  213. $new_processor_number = count($this->processors) - 1;
  214. }
  215. $offset_made = $new_processor_number - $this->processor_number;
  216. $this->processor_number = $new_processor_number;
  217. $this->processor = $this->processors[$new_processor_number];
  218. return $offset_made;
  219. }
  220. }
  221. /**
  222. * Class W3_DbProcessor
  223. * Does separate operation without inheritance
  224. */
  225. class W3_DbProcessor {
  226. /**
  227. * Top database-connection object.
  228. * Initialized by W3_Db::instance
  229. *
  230. * @var object
  231. */
  232. var $manager = null;
  233. /**
  234. * Database-connection using overrides of next processor in queue
  235. * Initialized by W3_Db::instance
  236. *
  237. * @var object
  238. */
  239. var $underlying_manager = null;
  240. /**
  241. * Placeholder for database initialization
  242. */
  243. function initialize() {
  244. return $this->manager->default_initialize();
  245. }
  246. /**
  247. * Placeholder for apropriate wp_db method replacement.
  248. * By default calls wp_db implementation
  249. */
  250. function insert($table, $data, $format = null) {
  251. return $this->manager->default_insert($table, $data, $format);
  252. }
  253. /**
  254. * Placeholder for apropriate wp_db method replacement.
  255. * By default calls wp_db implementation
  256. */
  257. function query($query) {
  258. return $this->manager->default_query($query);
  259. }
  260. /**
  261. * Placeholder for apropriate wp_db method replacement.
  262. * By default calls wp_db implementation
  263. */
  264. function replace($table, $data, $format = null) {
  265. return $this->manager->default_replace($table, $data, $format);
  266. }
  267. /**
  268. * Placeholder for apropriate wp_db method replacement.
  269. * By default calls wp_db implementation
  270. */
  271. function update($table, $data, $where, $format = null, $where_format = null) {
  272. return $this->manager->default_update($table, $data, $where, $format, $where_format);
  273. }
  274. /**
  275. * Placeholder for apropriate wp_db method replacement.
  276. * By default calls wp_db implementation
  277. */
  278. function init_charset() {
  279. return $this->manager->default_init_charset();
  280. }
  281. /**
  282. * Placeholder for apropriate wp_db method replacement.
  283. * By default calls wp_db implementation
  284. */
  285. function set_charset($dbh, $charset = null, $collate = null) {
  286. return $this->manager->default_set_charset($dbh, $charset, $collate);
  287. }
  288. /**
  289. * Placeholder for apropriate wp_db method replacement.
  290. * By default calls wp_db implementation
  291. */
  292. function flush() {
  293. return $this->manager->default_flush();
  294. }
  295. /**
  296. * Placeholder for apropriate wp_db method replacement.
  297. * By default calls wp_db implementation
  298. */
  299. function check_database_version($dbh_or_table = false) {
  300. return $this->manager->default_check_database_version($dbh_or_table);
  301. }
  302. /**
  303. * Placeholder for apropriate wp_db method replacement.
  304. * By default calls wp_db implementation
  305. */
  306. function supports_collation( $dbh_or_table = false ) {
  307. return $this->manager->default_supports_collation($dbh_or_table);
  308. }
  309. /**
  310. * Placeholder for apropriate wp_db method replacement.
  311. * By default calls wp_db implementation
  312. */
  313. function has_cap( $db_cap, $dbh_or_table = false ) {
  314. return $this->manager->default_has_cap($db_cap, $dbh_or_table);
  315. }
  316. /**
  317. * Placeholder for apropriate wp_db method replacement.
  318. * By default calls wp_db implementation
  319. */
  320. function db_version( $dbh_or_table = false ) {
  321. return $this->manager->default_db_version($dbh_or_table);
  322. }
  323. }
  324. /**
  325. * Class W3_DbCallUnderlying
  326. */
  327. class W3_DbCallUnderlying {
  328. function __construct($manager) {
  329. $this->manager = $manager;
  330. }
  331. /**
  332. * Calls underlying processor's aproptiate method of wp_db
  333. */
  334. function initialize() {
  335. $switched = $this->manager->switch_active_processor(1);
  336. try {
  337. $r = $this->manager->initialize();
  338. $this->manager->switch_active_processor(-$switched);
  339. return $r;
  340. } catch (Exception $e) {
  341. $this->manager->switch_active_processor(-$switched);
  342. throw $e;
  343. }
  344. }
  345. /**
  346. * Calls underlying processor's aproptiate method of wp_db
  347. */
  348. function flush() {
  349. $switched = $this->manager->switch_active_processor(1);
  350. try {
  351. $r = $this->manager->flush();
  352. $this->manager->switch_active_processor(-$switched);
  353. return $r;
  354. } catch (Exception $e) {
  355. $this->manager->switch_active_processor(-$switched);
  356. throw $e;
  357. }
  358. }
  359. /**
  360. * Calls underlying processor's aproptiate method of wp_db
  361. */
  362. function query($query) {
  363. $switched = $this->manager->switch_active_processor(1);
  364. try {
  365. $r = $this->manager->query($query);
  366. $this->manager->switch_active_processor(-$switched);
  367. return $r;
  368. } catch (Exception $e) {
  369. $this->manager->switch_active_processor(-$switched);
  370. throw $e;
  371. }
  372. }
  373. /**
  374. * Calls underlying processor's aproptiate method of wp_db
  375. */
  376. function insert($table, $data, $format = null) {
  377. $switched = $this->manager->switch_active_processor(1);
  378. try {
  379. $r = $this->manager->insert($table, $data, $format);
  380. $this->manager->switch_active_processor(-$switched);
  381. return $r;
  382. } catch (Exception $e) {
  383. $this->manager->switch_active_processor(-$switched);
  384. throw $e;
  385. }
  386. }
  387. /**
  388. * Calls underlying processor's aproptiate method of wp_db
  389. */
  390. function replace($table, $data, $format = null) {
  391. $switched = $this->manager->switch_active_processor(1);
  392. try {
  393. $r = $this->manager->replace($table, $data, $format);
  394. $this->manager->switch_active_processor(-$switched);
  395. return $r;
  396. } catch (Exception $e) {
  397. $this->manager->switch_active_processor(-$switched);
  398. throw $e;
  399. }
  400. }
  401. /**
  402. * Calls underlying processor's aproptiate method of wp_db
  403. */
  404. function update($table, $data, $where, $format = null, $where_format = null) {
  405. $switched = $this->manager->switch_active_processor(1);
  406. try {
  407. $r = $this->manager->update($table, $data, $where, $format, $where_format);
  408. $this->manager->switch_active_processor(-$switched);
  409. return $r;
  410. } catch (Exception $e) {
  411. $this->manager->switch_active_processor(-$switched);
  412. throw $e;
  413. }
  414. }
  415. }
  416. }
  417. ?>