<?php
/*
--------------------------------------------------------------------------------
PhpDig 1.6.x
This program is provided under the GNU/GPL license.
See LICENSE file for more informations
All contributors are listed in the CREDITS file provided with this package

PhpDig Website : http://phpdig.toiletoine.net/
Contact email : phpdig@toiletoine.net
Author and main maintainer : Antoine Bajolet (fr) bajolet@toiletoine.net
--------------------------------------------------------------------------------
*/
$relative_script_path = '../../../html/cercar/';
include "$relative_script_path/includes/config.php";
include "$relative_script_path/libs/auth.php";

// extract http vars
extract(phpdigHttpVars(array('type' => 'string')));

set_time_limit(300);
?>
<?php include $relative_script_path.'/libs/htmlheader.php' ?>
<head>
<title>PhpDig : <?php phpdigPrnMsg('statistics') ?> </title>
<?php include $relative_script_path.'/libs/htmlmetas.php' ?>
</head>
<body bgcolor="white">
<table><tr><td valign="top">
<img src="../phpdig_logo_2.png" width="200" height="114" alt="PhpDig <?php print PHPDIG_VERSION ?>" /><br />
<h1><?php phpdigPrnMsg('statistics') ?></h1>
<p class='grey'>
<a href="statistics.php?type=mostkeys"><?php phpdigPrnMsg('mostkeywords') ?></a>
<br /><a href="statistics.php?type=mostpages"><?php phpdigPrnMsg('richestpages') ?></a>
<br /><a href="statistics.php?type=mostterms"><?php phpdigPrnMsg('mostterms') ?></a>
<br /><a href="statistics.php?type=largestresults"><?php phpdigPrnMsg('largestresults') ?></a>
<br /><a href="statistics.php?type=mostempty"><?php phpdigPrnMsg('mostempty') ?></a>
<br /><a href="statistics.php?type=lastqueries"><?php phpdigPrnMsg('lastqueries') ?></a>
</p>
<a href="index.php" target="_top">[<?php phpdigPrnMsg('back') ?>]</a> <?php phpdigPrnMsg('to_admin') ?>.
</td><td valign="top">
<?php
if ($type)
    {
    $query = "SET OPTION SQL_BIG_SELECTS=1";
    mysql_query($query,$id_connect);

    $start_table_template = "<table class=\"borderCollapse\">\n";
    $end_table_template = "</table>\n";
    $line_template = "<tr>%s</tr>\n";
    $title_cell_template = "\t<td class=\"blueForm\">%s</td>\n";
    $cell_template[0] = "\t<td class=\"greyFormDark\">%s</td>\n";
    $cell_template[1] = "\t<td class=\"greyForm\">%s</td>\n";
    $cell_template[2] = "\t<td class=\"greyFormLight\">%s</td>\n";
    $cell_template[3] = "\t<td class=\"greyForm\">%s</td>\n";

    $mod_template = count($cell_template);
    flush();

    switch ($type)
            {
            case 'mostkeys':
                  $query = 'SELECT k.keyword ,sum(e.weight) as num
                  FROM '.PHPDIG_DB_PREFIX.'keywords k, '.PHPDIG_DB_PREFIX.'engine e
                  WHERE k.key_id = e.key_id
                  GROUP BY k.keyword
                  ORDER BY num DESC LIMIT 30';
                  $result = phpdigMySelect($id_connect,$query);
            break;

            case 'mostpages':
                  $query = 'SELECT CONCAT(st.site_url,s.path,s.file) as page,s.num_words
                  FROM '.PHPDIG_DB_PREFIX.'spider s, '.PHPDIG_DB_PREFIX.'sites st
                  WHERE s.site_id = st.site_id
                  ORDER BY num_words DESC LIMIT 30';
                  $result = phpdigMySelect($id_connect,$query);
            break;

            case 'mostterms':
                  $query = 'SELECT l_includes as search_terms,
                  count(l_id) as num_time,
                  sum(l_num) as total_results,
                  round(avg(l_time),2) as avg_time
                  FROM '.PHPDIG_DB_PREFIX.'logs
                  GROUP BY search_terms
                  ORDER BY num_time DESC LIMIT 30';
                  $result = phpdigMySelect($id_connect,$query);
            break;

            case 'largestresults':
                  $query = 'SELECT count(l_id) as queries,
                  l_includes as with_terms,
                  l_excludes as and_without_terms,
                  round(avg(l_num)) as average_results,
                  round(avg(l_time),2) as avg_time
                  FROM '.PHPDIG_DB_PREFIX.'logs
                  GROUP BY with_terms, and_without_terms
                  ORDER BY average_results DESC LIMIT 30';
                  $result = phpdigMySelect($id_connect,$query);
            break;

            case 'mostempty':
                  $query = 'SELECT count(l_id) as queries,
                  l_includes as with_terms,
                  l_excludes as and_without_terms
                  FROM '.PHPDIG_DB_PREFIX.'logs
                  WHERE l_num = 0
                  GROUP BY with_terms, and_without_terms
                  ORDER BY queries DESC LIMIT 30';
                  $result = phpdigMySelect($id_connect,$query);
            break;

            case 'lastqueries':
                 $query = 'SELECT DATE_FORMAT(l_ts,\'%Y-%m-%d %H:%i%:%S\') as date,
                  l_includes as with_terms,
                  l_excludes as and_without_terms,
                  l_num as results,
                  l_mode as "start/any/exact",
                  l_time as search_time
                  FROM '.PHPDIG_DB_PREFIX.'logs
                  ORDER BY l_ts DESC LIMIT 30';
                  $result = phpdigMySelect($id_connect,$query);
            break;
            }

    if (is_array($result)) {
        print $start_table_template;
        // title line
        $title_line = '';
        list($i,$titles) = each($result);
        foreach($titles as $field => $useless) {
            $title_line .= sprintf($title_cell_template,ucwords(str_replace('_',' ',$field)));
        }
        printf($line_template,$title_line);
        foreach($result as $id => $row) {
           $this_line = '';
           $id_row_style = $id % $mod_template;
           foreach ($row as $value) {
                $this_line .= sprintf($cell_template[$id_row_style],$value);
           }
           printf($line_template,$this_line);
        }
        print $end_table_template;
    }
    }
?>
</td></tr></table>
</body>
</html>