-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd.module
More file actions
381 lines (352 loc) · 10.9 KB
/
Copy pathstd.module
File metadata and controls
381 lines (352 loc) · 10.9 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
<?php
/**
* @file
* Code for the OG Importer module
*/
/**
* Implements hook_menu
*/
function std_menu() {
$items['ajax/resemblejs'] = array(
'type' => MENU_CALLBACK,
'title' => 'Resemble JS callback',
'page callback' => 'std_resemble_ajax',
'access callback' => TRUE,
);
return $items;
}
/**
* Ajax callback to create a diff image
*/
function std_resemble_ajax() {
if (empty($_POST['image_data']) || empty($_POST['nid'])) {
return 'No data received';
}
// security sucks, for now complete lockdown is good, we need htpassword lock down
// create a managed file entry
// also the post data should contain the node id of the node this goes to, perform some action to save the image there
$nid = $_POST['nid'];
$node = node_load($nid);
$lang = $node->language;
$image = substr($_POST['image_data'], 22);
$file = file_save_data(base64_decode($image), "public://diff/" . $nid . '.png', FILE_EXISTS_REPLACE);
$node->field_image_diff[$lang][0] = array(
'fid' => $file->fid,
);
// Add the diff percentage
if (!empty($_POST['diff'])) {
$node->field_difference[$lang][0]['value'] = check_plain($_POST['diff']);
}
node_save($node);
}
/**
* Implements hook_cron
*/
function std_cron() {
// need a way to clean up the web pages that are no longer in the sets
// maybe make it a cron runner
}
/**
* Adding our custom js for diff on the main view
* @param $vars
*/
function std_preprocess_views_view(&$vars) {
if ($vars['view']->name === 'regression_test') {
$path = drupal_get_path('module', 'std');
drupal_add_js($path . '/resemble.js');
}
}
/**
* Implements hook_form_FORMID_alter
*/
function std_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
if (isset($form['tid']['#value'])) {
// adding button for capture screenshots
$form['screenshot'] = array(
'#type' => 'fieldset',
'#title' => t('Screenshots'),
'#weight' => 50,
'#collapsible' => FALSE,
);
$form['screenshot']['capture_before'] = array(
'#type' => 'submit',
'#value' => t('Capture Before'),
);
$form['screenshot']['capture_after'] = array(
'#type' => 'submit',
'#value' => t('Capture After'),
);
$form['screenshot']['generate_diff'] = array(
'#type' => 'submit',
'#value' => t('Generate Diff'),
);
}
$form['#validate'][] = 'std_set_url_validate';
$form['#submit'][] = 'std_set_submit';
}
/**
* Our own custom validation handler to check the entered URL
* @param $form
* @param $form_state
*/
function std_set_url_validate($form, &$form_state) {
if ($form_state['input']['op'] == 'Delete') {
return;
}
// @todo: add drupal_request later to make sure we don't have a number of 403 and 404
$_urls = $form_state['values']['field_set_urls'][LANGUAGE_NONE][0]['value'];
$_urls = nl2br($_urls);
$urls = explode('<br />', $_urls);
foreach ($urls as $key => $url) {
$url = trim($url);
$urls[$key] = $url;
if (!valid_url($url, TRUE)) {
form_set_error('field_set_urls', $url . ' is not a valid url');
}
}
// hold these in the variables
variable_set('std_set', $urls);
$form_state['set'] = $urls;
}
/**
* Submission handler for the set, this will trigger a batch to sync the set contents
* @param $form
* @param $form_state
*/
function std_set_submit($form, &$form_state) {
if ($form_state['input']['op'] == 'Delete') {
return;
}
switch($form_state['input']['op']) {
case 'Capture Before':
$op = 'before';
$nids = _std_nodes_in_set($form_state['values']['tid']);
break;
case 'Capture After':
$op = 'after';
$nids = _std_nodes_in_set($form_state['values']['tid']);
break;
case 'Generate Diff':
$op = 'diff';
$nids = _std_nodes_in_set($form_state['values']['tid']);
break;
default:
$op = 'sync';
// These are just URLs in this case, not nids
$nids = $form_state['set'];
break;
}
if (empty($nids)) {
drupal_set_message('No test pages to sync or capture screenshots for.');
return;
}
$function = 'std_set_batch';
$batch = $function($op, $form_state['values']['tid'], $nids);
batch_set($batch);
}
/**
* Batch operation function
* @param $op The type of operation we are batching: SYNC | BEFORE | AFTER
* @param $tid The term id of for the current set
* @param $nids The Either the list of nids or urls (in case of syncing)
* @return array
*/
function std_set_batch($op, $tid, $nids) {
// 1. If are syncing
switch ($op) {
case 'before':
$function = 'std_batch_before_operation';
break;
case 'after':
$function = 'std_batch_after_operation';
break;
case 'diff':
$function = 'std_batch_diff_operation';
break;
case 'sync':
default:
$function = 'std_batch_sync_operation';
break;
}
$num_operations = count($nids);
drupal_set_message(ucwords($op) . t('ing @num test set web pages', array('@num' => $num_operations)));
$operations = array();
for ($i = 0; $i < $num_operations; $i++) {
$operations[] = array(
$function,
array(
$nids[$i],
$tid,
t('(Operation @operation)', array('@operation' => $i)),
),
);
}
$batch = array(
'operations' => $operations,
'finished' => 'std_batch_finished',
);
return $batch;
}
/**
* Batch worker callback for capturing before picture
* @param $nid
* @param $tid
* @param $operation_details
* @param $context
*/
function std_batch_before_operation($nid, $tid, $operation_details, &$context) {
_std_batch_screen_capture($nid);
$context['message'] = t('Capturing before screenshots for @url', array('@url' => $nid)) . ' ' . $operation_details;
$context['results'][] = $nid;
}
/**
* Batch worker callback for capturing after picture
* @param $nid
* @param $tid
* @param $operation_details
* @param $context
*/
function std_batch_after_operation($nid, $tid, $operation_details, &$context) {
_std_batch_screen_capture($nid, 'after');
$context['message'] = t('Capturing after screenshots for @nid', array('@nid' => $nid)) . ' ' . $operation_details;
$context['results'][] = $nid;
}
/**
* Batch worker callback for capturing after picture
* @param $nid
* @param $tid
* @param $operation_details
* @param $context
*/
function std_batch_diff_operation($nid, $tid, $operation_details, &$context) {
// 1. Load the node and see what's up
// 2. Make a request to the plain html file for diff comparison
// 3. Sync managed file table
$node = node_load($nid);
if (empty($node->field_image_before) || empty($node->field_image_after)) {
return;
}
global $base_url;
$script = drupal_get_path('module', 'std') . '/ajax.js';
$request_url = $base_url . '/' . drupal_get_path('module', 'std') . '/compare.html';
$lang = $node->language;
// We use Phantom js to open this page
shell_exec('phantomjs ' . $script . ' ' . $request_url . ' ' . file_create_url($node->field_image_before[$lang][0]['uri']) . ' ' . file_create_url($node->field_image_after[$lang][0]['uri']) . ' ' . $nid);
// $result = drupal_http_request($request_url . $query);
$context['message'] = t('Generating diff for @nid', array('@nid' => $nid)) . ' ' . $operation_details;
}
/**
* Perform screen capturing
* @param $nid
* @param string $type
*/
function _std_batch_screen_capture($nid, $type = 'before') {
$dir = 'public://' . $type;
if (!file_prepare_directory($dir)) {
drupal_mkdir($dir);
}
$node = node_load($nid);
// @todo: how do we enforce the phantomjs requirement
// Do the screenshot capture
$path = drupal_get_path('module', 'std') . '/screen.js';
shell_exec('phantomjs ' . $path . ' ' . $node->title . ' sites/default/files/' . $type . '/' . $nid . '.png');
// saving successfully saved files into the file_managed table
if (!file_exists('sites/default/files/' . $type . '/' . $nid . '.png')) {
return;
}
// otherwise we will get error when re-generating the screenshots
// if the screenshot has already been updated by phantom, there is no need to do anything to the files table
$files = file_load_multiple(array(), array('uri' => 'public://' . $type . '/' . $nid . '.png'));
$file = reset($files);
if (!empty($file)) {
// perhaps update it? (update filesize and date)
$file->timestamp = REQUEST_TIME;
$file->filesize = sizeof('sites/default/files/' . $type . '/' . $nid . '.png');
file_save($file);
return;
}
$save = new stdClass();
$save->uid = 1;
$save->uri = 'public://' . $type . '/' . $nid . '.png';
$save->filename = $nid . '.png';
$save->filemime = 'image/png';
$save->status = 1;
$saved = file_save($save);
// put new saved file into the node
$field = 'field_image_' . $type;
$node->{$field}[LANGUAGE_NONE][0]['fid'] = $saved->fid;
node_save($node);
}
/**
* Batch worker callback for syncing
* @param $url
* @param $tid
* @param $operation_details
* @param $context
*/
function std_batch_sync_operation($url, $tid, $operation_details, &$context) {
global $user;
// 1. Check to see if any web page CT has been created with the url as its title (In the current SET tagged as taxonomy)
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'web_page')
->propertyCondition('status', 1)
->propertyCondition('title', $url)
->fieldCondition('field_set_ref', 'tid', $tid);
$result = $query->execute();
// 2a. If yes let's skip it
// 2b. If No let's programmatically create the node
if (empty($result)) {
$node = new stdClass();
$node->title = $url;
$node->type = "web_page";
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->status = 1;
$node->promote = 0;
$node->comment = 0;
$node->field_set_ref[LANGUAGE_NONE][]['tid'] = $tid;
node_save($node);
$context['results'][] = $node->title;
}
// Optional message displayed under the progressbar.
$context['message'] = t('Syncing @url', array('@url' => $url)) . ' ' . $operation_details;
}
/**
* Batch 'finished' callback used by both batch 1 and batch 2.
*/
function std_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('Process finished... with @results items performed', array('@results' => count($results))));
}
else {
$error_operation = reset($operations);
drupal_set_message(
t('An error occurred while processing @operation with arguments : @args',
array(
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
)
)
);
}
}
/**
* A quick EFQ to find out how many nodes in a SET
* @param $tid
* @return mixed
*/
function _std_nodes_in_set($tid) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'web_page')
->propertyCondition('status', 1)
->fieldCondition('field_set_ref', 'tid', $tid);
$result = $query->execute();
if (empty($result)) {
return array();
} else {
return array_keys($result['node']);
}
}