PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/DbCache_Wpdb.php

https://bitbucket.org/teamloerchner/cabinscape
PHP | 504 lines | 285 code | 92 blank | 127 comment | 13 complexity | 0f29087232b2ee070f9e174fa05a4d7c MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, Apache-2.0, MIT, BSD-3-Clause
  1. <?php
  2. namespace W3TC;
  3. /**
  4. * class Db
  5. * Database access mediator
  6. */
  7. class DbCache_Wpdb extends DbCache_WpdbBase {
  8. /**
  9. * Returns object instance. Called by WP engine
  10. *
  11. * @return DbCache_Wpdb
  12. */
  13. static function instance() {
  14. static $instance = null;
  15. if ( is_null( $instance ) ) {
  16. $processors = array();
  17. $call_default_constructor = true;
  18. // no caching during activation
  19. $is_installing = ( defined( 'WP_INSTALLING' ) && WP_INSTALLING );
  20. $config = Dispatcher::config();
  21. if ( !$is_installing && $config->get_boolean( 'dbcache.enabled' ) ) {
  22. $processors[] = new DbCache_WpdbInjection_QueryCaching();
  23. }
  24. if ( Util_Environment::is_dbcluster() ) {
  25. // dbcluster use mysqli only since other is obsolete now
  26. if ( !defined( 'WP_USE_EXT_MYSQL' ) ) {
  27. define( 'WP_USE_EXT_MYSQL', false );
  28. }
  29. $processors[] = new Enterprise_Dbcache_WpdbInjection_Cluster();
  30. }
  31. $processors[] = new DbCache_WpdbInjection();
  32. $class = __CLASS__;
  33. $o = new $class( $processors );
  34. $next_injection = new _CallUnderlying( $o );
  35. foreach ( $processors as $processor ) {
  36. $processor->initialize_injection( $o, $next_injection );
  37. }
  38. // initialize after processors configured
  39. $o->initialize();
  40. $instance = $o;
  41. }
  42. return $instance;
  43. }
  44. private $active_processor_number;
  45. private $active_processor;
  46. private $processors;
  47. private $debug;
  48. private $request_time_start = 0;
  49. /*
  50. * @param boolean $call_default_constructor
  51. */
  52. public function __construct( $processors = null ) {
  53. // required to initialize $use_mysqli which is private
  54. parent::__construct( '', '', '', '' );
  55. // cant force empty parameter list due to wp requirements
  56. if ( !is_array( $processors ) )
  57. throw new Exception( 'called incorrectly, use instance()' );
  58. $this->processors = $processors;
  59. $this->active_processor = $processors[0];
  60. $this->active_processor_number = 0;
  61. $c = Dispatcher::config();
  62. $this->debug = $c->get_boolean( 'dbcache.debug' );
  63. if ( $this->debug )
  64. $this->_request_time_start = microtime( true );
  65. }
  66. /**
  67. * Called by Root_Loader when all w3tc plugins loaded,
  68. * i.e. later that object instantiated
  69. */
  70. function on_w3tc_plugins_loaded() {
  71. $o = $this;
  72. if ( $this->debug ) {
  73. add_action( 'shutdown', array( $o, 'debug_shutdown' ) );
  74. }
  75. add_filter( 'w3tc_footer_comment', array(
  76. $o, 'w3tc_footer_comment' ) );
  77. add_action( 'w3tc_usage_statistics_of_request', array(
  78. $o, 'w3tc_usage_statistics_of_request' ), 10, 1 );
  79. }
  80. function w3tc_footer_comment( $strings ) {
  81. foreach ( $this->processors as $processor )
  82. $strings = $processor->w3tc_footer_comment( $strings );
  83. return $strings;
  84. }
  85. function debug_shutdown() {
  86. $strings = array();
  87. foreach ( $this->processors as $processor )
  88. $strings = $processor->w3tc_footer_comment( $strings );
  89. $request_time_total = microtime( true ) - $this->request_time_start;
  90. $data = sprintf( "\n[%s] [%s] [%s]\n", date( 'r' ),
  91. $_SERVER['REQUEST_URI'], round( $request_time_total, 4 ) ) .
  92. implode( "\n", $strings ) . "\n";
  93. $data = strtr( $data, '<>', '..' );
  94. $filename = Util_Debug::log_filename( 'dbcache' );
  95. @file_put_contents( $filename, $data, FILE_APPEND );
  96. }
  97. function w3tc_usage_statistics_of_request( $storage ) {
  98. foreach ( $this->processors as $processor )
  99. $processor->w3tc_usage_statistics_of_request( $storage );
  100. }
  101. function flush_cache( $extras = array() ) {
  102. $v = true;
  103. foreach ( $this->processors as $processor )
  104. $v &= $processor->flush_cache( $extras );
  105. return $v;
  106. }
  107. function db_connect( $allow_bail = true ) {
  108. if ( empty( $this->dbuser ) ) {
  109. // skip connection - called from constructor
  110. } else
  111. return parent::db_connect( $allow_bail );
  112. }
  113. /**
  114. * Initializes object after processors configured. Called from instance() only
  115. */
  116. function initialize() {
  117. return $this->active_processor->initialize();
  118. }
  119. /**
  120. * Overriten logic of wp_db by processor.
  121. */
  122. function insert( $table, $data, $format = null ) {
  123. do_action( 'w3tc_db_insert', $table, $data, $format );
  124. return $this->active_processor->insert( $table, $data, $format );
  125. }
  126. /**
  127. * Overriten logic of wp_db by processor.
  128. */
  129. function query( $query ) {
  130. return $this->active_processor->query( $query );
  131. }
  132. function _escape( $data ) {
  133. return $this->active_processor->_escape( $data );
  134. }
  135. /**
  136. * Overriten logic of wp_db by processor.
  137. */
  138. function replace( $table, $data, $format = null ) {
  139. do_action( 'w3tc_db_replace', $table, $data, $format );
  140. return $this->active_processor->replace( $table, $data, $format );
  141. }
  142. /**
  143. * Overriten logic of wp_db by processor.
  144. */
  145. function update( $table, $data, $where, $format = null, $where_format = null ) {
  146. do_action( 'w3tc_db_update', $table, $data, $where, $format,
  147. $where_format );
  148. return $this->active_processor->update( $table, $data, $where, $format, $where_format );
  149. }
  150. /**
  151. * Overriten logic of wp_db by processor.
  152. */
  153. function delete( $table, $where, $where_format = null ) {
  154. do_action( 'w3tc_db_delete', $table, $where, $where_format );
  155. return $this->active_processor->delete( $table, $where, $where_format );
  156. }
  157. /**
  158. * Overriten logic of wp_db by processor.
  159. */
  160. function init_charset() {
  161. return $this->active_processor->init_charset();
  162. }
  163. /**
  164. * Overriten logic of wp_db by processor.
  165. */
  166. function set_charset( $dbh, $charset = null, $collate = null ) {
  167. return $this->active_processor->set_charset( $dbh, $charset, $collate );
  168. }
  169. /**
  170. * Overriten logic of wp_db by processor.
  171. */
  172. function flush() {
  173. return $this->active_processor->flush();
  174. }
  175. /**
  176. * Overriten logic of wp_db by processor.
  177. */
  178. function check_database_version( $dbh_or_table = false ) {
  179. return $this->active_processor->check_database_version( $dbh_or_table );
  180. }
  181. /**
  182. * Overriten logic of wp_db by processor.
  183. */
  184. function supports_collation( $dbh_or_table = false ) {
  185. return $this->active_processor->supports_collation( $dbh_or_table );
  186. }
  187. /**
  188. * Overriten logic of wp_db by processor.
  189. */
  190. function has_cap( $db_cap, $dbh_or_table = false ) {
  191. return $this->active_processor->has_cap( $db_cap, $dbh_or_table );
  192. }
  193. /**
  194. * Overriten logic of wp_db by processor.
  195. */
  196. function db_version( $dbh_or_table = false ) {
  197. return $this->active_processor->db_version( $dbh_or_table );
  198. }
  199. /**
  200. * Default initialization method, calls wp_db apropriate method
  201. */
  202. function default_initialize() {
  203. parent::__construct( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
  204. }
  205. /**
  206. * Default implementation, calls wp_db apropriate method
  207. */
  208. function default_insert( $table, $data, $format = null ) {
  209. return parent::insert( $table, $data, $format );
  210. }
  211. /**
  212. * Default implementation, calls wp_db apropriate method
  213. */
  214. function default_query( $query ) {
  215. return parent::query( $query );
  216. }
  217. function default__escape( $data ) {
  218. return parent::_escape( $data );
  219. }
  220. /**
  221. * Default implementation, calls wp_db apropriate method
  222. */
  223. function default_replace( $table, $data, $format = null ) {
  224. return parent::replace( $table, $data, $format );
  225. }
  226. /**
  227. * Default implementation, calls wp_db apropriate method
  228. */
  229. function default_update( $table, $data, $where, $format = null, $where_format = null ) {
  230. return parent::update( $table, $data, $where, $format, $where_format );
  231. }
  232. /**
  233. * Default implementation, calls wp_db apropriate method
  234. */
  235. function default_delete( $table, $where, $where_format = null ) {
  236. return parent::delete( $table, $where, $where_format );
  237. }
  238. /**
  239. * Default implementation, calls wp_db apropriate method
  240. */
  241. function default_init_charset() {
  242. return parent::init_charset();
  243. }
  244. /**
  245. * Default implementation, calls wp_db apropriate method
  246. */
  247. function default_set_charset( $dbh, $charset = null, $collate = null ) {
  248. return parent::set_charset( $dbh, $charset, $collate );
  249. }
  250. /**
  251. * Default implementation, calls wp_db apropriate method
  252. */
  253. function default_flush() {
  254. return parent::flush();
  255. }
  256. /**
  257. * Default implementation, calls wp_db apropriate method
  258. */
  259. function default_check_database_version( $dbh_or_table = false ) {
  260. return parent::check_database_version( $dbh_or_table );
  261. }
  262. /**
  263. * Default implementation, calls wp_db apropriate method
  264. */
  265. function default_supports_collation( $dbh_or_table = false ) {
  266. return parent::supports_collation( $dbh_or_table );
  267. }
  268. /**
  269. * Default implementation, calls wp_db apropriate method
  270. */
  271. function default_has_cap( $db_cap, $dbh_or_table = false ) {
  272. return parent::has_cap( $db_cap, $dbh_or_table );
  273. }
  274. /**
  275. * Default implementation, calls wp_db apropriate method
  276. */
  277. function default_db_version( $dbh_or_table = false ) {
  278. return parent::db_version( $dbh_or_table );
  279. }
  280. /**
  281. * Default implementation, calls wp_db apropriate method
  282. */
  283. function switch_active_processor( $offset ) {
  284. $new_processor_number = $this->active_processor_number + $offset;
  285. if ( $new_processor_number <= 0 ) {
  286. $new_processor_number = 0;
  287. } else if ( $new_processor_number >= count( $this->processors ) ) {
  288. $new_processor_number = count( $this->processors ) - 1;
  289. }
  290. $offset_made = $new_processor_number - $this->active_processor_number;
  291. $this->active_processor_number = $new_processor_number;
  292. $this->active_processor = $this->processors[$new_processor_number];
  293. return $offset_made;
  294. }
  295. }
  296. /**
  297. * class CallUnderlying
  298. */
  299. class _CallUnderlying {
  300. function __construct( $manager ) {
  301. $this->wpdb_mixin = $manager;
  302. }
  303. /**
  304. * Calls underlying processor's aproptiate method of wp_db
  305. */
  306. function initialize() {
  307. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  308. try {
  309. $r = $this->wpdb_mixin->initialize();
  310. $this->wpdb_mixin->switch_active_processor( -$switched );
  311. return $r;
  312. } catch ( \Exception $e ) {
  313. $this->wpdb_mixin->switch_active_processor( -$switched );
  314. throw $e;
  315. }
  316. }
  317. /**
  318. * Calls underlying processor's aproptiate method of wp_db
  319. */
  320. function flush() {
  321. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  322. try {
  323. $r = $this->wpdb_mixin->flush();
  324. $this->wpdb_mixin->switch_active_processor( -$switched );
  325. return $r;
  326. } catch ( \Exception $e ) {
  327. $this->wpdb_mixin->switch_active_processor( -$switched );
  328. throw $e;
  329. }
  330. }
  331. /**
  332. * Calls underlying processor's aproptiate method of wp_db
  333. */
  334. function query( $query ) {
  335. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  336. try {
  337. $r = $this->wpdb_mixin->query( $query );
  338. $this->wpdb_mixin->switch_active_processor( -$switched );
  339. return $r;
  340. } catch ( \Exception $e ) {
  341. $this->wpdb_mixin->switch_active_processor( -$switched );
  342. throw $e;
  343. }
  344. }
  345. /**
  346. * Calls underlying processor's aproptiate method of wp_db
  347. */
  348. function _escape( $data ) {
  349. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  350. try {
  351. $r = $this->wpdb_mixin->_escape( $data );
  352. $this->wpdb_mixin->switch_active_processor( -$switched );
  353. return $r;
  354. } catch ( \Exception $e ) {
  355. $this->wpdb_mixin->switch_active_processor( -$switched );
  356. throw $e;
  357. }
  358. }
  359. /**
  360. * Calls underlying processor's aproptiate method of wp_db
  361. */
  362. function insert( $table, $data, $format = null ) {
  363. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  364. try {
  365. $r = $this->wpdb_mixin->insert( $table, $data, $format );
  366. $this->wpdb_mixin->switch_active_processor( -$switched );
  367. return $r;
  368. } catch ( \Exception $e ) {
  369. $this->wpdb_mixin->switch_active_processor( -$switched );
  370. throw $e;
  371. }
  372. }
  373. /**
  374. * Calls underlying processor's aproptiate method of wp_db
  375. */
  376. function replace( $table, $data, $format = null ) {
  377. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  378. try {
  379. $r = $this->wpdb_mixin->replace( $table, $data, $format );
  380. $this->wpdb_mixin->switch_active_processor( -$switched );
  381. return $r;
  382. } catch ( \Exception $e ) {
  383. $this->wpdb_mixin->switch_active_processor( -$switched );
  384. throw $e;
  385. }
  386. }
  387. /**
  388. * Calls underlying processor's aproptiate method of wp_db
  389. */
  390. function update( $table, $data, $where, $format = null, $where_format = null ) {
  391. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  392. try {
  393. $r = $this->wpdb_mixin->update( $table, $data, $where, $format, $where_format );
  394. $this->wpdb_mixin->switch_active_processor( -$switched );
  395. return $r;
  396. } catch ( \Exception $e ) {
  397. $this->wpdb_mixin->switch_active_processor( -$switched );
  398. throw $e;
  399. }
  400. }
  401. function delete( $table, $where, $where_format = null ) {
  402. $switched = $this->wpdb_mixin->switch_active_processor( 1 );
  403. try {
  404. $r = $this->wpdb_mixin->delete( $table, $where, $where_format );
  405. $this->wpdb_mixin->switch_active_processor( -$switched );
  406. return $r;
  407. } catch ( \Exception $e ) {
  408. $this->wpdb_mixin->switch_active_processor( -$switched );
  409. throw $e;
  410. }
  411. }
  412. }