root > WikiSense-trunk > web > Matrix.php

Matrix.php

application/x-php, 5595 bytes (load raw)
<?php

/*
RandomPage is a simple web interface to get a list of
random page names from a wiki.
Copyright (C) 2006, Daniel Kinzler, brightbyte.de

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/



if ( defined( 'WS_TOC' ) ) {
        $info['ignore'] = true;
        return;
}

die("broken!");

define("WS_WEB",true);
require_once( "common/WebInit.php" );

function renderMatrix( $data, $header=true, $legend= true ) {
    global $mincol, $maxcol, $neutralcol;

    $min= array();
    $max= array();
   
    foreach ($data as $row) {
        foreach ($row as $col => $v) {
            if (!is_int($v) && !is_float($v) && !is_numeric($v)) continue;
           
            if (!isset($min[$col]) || $min[$col]>$v) $min[$col]= $v;
            if (!isset($max[$col]) || $max[$col]<$v) $max[$col]= $v;
        }
    }
   
    print "\t<table class='matrix'>";
   
    #TODO: header!
   
    foreach ($data as $r => $row) {
        print "\t\t<tr>";
       
        foreach ($row as $col => $v) {
            $color= $neutralcol;
           
            if (isset($min[$col]) && isset($max[$col]) && $min[$col] != $max[$col] && (is_int($v) || is_float($v) || is_numeric($v))) {
                $f= ( $v - $min[$col] ) / ( $max[$col] - $min[$col] );
               
                $color= array(
                    (int)($mincol[0] + ( $maxcol[0] - $mincol[0] ) * $f),
                    (int)($mincol[1] + ( $maxcol[1] - $mincol[1] ) * $f),
                    (int)($mincol[2] + ( $maxcol[2] - $mincol[2] ) * $f),
                );
            }
           
            $color= sprintf('#%02X%02X%02X', $color[0], $color[1], $color[2]);
           
            $tag= "td";
            if ($header && $r === 0) $tag= "th";
            if ($legend && $col === 0) $tag= "th";
           
            print "<$tag style='background-color:$color;'>".escapeHtml($v)."</$tag>";
        }
       
        print "</tr>\n";
    }
    print "</table>\n";
}

function loadData($file) {
    $rows= file($file);
   
    if (!$rows) return $rows;
   
    foreach ($rows as $i => $line) {
        $rows[$i]= explode("\t", $line);
    }
   
    return $rows;
}

function findDataFile($name) {
    global $wsgDataDir;
   
    $name= urlencode($name);
    $f= "$wsgDataDir/$name-*.tsv";
   
    $ff= glob($f);
    if (!$ff) return false;
   
    $f= $ff[sizeof($ff)-1];
   
    return $f;
}

function findGraphFile($name) {
    global $wsgDataDir;
   
    $name= urlencode($name);
    $f= "$wsgDataDir/$name-*.png";
   
    $ff= glob($f);
    if (!$ff) return false;
   
    $f= $ff[sizeof($ff)-1];
   
    return $f;
}

function printPageTop($title) {
    header("Content-Type: text/html; charset=utf-8");
   
    print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
   
    <head>
        <title><?=escapeHtml($title)?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
       
        <style type='text/css'>
            table.matrix { background-color: #F0F0F0; }
            table.matrix th { text-align:left; }
            table.matrix td { text-align:right; }
        </style>
    </head>
   
    <body>
        <div style='margin-left:auto; margin-right:auto; width:720px;'>
        <? wsfHeader() ?>
        <h1><?=escapeHtml($title)?></h1>
    <?
}

function printPageBottom() {
    wsfFooter();
   
    ?>
      </div>
    </body>
    </html>
    <?
}

if (!isset($wsgDataDir)) $wsgDataDir= '/tmptmp';

$mincol= array( 255, 255, 255 );
$maxcol= array( 255, 255, 0 );
$neutralcol= array( 255, 255, 255 );

if (@$_REQUEST['data']) {
    $f= findDataFile($_REQUEST['data']);
    if (!$f) httpError(404,"Not Found","can't find data file \"{$_REQUEST['image']}\"");
    if (!is_readable($f)) httpError(403,"Forbidden","can't access data file \"{$_REQUEST['image']}\"");
   
    $d= loadData($f);
    if (!$d) httpError(500,"Internal Server Error","failed to load data file \"{$_REQUEST['image']}\"");
   
    printPageTop($_REQUEST['data']);
    renderMatrix($d);
    printPageBottom();
}
else if (@$_REQUEST['graph']) {
    $g= $_REQUEST['graph'];
   
    $u= $_SERVER['PHP_SELF'].'?image='.urlencode($g);
   
    printPageTop($_REQUEST['graph']);
    print "<img src='$u' alt='".escapeHtml($g,ENT_QUOTES)."'/>";
    printPageBottom();
}
else if (@$_REQUEST['image']) {
    $f= findGraphFile($_REQUEST['image']);
    if (!$f) httpError(404,"Not Found","can't find image file \"{$_REQUEST['image']}\"");
    if (!is_readable($f)) httpError(403,"Forbidden","can't access image file \"{$_REQUEST['image']}\"");
   
    header("Content-Type: image/png"); #TODO: other formats?...
   
    $r= readfile($f);
    if (!$r) httpError(500,"Internal Server Error","failed to read image file \"{$_REQUEST['image']}\"");
}
else {
    if (!$r) httpError(400,"Bad Request","missing arguments");
}
?>