forked from voxpelli/drupal-materialized-views
-
Notifications
You must be signed in to change notification settings - Fork 0
/
materialized_view.module
176 lines (149 loc) · 4.49 KB
/
materialized_view.module
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
<?php
require 'materialized_view.class.inc';
/**
* Implementation of hook_menu().
*/
function materialized_view_menu() {
$items = array();
$items['admin/reports/materialized_view'] = array(
'title' => 'Materialized view status',
'description' => 'Check the status of indexing on materialized views.',
'page callback' => 'materialized_view_status',
'access arguments' => array('administer materialized views'),
'file' => 'materialized_view.admin.inc',
'file path' => drupal_get_path('module', 'materialized_view'),
);
$items['cron/materialized_view/%'] = array(
'page callback' => 'materialized_view_index',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function materialized_view_perm() {
return array('administer materialized views');
}
/**
* Implementation of hook_exit().
*/
function materialized_view_exit() {
MVJobQueue::run();
}
/**
* Implementation of hook_cron().
*/
function materialized_view_cron() {
materialized_view_index();
}
function materialized_view_index($batch_size = NULL) {
materialized_view_reconcile();
if (!$batch_size) {
$batch_size = variable_get('mv_indexing_batch_size', 100);
}
$materialized_views = materialized_view_get();
foreach ($materialized_views as $materialized_view) {
$materialized_view->index($batch_size);
}
}
/**
* Check that all materialized views are registered for indexing
* and have up-to-date schema.
*/
function materialized_view_reconcile() {
$effect = FALSE;
// Load the list of materialized views.
$materialized_views = materialized_view_get();
foreach ($materialized_views as $materialized_view) {
if ($materialized_view->verifySchema()) {
$effect = TRUE;
}
}
// Remove tables and indexing data for views that no longer have registration.
$res = db_query('SELECT mvid FROM {materialized_view}');
while ($row = db_fetch_array($res)) {
if (!array_key_exists($row['mvid'], $materialized_views)) {
// Create a dummy MV to "uninstall."
$materialized_view_to_drop = new MaterializedView($row['mvid']);
$materialized_view_to_drop->uninstallSchema();
$effect = TRUE;
}
}
return $effect;
}
/**
* Retrieve the array for an entity type by name
* or retrieve an array of all entity types.
*
* @param $name
* The name of the entity type.
* Pass NULL to get all entity types.
*/
function materialized_view_entity_type_get($name = NULL, $reset = FALSE) {
static $entity_types = NULL;
// Check if the set of entity types is in the static cache.
if (is_null($entity_types) || $reset) {
$entity_types = module_invoke_all('entity_type_info');
}
// Return just one entity object if a name was specified.
if (!is_null($name)) {
return $entity_types[$name];
}
return $entity_types;
}
/**
* Retrieve the object for a materialized view by name
* or retrieve an array of all materialized views.
*
* @param $name
* The name of the materialized view.
* Pass NULL to get all materialized views.
*/
function materialized_view_get($name = NULL, $reset = FALSE) {
static $materialized_views = NULL;
// Check if the set of materialized views is in the static cache.
if (is_null($materialized_views) || $reset) {
$materialized_views = array();
$hook_results = module_invoke_all('materialized_view_info');
// Loop through materialized views and load them into the cache,
// keyed by name.
foreach ($hook_results as $hook_result) {
$materialized_views[$hook_result->getName()] = $hook_result;
}
}
// Return just one MV object if a name was specified.
if (!is_null($name)) {
return $materialized_views[$name];
}
return $materialized_views;
}
/**
* Implementation of hook_modules_enabled().
*
* This hook doesn't exist in D6.
*/
function materialized_view_modules_enabled($modules) {
materialized_view_reconcile();
}
/**
* Implementation of hook_modules_disabled(). This hook doesn't exist in D6.
*
* This hook doesn't exist in D6.
*/
function materialized_view_modules_disabled($modules) {
// Clean up MV tables from the disabled module(s).
foreach ($modules as $module) {
$function = $module . '_materialized_view_info';
if (function_exists($function)) {
$materialized_views = $function();
if (is_array($materialized_views)) {
foreach ($materialized_views as $materialized_view) {
$materialized_view->uninstallSchema();
}
}
}
}
}