-
Notifications
You must be signed in to change notification settings - Fork 10
/
crot_cron.php
599 lines (543 loc) · 22.4 KB
/
crot_cron.php
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
<?php
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
}
// overrides php limits
$maxtimelimit = ini_get('max_execution_time');
ini_set('max_execution_time', 18000);
$maxmemoryamount = ini_get('memory_limit');
// set large amount of memory for the processing
// fingeprint calcualtion mey be very memory consuming especially for large documents from the internet
ini_set('memory_limit', '1024M');
// store current time for perf measurements
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
global $CFG, $DB;
require_once($CFG->dirroot.'/plagiarism/crot/lib.php');
require_once($CFG->dirroot.'/plagiarism/crot/locallib.php');
require_once($CFG->dirroot."/course/lib.php");
require_once($CFG->dirroot."/mod/assignment/lib.php");
require_once($CFG->dirroot.'/config.php');
$plagiarismsettings = (array)get_config('plagiarism');
$gram_size = $plagiarismsettings['crot_grammarsize'];
$window_size = $plagiarismsettings['crot_windowsize'];
$query_size = $plagiarismsettings['crot_global_search_query_size'];
$msnkey = $plagiarismsettings['crot_live_key'];
$culture_info = $plagiarismsettings['crot_culture_info'];
$globs = $plagiarismsettings['crot_percentage_of_search_queries'];
$todown = $plagiarismsettings['crot_number_of_web_documents'];
if (empty($gram_size)||empty($window_size)) {
die('The plugin is not properly set. Please set the plugin in admin/plugins/plagiarism prevention menu'); /// the initial settigns were not properly set up
}
// main loop on crot_files table - check if there are files marked for the check up
//$link = mysql_connect("$CFG->dbhost", "$CFG->dbuser", "$CFG->dbpass") or die("Could not connect");
//mysql_select_db("$CFG->dbname") or die ("Could not select database");
$sql_query = "SELECT cf.* FROM {plagiarism_crot_files} cf where cf.status = 'queue'";
$files = $DB->get_records_sql($sql_query);
if (!empty($files)){
foreach ($files as $afile){
try {
$afile->status = 'in_processing';
$DB->update_record('plagiarism_crot_files', $afile);
echo "\nfile $afile->id was not processed yet. start processing now ... \n" ;
$atime = microtime();
$atime = explode(" ",$atime);
$atime = $atime[1] + $atime[0];
$astarttime = $atime;
$fs = get_file_storage();
$file = $fs->get_file_by_id($afile->file_id);
$filename = $file->get_filename(); // get file name
$arrfilename = explode(".",$filename);
$ext = $arrfilename[count($arrfilename)-1];// get file extension
$l1 = $afile->path[0].$afile->path[1];
$l2 = $afile->path[2].$afile->path[3];
$apath= $CFG->dataroot."/filedir/$l1/$l2/$afile->path"; // get file path
// call tokenizer to get plain text and store it in plagiarism_crot_documents
$atext = tokenizer ($apath, $ext);
$atext = mb_substr($atext, 0, 999000);
// insert into plagiarism_crot_documents
// $docrecord = new object();
$docrecord->crot_submission_id = $afile->id;
$docrecord->content = addslashes($atext);
$docid = $DB->insert_record('plagiarism_crot_documents', $docrecord);
//$content = addslashes($atext);
//$query = "INSERT INTO {$CFG->prefix}plagiarism_crot_documents (content, crot_submission_id) VALUES ('$content','$afile->id')";
//mysql_query($query);
// $docid = mysql_insert_id();
// fingerprinting - calculate and store the fingerprints into the table
$atext=mb_strtolower($atext, "utf-8");
// get f/print
$fingerp = array();
$fingerp = GetFingerprint($atext);
// store fingerprint
foreach ($fingerp as $fp) {
$hashrecord->position = $fp->position;
$hashrecord->crot_doc_id = $docid;
$hashrecord->value = $fp->value;
$DB->insert_record("plagiarism_crot_fingerprint", $hashrecord);
}
// local search
echo "starting local search \n";
$plagiarismvalues = $DB->get_records_menu('plagiarism_crot_config', array('cm'=>$afile->cm),'','name,value');
if ($plagiarismvalues['crot_local']==1) {
// comparing fingerprints and updating plagiarism_crot_spair table
// select all submissions that has at least on common f/print with the current document
$sql_query ="SELECT id
FROM {$CFG->prefix}plagiarism_crot_documents asg
WHERE exists (
select * from
{$CFG->prefix}plagiarism_crot_fingerprint fp1
inner join {$CFG->prefix}plagiarism_crot_fingerprint fp2 on fp1.value = fp2.value
where fp2.crot_doc_id = asg.id and fp1.crot_doc_id = $docid
)";
$pair_submissions = $DB->get_records_sql($sql_query);
foreach ($pair_submissions as $pair_submission){
// check if id exists in web_doc table then don't compare because
// we consider only local documents here
if ($webdoc = $DB->get_record("plagiarism_crot_webdoc", array('document_id'=>$pair_submission->id)))
continue;
//compare two fingerprints to get the number of same hashes
if ($docid!=$pair_submission->id){
$sql_query = "select sum(case when cnt1 < cnt2 then cnt1 else cnt2 end) cnt
from
(
select count(*) as cnt1, (
select count(*)
from {$CFG->prefix}plagiarism_crot_fingerprint fp2
where fp2.crot_doc_id = $docid
and fp2.value = fp1.value
) as cnt2
from {$CFG->prefix}plagiarism_crot_fingerprint fp1
where fp1.crot_doc_id = $pair_submission->id
group by fp1.value
) t";
$similarnumber = $DB->get_record_sql($sql_query);
// takes id1 id2 and create/update record with the number of similar hashes
$sql_query ="SELECT * FROM {$CFG->prefix}plagiarism_crot_spair
where (submission_a_id = $afile->id and submission_b_id = $pair_submission->id)
OR (submission_a_id = $pair_submission->id and submission_b_id = $afile->id)";
$pair = $DB->get_record_sql($sql_query);
if (! $pair){
// insert
$pair_record->submission_a_id = $docid;
$pair_record->submission_b_id = $pair_submission->id;
$pair_record->number_of_same_hashes = $similarnumber->cnt;
$DB->insert_record("plagiarism_crot_spair", $pair_record);
} else {
// TODO update
}
} // end of comparing with local documents
}
} // end for local search
if ($plagiarismvalues['crot_global']==1) {
// global search
echo "\nfile $afile->id is selected for global search. Starting global search\n";
// strip text
$atext = StripText($atext," ");
// create search queries
$words = array();
$words = preg_split("/[\s]+/", trim(StripText($atext, " ")));
$max = sizeof($words) - $query_size +1;
$queries = array ();
for ($i=0; $i < $max; $i++) {
$query = "";
for ($j=$i; ($j-$i)<$query_size; $j++){
$query = $query." ".$words[$j];
}
$queries[] = $query;
} // queries are ready!
// create list of URLs
srand((float) microtime() * 10000000);
// randomly select x% of queries
$rand_keys = array_rand($queries, (sizeof($queries)/100)*$globs);
$narr = array();
foreach ($rand_keys as $mkey) {
$narr[]=$queries[$mkey];
}
$queries = $narr;
$tarr = getTopResults($queries,$todown,$msnkey,$culture_info);
$k=0;
// get top results
foreach($tarr as $manUrl) {
//get content of downloaded web document
// in php ini allow_url_fopen = On
$path = $manUrl->mainUrl;
// get content from the remote file
$mega = array ();
// get content and get encoding
if (trim($path)!="") {
try {
$result = getremotecontent( $path );
if (trim($result)==""){
continue;
}
}
catch (Exception $e) {
print_error("exception in downloading!\n");
$result = "Was not able to download the respective resource";
}
}
else {
continue;
}
$result = mb_ereg_replace('#\s{2,}#',' ',$result);
// split into strings and remove empty ones
$strs = explode ("\n", $result);
$result = "";
foreach ($strs as $st) {
$st = trim($st);
if ($st!="") {
$result = $result.mb_ereg_replace('/\s\s+/', ' ', $st)." \n";
}
}
// insert doc into crot_doc table
$wdocrecord->crot_submission_id = 0;
$wdocrecord->content = addslashes($result);
$wdocid = $DB->insert_record("plagiarism_crot_documents", $wdocrecord);
//$wdoc_content = addslashes($result);
//$query = "INSERT INTO {$CFG->prefix}plagiarism_crot_documents (content, crot_submission_id) VALUES ('$wdoc_content','0')";
// mysql_query($query);
//$wdocid = mysql_insert_id();
// insert doc into web_doc table
$webdocrecord->document_id = $wdocid;
$webdocrecord->link=urlencode($manUrl->mainUrl);
$webdocrecord->link_live=urlencode($manUrl->msUrl);
$webdocrecord->is_from_cache=false;
$webdocrecord->related_doc_id = $docid;
$webdocid = $DB->insert_record("plagiarism_crot_webdoc", $webdocrecord);
//$weblink = urlencode($manUrl->mainUrl);
//$weblinklive = urlencode($manUrl->msUrl);
//$query = "INSERT INTO {$CFG->prefix}plagiarism_crot_webdoc (document_id,link,link_live,is_from_cache,related_doc_id) VALUES ('$wdocid','$weblink','$weblinklive','false','$docid')";
//mysql_query($query);
//$webdocid = mysql_insert_id();
// fingerprinting - calculate and store the fingerprints into the table
$result=mb_convert_case($result, MB_CASE_LOWER, "UTF-8");
$fingerp = array();
try {
$fingerp = GetFingerprint($result);
}
catch (Exception $e)
{
print_error("exception in FP calc\n");
continue;
}
// store fingerprint
foreach ($fingerp as $fp)
{
$hashrecord->position = $fp->position;
$hashrecord->crot_doc_id = $wdocid;
$hashrecord->value = $fp->value;
$DB->insert_record("plagiarism_crot_fingerprint", $hashrecord);
}
//compare two fingerprints to get the number of same hashes
$sql_query = "select sum(case when cnt1 < cnt2 then cnt1 else cnt2 end) cnt
from
(
select count(*) as cnt1, (
select count(*)
from {$CFG->prefix}plagiarism_crot_fingerprint fp2
where fp2.crot_doc_id = $docid
and fp2.value = fp1.value
) as cnt2
from {$CFG->prefix}plagiarism_crot_fingerprint fp1
where fp1.crot_doc_id = $wdocid
group by fp1.value
) t";
try {
$similarnumber = $DB->get_record_sql($sql_query);
}
catch (Exception $e) {
print_error("exception in query\n");
continue;
}
// check that the number of same hashes is not null
if(!is_null($similarnumber->cnt) && $similarnumber->cnt!=0 ){
// add record to pair table
$pair_record->submission_a_id = $docid;
$pair_record->submission_b_id = $wdocid;
$pair_record->number_of_same_hashes = $similarnumber->cnt;
$ppair = $DB->insert_record("plagiarism_crot_spair", $pair_record);
} else {
//if null then remove the web document and its fingerprint
// remove from doc
$DB->delete_records("plagiarism_crot_documents", array("id"=>$wdocid));
// remove from fingerprints
$DB->delete_records("plagiarism_crot_fingerprint", array("crot_doc_id"=>$wdocid));
}
}
} // end global search
$afile->status = 'end_processing';
$DB->update_record('plagiarism_crot_files', $afile);
echo "\nfile $afile->id was sucessfully processed\n" ;
}
catch (Exception $e)
{
print_error("Error in processing file $afile->id!\n");
continue;
}
}// end of the main loop
}
else{
echo "No uploaded assignments to process!";
}
// set back normal values for php limits
ini_set('max_execution_time', $maxtimelimit);
ini_set('memory_limit', $maxmemoryamount);
// calc and display exec time
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "\nThe uploaded assignments were processed by crot in ".$totaltime." seconds\n";
// Online Text/TYPE-IN Processing
// @author Hamman Samuel (hwsamuel@ualberta.ca)
/*
$sql_query = "SELECT cf.* FROM {plagiarism_crot_files} cf where cf.status = 'onlinetext'";
$files = $DB->get_records_sql($sql_query);
if (!empty($files)){
foreach ($files as $afile){
try {
$afile->status = 'in_processing';
$DB->update_record('plagiarism_crot_files', $afile);
echo "\nfile $afile->id was not processed yet. start processing now ... \n" ;
$atime = microtime();
$atime = explode(" ",$atime);
$atime = $atime[1] + $atime[0];
$astarttime = $atime;
// call tokenizer to get plain text and store it in plagiarism_crot_documents
$atext = $afile->path; //tokenizer ($apath, $ext);
$atext = mb_substr($atext, 0, 999000);
$docrecord->crot_submission_id = $afile->id;
$docrecord->content = addslashes($atext);
$docid = $DB->insert_record('plagiarism_crot_documents', $docrecord);
// fingerprinting - calculate and store the fingerprints into the table
$atext=mb_strtolower($atext, "utf-8");
// get f/print
$fingerp = array();
$fingerp = GetFingerprint($atext);
// store fingerprint
foreach ($fingerp as $fp) {
$hashrecord->position = $fp->position;
$hashrecord->crot_doc_id = $docid;
$hashrecord->value = $fp->value;
$DB->insert_record("plagiarism_crot_fingerprint", $hashrecord);
}
// local search
echo "starting local search \n";
$plagiarismvalues = $DB->get_records_menu('plagiarism_crot_config', array('cm'=>$afile->cm),'','name,value');
if ($plagiarismvalues['crot_local']==1) {
// comparing fingerprints and updating plagiarism_crot_spair table
// select all submissions that has at least on common f/print with the current document
$sql_query ="SELECT id
FROM {$CFG->prefix}plagiarism_crot_documents asg
WHERE exists (
select * from
{$CFG->prefix}plagiarism_crot_fingerprint fp1
inner join {$CFG->prefix}plagiarism_crot_fingerprint fp2 on fp1.value = fp2.value
where fp2.crot_doc_id = asg.id and fp1.crot_doc_id = $docid
)";
$pair_submissions = $DB->get_records_sql($sql_query);
foreach ($pair_submissions as $pair_submission){
// check if id exists in web_doc table then don't compare because
// we consider only local documents here
if ($webdoc = $DB->get_record("plagiarism_crot_webdoc", array('document_id'=>$pair_submission->id)))
continue;
//compare two fingerprints to get the number of same hashes
if ($docid!=$pair_submission->id){
$sql_query = "select sum(case when cnt1 < cnt2 then cnt1 else cnt2 end) cnt
from
(
select count(*) as cnt1, (
select count(*)
from {$CFG->prefix}plagiarism_crot_fingerprint fp2
where fp2.crot_doc_id = $docid
and fp2.value = fp1.value
) as cnt2
from {$CFG->prefix}plagiarism_crot_fingerprint fp1
where fp1.crot_doc_id = $pair_submission->id
group by fp1.value
) t";
$similarnumber = $DB->get_record_sql($sql_query);
// takes id1 id2 and create/update record with the number of similar hashes
$sql_query ="SELECT * FROM {$CFG->prefix}plagiarism_crot_spair
where (submission_a_id = $afile->id and submission_b_id = $pair_submission->id)
OR (submission_a_id = $pair_submission->id and submission_b_id = $afile->id)";
$pair = $DB->get_record_sql($sql_query);
if (! $pair){
// insert
$pair_record->submission_a_id = $docid;
$pair_record->submission_b_id = $pair_submission->id;
$pair_record->number_of_same_hashes = $similarnumber->cnt;
$DB->insert_record("plagiarism_crot_spair", $pair_record);
} else {
// TODO update
}
} // end of comparing with local documents
}
} // end for local search
if ($plagiarismvalues['crot_global']==1) {
// global search
echo "\nfile $afile->id is selected for global search. Starting global search\n";
// strip text
$atext = StripText($atext," ");
// create search queries
$words = array();
$words = preg_split("/[\s]+/", trim(StripText($atext, " ")));
$max = sizeof($words) - $query_size +1;
$queries = array ();
for ($i=0; $i < $max; $i++) {
$query = "";
for ($j=$i; ($j-$i)<$query_size; $j++){
$query = $query." ".$words[$j];
}
$queries[] = $query;
} // queries are ready!
// create list of URLs
srand((float) microtime() * 10000000);
// randomly select x% of queries
$rand_keys = array_rand($queries, (sizeof($queries)/100)*$globs);
$narr = array();
foreach ($rand_keys as $mkey) {
$narr[]=$queries[$mkey];
}
$queries = $narr;
$tarr = getTopResults($queries,$todown,$msnkey,$culture_info);
$k=0;
// get top results
foreach($tarr as $manUrl) {
//get content of downloaded web document
// in php ini allow_url_fopen = On
$path = $manUrl->mainUrl;
// get content from the remote file
$mega = array ();
// get content and get encoding
if (trim($path)!="") {
try {
$result = getremotecontent( $path );
if (trim($result)==""){
continue;
}
}
catch (Exception $e) {
print_error("exception in downloading!\n");
$result = "Was not able to download the respective resource";
}
}
else {
continue;
}
$result = mb_ereg_replace('#\s{2,}#',' ',$result);
// split into strings and remove empty ones
$strs = explode ("\n", $result);
$result = "";
foreach ($strs as $st) {
$st = trim($st);
if ($st!="") {
$result = $result.mb_ereg_replace('/\s\s+/', ' ', $st)." \n";
}
}
// insert doc into crot_doc table
$wdocrecord->crot_submission_id = 0;
$wdocrecord->content = addslashes($result);
$wdocid = $DB->insert_record("plagiarism_crot_documents", $wdocrecord);
//$wdoc_content = addslashes($result);
//$query = "INSERT INTO {$CFG->prefix}plagiarism_crot_documents (content, crot_submission_id) VALUES ('$wdoc_content','0')";
// mysql_query($query);
//$wdocid = mysql_insert_id();
// insert doc into web_doc table
$webdocrecord->document_id = $wdocid;
$webdocrecord->link=urlencode($manUrl->mainUrl);
$webdocrecord->link_live=urlencode($manUrl->msUrl);
$webdocrecord->is_from_cache=false;
$webdocrecord->related_doc_id = $docid;
$webdocid = $DB->insert_record("plagiarism_crot_webdoc", $webdocrecord);
//$weblink = urlencode($manUrl->mainUrl);
//$weblinklive = urlencode($manUrl->msUrl);
//$query = "INSERT INTO {$CFG->prefix}plagiarism_crot_webdoc (document_id,link,link_live,is_from_cache,related_doc_id) VALUES ('$wdocid','$weblink','$weblinklive','false','$docid')";
//mysql_query($query);
//$webdocid = mysql_insert_id();
// fingerprinting - calculate and store the fingerprints into the table
$result=mb_convert_case($result, MB_CASE_LOWER, "UTF-8");
$fingerp = array();
try {
$fingerp = GetFingerprint($result);
}
catch (Exception $e)
{
print_error("exception in FP calc\n");
continue;
}
// store fingerprint
foreach ($fingerp as $fp)
{
$hashrecord->position = $fp->position;
$hashrecord->crot_doc_id = $wdocid;
$hashrecord->value = $fp->value;
$DB->insert_record("plagiarism_crot_fingerprint", $hashrecord);
}
//compare two fingerprints to get the number of same hashes
$sql_query = "select sum(case when cnt1 < cnt2 then cnt1 else cnt2 end) cnt
from
(
select count(*) as cnt1, (
select count(*)
from {$CFG->prefix}plagiarism_crot_fingerprint fp2
where fp2.crot_doc_id = $docid
and fp2.value = fp1.value
) as cnt2
from {$CFG->prefix}plagiarism_crot_fingerprint fp1
where fp1.crot_doc_id = $wdocid
group by fp1.value
) t";
try {
$similarnumber = $DB->get_record_sql($sql_query);
}
catch (Exception $e) {
print_error("exception in query\n");
continue;
}
// check that the number of same hashes is not null
if(!is_null($similarnumber->cnt) && $similarnumber->cnt!=0 ){
// add record to pair table
$pair_record->submission_a_id = $docid;
$pair_record->submission_b_id = $wdocid;
$pair_record->number_of_same_hashes = $similarnumber->cnt;
$ppair = $DB->insert_record("plagiarism_crot_spair", $pair_record);
} else {
//if null then remove the web document and its fingerprint
// remove from doc
$DB->delete_records("plagiarism_crot_documents", array("id"=>$wdocid));
// remove from fingerprints
$DB->delete_records("plagiarism_crot_fingerprint", array("crot_doc_id"=>$wdocid));
}
}
} // end global search
$afile->status = 'end_processing';
$DB->update_record('plagiarism_crot_files', $afile);
echo "\nfile $afile->id was sucessfully processed\n" ;
}
catch (Exception $e)
{
print_error("Error in processing file $afile->id!\n");
continue;
}
}// end of the main loop
}
else{
echo "No TYPE-IN assignments to process!";
}
// set back normal values for php limits
ini_set('max_execution_time', $maxtimelimit);
ini_set('memory_limit', $maxmemoryamount);
// calc and display exec time
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "\nThe TYPE-IN assignments were processed by crot in ".$totaltime." seconds\n";
*/
?>