-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizedb.module
More file actions
918 lines (793 loc) · 25.4 KB
/
optimizedb.module
File metadata and controls
918 lines (793 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
<?php
/**
* @file
* Database Optimization.
*
* Optimizing the site database and clearing tables cache_form.
*/
// Delete cache in table "cache_form" which expired.
define('OPTIMIZEDB_CLEAR_EXPIRED_CACHE', 0);
// Delete entire cache in table "cache_form".
define('OPTIMIZEDB_CLEAR_ENTIRE_CACHE', 1);
// Step for time clear period.
define('OPTIMIZEDB_CLEAR_PERIOD_STEP', 6);
/**
* Implements hook_menu().
*/
function optimizedb_menu() {
$items['admin/config/development/optimizedb'] = array(
'title' => 'Database Optimization',
'description' => 'Optimizing the site database and clearing tables cache_form.',
'page callback' => 'drupal_get_form',
'page arguments' => array('optimizedb_admin'),
'access arguments' => array('administer optimizedb settings'),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/development/optimizedb/settings'] = array(
'title' => 'Database Optimization',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/development/optimizedb/hide'] = array(
'title' => 'Hide notification',
'page callback' => 'optimizedb_hide_notification',
'access arguments' => array('administer optimizedb settings'),
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/development/optimizedb/list_tables'] = array(
'title' => 'List of tables in the database',
'page callback' => 'drupal_get_form',
'page arguments' => array('optimizedb_list_tables'),
'access arguments' => array('administer optimizedb settings'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function optimizedb_permission() {
return array(
'administer optimizedb settings' => array(
'title' => t('Database Optimization'),
'description' => t('Startup and configuration module.'),
),
);
}
/**
* Database Drivers with which works module.
*
* @return array
* List drivers.
*/
function optimizedb_available_db_drivers() {
return array(
'mysql',
'pgsql',
);
}
/**
* Check the module can work with the database driver.
*
* @return string|false
*/
function optimizedb_check_works() {
// Ensure translations don't break during installation.
$t = get_t();
$list_drivers = optimizedb_available_db_drivers();
$db_driver = db_driver();
if (!in_array($db_driver, $list_drivers)) {
return $t('The module Optimizedb does not work with db driver "@name".', array(
'@name' => $db_driver,
));
}
return FALSE;
}
/**
* Configuring the module.
*
* @see optimizedb_admin_clear_table_submit()
* @see optimizedb_admin_clear_table_all_submit()
* @see optimizedb_admin_optimize_table_submit()
*
* @ingroup forms
*/
function optimizedb_admin($form, &$form_state) {
if ($message = optimizedb_check_works()) {
$form['error'] = array(
'#markup' => $message,
);
return $form;
}
$form['executing_commands'] = array(
'#type' => 'fieldset',
'#title' => t('Executing commands manually'),
);
$form['executing_commands']['clear'] = array(
'#type' => 'submit',
'#value' => t('Clear cache_form table'),
'#submit' => array('optimizedb_admin_clear_table_submit'),
);
$form['executing_commands']['clear_all'] = array(
'#type' => 'submit',
'#value' => t('Clear an entire table cache_form'),
'#submit' => array('optimizedb_admin_clear_table_all_submit'),
);
$form['executing_commands']['optimize'] = array(
'#type' => 'submit',
'#value' => t('Optimize tables'),
'#submit' => array('optimizedb_admin_optimize_table_submit'),
);
$form['executing_commands']['info'] = array(
'#markup' => t('<strong>Clear cache_form table</strong> - clear the cache in a table cache_form, which has expired.<br /><strong>Clear an entire table cache_form</strong> - deleting all the cache in a table cache_form.'),
'#prefix' => '<div class="clearfix"></div>',
);
$form['optimizedb_auto'] = array(
'#type' => 'fieldset',
'#title' => t('Automatic clear cache_form table.'),
);
$form['optimizedb_auto']['optimizedb_clear_type'] = array(
'#type' => 'select',
'#title' => t('Cache removal option'),
'#options' => array(
0 => t('Delete cache which expired'),
1 => t('Delete entire cache'),
),
'#default_value' => variable_get('optimizedb_clear_type', 0),
);
$form['optimizedb_auto']['optimizedb_clear_limit'] = array(
'#type' => 'select',
'#title' => t('Remove for one time'),
'#description' => t('The number of deleted records from cache_form table at a time.'),
'#default_value' => variable_get('optimizedb_clear_limit', 0),
'#options' => array(
0 => t('Without limits'),
1000 => 1000,
5000 => 5000,
10000 => 10000,
50000 => 50000,
100000 => 100000,
500000 => 500000,
),
'#states' => array(
'visible' => array(
'select[name="optimizedb_clear_type"]' => array(
'value' => '0',
),
),
),
);
$last_clear = variable_get('optimizedb_last_clear', 0);
$form['optimizedb_auto']['optimizedb_clear_period'] = array(
'#type' => 'select',
'#title' => t('Clear cache_form table every'),
'#description' => t('Last run: @date ago.', array(
'@date' => _optimizedb_date($last_clear),
)),
'#default_value' => variable_get('optimizedb_clear_period', 0),
'#options' => array(
0 => t('Disabled'),
100 => t('When performing Cron'),
1 => t('@count day', array('@count' => 1)),
2 => t('2 day'),
7 => t('7 days'),
14 => t('14 days'),
30 => t('30 days'),
60 => t('60 days'),
),
);
$time_array = array(
t('Disabled'),
);
foreach (range(0, 23, OPTIMIZEDB_CLEAR_PERIOD_STEP) as $time) {
$time_finish = $time + OPTIMIZEDB_CLEAR_PERIOD_STEP;
$time_format = number_format($time, 2, ':', '') . ' - ' . number_format($time_finish, 2, ':', '');
$time_array[$time_finish] = $time_format;
}
$form['optimizedb_auto']['optimizedb_clear_period_time'] = array(
'#type' => 'select',
'#title' => t('The period of time when you need to clean'),
'#default_value' => variable_get('optimizedb_clear_period_time', 0),
'#options' => $time_array,
'#description' => t('<b>Warning!</b> If Cron will not be executed in the selected time period, the cleaning is not going to happen.'),
);
// Default values.
$table_length = new stdClass();
$table_length->Data_length = 0;
$table_length->Index_length = 0;
switch (db_driver()) {
case 'mysql':
$table_length = db_query("SHOW TABLE STATUS LIKE '{cache_form}';")->fetchObject();
break;
case 'pgsql':
$table_length = db_query("SELECT pg_total_relation_size('{cache_form}') AS \"Data_length\",
0 AS \"Index_length\"")->fetchObject();
break;
}
$table_length = $table_length->Data_length + $table_length->Index_length;
$form['optimizedb_auto']['length'] = array(
'#markup' => t('The current size of the table <strong>@length</strong>.', array(
'@length' => format_size($table_length),
)),
);
$form['optimize_table'] = array(
'#type' => 'fieldset',
'#title' => t('Optimization settings database'),
);
$last_optimization = variable_get('optimizedb_last_optimization', 0);
$form['optimize_table']['optimizedb_optimization_period'] = array(
'#type' => 'select',
'#title' => t('Receive notification of the need to optimize the database, every.'),
'#description' => t('Last run: @date ago.', array(
'@date' => _optimizedb_date($last_optimization),
)),
'#default_value' => variable_get('optimizedb_optimization_period', 0),
'#options' => array(
0 => t('Disabled'),
1 => t('@count day', array('@count' => 1)),
2 => t('2 day'),
7 => t('7 days'),
14 => t('14 days'),
30 => t('30 days'),
60 => t('60 days'),
),
);
$size_tables = format_size(variable_get('optimizedb_tables_size', 0));
$form['optimize_table']['tables'] = array(
'#type' => 'item',
'#title' => t('Current information on all database tables.'),
'#markup' => t('The size of all tables in the database: <b>@size</b>. View the size of the tables separately, you can on the page - <a href="@url">List of tables in the database</a>.', array(
'@size' => $size_tables,
'@url' => url('admin/config/development/optimizedb/list_tables'),
)),
);
return system_settings_form($form);
}
/**
* Page hide notification.
*/
function optimizedb_hide_notification() {
if ($message = optimizedb_check_works()) {
return $message;
}
$time = REQUEST_TIME;
$notify_optimize = variable_get('optimizedb_notify_optimize', FALSE);
// There is a need to disable the notification?
if ($notify_optimize) {
variable_set('optimizedb_notify_optimize', FALSE);
// If the notification of the need to optimize hiding, so she runs.
variable_set('optimizedb_last_optimization', $time);
$optimization_period = (int) variable_get('optimizedb_optimization_period', 0);
$time_next_optimization = strtotime('+ ' . $optimization_period . ' day', $time);
drupal_set_message(t('The following message on the need to perform optimization, you get - @date.', array(
'@date' => format_date($time_next_optimization),
)));
}
else {
drupal_set_message(t('Alerts are not available.'));
}
return '';
}
/**
* List of tables in the database with the size and sorting.
*
* @see optimizedb_list_tables_check_tables_submit()
* @see optimizedb_list_tables_repair_tables_submit()
* @see optimizedb_list_tables_optimize_tables_submit()
*
* @ingroup forms
*/
function optimizedb_list_tables($form, &$form_state) {
if ($message = optimizedb_check_works()) {
$form['error'] = array(
'#markup' => $message,
);
return $form;
}
$headers = array(
'name' => array(
'data' => t('Table name'),
),
'size' => array(
'data' => t('Table size'),
'field' => 'size',
'sort' => 'desc',
),
);
$tables = _optimizedb_tables_list();
// Desc or asc.
$sort = tablesort_get_sort($headers);
$sort_table = &drupal_static('_optimizedb_list_tables_sort:sort');
$sort_table = $sort;
// Sort descending and ascending.
usort($tables, '_optimizedb_list_tables_sort');
$rows = array();
foreach ($tables as $table) {
// Parameter "size_byte" us only needed to sort, now his unit to remove.
unset($table['size_byte']);
$rows[$table['name']] = $table;
}
$operations_tables_result = isset($_SESSION['optimizedb_list_tables_operations']) ? $_SESSION['optimizedb_list_tables_operations'] : NULL;
if (!is_null($operations_tables_result)) {
if ($operations_tables_result == array()) {
drupal_set_message(t('The operation completed successfully.'));
}
else {
$form['operations_tables'] = array(
'#type' => 'fieldset',
'#title' => t('Errors that arose during the operation:'),
);
$form['operations_tables']['errors'] = array(
'#markup' => theme('table', array(
'header' => array(
array('data' => t('Table name')),
array('data' => t('Type of problem')),
array('data' => t('Information about the problem')),
),
'rows' => $operations_tables_result,
)),
);
}
}
$_SESSION['optimizedb_list_tables_operations'] = NULL;
if (db_driver() == 'mysql') {
$form['operations'] = array(
'#type' => 'fieldset',
'#title' => t('Operations with tables:'),
);
$form['operations']['check_tables'] = array(
'#type' => 'submit',
'#value' => t('Check tables'),
'#submit' => array('optimizedb_list_tables_check_tables_submit'),
);
$form['operations']['repair_tables'] = array(
'#type' => 'submit',
'#value' => t('Repair tables'),
'#submit' => array('optimizedb_list_tables_repair_tables_submit'),
);
$form['operations']['optimize_tables'] = array(
'#type' => 'submit',
'#value' => t('Optimize tables'),
'#submit' => array('optimizedb_list_tables_optimize_tables_submit'),
);
}
$form['tables'] = array(
'#type' => 'tableselect',
'#header' => $headers,
'#options' => $rows,
'#empty' => t('No content available.'),
);
return $form;
}
/**
* Form submission handler for optimizedb_list_tables().
*
* Checking the selected tables to find errors.
*/
function optimizedb_list_tables_check_tables_submit($form, &$form_state) {
_optimizedb_list_tables_operation_execute($form, $form_state, 'CHECK TABLE');
}
/**
* Form submission handler for optimizedb_list_tables().
*
* Repair selected tables.
*/
function optimizedb_list_tables_repair_tables_submit($form, &$form_state) {
_optimizedb_list_tables_operation_execute($form, $form_state, 'REPAIR TABLE');
}
/**
* Form submission handler for optimizedb_list_tables().
*
* Optimization of the selected tables.
*/
function optimizedb_list_tables_optimize_tables_submit($form, &$form_state) {
_optimizedb_list_tables_operation_execute($form, $form_state, 'OPTIMIZE TABLE');
}
/**
* Performing operations on tables.
*
* @param string $operation_name
* The operation to be performed with tables.
*
* @see _optimizedb_list_tables_operation_execute_run()
*/
function _optimizedb_list_tables_operation_execute($form, $form_state, $operation_name) {
$tables_selected = $form_state['values']['tables'];
if (db_driver() == 'mysql') {
if (!empty($tables_selected)) {
$operations = array();
foreach ($tables_selected as $table_name => $selected) {
if ($table_name === $selected) {
$operations[] = array('_optimizedb_list_tables_operation_execute_run', array(array(
'name' => $table_name,
'operation' => $operation_name,
)));
}
}
if (empty($operations)) {
drupal_set_message(t('To execute, you must select at least one table from the list.'), 'error');
}
else {
$batch = array(
'operations' => $operations,
'finished' => '_optimizedb_list_tables_operation_execute_finished',
);
batch_set($batch);
}
}
else {
drupal_set_message(t('To execute, you must select at least one table from the list.'), 'error');
}
}
else {
drupal_set_message(t('Function to check tables is only available for databases MySQL.'), 'error');
}
}
/**
* Batch callback: Common tasks checking, repairs and optimizes tables.
*
* @param array $param
* Table names and types of operations.
* @param array $context
* Reference to an array used for Batch API storage.
*/
function _optimizedb_list_tables_operation_execute_run($param, &$context) {
$operation_result = db_query($param['operation'] . ' ' . $param['name'])->fetchAll();
foreach ($operation_result as $result) {
if (isset($result->Msg_type) && in_array(strtolower($result->Msg_type), array('error', 'warning'))) {
$context['results']['errors'][] = array(
array('data' => $param['name']),
array('data' => $result->Msg_type),
array('data' => $result->Msg_text),
);
}
}
}
/**
* Output results when a batch is complete.
*
* Callback for batch_set().
*
* @param bool $success
* A boolean indicating whether the batch operation successfully concluded.
* @param int $results
* The results from the batch process.
* @param array $operations
* The batch operations that remained unprocessed. Only relevant if $success
* is FALSE.
*
* @ingroup callbacks
*/
function _optimizedb_list_tables_operation_execute_finished($success, $results, $operations) {
if ($success) {
if (isset($results['errors'])) {
$result = $results['errors'];
}
else {
$result = array();
}
$_SESSION['optimizedb_list_tables_operations'] = $result;
}
else {
drupal_set_message(t('Completed with errors.'), 'error');
}
}
/**
* Sorting table data size.
*
* Callback for usort() in optimizedb_list_tables().
*/
function _optimizedb_list_tables_sort($a, $b) {
$sort = &drupal_static(__FUNCTION__ . ':sort');
if ($sort == 'asc') {
return $a['size_byte'] > $b['size_byte'];
}
return $a['size_byte'] < $b['size_byte'];
}
/**
* Output how much time passed from the specified date.
*
* @param int $timestamp
* The date in Unix format.
*
* @return string
* The formatted date.
*/
function _optimizedb_date($timestamp) {
$timestamp = (int) $timestamp;
if ($timestamp == 0) {
return t('None');
}
$difference = REQUEST_TIME - $timestamp;
$text = format_interval($difference, 1);
return $text;
}
/**
* List and the size of the database tables.
*
* @return array
* An array with a list of database tables.
*/
function _optimizedb_tables_list() {
$tables = FALSE;
switch (db_driver()) {
case 'mysql':
$tables = db_query("SHOW TABLE STATUS", array(), array(
'fetch' => PDO::FETCH_OBJ,
));
break;
case 'pgsql':
$tables = db_query("SELECT table_name as \"Name\",
pg_total_relation_size(table_name) AS \"Data_length\",
0 as \"Index_length\"
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name", array(), array('fetch' => PDO::FETCH_OBJ));
break;
}
$result = array();
if ($tables) {
$size_tables = 0;
foreach ($tables as $table) {
$length = $table->Data_length + $table->Index_length;
$result[$table->Name] = array(
'name' => $table->Name,
'size' => format_size($length),
'size_byte' => $length,
);
$size_tables += $length;
}
// The total size of the tables.
variable_set('optimizedb_tables_size', $size_tables);
}
return (array) $result;
}
/**
* Form submission handler for optimizedb_admin().
*
* Optimization of all database tables. Getting a list of all tables
* and transfer of each table in the batch.
*/
function optimizedb_admin_optimize_table_submit($form, &$form_state) {
$tables = array();
switch (db_driver()) {
case 'mysql':
$tables = db_query("SHOW TABLES", array(), array(
'fetch' => PDO::FETCH_NUM,
));
break;
case 'pgsql':
$tables = db_query("SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name", array(), array('fetch' => PDO::FETCH_NUM));
break;
}
$operations = array();
foreach ($tables as $table) {
$operations[] = array('optimizedb_optimize_batch_run', array($table[0]));
}
$batch = array(
'operations' => $operations,
'finished' => 'optimizedb_optimize_batch_finished',
);
batch_set($batch);
// Calling this function, you need to run the optimization from
// the command line.
if (PHP_SAPI == 'cli') {
drush_backend_batch_process();
}
}
/**
* Batch callback: Optimization of a database table.
*
* @param string $table
* Table name.
* @param array $context
* Reference to an array used for Batch API storage.
*/
function optimizedb_optimize_batch_run($table, &$context) {
// Standard the status the result operation.
$status = 'success';
switch (db_driver()) {
case 'mysql':
try {
$result = db_query('OPTIMIZE TABLE ' . $table)->fetchObject();
if (isset($result->Msg_type) && in_array(strtolower($result->Msg_type), array('error', 'warning'))) {
$status = 'error';
}
}
catch (Exception $e) {
$status = 'error';
}
break;
case 'pgsql':
try {
db_query('VACUUM ANALYZE ' . $table);
}
catch (Exception $e) {
$status = 'error';
}
break;
}
if (PHP_SAPI == 'cli') {
drush_print(dt('Table "@name" been optimized.', array(
'@name' => $table,
)));
}
$context['results'][$status][] = $table;
}
/**
* Output results when a batch is complete.
*
* Callback for batch_set().
*
* @param bool $success
* A boolean indicating whether the batch operation successfully concluded.
* @param int $results
* The results from the batch process.
* @param array $operations
* The batch operations that remained unprocessed. Only relevant if $success
* is FALSE.
*
* @ingroup callbacks
*/
function optimizedb_optimize_batch_finished($success, $results, $operations) {
if ($success) {
if (isset($results['error'])) {
$message_error = 'When optimizing experiencing errors with tables:
@tables. You need as quickly as possible <a href="@url">to check the table</a>. Other tables
were successfully optimized.';
if (PHP_SAPI == 'cli') {
drush_log(dt($message_error, array(
'@tables' => implode(', ', $results['error']),
'@url' => url('admin/config/development/optimizedb/list_tables'),
)), 'error');
}
else {
drupal_set_message(t($message_error, array(
'@tables' => implode(', ', $results['error']),
'@url' => url('admin/config/development/optimizedb/list_tables'),
)), 'error');
}
}
else {
$message_success = 'Optimized @count tables.';
if (PHP_SAPI == 'cli') {
drush_log(dt($message_success, array(
'@count' => count($results['success']),
)), 'success');
}
else {
drupal_set_message(t($message_success, array(
'@count' => count($results['success']),
)));
}
}
}
else {
$message_error = 'Completed with errors.';
if (PHP_SAPI == 'cli') {
drush_log(dt($message_error), 'error');
}
else {
drupal_set_message(t($message_error), 'error');
}
}
variable_set('optimizedb_notify_optimize', FALSE);
variable_set('optimizedb_last_optimization', REQUEST_TIME);
}
/**
* Form submission handler for optimizedb_admin().
*
* Clear cache in table "cache_form" which has expired.
*/
function optimizedb_admin_clear_table_submit($form, &$form_state) {
_optimizedb_clear_cache_form_table(OPTIMIZEDB_CLEAR_EXPIRED_CACHE);
drupal_set_message(t('The table is cleared.'));
}
/**
* Form submission handler for optimizedb_admin().
*
* Clear all cache in table "cache_form".
*/
function optimizedb_admin_clear_table_all_submit($form, &$form_state) {
_optimizedb_clear_cache_form_table(OPTIMIZEDB_CLEAR_ENTIRE_CACHE);
drupal_set_message(t('The table is cleared.'));
}
/**
* Cleaning table cache_form and cleaning time record.
*
* @param mixed $type
* - NULL: Default type, which is selected by the user.
* - OPTIMIZEDB_CLEAR_EXPIRED_CACHE: Delete cache which expired.
* - OPTIMIZEDB_CLEAR_ENTIRE_CACHE: Delete entire cache.
*/
function _optimizedb_clear_cache_form_table($type = NULL) {
if (is_null($type)) {
$type = variable_get('optimizedb_clear_type', 0);
}
if ((int) $type == 1) {
db_truncate('cache_form')->execute();
}
else {
$count_all = (int) db_select('cache_form')
->countQuery()
->execute()
->fetchField();
if ($count_all !== 0) {
$count_expire = (int) db_select('cache_form')
->condition('expire', REQUEST_TIME, '<')
->countQuery()
->execute()
->fetchField();
$limit = (int) variable_get('optimizedb_clear_limit', 0);
$limit_status = $limit == 0 ? TRUE : ($count_all <= $limit);
if ($count_all == $count_expire && $limit_status) {
db_truncate('cache_form')->execute();
}
else {
if ($limit > 0) {
$query = db_select('cache_form', 'cf');
$query->fields('cf', array('cid'));
$query->condition('cf.expire', REQUEST_TIME, '<');
$query->orderBy('cf.expire', 'ASC');
$query->range(0, $limit);
$ids = $query->execute()->fetchCol();
if (!empty($ids)) {
db_delete('cache_form')
->condition('cid', $ids, 'IN')
->execute();
}
}
else {
db_delete('cache_form')
->condition('expire', REQUEST_TIME, '<')
->execute();
}
}
}
}
// Update size tables.
_optimizedb_tables_list();
variable_set('optimizedb_last_clear', REQUEST_TIME);
}
/**
* Implements hook_cron().
*
* In Cron operations are performed:
* - Cleaning the table "cache_form" if it is necessary in time.
* - Display a message on the need to optimize.
*
* @see _optimizedb_clear_cache_form_table()
*/
function optimizedb_cron() {
// Clear cache_form table.
$clear_period = (int) variable_get('optimizedb_clear_period', 0);
if ($clear_period !== 0) {
$last_clear = variable_get('optimizedb_last_clear', 0);
$time_next_clear = strtotime('+ ' . $clear_period . ' day',
($last_clear == 0 ? REQUEST_TIME : $last_clear));
if ($clear_period == 100 || $time_next_clear <= REQUEST_TIME) {
$clear_cache_execute = TRUE;
// Performing cleaning table only in a given period of time.
if ($period_time = variable_get('optimizedb_clear_period_time', 0)) {
$period_time_start = $period_time - OPTIMIZEDB_CLEAR_PERIOD_STEP;
$current_hour = (int) date('H');
$clear_cache_execute = ($current_hour >= $period_time_start && $current_hour <= $period_time);
}
if ($clear_cache_execute) {
_optimizedb_clear_cache_form_table();
}
}
}
// Check whether there is a need to optimize.
$optimization_period = (int) variable_get('optimizedb_optimization_period', 0);
if ($optimization_period !== 0) {
$last_optimization = variable_get('optimizedb_last_optimization', 0);
$time_next_optimization = strtotime('+ ' . $optimization_period . ' day',
($last_optimization == 0 ? REQUEST_TIME : $last_optimization));
if ($time_next_optimization <= REQUEST_TIME) {
variable_set('optimizedb_notify_optimize', TRUE);
}
}
}