<?php

error_reporting
E_ALL E_NOTICE );
#ini_set("display_errors",0);

function trim_value(&$value)
{
    
$value trim($value);
}



function 
findtranslation ($tolang)        #What:parameter or value 
{
Global 
$lang;
$fromlang=$lang;
Global 
$template;

Global 
$totemplate;
Global 
$frompara;
Global 
$topara;
Global 
$fromvalue;
Global 
$tovalue;

$sql="SELECT /* LIMIT:60 */`totemplate`,`frompara`, `topara`, `fromvalue`, `tovalue` FROM `pub_tt_trans` WHERE 
`fromlang` LIKE  '
$fromlang' AND `tolang` LIKE '$tolang'
AND `fromtemplate` LIKE '
$template' ";

 
$query mysqli_query($db,$sql);

 while(
$row mysqli_fetch_row($query)) {
      
$row[1]=explode('|',$row[1]);    array_walk($row[1], 'trim_value');
      
$row[2]=explode('|',$row[2]);    array_walk($row[2], 'trim_value');
$totemplate=$row[0];
$frompara=$row[1];
$topara=$row[2];
$fromvalue=$row[3];
$tovalue=$row[4];


    }     
 }


function 
translate ($tolang,$what,$string)        #What:parameter or value 
{
Global 
$lang;
$fromlang=$lang;
Global 
$template;

Global 
$totemplate;
Global 
$frompara;
Global 
$topara;
Global 
$fromvalue;
Global 
$tovalue;
  
     if (
$what=="parameter"){
         
$from=$frompara;$to=$topara;}
     if (
$what=="value"){
         
$from=$fromvalue;$to=$tovalue;}
     
 
$responce=str_replace($from,$to,$string);


 return 
$responce;
}


$pagebegin='
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="content-type"
 content="text/html; charset=UTF-8">
<meta name="robots" content="nofollow" />
  <title>TT-Table '
.$_GET[template].'</title>

  <style type="text/css">
  body { margin-left:10px;font-family:helvetica; }
  p,div,ul,ol { margin-left:9px;font-family:helvetica;  }
   table { margin-left:9px;font-family:helvetica;font-size:12px;border-width:1px;border-style:solid;padding-top:2px;
    border-collapse:collapse; 
    white-space:nowrap;
      border-color:blue;
      background-color:#EEEEEE;  }
  a:link { text-decoration:none; }
  a:hover {text-decoration:underline; }

</style>


</head>
<body>

<div style="float:right">
<small>Templatetiger <a target="new" href="http://de.wikipedia.org/wiki/Wikipedia:WikiProjekt_Vorlagenauswertung">(de) </a>
<a target="new" href="http://de.wikipedia.org/wiki/Wikipedia:WikiProjekt_Vorlagenauswertung/en">(en) </a></small>
</div>
'
;



#database-connection

$ts_pw posix_getpwuid(posix_getuid());
$ts_mycnf parse_ini_file($ts_pw['dir'] . "/.my.cnf");

$db mysqli_connect("tools-db",$ts_mycnf['user'], $ts_mycnf['password']);
unset(
$ts_mycnf$ts_pw);

mysqli_set_charset($db"latin1") ;
#printf("Initial character set: %s\n", mysqli_character_set_name($db)); 

mysqli_select_db($db,'s51071__templatetiger_p');


 
$lang=addslashesrawurldecode$_GET['lang'] ) );  #input
 
if (strlen($lang)<4) {$lang=$lang."wiki";}

 
$template=str_replace"_"" "addslashesrawurldecode$_GET['template'] ) ) );
  if (
$template==""){ $template="Personendaten";}
  
$totemplate=$template;

  
$whereURL=rawurlencode($_GET['where']);
 
$where=addslashes($_GET['where']);

  
$isURL=rawurlencode($_GET['is']);
 
$is=addslashes($_GET['is']);

  
$offset=addslashes(rawurldecode($_GET['offset']));
    if (
$offset==""){ $offset="0";}

 
$format=rawurlencode($_GET['format']);
 if (
$format<>"") {$formatURL="&format=".$format;}
 if (
$format=="") {$format="table";}


 
$limit=addslashes(rawurldecode($_GET['limit']));
    if (
$limit==""){ $limit="30";}
    if (
$limit>3000){ $limit="3000";}

  
$notURL=rawurlencode($_GET['not']);
$not=addslashes(rawurldecode($_GET['not']));
if (
$not=="yes") { $not=" NOT "$notURL='&amp;not=yes';} else { $not="";$notURL="";}


 
$orderURL=rawurlencode($_GET['order']);
$orderbyexp=addslashes(rawurldecode($_GET['order']));
if (
$orderbyexp<>"") {    $orderURL="&order=".$orderURL;}
 

if (
$format<>"csv"){echo $pagebegin;}

$trans=rawurlencode($_GET['trans']);
if (
$trans<>"") {$transURL="&trans=".$trans;}

  
$orderby="";


  
$columnsURL=rawurlencode($_GET['columns']);
  if (
$columnsURL<>"") {$columnsURL="&columns=".$columnsURL;
     
$columnsexp=addslashes(rawurldecode($_GET['columns']));
     
$columns preg_split('/,|\s/'$columnsexp);
   foreach(
$columns as $column){
            if (
$column_idd==""){$or="";} else {$or=" OR ";} 
            
$column_idd=$column_idd.$or `entry_name`='$column'";
                                            }
            
$column_idd="AND ( ".$column_idd.")";
                            }



 
$regexp=addslashes(rawurldecode($_GET['regexp']));
        if (
$regexp=="yes") { $like="$not REGEXP '";$like2="";$reg="&regexp=yes";} else { $like="$not LIKE '";$like2="%";$reg="";}




if ((
$where=="") OR ($is==""))  {$whereis=""$SQL_CALC_FOUND_ROWS="";          #construction of the first query for article IDs

     
if ($orderbyexp<>""
     {
         if (
$orderbyexp=="article"
            {
$orderby=" ORDER BY `name` ASC "
              
$whereis="";$SQL_CALC_FOUND_ROWS="";
            } 
                else 
            {
$orderby=" ORDER BY `Value` ASC ";
                
$whereis="AND `entry_name` LIKE '$orderbyexp' ";$SQL_CALC_FOUND_ROWS="SQL_CALC_FOUND_ROWS";}
     } 
  

else    
{
$whereis="AND `entry_name` LIKE '$where' AND `Value` $like$is$like2'"$SQL_CALC_FOUND_ROWS="SQL_CALC_FOUND_ROWS";}

  
$z=0;

     
$sql="SELECT /* LIMIT:60 */ DISTINCT $SQL_CALC_FOUND_ROWS `name_id`,`name` FROM `$lang` WHERE `tp_name` LIKE '$template$whereis $orderby LIMIT $offset,$limit";                

 if (
$format<>"csv"){     echo "$sql <br>";}
 
  
 if (
$trans<>"")                        #translation
        
{
            
findtranslation ($trans);
            echo 
"<br>You can read and edit the <a href=\"./template-trans.php?tolang=$trans&fromlang=$lang&fromtemplate=$template\">Translation-parameters</a>.<p>";
        }



    
$query mysqli_query($db,"$sql");            # first query

if ($query !== false){    
while(
$row mysqli_fetch_row($query)) {
    if (
$name_idd==""){$or="";} else {$or=" OR ";} 

    
$name_idd=$name_idd.$or `name_id`='$row[0]'";
      
$namen2[$z]= $row[1];
      
$z=$z+1;
          }}

     
$name_idd="AND ( ".$name_idd.")";         #result of first query is a string with article-IDs for second query

  
if (($where<>"") OR ($is<>"") OR ($orderbyexp<>"") and ($orderbyexp<>"article"))  
          {     
     
$query2 mysqli_query($db," SELECT FOUND_ROWS(); ");
     while(
$row mysqli_fetch_row($query2)) { $counter=$row[0];}}  else
     {
       
$sql2="SELECT `sum` FROM `".$lang."_sum` WHERE `tp_name` LIKE '$template' ";
       
$query mysqli_query($db,"$sql2"); 
      if (
$query !== false){
        while(
$row mysqli_fetch_row($query)) { $counter=$row[0];}
                }}


if (
$format<>"csv"){

if (
$offset>0) {echo '<a href="tt-table4.php?lang='.$lang.'&template='."$template&where=$whereURL&is=$isURL$notURL$orderURL$columnsURL$formatURL&$transURL&offset=".($offset-$limit)."&limit=$limit$reg".'"> <<< </a>';}
if (
$limit+$offset<$counter) {echo ' <a href="tt-table4.php?lang='.$lang.'&template='."$template&where=$whereURL&is=$isURL$notURL$orderURL$columnsURL$formatURL&$transURL&offset=".($offset+$limit)."&limit=$limit$reg".'"> >>> </a>';}
echo 
"  $offset - ".min(($offset+$limit),$counter)." of $counter"
echo 
"<br>";
}
                                           
if (
$z==0) {echo "<p><b>no entries</b>";}    else {

$sql "SELECT  /* LIMIT:60 */ `name` , `entry_name` , `Value` 
 FROM `
$lang` WHERE (`tp_name` LIKE '$template') $name_idd $column_idd ";

 
#echo $sql;
 
$z=0;
  
$query mysqli_query($db,$sql);                #second query 
while($row mysqli_fetch_row($query)) {

    if (
$trans<>"")                        #translation
            
{
                
$row[1]=translate ($trans,"parameter",$row[1]);
                            }
      
    
     if (!isset(
$ausgabe [$row[0]][$row[1]]))
        {
$ausgabe [$row[0]][$row[1]]=$row[2];} else

        {if (
$format=="table"
            {
$ausgabe [$row[0]][$row[1]].=    "<p>".$row[2];}    else
            {
$ausgabe [$row[0]][$row[1]] =    $row[2];}
               }       
#result is an array $ausgabe[articlename][entryname]=value1 <p> value2 ....   

#    $namen[$z]= $row[0];
    
$bezeichner[$z]=$row[1];

     
#print_r ($row);
   
$z=$z+1;
}

                               

 
#print_r ($ausgabe);
 
if ($z==0) {echo "no entries";} else {           #transform the array to an table
$namen        array_unique($namen2);
#$namen2        = array_unique($namen2);
$bezeichner    array_unique($bezeichner);

mysqli_close($db);

if (
$lang=="commonswiki") {$project=".wikimedia.org";} else {$project=".wikipedia.org";}
$langproject=str_replace("wiki","",$lang);


if ( 
$format === 'table' ) {
    echo 
"<table border=\"1\">";

    echo 
"<tr><th></th>";
    foreach ( 
$bezeichner as $valuename ) {
        echo 
'<th>' $valuename '</th>';
    }
    echo 
"</tr>";

    foreach ( 
$namen as $name ) {
        echo 
'<tr>';
        echo 
'<td><a href="http://' $langproject $project '/wiki/' $name '">' $name '</a></td>';

        foreach ( 
$bezeichner as $valuename ) {
            if ( !isset( 
$ausgabe[$name][$valuename] ) ) {
                echo 
'<td>&#160;</td>';
            } else {
                echo 
'<td>' $ausgabe[$name][$valuename] . '</td>';
            }
        }
        echo 
"</tr>\n";
    }

    echo 
"</table>";
}
}
 


if (
$format=="csv")
{

echo 
"article    ";
 foreach(
$bezeichner as $valuename){
      echo 
$valuename.'    ';
    } 
echo 
"\n";
 
 foreach(
$namen as $name){
         echo 
"$name    ";

    foreach(
$bezeichner as $valuename){
     if (
$ausgabe [$name][$valuename]=="")
     {echo 
'    ';} else
     {echo 
$ausgabe [$name][$valuename]."    ";}
    }
    echo 
"\n";
  }




if (
$format=="template")
{
 
 foreach(
$namen as $name){
         echo 
'<p><a href="http://'.$lang.$langproject.$project.'/wiki/'.$name.'">'.$name.'</a>';
         echo 
'<br>{{'.$totemplate.'<br>';

    
    foreach(
$bezeichner as $valuename){
     echo 
"|$valuename=".$ausgabe [$name][$valuename]."<br>";
    }

    echo 
"}}<br>";
  }

}



?>