17 require_once SQ_CORE_PACKAGE_PATH.
'/log_report/log_report_edit_fns.inc';
18 require_once SQ_FUDGE_PATH.
'/general/file_system.inc';
19 require_once SQ_LIB_PATH.
'/html_form/html_form.inc';
48 $job_vars =& $job->_running_vars;
51 $meta[
'report'] =& $job_vars[
'report'];
54 if (isset($job_vars[
'iterator'])) {
55 $iterator =& $job_vars[
'iterator'];
57 $iterator =& $GLOBALS[
'SQ_SYSTEM']->log_manager->getLogIterator($meta[
'report']->logname, 1);
61 if (!isset($job_vars[
'mode'])) {
62 $job_vars[
'mode'] =
'terms';
67 if ($job_vars[
'mode'] ==
'terms') {
71 if (!empty($job_vars[
'carryover'])) {
72 $line = $job_vars[
'carryover'];
73 unset($job_vars[
'carryover']);
75 $line = $iterator->getNextEntry();
79 $data = array_get_index($line,
'data', Array());
82 $meta[
'userid'] = $line[
'userid'];
83 $meta[
'user'] = $line[
'user'];
84 $meta[
'date'] = $line[
'date'];
85 $meta[
'level'] = $line[
'level'];
86 $meta[
'search_assetid'] = array_get_index($data,
'assetid', 0);
89 if (!isset($job_vars[
'previous_date'])) {
95 if (!is_null(array_get_index($job_vars,
'previous_date', NULL))) {
96 switch ($job_vars[
'report']->attr(
'rollover_period')) {
99 if (date(
'Ymd', $meta[
'date']) > date(
'Ymd', $job_vars[
'previous_date'])) {
106 if (date(
'Ym', $meta[
'date']) > date(
'Ym', $job_vars[
'previous_date'])) {
114 $next_week_start_time = $job_vars[
'previous_date'] + ((7 - date(
'w', $job_vars[
'previous_date'])) * 86400);
115 if (date(
'Ymd', $meta[
'date']) >= date(
'Ymd', $next_week_start_time)) {
123 $job_vars[
'carryover'] = $line;
124 $job_vars[
'mode'] =
'indices';
129 $job_vars[
'previous_date'] = $line[
'date'];
133 $terms = array_get_index($data,
'terms', Array());
134 foreach ($terms as $term => $result) {
135 while ($term != strip_tags($term)) {
136 $term = strip_tags($term);
144 $progress =& $iterator->getCurrentProgress();
145 $total = array_get_index($progress,
'total', 0);
146 $current = array_get_index($progress,
'current', $total);
148 $step_data[
'percent_done'] = 50;
150 $step_data[
'percent_done'] = (int) ($current * 100 / $total) / 2;
154 if ($current >= $total) $job_vars[
'mode'] =
'indices';
161 if ($job_vars[
'mode'] ==
'indices') {
163 $term_dir_path = $meta[
'report']->data_path.
'/terms';
166 if (!isset($job_vars[
'term_index'])) {
168 $ids = glob($term_dir_path.
'/*');
169 foreach ($ids as $id) {
170 $id = str_replace($term_dir_path.
'/',
'', $id);
172 $terms = glob($term_dir_path.
'/'.$id.
'/*/*/*');
173 foreach ($terms as $term) {
174 $term = basename($term,
'.term');
176 $job_vars[
'term_index'][$term][] = $id;
182 if (!isset($job_vars[
'term_index_total'])) {
183 $job_vars[
'term_index_total'] = count($job_vars[
'term_index']);
186 if (!isset($job_vars[
'term_index_count'])) {
187 $job_vars[
'term_index_count'] = 0;
189 if ($job_vars[
'term_index_count'] >= $job_vars[
'term_index_total']) {
190 $job_vars[
'term_index_count'] = $job_vars[
'term_index_total'];
192 $job_vars[
'term_index_count']++;
197 if (!empty($job_vars[
'term_index'])) {
199 $term = end(array_keys($job_vars[
'term_index']));
200 $ids = array_pop($job_vars[
'term_index']);
202 foreach ($ids as $id) {
203 $data = $this->
_readTerm($term, $term_dir_path.
'/'.$id.
'/'.$this->_getTermHash($term));
205 foreach ($data as $date => $results) {
207 if (!isset($job_vars[
'indices'][$id][$date][
'index_popular'])) {
208 $job_vars[
'indices'][$id][$date][
'index_popular'] = Array();
210 if (!isset($job_vars[
'indices'][$id][$date][
'index_failed'])) {
211 $job_vars[
'indices'][$id][$date][
'index_failed'] = Array();
213 $row_count = $meta[
'report']->attr(
'row_count');
216 $total_value = array_get_index($results,
'total_searches', 0);
217 $zero_value = array_get_index($results,
'zero_results', 0);
219 $this->
_updateIndex($job_vars[
'indices'][$id][$date][
'index_failed'], $term, $zero_value, $row_count);
221 $this->
_updateIndex($job_vars[
'indices'][$id][$date][
'index_popular'], $term, $total_value, $row_count);
227 unset($job_vars[
'term_index'][$term]);
230 unset($job_vars[
'term_index']);
231 $job_vars[
'mode'] =
'generate';
235 $step_data[
'percent_done'] = (($job_vars[
'term_index_count'] * 100 / $job_vars[
'term_index_total']) / 2) + 50;
241 if ($job_vars[
'mode'] ==
'generate') {
245 $progress =& $iterator->getCurrentProgress();
246 $total = array_get_index($progress,
'total', 0);
247 $current = array_get_index($progress,
'current', $total);
249 if ($current >= $total) {
251 $step_data[
'percent_done'] = 100;
252 $step_data[
'complete'] = TRUE;
256 $job_vars[
'mode'] =
'rollover';
262 if ($job_vars[
'mode'] ==
'rollover') {
264 $job_vars[
'indices'] = Array();
266 $progress =& $iterator->getCurrentProgress();
267 $total = array_get_index($progress,
'total', 0);
268 $current = array_get_index($progress,
'current', $total);
269 if ($current >= $total && !isset($job_vars[
'carryover'])) {
270 $step_data[
'percent_done'] = 100;
271 $step_data[
'complete'] = TRUE;
274 $job_vars[
'mode'] =
'terms';
279 $job_vars[
'iterator'] =& $iterator;
280 $job_vars[
'complete'] = $step_data[
'complete'];
312 if (empty($term) || empty($meta)) {
316 $search_assetid = array_get_index($meta,
'search_assetid',
'0');
317 $meta_date = date(
'Y-m-d H', $meta[
'date']);
327 date(
'Y-m-d H', $meta[
'date']),
331 foreach ($assetids as $id) {
332 $term_dir_path = $meta[
'report']->data_path.
'/terms'.(
'/'.$id).$this->
_getTermHash($term);
335 $data_collection[$id] = $this->
_readTerm($term, $term_dir_path);
336 foreach ($dates as $date) {
338 if (!isset($data_collection[$id][$date])) {
339 $data_collection[$id][$date] = Array();
344 $this->
_writeTerm($term, $term_dir_path, $data_collection[$id]);
364 if (empty($term))
return FALSE;
367 $total_searches = array_get_index($data,
'total_searches', 0) + 1;
370 $zero_results = array_get_index($data,
'zero_results', 0) + 1;
372 $zero_results = array_get_index($data,
'zero_results', 0);
375 if ($total_searches > 0) {
376 $average_result = ((array_get_index($data,
'average_result', 0) * ($total_searches - 1)) + $result) / $total_searches;
378 $average_result = $result;
381 if (is_null(array_get_index($data,
'max_result', NULL))) {
382 $max_result = $result;
384 $max_result = ($result > $data[
'max_result'] ? $result : $data[
'max_result']);
387 if (is_null(array_get_index($data,
'min_result', NULL))) {
388 $min_result = $result;
390 $min_result = ($result < $data[
'min_result'] ? $result : $data[
'min_result']);
394 'total_searches' => $total_searches,
395 'zero_results' => $zero_results,
396 'average_result' => round($average_result, 2),
397 'max_result' => $max_result,
398 'min_result' => $min_result,
417 if (empty($term) || empty($term_dir_path)) {
421 $term_path = $term_dir_path.
'/'.$term.
'.term';
425 if (file_exists($term_path)) {
426 $fp = fopen($term_path,
'r');
429 $data = unserialize(fgets($fp));
452 if (empty($term) || empty($term_dir_path) || empty($data)) {
457 if (!is_dir($term_dir_path) && !create_directory($term_dir_path)) {
462 $term = str_replace(
'/' ,
'_' , $term);
463 $term = urlencode($term);
464 $term = substr_replace($term,
'', 250, strlen($term));
466 $term_path = $term_dir_path.
'/'.$term.
'.term';
469 $fp = fopen($term_path,
'w');
471 fwrite($fp, serialize($data));
495 if (empty($index) || $value <= 0 || $limit < 1) {
500 if (isset($array[$index]) || count($array) < $limit) {
501 $array[$index] = $value;
505 $threshold = end($array);
509 if ($value > $threshold) {
511 $array[$index] = $value;
533 if (empty($meta))
return FALSE;
535 $logname = $meta[
'report']->logname;
536 $data_path = $meta[
'report']->data_path;
537 $term_dir_path = $data_path.
'/terms';
538 $generated_dir_path = $data_path.
'/generated';
540 if (empty($data_path))
return FALSE;
544 if (empty($assetids)) {
552 foreach ($assetids as $id) {
554 if (!isset($indices[$id][
'total'][
'index_popular'])) {
555 $indices[$id][
'total'][
'index_popular'] = Array();
557 if (!isset($indices[$id][
'total'][
'index_failed'])) {
558 $indices[$id][
'total'][
'index_failed'] = Array();
561 $index_popular = $indices[$id][
'total'][
'index_popular'];
562 $index_failed = $indices[$id][
'total'][
'index_failed'];
565 $output_index_popular =
'';
566 foreach ($index_popular as $term => $result) {
567 $data_collection = $this->
_readTerm($term, $term_dir_path.(
'/'.$id).$this->_getTermHash($term));
568 if (isset($data_collection[
'total'])) {
569 $data = $data_collection[
'total'];
570 $output_index_popular .=
'
572 <td class="sq-backend-table-cell">'.urldecode($term).
'</td>
573 <td class="sq-backend-table-cell" style="width: 25px;">'.array_get_index($data,
'total_searches', 0).
'</td>
574 <td class="sq-backend-table-cell" style="width: 25px;">'.array_get_index($data,
'average_result', 0).
'</td>
575 <td class="sq-backend-table-cell" style="width: 25px;">'.array_get_index($data,
'min_result', 0).
'</td>
576 <td class="sq-backend-table-cell" style="width: 25px;">'.array_get_index($data,
'max_result', 0).
'</td>
583 $output_index_failed =
'';
584 foreach ($index_failed as $term => $result) {
585 $output_index_failed .=
'
587 <td class="sq-backend-table-cell">'.urldecode($term).
'</td>
588 <td class="sq-backend-table-cell" style="width: 25px;">'.$result.
'</td>
595 if ($id ==
'total') {
596 $output_heading =
'<b>'.translate(
'sch_log_report_all_search_pages').
'</b>';
598 if ($GLOBALS[
'SQ_SYSTEM']->am->assetExists($id)) {
599 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAssetInfo(Array($id), Array(), FALSE,
'name');
600 $asset = $asset[$id];
603 if (!$asset) $asset =
'Asset';
605 $output_heading =
'<b>'.$asset.
' (Id: #'.$id.
')</b>';
607 $output[$id] = $output_heading.
'
609 <table class="sq-backend-table">
611 <td class="sq-backend-table-header" style="width: 200px">'.translate(
'sch_log_report_popular_search_terms').
'</td>
612 <td class="sq-backend-table-header">'.translate(
'sch_log_report_total_searches').
'</td>
613 <td class="sq-backend-table-header">'.translate(
'sch_log_report_average_results').
'</td>
614 <td class="sq-backend-table-header">'.translate(
'lowest').
'</td>
615 <td class="sq-backend-table-header">'.translate(
'highest').
'</td>
617 '.(!empty($output_index_popular) ? $output_index_popular :
'<td class="sq-backend-table-cell" colspan="5">'.translate(
'sch_log_report_no_successful_searches').
'</td>').
'
620 <table class="sq-backend-table">
622 <td class="sq-backend-table-header" style="width: 200px">'.translate(
'sch_log_report_failed_search_terms').
'</td>
623 <td class="sq-backend-table-header">'.translate(
'sch_log_report_total_failed_searches').
'</td>
625 '.(!empty($output_index_failed) ? $output_index_failed :
'<td class="sq-backend-table-cell" colspan="2">'.translate(
'sch_log_report_no_failed_searches').
'</td>').
'
631 if (!file_exists($generated_dir_path)) {
632 if (!create_directory($generated_dir_path)) {
644 unset($output[
'total']);
645 $report_output .= implode(
"\n", $output);
647 $result = string_to_file($report_output, $generated_dir_path.
'/'.$logname.
'.html');
649 $meta[
'report']->setAttrValue(
'generated_date', date(
'F j, Y, g:i a'));
650 $meta[
'report']->setAttrValue(
'generated_user', $GLOBALS[
'SQ_SYSTEM']->user->name.
' (#'.$GLOBALS[
'SQ_SYSTEM']->user->id.
')');
652 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
653 $meta[
'report']->saveAttributes();
654 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
672 if (empty($report) || empty($date)) {
676 $formatted_date = date(
'Y-m-d', $date);
677 $logname = $report->logname;
678 $data_path = $report->data_path;
679 $term_dir_path = $data_path.
'/terms';
680 $generated_dir_path = $data_path.
'/generated';
683 if (!delete_directory($term_dir_path)) {
684 trigger_localised_error(
'SCH0025', E_USER_WARNING);
689 $from = $generated_dir_path.
'/'.$logname.
'.html';
690 $to = $generated_dir_path.
'/'.$logname.
'.'.$formatted_date.
'.html';
691 if (!rename($from, $to)) {
692 trigger_localised_error(
'SCH0024', E_USER_WARNING);
697 $generated_reports = $report->attr(
'generated_reports');
698 $generated_reports[$logname.
'.'.$formatted_date] = $formatted_date.
' ('.ucwords($report->attr(
'rollover_period')).
')';
699 arsort($generated_reports);
701 $report->setAttrValue(
'generated_reports', $generated_reports);
702 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
703 $report->saveAttributes();
704 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
721 if (empty($term_dir_path))
return FALSE;
724 if (is_dir($term_dir_path)) {
725 if ($dir_handle = opendir($term_dir_path)) {
726 while (($file = readdir($dir_handle)) !== FALSE) {
727 if ($file !=
'.' && $file !=
'..' && is_dir($term_dir_path.
'/'.$file)) {
749 if (trim($term) ==
'')
return FALSE;
750 $term = urldecode($term);
755 for ($ii = 0; $ii < $depth; $ii++) {
757 if (strlen($term) > $ii) {
758 $hash_num[] .= ord($term[$ii]);
764 $hash = implode(
'/', $hash_num);
783 $generated_reports = $asset->attr(
'generated_reports');
786 $generated_reports[$asset->logname] =
'Current';
787 arsort($generated_reports);
789 if (isset($_REQUEST[$prefix][
'generated_reports'])) {
790 if (isset($generated_reports[$_REQUEST[$prefix][
'generated_reports']])) {
791 $report_path = $asset->data_path.
'/generated/'.$_REQUEST[$prefix][
'generated_reports'].
'.html';
792 $selected = $_REQUEST[$prefix][
'generated_reports'];
794 $report_path = $asset->data_path.
'/generated/'.$asset->logname.
'.html';
795 $selected = $asset->logname;
798 $report_path = $asset->data_path.
'/generated/'.$asset->logname.
'.html';
799 $selected = $asset->logname;
802 if (!is_file($report_path)) {
803 echo translate(
'sch_log_report_generate_at_next_rotation');
807 echo
'<h3>'.translate(
'sch_log_report_search_report').
'</h3>';
810 combo_box($prefix.
'[generated_reports]', $generated_reports, FALSE, $selected);
812 submit_button($prefix.
'[select_report]', translate(
'select_report'));
815 include_once($report_path);
832 $generated_user = $asset->attr(
'generated_user');
833 if (empty($generated_user)) {
834 echo translate(
'report_not_generated').
'<br />';
837 echo
' <b>'.translate(
'report_generated').
'</b><br />';
838 echo
' '.translate(
'date').
': '.$asset->attr(
'generated_date');
840 echo
' '.translate(
'user').
': '.$asset->attr(
'generated_user');
859 $generated_reports = $report->attr(
'generated_reports');
860 $latest_report = current(array_keys($generated_reports));
863 if ($latest_report) {
864 $last_date = strtotime(substr($latest_report, 7));