#!/usr/bin/php
<?php



/*
 *        CONFIGURATION
 */
$email '';
$password '';

$not_logged_str 'Not logged in';
$login_page 'https://launchpad.net/+login';

$email_form_name 'loginpage_email';
$passwd_form_name 'loginpage_password';
$login_button_name 'loginpage_submit_login';
$login_button_value 'Log In';
define('TMP_FILE_NAME''launchpad_tmp.txt');
define('SLEEP_TIME'10);

$step 50;

$long_packages = array(
    
'iso-639',
    
'iso-3166-2',
    
'gcc-3.4',
    
'gcc-4.0',
    
'blender',
    
'postgres-8.1',
    );

/*
 *        INCLUSION
 */
include_once('./class.db_layer.php');
include_once(
'/var/www/argp.php');

/*
 *        ARGUMENT PARSING
 */
$ap = new arg_parse($_SERVER['argv']);

if (
$ap->has_arg('help'))
{
echo 
'
Options:
    --refresh-packages
    --refresh-by-id="ID1,ID2"
    --refresh-by-name="ID1,ID2"
    --without-ftp-refresh
    --step=
'
;
    exit;
}

$refresh_b_id $ap->get_arg_value('refresh-by-id');
$refresh_b_name $ap->get_arg_value('refresh-by-name');
//$step = $ap->get_arg_value('step');

/*
 *        MAIN
 */
$db = new db('localhost''root''''c_trans');

//my_fetch('https://launchpad.net/distros/ubuntu/dapper/+lang/hu?batch=1200');

$hu_trans_main_page get_last_result();

if (
strpos($hu_trans_main_page$not_logged_str) !== false)
{
    echo 
'login';
    echo 
"\n";
    
// login
    
$form_vars = array(
        
$email_form_name => $email,
        
$passwd_form_name => $password,
        
$login_button_name => $login_button_value,
        );

    
my_submit($login_page$form_vars);
}

/*
 *        PACKAGES REFRESH
 */
if ($ap->has_arg('refresh-packages'))
{
    
$hu_trans_main_page substr($hu_trans_main_pagestrpos($hu_trans_main_page'<table'), strpos($hu_trans_main_page'</table'));

    
$hu_trans_main_page substr($hu_trans_main_pagestrpos($hu_trans_main_page'<tbody'), strpos($hu_trans_main_page'</tbody'));

    while (
strpos($hu_trans_main_page'<tr>') !== false)
    {
        
$tr_section substr($hu_trans_main_pagestrpos($hu_trans_main_page'<tr'), strpos($hu_trans_main_page'</tr')+2);

        
$hu_trans_main_page substr($hu_trans_main_pagestrlen($tr_section));

        
/*
            <th>Template name</th>
        */

        
$td_source substr($tr_sectionstrpos($tr_section'<td'), strpos($tr_section'</td'));
        
$tr_section substr($tr_sectionstrlen($td_source));

        
preg_match('!<a href="([^"]+)!i'$td_source$matches);
        
$link $matches[1];
        
preg_match('!translate">([^<]+)</a>!i'$td_source$matches);
        
$name $matches[1];

        
$sql 'SELECT id FROM rosetta WHERE link = \''$link .'\'';
        
$id = (int) $db->Get_One($sql);

        
$f = array(
            
'link' => $link,
            
'name' => $name,
            );

        if (
$id)
        {
            
$db->update_fields('rosetta'$f'id = \''$id .'\'');
        }
        else
        {
            
$db->insert_fields('rosetta'$f);
        }

    }
    exit;
}

/*
 *        FUNCTIONS
 */
function my_fetch($link$i 0)
{
    if (
$i == 10)
    {
        echo 
'Too much oops';
    }
    echo 
$link;
    echo 
"\n";

     
$com 'wget -q --load-cookies="/tmp/cookies" --output-document=/tmp/'TMP_FILE_NAME .' "'$link .'"';
    
exec($com);

    
sleep(SLEEP_TIME);

    
$result get_last_result();

    if (
strpos($result'<title>Oops!</title>') !== false)
    {
        
my_fetch($link, ++$i);
    }
}

function 
get_last_result()
{
    if (!
is_file('/tmp/'TMP_FILE_NAME))
    {
        return 
"";
    }
    return 
implode(''file('/tmp/'TMP_FILE_NAME));
}

function 
my_submit($link$form_vars)
{
    
$tmp = array();
    foreach (
$form_vars as $key => $var)
    {
        
$tmp[] = $key .'='$var;
    }
    
$com 'wget -q --save-cookies="/tmp/cookies" --output-document=/tmp/'TMP_FILE_NAME .' --post-data="'implode('&'$tmp) .'" '$link;

    
exec($com);
    
sleep(SLEEP_TIME);
}

function 
get_results_number($text)
{
    
$text substr($textstrpos($text'&rarr;'));
    
$text substr($text0strpos($text'</td>'));
    
preg_match('!([0-9]+) result!'$text$matches);
    return (int) 
$matches[1];
}

$where_pls '';
$refresh = array();

if (
$refresh_b_id !== false)
{
    
$refresh_by[] = $db->sql_field_in('id'explode(','$refresh_b_id));
}

if (
$refresh_b_name !== false)
{
    
$refresh_by[] = $db->sql_field_in('name'explode(','$refresh_b_name));
}

if (
count($refresh_by))
{
    
$where_pls ' AND ('implode(' OR '$refresh_by) .') ';
}

//
$sql 'SELECT * FROM rosetta WHERE status <> \'fine\' '$where_pls;
$result $db->execute($sql);
if (
$result && $result->record_count())
{
    while (
$result->fetch_into($row))
    {
        if (
in_array($row['name'], $long_packages))
        {
            continue;
        }
        echo 
$row['id'];
        echo 
' ';

        
my_fetch($row['link'] .'?batch='$step .'&show=untranslated');

        
$package_page get_last_result();

        
$pos strpos($package_page'To do:</b>')+strlen('To do:</b>');
        
preg_match('![\ \    ]+([0-9]+)!'substr($package_page$pos), $matches);

        
$todo = (int) trim($matches[1]);

        if (
$todo === 0)
        {
            
$db->update_fields('rosetta', array('status' => 'fine''todo' => $todo), 'id = \''$row['id'] .'\'');
            continue;
        }
        
$text get_last_result();

        
$page 0;

        
$sugg 0;
        
$sugg 0;
        
$sugg_e 0;
        
$used_e 0;
        
$n_o_untr 0;

        if (!
strpos($text'There are no messages that match this filtering.'))
        {
            
$n_o_untr get_results_number($text);

            while (
$text !== false)
            {
                
$sugg += substr_count($text'Suggestion:');
                
$sugg += substr_count($text'Suggestions:');
                
$sugg_e += substr_count($text'Suggested elsewhere:');
                
$used_e += substr_count($text'Used elsewhere:');

                if (
strpos($text'<span class="unavailable">&mdash; Next</span>') !== false)
                {
                    
$text false;
                    continue;
                }

                
$page += $step;
                
my_fetch($row['link'] .'?batch='$step .'&show=untranslated&start='$page);
                
$text get_last_result();

            }
        }

        
$f = array(
            
'todo' => $todo,
            
'suggestion' => $sugg,
            
'suggestion_elsewhere' => $sugg_e,
            
'used_elsewhere' => $used_e,
            
'number_of_untr' => $n_o_untr,
            );

        
print_r($f);

        
$db->update_fields('rosetta'$f'id = \''$row['id'] .'\'');
    }
}

if (!
$ap->has_arg('without-ftp-refresh'))
{
    echo 
"Refresh the server\n";

    
system('./upload.php');
}

?>