I like the idea using something standalone rather than use some plugin which
could make an upgrade of dspace difficult.

So far I'm using cronolog for apache to make an access_log pr. month. I then
pass this logfile in a php script.

I fetch out those metavalues we need for stats and make another logfile or
insert in another database. I plan on caching the result, so we don't have
to hit dspace database for each request.

I post the php script here, hoping you can use it or be inspired somehow :

#!/usr/bin/php
<?

# 200901 Klaus Hessellund @ CBS DK



# parses apache accesslog filen and get timestamp, handle, papertype,
institute and author.



# xxx.xxx.xxx.xxx - - [13/Jan/2009:05:42:02 +0100] "GET
/xmlui/bitstream/handle/123456789/19044/qfswhe%26mbox%3dinbox%26charset%3descaped_unicode%26uid%3d57%26number%3d3%26xxx.pdf?sequence=1
HTTP/1.1" 200 9 "http://130.226.36.214/xmlui/handle/123456789/19044";
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5)
Gecko/2008120122 Firefox/3.0.5"

        dbconnect();

        # get ids for metafields
  $sql = "select metadata_field_id from metadatafieldregistry where
element='type' and qualifier is null";
  $papertype_id = getonevalue ($sql);

  $sql = "select metadata_field_id from metadatafieldregistry where
element='contributor' and qualifier = 'departmentshort'";
  $institute_id = getonevalue ($sql);

  $sql = "select metadata_field_id from metadatafieldregistry where
element='contributor' and qualifier = 'author'";
  $author_id = getonevalue ($sql);


        # open stdin
        $fp = fopen ("php://stdin","r");

        while ($line = fgets($fp)) {
                # we only want to know about downloads            
                if (!preg_match ("/\[(.*?):.*GET
\/.*bitstream\/handle\/(.*?\/.*?)\//",$line,$found)) { continue; };

                if ($found) {
                        #print_r ($found);

                        $epoch = strtotime(preg_replace("/\//","
",$found[1]));

                        $handle = $found[2];
                        $papertype = get_metavalue_from_id
($handle,$papertype_id);
                        $institute = get_metavalue_from_id
($handle,$institute_id);
                        $author = get_metavalue_from_id
($handle,$author_id);

                        $sql = "insert into handle_log
('date','handle_id','papertype','institute','author') 
                                                values
(FROM_UNIXTIME('$epoch'),'$handle','$papertype','$institute','$author')";
                        echo
"$epoch:$handle:$papertype:$institute:$author\n";
                        #echo $sql . "\n";;

                }

        }




#  some helpful functions

function get_metavalue_from_id($handle,$id) {

        global $type_id;

        $sql = "select text_value from handle h
                inner join item i on (h.resource_id = i.item_id) 
                                        inner join metadatavalue m on
(i.item_id = m.item_id)
                                        where m.metadata_field_id = '$id'
                                        and h.handle = '$handle'";

        #echo $sql . "\n";
        $value = getonevalue($sql);
        return ($value);


}
function getonevalue($sql) {

  $sth = query ($sql);
  $r = pg_fetch_row($sth);
  return ($r[0]);
}

function dbconnect () {

  global $db;

  $db = pg_connect ("host=localhost dbname=xxxxxxx user=xxxxxx
password=xxxxxxx");
  if (!$db) {
    error_log ("DB connect failed: " . pg_last_error());
    exit;
  }

}


function query ($sql) {
  global $db;

  $sth = pg_query ($sql);
  if (!$sth) {
    error_log (pg_last_error());
    exit;
  }
  return ($sth);

}


?>
-- 
View this message in context: 
http://www.nabble.com/Reading-dspace-DB-from-other-applications-tp21782514p21807275.html
Sent from the DSpace - Tech mailing list archive at Nabble.com.


------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
DSpace-tech mailing list
DSpace-tech@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dspace-tech

Reply via email to