コード例 #1
0
ファイル: querybin.php プロジェクト: pwikstrom/dmi-tcat
function do_view_or_export_tweets(array $querybin, $dt_start, $dt_end, $export)
{
    global $api_timezone;
    global $utc_tz;
    // Get information about tweets from the query bin
    $info = tweet_info($querybin, $dt_start, $dt_end);
    $time_to_purge = est_time_to_purge($querybin['notweets'], $dt_start, $dt_end, $info['tweets']);
    // Show result
    $response_mediatype = choose_mediatype(['text/html', 'text/csv', 'text/tab-separated-values']);
    if (isset($export)) {
        // Force exporting of tweets when viewed in a Web browser
        // that cannot set the accepted mediatypes header.
        if ($export === 'tsv') {
            $response_mediatype = 'text/tab-separated-values';
        } else {
            $response_mediatype = 'text/csv';
        }
    }
    switch ($response_mediatype) {
        case 'text/csv':
        case 'text/tab-separated-values':
            $ds = $querybin['bin'];
            $depth = count(explode('/', $_SERVER['PATH_INFO']));
            $rel = str_repeat('../', $depth);
            $qparams = 'dataset=' . urlencode($ds);
            # Note: the export URL expected the times to be in UTC
            # and formatted as 'YYYY-MM-DD HH:MM:SS'
            if (isset($dt_start)) {
                $dt_start->setTimezone($utc_tz);
                $qparams .= '&startdate=';
                $qparams .= urlencode($dt_start->format(DT_PARAM_PATTERN));
            }
            if (isset($dt_end)) {
                $dt_end->setTimezone($utc_tz);
                $qparams .= '&enddate=';
                $qparams .= urlencode($dt_end->format(DT_PARAM_PATTERN));
            }
            if ($response_mediatype == 'text/tab-separated-values') {
                $qparams .= '&outputformat=tsv';
            } else {
                $qparams .= '&outputformat=csv';
            }
            // Redirect to existing export function
            header("Location: {$rel}analysis/mod.export_tweets.php?{$qparams}");
            exit(0);
            break;
        case 'text/html':
            $script_url = $_SERVER['SCRIPT_URL'];
            $components = explode('/', $script_url);
            array_pop($components);
            $query_bin_info = implode('/', $components);
            array_pop($components);
            $query_bin_list = implode('/', $components);
            html_begin("Query bin tweets: {$querybin['bin']}", [["Query Bins", $query_bin_list], [$querybin['bin'], $query_bin_info], ["View Tweets"]]);
            $total_num = $querybin['notweets'];
            // Selection times as text for start/end text field values
            $val_A = isset($dt_start) ? dt_format_text($dt_start, $api_timezone) : "";
            $val_B = isset($dt_end) ? dt_format_text($dt_end, $api_timezone) : "";
            if (0 < $total_num) {
                // Has tweets: show form to select tweets
                $min_t = dt_from_utc($querybin['mintime']);
                $max_t = dt_from_utc($querybin['maxtime']);
                // First/last tweet times as text for placeholders
                $t1_text = dt_format_text($min_t, $api_timezone);
                $t2_text = dt_format_text($max_t, $api_timezone);
                // First/last tweet times as HTML for description
                $t1_html = dt_format_html($min_t, $api_timezone);
                $t2_html = dt_format_html($max_t, $api_timezone);
                echo <<<END

<p>This query bin contains a total of {$total_num} tweets
from {$t1_html} to {$t2_html}.</p>

<form method="GET">
  <table>
    <tr>
      <th style="white-space: nowrap">
        <label for="startdate">Start time</label></th>
      <td><input type="text" id="startdate" name="startdate"
           placeholder="{$t1_text}" value="{$val_A}"/></td>
    </tr>
    <tr>
      <th style="white-space: nowrap">
        <label for="enddate">End time</label></th>
      <td><input type="text" id="enddate" name="enddate"
          placeholder="{$t2_text}" value="{$val_B}"/></td>
    </tr>
    <tr>
      <td></td>
      <td><input type="submit" value="Update selection"/></td>
    <tr>

<tr>
<td></td>
<td>

<p>
END;
                if (isset($api_timezone) && $api_timezone !== '') {
                    // Default timezone available
                    echo <<<END
Start and end times may include an explicit timezone.
If there is no timezone, the time will be interpreted to be in
the <em>{$api_timezone}</em> timezone.
END;
                } else {
                    // No default timezone
                    echo <<<END
Start and end times must include an explicit timezone.
END;
                }
                echo <<<END

Acceptable timezone values are "Z", "UTC" or an offset in the form of
[+-]HH:MM.

No entered values means the time of the first or last tweet.  Partial
times are also permitted: only the year is mandatory, the remaining
components will be automatically added. Times are inclusive (i.e. any
tweets with timestamps equal to those times will be included in the
selection).</p>

</td>
</tr>

  </table>
</form>
END;
                $title = "Selected tweets";
            } else {
                // No tweets: do not show form to select tweets
                echo '<p>This query bin contains no tweets.</p>';
                $title = "Tweets";
            }
            // Show view of selected tweets (or no tweets)
            // Selection times as HTML for display
            $hB = isset($dt_end) ? dt_format_html($dt_end, $api_timezone) : "";
            if (isset($dt_start)) {
                $hA = dt_format_html($dt_start, $api_timezone);
                if (isset($dt_end)) {
                    $dt_desc_html = "From {$hA} to {$hB}";
                } else {
                    $dt_desc_html = "From {$hA} to last tweet";
                }
            } else {
                if (isset($dt_end)) {
                    $dt_desc_html = "From first tweet to {$hB}";
                } else {
                    $dt_desc_html = "All tweets from first to last";
                }
            }
            echo <<<END
<fieldset>
  <legend>{$title}</legend>

  <table>
    <tr><th>Selected range</th><td>{$dt_desc_html}</td></tr>
    <tr><th>Number of tweets</th><td>{$info['tweets']} of {$total_num}</td></tr>
  </table>
END;
            if (0 < $total_num) {
                // Has tweets: show button to purge tweets
                echo <<<END
    <div id="export-tweets-form">
    <form method="GET">
        <input type="hidden" name="action" value="export-tweets"/>
        <input type="hidden" name="startdate" value="{$val_A}"/>
        <input type="hidden" name="enddate" value="{$val_B}"/>
        <input type="submit" value="Export selected tweets"/>

        <input type="radio" name="format" value="csv" id="csv" checked="checked"/>
        <label for="csv">CSV</label>
        <input type="radio" name="format" value="tsv" id="tsv"/>
        <label for="tsv">TSV</label>
    </form>
    </div>
    <div id="purge-tweets-form">
    <form method="POST">
        <input type="hidden" name="action" value="purge-tweets"/>
        <input type="hidden" name="startdate" value="{$val_A}"/>
        <input type="hidden" name="enddate" value="{$val_B}"/>
        <input type="submit" value="Purge selected tweets"/>
    </form>
    </div>
END;
                if (isset($time_to_purge)) {
                    echo <<<END
<p id="purge-time-warning">
Warning: purging could take a long time (~ {$time_to_purge}).</p>
END;
                }
            }
            // End of view of tweets
            echo "</fieldset>\n";
            // End of page
            html_end();
            break;
        default:
            $from = isset($dt_start) ? dt_format_text($dt_start, $api_timezone) : "";
            $to = isset($dt_end) ? dt_format_text($dt_end, $api_timezone) : "";
            if (isset($dt_start)) {
                if (isset($dt_end)) {
                    $dt_desc = "from {$from} to {$to}";
                } else {
                    $dt_desc = "from {$from} to last tweet";
                }
            } else {
                if (isset($dt_end)) {
                    $dt_desc = "first tweet up to {$to}";
                } else {
                    $dt_desc = "all tweets";
                }
            }
            $total_num = $querybin['notweets'];
            if (0 < $total_num) {
                // Has tweets
                $min_t = dt_from_utc($querybin['mintime']);
                $max_t = dt_from_utc($querybin['maxtime']);
                $t1_text = dt_format_text($min_t, $api_timezone);
                $t2_text = dt_format_text($max_t, $api_timezone);
                echo <<<END
Query bin: {$querybin['bin']}
  Earliest tweet: {$t1_text}
    Latest tweet: {$t2_text}
  Tweets: ({$dt_desc})
    Number of tweets: {$info['tweets']} of {$querybin['notweets']}

END;
                if (isset($time_to_purge)) {
                    print "Warning: purging could take a long time (~ {$time_to_purge}).\n";
                }
            } else {
                // No tweets
                echo "Query bin: {$querybin['bin']}\n";
                echo "  No tweets\n";
            }
            break;
    }
}
コード例 #2
0
ファイル: dt_util.php プロジェクト: pwikstrom/dmi-tcat
function dt_format_html(DateTime $dt, $tz_name = NULL)
{
    $html = '<span class="datetime" title="';
    $html .= htmlspecialchars(dt_format_text($dt, NULL));
    // always in UTC
    $html .= '">';
    $html .= htmlspecialchars(dt_format_text($dt, $tz_name));
    $html .= '</span>';
    return $html;
}