package LinuxFocus::DB;

use strict;
use vars qw ( $VERSION @ISA @EXPORT_OK );
use XML::DOM;

BEGIN {
  require XML::DOM;

  $VERSION = '0.7';      # 2000-09-07

  @ISA         = qw( Exporter );
  @EXPORT_OK   = qw( &load );

  return 1;
}

sub new {
  my ($class, %args) = @_;

  $args{DOMparser_args} ||= {'KeepCDATA' => 1}; # Default #
  $args{debug} ||= 1;         # False if undef #
  $args{indent} ||= 0;         # Default to 0   #
  $args{indent_incr} ||= 1;    # Default to 1   #

  my $parser = bless { DOMparser => XML::DOM::Parser->new (),
                       DOMparser_args => $args{DOMparser_args},
	               variables => \%args,
		       debug => $args{debug},
                       doc => undef,
                       article => undef,
                       lang => "en",
                       articleid => undef
		     }, $class;
  return $parser;
}

sub load {
    my ($class, $file, @dummy) = @_;  

    my $parser = new XML::DOM::Parser;
    die "$file not found!\n" if (!(-e $file));
    $class->{doc} = $parser->parsefile ($file);
}

sub getDBLang {
    my ($class, @dummy) = @_;
    die "Database not loaded!\n" if (!defined $class->{doc});
    
    my @root = $class->{doc}->getElementsByTagName("database");
    my $result = ($root[0])->getAttribute("xml:lang");
    $result = ($root[0])->getAttribute("name") if ($result eq "");
    if (!defined $result) {
        $result = $class->{lang};
    }
    return $result;
}

sub setLang {
    my $class = shift @_;
    $class->{lang} = shift @_;
} 

sub toString {
    my ($class, @dummy) = @_;
    return $class->{doc}->toString();
}

sub getCurrentMonthID {
    my ($class, %args) = @_;

    my $result = "May2000";
    my $resultcode = "200005";
    foreach my $kid ($class->{doc}->getElementsByTagName("issue")) { 
        my $id = $kid->getAttribute("id");
        my $code = $kid->getAttribute("code");
        #print STDERR "DEBUG: $id, $code ($resultcode)$/" if ($id ne "");
        #print STDERR " is greater!!!$/" if ($code gt $resultcode);
        #print STDERR " is published!!!$/" if (issueIsPublished($class, $id));
        if (($id ne "") && ($code ne "") && ($code > $resultcode) &&
            issueIsPublished($class, $id)) {
            $resultcode = $code;
            $result = $kid->getAttribute("id");
	}
    }
    return ($result, $resultcode);
}

sub getMonthIDs {
    my ($class, %args) = @_;
    my ($sort, $limit);
    if (%args) {
      $sort = $args{"sort"};
      $limit = $args{"limit"};
    }
    my $raw       = $args{"raw"} || "no";

    my %temp = ();
    my @result = ();
    foreach my $kid ($class->{doc}->getElementsByTagName("issue")) { 
        if ($kid->getAttribute("id") ne "" &&
            $kid->getAttribute("code") ne "") {
            $temp{$kid->getAttribute("code")} = $kid->getAttribute("id");
        }
    }

    my @keys = keys %temp;
    if (defined $sort) {
        @keys = sort @keys;
        if ($sort eq "asc") {
        } else {
            # assume descending order
            @keys = reverse @keys;
        }
    } else {
        # assume random order is oke
    }
    my $counter = 0;
    foreach my $key (@keys) {
        $counter++;
        if ((!defined $limit) || $counter <= $limit) {
            if ($raw eq "yes") {
                push(@result, $key . "{" . $temp{$key});
            } else {
                push(@result, $temp{$key});
	    }
        }
    }
    return @result;
}

sub getPersonIDs {
    my ($class, %args) = @_;
    my ($team, $sort, $limit);
    if (%args) {
	$team = $args{"team"};
        $sort = $args{"sort"};
        $limit = $args{"limit"};
    }
    my $raw       = $args{"raw"} || "no";

    my %temp = ();
    my @result = ();
    foreach my $kid ($class->{doc}->getElementsByTagName("person")) { 
        if ($kid->getAttribute("id") ne "") {
            my @teams = $kid->getElementsByTagName("team");
   	    if (defined $teams[0]) {
                if ($teams[0]->getAttribute("xml:lang") eq $team) {
                    $temp{getPersonName($class, $kid->getAttribute("id"))} = 
                        $kid->getAttribute("id");
                }
            } 
        }
    }

    my @keys = keys %temp;
    if (defined $sort) {
        @keys = sort @keys;
        if ($sort eq "asc") {
        } else {
            # assume descending order
            @keys = reverse @keys;
        }
    } else {
        # assume random order is oke
    }
    my $counter = 0;
    foreach my $key (@keys) {
        $counter++;
        if ((!defined $limit) || $counter <= $limit) {
            if ($raw eq "yes") {
                push(@result, $key . "{" . $temp{$key});
            } else {
                push(@result, $temp{$key});
	    }
        }
    }
    return @result;
}

sub getArticleIDs {
    my ($class, %args) = @_;
    my ($month, $translated, $to, $from); 
    my $limit;
    if (%args) {
      # Use month => "" to get IDs of article in a certain month
      $month = $args{"month"};

      # Use translated => "yes" with either/both to => "" or/and
      #   from => "" to select tranlated articles
      $translated = $args{"translated"};
      $to = $args{"to"};
      $from = $args{"from"};
      
      # general variables
      $limit = $args{"limit"};
    }
    my $sort      = $args{"sort"} || "no";
    my $published = $args{"published"} || "no";
    my $cover     = $args{"coverpages"} || "no";
    my $raw       = $args{"raw"} || "no";

    my %temp = ();
    if (defined $month) {
        foreach my $kid ($class->{doc}->getElementsByTagName("article")) {
            my $id = $kid->getAttribute("id");
            selectArticle($class, $id);
            if (getArticleMonthID($class) eq $month) {
                $temp{$id} = $id;
	    }
        }
    } elsif (defined $translated && (defined $to || defined $from)) {
        foreach my $kid ($class->{doc}->getElementsByTagName("article")) { 
            my $id = $kid->getAttribute("id");
            selectArticle($class, $id);
            my $date = "";
            if (defined $to || !defined $from) {
                $date = getArticleTranslationDate($class, to => "$to") || "";
            } elsif (defined $from) {
                $date = getArticleTranslationDate($class, from => "$from") || "";
	    } else {
                $date = getArticleTranslationDate($class, from => "$from",
                                                  to   => "$to") || "";
	    }
            if ($date ne "") {
       	        $temp{"$date$id"} = $id;
	    }
        }
    } else {
        foreach my $kid ($class->{doc}->getElementsByTagName("article")) { 
   	     my $id = $kid->getAttribute("id");
             $temp{$id} = $id;
        }
    }

    my @result = ();
    my @keys = keys %temp;
    if ($sort ne "no") {
        @keys = sort @keys;
        if ($sort eq "desc") {
            @keys = reverse @keys;
        }
    }
    my $counter = 0;
    foreach my $key (@keys) {
        if ((!defined $limit) || $counter <= $limit) {
            selectArticle($class, $temp{$key});
            my $criterium = 1;
            $criterium = 0 if ($cover eq "no" && articleIsCover($class));
            $criterium = 0 if ($published eq "yes" && !articleIsPublished($class));
            if ($criterium) {
                $counter++;
                if ($raw eq "yes") {
                    push(@result, $key . "{" . $temp{$key});
		} else {
                    push(@result, $temp{$key});
		}
	    }
        }
    }
    return @result;
}

sub selectArticle {
    my ($class, $artid) = @_;
    die "No article ID given!$/" if (!defined $artid);

    $class->{articleid} = $artid;
    my $nodes = $class->{doc}->getElementsByTagName ("article");
    my $n = $nodes->getLength;

    for (my $i = 0; $i < $n; $i++) {
        my $node = $nodes->item ($i);
        my $id = $node->getAttribute ("id");
        if ($id eq $artid) {
	    $class->{article} = $node;
	}
    }    
}

sub articleIsPublished {
    my ($class) = @_;

    my $result = 0;
    if (issueIsPublished($class, getArticleMonthID($class))) {
	$result = 1;
    }
    return $result;
}

sub articleIsCover {
    my ($class) = @_;

    my $result = 0;

    my $arttype = $class->{article}->getAttribute("type") || "article";
    if ($arttype eq "coverpage") {
	$result = 1;
    }
    return $result;
}

sub getArticleMonthID {
    my ($class, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    my $result = "";
    return $result if (!defined $class->{article});

    foreach my $kid ($class->{article}->getElementsByTagName ("issue")) {
	$result = $kid->getAttribute("href");
    }
    return $result;
}

sub getArticleMonth {
    my ($class, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    my $result = "";
    return $result if (!defined $class->{article});

    foreach my $kid ($class->{article}->getElementsByTagName ("issue")) {
	my $id = $kid->getAttribute("href");
        $result = getMonthName($class, $id, $olang);
    }
    return $result;
}

sub getArticleTitle {
    my ($class, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    my $title = "";
    return $title if (!defined $class->{article});

    foreach my $kid ($class->{article}->getElementsByTagName ("title")) {
        if ($kid->getAttribute("xml:lang") eq $olang) {
            my @titlenode = $kid->getChildNodes();
            if (scalar @titlenode > 0) {
      	        $title = $titlenode[0]->toString();
	    }
	} elsif (($kid->getAttribute("xml:lang") eq "en") &&
                 ($title eq "")) {
            $title = (($kid->getChildNodes())->item(0))->toString();
        } elsif (($kid->getAttribute("xml:lang") eq getArticleOriginalLanguage($class)) &&
                 ($title eq "")) {
            $title = (($kid->getChildNodes())->item(0))->toString();
        }
    }
    $title = "" if (!defined $title);
    return $title;
}

sub getIssueRL {
    my ($class, $id, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    die "No issue is selected!$/" if (!defined $id);

    my $result = $id;

    my $issues = $class->{doc}->getElementsByTagName("issues");
    return $result if (!defined $issues->item(0));

    foreach my $kid ($issues->item(0)->getElementsByTagName ("issue")) {
	if ($kid->getAttribute("id") eq $id) {
            my @titles = $kid->getElementsByTagName("file");
            foreach my $title (@titles) {
                if ($title->getAttribute("xml:lang") eq $olang) {
                    $result = (($title->getChildNodes())->item(0))->toString();
		}
 	    }
        };
    }
    return $result;
}

sub getArticleRL {
    my ($class, $olang, %args) = @_;
    $olang = $class->{lang} if (!defined $olang);
    my $meta = $args{meta} || "target";

    my $result = "";
    return $result if (!defined $class->{article});

    foreach my $kid ($class->{article}->getElementsByTagName ("file")) {
        my $type = $kid->getAttribute("type") || "target";
        if ($kid->getAttribute("xml:lang") eq $olang && ($type eq $meta)) {
	    if (($kid->getChildNodes())->item(0)) {
		$result = (($kid->getChildNodes())->item(0))->toString();
	    }
	}
    }
    return $result;
}


sub getArticleThemeID {
    my ($class, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    die "No article is selected!$/" if (!defined $class->{article});

    my $result = "";
    foreach my $kid ($class->{article}->getElementsByTagName ("theme")) {
	$result = $kid->getAttribute("href");
    }
    return $result;
}

sub getArticleTheme {
    my ($class, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    my $result = "";
    return $result if (!defined $class->{article});

    if (my $theme = getArticleThemeID($class)) {
	$result = getTheme($class, $theme, $olang);
    }
    return $result;
}

sub getArticleOriginalLanguage {
    my ($class, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    my $result = "";
    return $result if (!defined $class->{article});

    $result = $class->{article}->getAttribute("xml:lang") || "";
    return $result;
}

sub getArticleTranslationDate {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    my $result = "";

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));
    return $result if (!defined $class->{article});
    
    my @transnodes = $class->{article}->getElementsByTagName("translation");
    if (getArticleOriginalLanguage($class) eq $to) {
        $result = "1900-01-01";
    } else {
        foreach my $transnode (@transnodes) {
            if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
                ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
                my @nodes = $transnode->getElementsByTagName("finished");
                if (scalar @nodes > 0) {
                    $result = "1900-01-01";
                    my @datenode = $nodes[0]->getChildNodes();
                    if (scalar @datenode > 0) {
                        $result = ($datenode[0])->toString();
		    } else {
                        # date is unknown
		    }
                }
            }
	}
    }

    return $result;
}

sub getArticleTranslationPerson {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));
    
    my $result = "";
    return $result if (!defined $class->{article});
    
    my @transnodes = $class->{article}->getElementsByTagName("translation");
    if (getArticleOriginalLanguage($class) eq $to) {
        $result = getArticleAuthor($class);
    } else {
        foreach my $transnode (@transnodes) {
            if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
                ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
                $result = getPersonName($class, ($transnode->getElementsByTagName("person"))->item(0)->getAttribute("href"));
	    }
	}
    }
    return $result;
}   

sub getArticleTranslationPersonID {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));
    
    my $result = "";
    return $result if (!defined $class->{article});
    
    my @transnodes = $class->{article}->getElementsByTagName("translation");
    if (getArticleOriginalLanguage($class) eq $to) {
        $result = getArticleAuthor($class);
    } else {
        foreach my $transnode (@transnodes) {
            if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
                ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
                $result = ($transnode->getElementsByTagName("person"))->item(0)->getAttribute("href");
	    }
	}
    }
    return $result;
}   

sub getArticleTranslationPersonEmail {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));
    
    my $result = "";
    return $result if (!defined $class->{article});

    my @transnodes = $class->{article}->getElementsByTagName("translation");
    return $result if (!@transnodes);

    if (getArticleOriginalLanguage($class) eq $to) {
        $result = getArticleAuthorEmail($class);
    } else {
        foreach my $transnode (@transnodes) {
            if (defined $transnode) {
              if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
                  ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
                  $result = getPersonEmail($class, ($transnode->getElementsByTagName("person"))->item(0)->getAttribute("href"));
	      }
	    }
	}
    }
    return $result;
}

sub getArticleTranslationReservationDate {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    return "" if (!defined $class->{article});
    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));

    my $result = "";
    my @transnodes = $class->{article}->getElementsByTagName("translation");
    foreach my $transnode (@transnodes) {
        if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
            ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
            my $nodes = $transnode->getElementsByTagName("reserved");
            if ($nodes && $nodes->getLength > 0) {
                if ($nodes->item(0)->getChildNodes()->item(0)) {
		    $result = $nodes->item(0)->getChildNodes()->item(0)->toString();
		}
            }
	}
    }
    return $result;
}   

sub getArticleTranslationProofreadDate {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));

    my $result = "";
    return $result if (!defined $class->{article});

    my @transnodes = $class->{article}->getElementsByTagName("translation");
    foreach my $transnode (@transnodes) {
        if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
            ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
            my $nodes = $transnode->getElementsByTagName("proofread");
            if ($nodes->getLength > 0) {
                $result = $nodes->item(0)->getChildNodes()->item(0)->toString();
            }
	}
    }
    return $result;
}   

sub getArticleTranslationProofreadPerson {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));
    
    my $result = "";
    return $result if (!defined $class->{article});
    
    my @transnodes = $class->{article}->getElementsByTagName("translation");
    foreach my $transnode (@transnodes) {
        if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
            ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
            my @proofreadnodes = $transnode->getElementsByTagName("proofread");
            $result = getPersonName($class, (($proofreadnodes[0])->getElementsByTagName("person")->item(0))->getAttribute("href"));
	}
    }
    return $result;
}   

sub getArticleTranslationProofreadPersonID {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));
    
    my $result = "";
    return $result if (!defined $class->{article});
    
    my @transnodes = $class->{article}->getElementsByTagName("translation");
    foreach my $transnode (@transnodes) {
        if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
            ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
            my @proofreadnodes = $transnode->getElementsByTagName("proofread");
            $result = (($proofreadnodes[0])->getElementsByTagName("person")->item(0))->getAttribute("href");
	}
    }
    return $result;
}   

sub getArticleTranslationProofreadPersonEmail {
    my ($class, %args) = @_;
    my $from = ($args{"from"} || "*");
    my $to = ($args{"to"} || "*");

    die "No translation given!$/" if (($from eq "*") && ($to eq "*"));
    
    my $result = "";
    return $result if (!defined $class->{article});
    
    my @transnodes = $class->{article}->getElementsByTagName("translation");
    foreach my $transnode (@transnodes) {
        if (($from eq "*" || $transnode->getAttribute("from") eq $from) &&
            ($to eq "*" || $transnode->getAttribute("to") eq $to)) {
            my @proofreadnodes = $transnode->getElementsByTagName("proofread");
            $result = getPersonEmail($class, (($proofreadnodes[0])->getElementsByTagName("person")->item(0))->getAttribute("href"));
	}
    }
    return $result;
}   

sub getArticleAuthor {
    my ($class) = @_;

    my $result = "";
    return $result if (!defined $class->{article});

    $result = getPersonName($class, getArticleAuthorID($class));
    return $result;
}

sub getArticleAuthorID {
    my ($class) = @_;
    
    my $result = "";
    return $result if (!defined $class->{article});

    my $node = $class->{article}->getElementsByTagName("person")->item(0);
    $result = $node->getAttribute("href") if (defined $node);
    return $result;
}

sub getArticleAbstract {
    my ($class) = @_;
    
    my $result = "";
    return $result if (!defined $class->{article});

    my $nodes = $class->{article}->getElementsByTagName("abstract");
    if ($nodes->getLength > 0) {
        $result = ($nodes->item(0)->getChildNodes())->item(0)->toString();
        $result =~ s/(\n|\r)//g;
        $result =~ s/\s\s/ / while ($result =~ /\s\s/);
        $result =~ s/^\s//g while ($result =~ /^\s/);
    }
    return $result;
}

sub getArticleAuthorEmail {
    my ($class) = @_;
    
    my $result = "";
    return $result if (!defined $class->{article});

    $result = getPersonEmail($class, getArticleAuthorID($class));
    return $result;
}

sub getTheme {
    my ($class, $themeid, $olang) = @_;
    die "No theme id is given!$/" if (!defined $themeid);
    die "No language given!$/" if (!defined $olang);

    my $result = "";
    my $themes = $class->{doc}->getElementsByTagName("themes");
    foreach my $kid ($themes->item(0)->getElementsByTagName ("theme")) {
	if ($kid->getAttribute("id") eq $themeid) {
            my @titles = $kid->getElementsByTagName("title");
            foreach my $title (@titles) {
                if ($title->getAttribute("xml:lang") eq $olang) {
                    $result = (($title->getChildNodes())->item(0))->toString();
		}
 	    }
        };
    }
    return $result;
}

sub getMonthName {
    my ($class, $id, $olang) = @_;
    die "No id is given!$/" if (!defined $id);
    $olang = $class->{lang} if (!defined $olang);

    my $result = "";
    my $themes = $class->{doc}->getElementsByTagName("issues");
    if ($themes->item(0)) {
	foreach my $kid ($themes->item(0)->getElementsByTagName ("issue")) {
	    if ($kid->getAttribute("id") eq $id) {
		my @titles = $kid->getElementsByTagName("title");
		foreach my $title (@titles) {
		    if ($title->getAttribute("xml:lang") eq $olang) {
			$result = (($title->getChildNodes())->item(0))->toString();
		    }
		}
 	    }
        };
    }
    return $result;
}

sub issueIsPublished {
    my ($class, $id) = @_;
    die "No issue id is given!$/" if (!defined $id);

    my $result = 0;
    my $themes = $class->{doc}->getElementsByTagName("issues");
    foreach my $kid ($themes->item(0)->getElementsByTagName ("issue")) {
	if ($kid->getAttribute("id") eq $id) {
            my @pub = $kid->getElementsByTagName("published");
            if (defined $pub[0]) {
                $result = 1;
            }
        };
    }
    return $result;
}

sub getPersonEmail {
    my ($class, $id) = @_;
    die "No person id is given!$/" if (!defined $id);

    my $result = "";
    my $persons = $class->{doc}->getElementsByTagName("persons");
    if (defined $persons->item(0)) {
        foreach my $kid ($persons->item(0)->getElementsByTagName ("person")) {
	    if ($kid->getAttribute("id") eq $id) {
                my $titles = $kid->getElementsByTagName("email");
		if ($titles->item(0)) {
		    $result = (($titles->item(0))->getChildNodes())->item(0)->toString();
		}
	    };
        };
    }
    return $result;
}

sub getPersonName {
    my ($class, $id) = @_;
    die "No person id is given!$/" if (!defined $id);

    my $result = "";
    my $persons = $class->{doc}->getElementsByTagName("persons");
    if (defined $persons->item(0)) {
        foreach my $kid ($persons->item(0)->getElementsByTagName ("person")) {
	    if ($kid->getAttribute("id") eq $id) {
                my $titles = $kid->getElementsByTagName("name");
		if ($titles->item(0)) {
		    $result = (($titles->item(0))->getChildNodes())->item(0)->toString();
		}
            };
	};
    }
    return $result;
}

sub getPersonNickname {
    my ($class, $id) = @_;
    die "No person id is given!$/" if (!defined $id);

    my $result = "";
    my $persons = $class->{doc}->getElementsByTagName("persons");
    foreach my $kid ($persons->item(0)->getElementsByTagName ("person")) {
	if ($kid->getAttribute("id") eq $id) {
            my @nicks = $kid->getElementsByTagName("nickname");
            my $node = $nicks[0];
            if (defined $node) {
                $result = $node->toString;
	    };
        };
    };
    return $result;
}

sub getPersonDescription {
    my ($class, $id) = @_;
    die "No person id is given!$/" if (!defined $id);

    my $result = "";
    my $persons = $class->{doc}->getElementsByTagName("persons");
    foreach my $kid ($persons->item(0)->getElementsByTagName ("person")) {
	if ($kid->getAttribute("id") eq $id) {
            my @nicks = $kid->getElementsByTagName("desc");
            my $node = $nicks[0];
            if (defined $node) {
                $result = $node->toString;
	    };
        };
    };
    return $result;
}

sub getPersonStats {
    my ($class, $id, %args) = @_;
    my $to = ($args{"to"} || $class->{lang});

    die "No person id is given!$/" if (!defined $id);

    my %result = ();
    my $translated = 0;
    my $proofread = 0;
    my $written = 0;
    foreach my $artid (getArticleIDs($class, coverpages => "yes")) {
        selectArticle($class, $artid);
        $written++ if (getArticleAuthorID($class) eq $id);
        $translated++ 
            if (getArticleTranslationDate($class, to => $to) &&
            getArticleTranslationPersonID($class, to => $to) eq $id);
        $proofread++ 
            if (getArticleTranslationProofreadDate($class, to => $to) &&
            getArticleTranslationProofreadPersonID($class, to => $to) eq $id);
    };
    $result{written} = $written;
    $result{translated} = $translated;
    $result{proofread} = $proofread;
    return %result;
}

sub getLanguageDir {
    my ($class, $olang) = @_;
    $olang = $class->{lang} if (!defined $olang);

    my $result = "English";
    if ($olang eq "nl") {
        $result = "Nederlands";
    } elsif ($olang eq "fr") {
        $result = "French";
    } elsif ($olang eq "de") {
        $result = "Deutsch";
    } elsif ($olang eq "es") {
        $result = "Castellano";
    }
    return $result;
}    

1;
