<commit_before>package org.wikipedia.bridge;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.github.kevinsawicki.http.HttpRequest;
import org.wikipedia.Site;
import org.wikipedia.Utils;
import org.wikipedia.WikipediaApp;
import org.wikipedia.recurring.RecurringTask;

import java.io.*;
import java.net.URLEncoder;
import java.util.Date;

public class StyleFetcherTask extends RecurringTask {

    private static final String[][] STYLE_SPECS = {
            {StyleLoader.BUNDLE_PAGEVIEW, "mobile.app.pagestyle"},
            {StyleLoader.BUNDLE_PREVIEW, "mobile.app.preview"},
            {StyleLoader.BUNDLE_ABUSEFILTER, "mobile.app.pagestyle"}
    };

    // The 'l' suffix is needed because stupid Java overflows constants otherwise
    private static final long RUN_INTERVAL_MILLI =  24L * 60L * 60L * 1000L;

    public StyleFetcherTask(Context context) {
        super(context);
    }

    @Override
    protected boolean shouldRun(Date lastRun) {
        return System.currentTimeMillis() - lastRun.getTime() >= RUN_INTERVAL_MILLI;
    }

    private String getRemoteURLFor(String modules) {
        Site site = ((WikipediaApp) context.getApplicationContext()).getPrimarySite();
        try {
            return String.format(
                    // This points to betalabs, which shows the master version of the styles used
                    // Should switch to production bits URL before release.
                    "http://bits.beta.wmflabs.org/%s.wikipedia.beta.wmflabs.org/load.php?debug=true&lang=en&modules=%s&only=styles&skin=vector&version=&*",
                    site.getLanguage(),
                    URLEncoder.encode(modules, "utf-8")
            );
        } catch (UnsupportedEncodingException e) {
            // This does not happen.
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void run(Date lastRun) {
        WikipediaApp app = (WikipediaApp) context.getApplicationContext();
        for (String[] styleSpec : STYLE_SPECS) {
            String url = getRemoteURLFor(styleSpec[1]);
            try {
                OutputStream fo = context.openFileOutput(styleSpec[0], Context.MODE_PRIVATE);
                Utils.copyStreams(HttpRequest.get(url).userAgent(app.getUserAgent()).stream(), fo);
                fo.close();
                Log.d("Wikipedia", String.format("Downloaded %s into %s", url, context.getFileStreamPath(styleSpec[0]).getAbsolutePath()));
            } catch (FileNotFoundException e) {
                // This doesn't actually seem to happen ever?
                throw new RuntimeException(e);
            } catch (IOException e) {
                // FIXME: Do not crash!
                throw new RuntimeException(e);
            }
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putString(WikipediaApp.PREFERENCE_STYLES_LAST_UPDATED, Utils.formatISO8601(new Date())).commit();

    }

    @Override
    protected String getName() {
        return "style-fetcher";
    }<commit_msg>Fix crash when there's no network during StyleFetcherTask.<commit_after>package org.wikipedia.bridge;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import com.github.kevinsawicki.http.HttpRequest;
import org.wikipedia.Site;
import org.wikipedia.Utils;
import org.wikipedia.WikipediaApp;
import org.wikipedia.recurring.RecurringTask;

import java.io.*;
import java.net.URLEncoder;
import java.util.Date;

public class StyleFetcherTask extends RecurringTask {

    private static final String[][] STYLE_SPECS = {
            {StyleLoader.BUNDLE_PAGEVIEW, "mobile.app.pagestyle"},
            {StyleLoader.BUNDLE_PREVIEW, "mobile.app.preview"},
            {StyleLoader.BUNDLE_ABUSEFILTER, "mobile.app.pagestyle"}
    };

    // The 'l' suffix is needed because stupid Java overflows constants otherwise
    private static final long RUN_INTERVAL_MILLI =  24L * 60L * 60L * 1000L;

    public StyleFetcherTask(Context context) {
        super(context);
    }

    @Override
    protected boolean shouldRun(Date lastRun) {
        return System.currentTimeMillis() - lastRun.getTime() >= RUN_INTERVAL_MILLI;
    }

    private String getRemoteURLFor(String modules) {
        Site site = ((WikipediaApp) context.getApplicationContext()).getPrimarySite();
        try {
            return String.format(
                    // This points to betalabs, which shows the master version of the styles used
                    // Should switch to production bits URL before release.
                    "http://bits.beta.wmflabs.org/%s.wikipedia.beta.wmflabs.org/load.php?debug=true&lang=en&modules=%s&only=styles&skin=vector&version=&*",
                    site.getLanguage(),
                    URLEncoder.encode(modules, "utf-8")
            );
        } catch (UnsupportedEncodingException e) {
            // This does not happen.
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void run(Date lastRun) {
        WikipediaApp app = (WikipediaApp) context.getApplicationContext();
        try {
            for (String[] styleSpec : STYLE_SPECS) {
                String url = getRemoteURLFor(styleSpec[1]);
                    OutputStream fo = context.openFileOutput(styleSpec[0], Context.MODE_PRIVATE);
                    Utils.copyStreams(HttpRequest.get(url).userAgent(app.getUserAgent()).stream(), fo);
                    fo.close();
                    Log.d("Wikipedia", String.format("Downloaded %s into %s", url, context.getFileStreamPath(styleSpec[0]).getAbsolutePath()));
            }

            //if any of the above code throws an exception, the following last-updated date will not
            //be updated, so the task will be retried on the next go.
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            prefs.edit().putString(WikipediaApp.PREFERENCE_STYLES_LAST_UPDATED, Utils.formatISO8601(new Date())).commit();

        } catch (FileNotFoundException e) {
            // This doesn't actually seem to happen ever?
            throw new RuntimeException(e);
        } catch (IOException e) {
            // FIXME: better error feedback?
            Log.d("StyleFetcherTask", e.getMessage());
            e.printStackTrace();
        } catch (HttpRequest.HttpRequestException e) {
            // FIXME: better error feedback?
            Log.d("StyleFetcherTask", e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    protected String getName() {
        return "style-fetcher";
    }
{'fraction_non_alphanumeric': 0.10282852091926929, 'fraction_numerical': 0.005303476723629935, 'mean_word_length': 2.584477296726505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3872651', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1825, 'n_words': 418}