$bugzilla_query_l10ncomponent = 'https://bugzilla.mozilla.org/buglist.cgi?' . '&query_format=advanced' . '&status_whiteboard_type=allwordssubstr' . '&status_whiteboard=webdashboard' . '&bug_status=UNCONFIRMED' . '&bug_status=NEW' . '&bug_status=ASSIGNED' . '&bug_status=REOPENED' . '&component=' . urlencode(Bugzilla::getBugzillaLocaleField($locale, 'l10n')) . '&classification=Client%20Software' . '&product=Mozilla%20Localizations';
/* Use cached requests if available, cache expires after 1 hour
 * Note: result can be empty, so I need to check strictly for false
 */
$cache_id = "bugs_mozillaorg_{$locale}";
$bugs_mozillaorg = Cache::getKey($cache_id, 60 * 60);
if ($bugs_mozillaorg === false) {
    $csv_mozillaorg = file($bugzilla_query_mozillaorg . '&ctype=csv');
    $bugs_mozillaorg = Bugzilla::getBugsFromCSV($csv_mozillaorg);
    Cache::setKey($cache_id, $bugs_mozillaorg);
}
$cache_id = "bugs_l10ncomponent_{$locale}";
$bugs_l10ncomponent = Cache::getKey($cache_id, 60 * 60);
if ($bugs_l10ncomponent === false) {
    $csv_l10ncomponent = file($bugzilla_query_l10ncomponent . '&ctype=csv');
    $bugs_l10ncomponent = Bugzilla::getBugsFromCSV($csv_l10ncomponent);
    Cache::setKey($cache_id, $bugs_l10ncomponent);
}
$bugs = $bugs_mozillaorg + $bugs_l10ncomponent;
$rss_data = [];
if (count($bugs) > 0) {
    foreach ($bugs as $bug_number => $bug_description) {
        $rss_data[] = ["Bug {$bug_number}: {$bug_description}", "https://bugzilla.mozilla.org/show_bug.cgi?id={$bug_number}", $bug_description];
    }
}
// Read status of external web projects, cache expires after 1 hour.
$cache_id = 'external_webprojects';
if (!($webprojects = Cache::getKey($cache_id, 60 * 60))) {
    $webprojects = $json_data->setURI(WEBPROJECTS_JSON)->fetchContent();
    Cache::setKey($cache_id, $webprojects);
}
*/
namespace Webdashboard;

use Bugzilla\Bugzilla;
use Cache\Cache;
require_once __DIR__ . '/../config/init.php';
// Include all data about our locales
include __DIR__ . '/../data/locales.php';
$results = [];
$results['types']["Webbugs"] = ["pluralLabel" => "Webbugs"];
$results['properties']["total_webbugs"] = ["valueType" => "number"];
$results['properties']["missing_webbugs"] = ["valueType" => "number"];
// All opened bugs for a locale in the mozilla.org/l10n component
foreach ($locales as $locale) {
    $bugzilla_query = 'https://bugzilla.mozilla.org/buglist.cgi?' . 'f1=cf_locale' . '&o1=equals' . '&query_format=advanced' . '&v1=' . urlencode(Bugzilla::getBugzillaLocaleField($locale)) . '&o2=equals' . '&f2=component' . '&v2=L10N' . '&bug_status=UNCONFIRMED' . '&bug_status=NEW' . '&bug_status=ASSIGNED' . '&bug_status=REOPENED' . '&classification=Other' . '&product=www.mozilla.org';
    /* Check if there is a cached request for this element.
     * For this request I use a ttl of 6 hours instead of default (15 minutes),
     * since it requires a lot of time.
     */
    $cache_id = "bugs_mozillaorg_{$locale}";
    $bugs = Cache::getKey($cache_id, 6 * 60 * 60);
    if ($bugs === false) {
        $csv = file($bugzilla_query . '&ctype=csv');
        $bugs = Bugzilla::getBugsFromCSV($csv);
        Cache::setKey($cache_id, $bugs);
    }
    // Generate all the bugs
    $results['items'][] = ["type" => "Webbugs", "label" => $locale, "missing_webbugs" => count($bugs), "total_webbugs" => count($bugs)];
}
$json_data = new Json();
print $json_data->outputContent($results, false, true);