<commit_before><?php

/**
 * Shaarli2Twitter plugin
 *
 * This plugin uses the Twitter API to automatically tweet public links published on Shaarli.
 * Note: this requires a valid API authentication using OAuth.
 *
 *
 * Compatibility: Shaarli v0.8.1 and higher.
 */

use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\TemplatePage;

/**
 * Maximum tweet length.
 */
const TWEET_LENGTH = 280;

/**
 * Length of t.co transformed URL.
 * Available at https://dev.twitter.com/rest/reference/get/help/configuration,
 * but manually updated here.
 */
const TWEET_URL_LENGTH = 23;

/**
 * Default tweet format if none is provided.
 */
const TWEET_DEFAULT_FORMAT = '#Shaarli: ${title} ${url} ${tags}';

/**
 * Authorized placeholders.
 */
const TWEET_ALLOWED_PLACEHOLDERS = ['url', 'permalink', 'title', 'tags', 'description'];

/**
 * Hide url when sharing a note
 * Values can be 'yes' or 'no'.
 */
const TWEET_HIDE_URL = 'no';


/**
 * Init function: check settings, and set default format.
 *
 * @param ConfigManager $conf instance.
 *
 * @return array|void Error if config is not valid.
 */
function shaarli2twitter_init($conf)
{
    $format = $conf->get('plugins.TWITTER_TWEET_FORMAT');
    if (empty($format)) {
        $conf->set('plugins.TWITTER_TWEET_FORMAT', TWEET_DEFAULT_FORMAT);
    }

    $hide = $conf->get('plugins.TWITTER_HIDE_URL');
    if (empty($hide)) {
        $conf->set('plugins.TWITTER_HIDE_URL', TWEET_HIDE_URL);
    }

    if (!s2t_is_config_valid($conf)) {
        return ['Please set up your Twitter API and token keys in plugin administration page.'];
    }
}

/**
 * Add the CSS file for editlink page
 *
 * @param array $data - header data.
 *
 * @return mixed - header data with s2t CSS file added.
 */
function hook_shaarli2twitter_render_includes($data)
{
    if ($data['_PAGE_'] == TemplatePage::EDIT_LINK) {
        $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/shaarli2twitter/shaarli2twitter.css';
    }

    return $data;
}

/**
 * Add the JS file: disable the tweet button if the link is set to private.
 *
 * @param array         $data New link values.
 * @param ConfigManager $conf instance.
 *
 * @return array $data with the JS file.
 */
function hook_shaarli2twitter_render_footer($data, $conf)
{
    if ($data['_PAGE_'] == TemplatePage::EDIT_LINK) {
        $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/shaarli2twitter/shaarli2twitter.js';
    }

    return $data;
}

/**
 * Hook save link: will automatically publish a tweet when a new public link is shaared.
 *
 * @param array         $data New link values.
 * @param ConfigManager $conf instance.
 *
 * @return array $data not altered.
 */
function hook_shaarli2twitter_save_link($data, $conf)
{
    // No tweet without config, for private links, or on edit.
    if (!s2t_is_config_valid($conf)
        || (isset($data['updated']) && $data['updated'] != false)
        || $data['private']
        || !isset($_POST['tweet'])
    ) {
        return $data;
    }

    // We make sure not to alter data
    $link = $data;

    // We will use an array to generate hashtags, then restore original shaare tags.
    $data['tags'] = array_values(array_filter(explode(' ', $data['tags'])));
    for ($i = 0; $i < count($data['tags']); $i++) {
        // Keep tags strictly alphanumerical because Twitter only allows that.
        $data['tags'][$i] = s2t_get_tagify($data['tags'][$i]);
    }


    $data['permalink'] = index_url($_SERVER) . '?' . $data['shorturl'];

    // In case of note, we use permalink
    if (s2t_is_link_note($data)) {
        $data['url'] = $data['permalink'];
    }

    $hide = $conf->get('plugins.TWITTER_HIDE_URL', TWEET_HIDE_URL);
    if (! empty($_POST['s2t-content'])) {
        $format = escape($_POST['s2t-content']);
    } else {
        $format = $conf->get('plugins.TWITTER_TWEET_FORMAT', TWEET_DEFAULT_FORMAT);
    }
    $tweet = s2t_format_tweet($data, $format, $hide);
    $response = s2t_tweet($conf, $tweet);
    $response = json_decode($response, true);
    // If an error has occurred, not blocking: just log it.
    if (isset($response['errors'])) {
        foreach ($response['errors'] as $error) {
            error_log('Twitter error ' . $error['code'] . ': ' . $error['message']);
            error_log('Tweet: "' . $tweet . '"');
        }<commit_msg>Update permalink URL to new format (Shaarli v0.12.0)<commit_after><?php

/**
 * Shaarli2Twitter plugin
 *
 * This plugin uses the Twitter API to automatically tweet public links published on Shaarli.
 * Note: this requires a valid API authentication using OAuth.
 *
 *
 * Compatibility: Shaarli v0.8.1 and higher.
 */

use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\TemplatePage;

/**
 * Maximum tweet length.
 */
const TWEET_LENGTH = 280;

/**
 * Length of t.co transformed URL.
 * Available at https://dev.twitter.com/rest/reference/get/help/configuration,
 * but manually updated here.
 */
const TWEET_URL_LENGTH = 23;

/**
 * Default tweet format if none is provided.
 */
const TWEET_DEFAULT_FORMAT = '#Shaarli: ${title} ${url} ${tags}';

/**
 * Authorized placeholders.
 */
const TWEET_ALLOWED_PLACEHOLDERS = ['url', 'permalink', 'title', 'tags', 'description'];

/**
 * Hide url when sharing a note
 * Values can be 'yes' or 'no'.
 */
const TWEET_HIDE_URL = 'no';


/**
 * Init function: check settings, and set default format.
 *
 * @param ConfigManager $conf instance.
 *
 * @return array|void Error if config is not valid.
 */
function shaarli2twitter_init($conf)
{
    $format = $conf->get('plugins.TWITTER_TWEET_FORMAT');
    if (empty($format)) {
        $conf->set('plugins.TWITTER_TWEET_FORMAT', TWEET_DEFAULT_FORMAT);
    }

    $hide = $conf->get('plugins.TWITTER_HIDE_URL');
    if (empty($hide)) {
        $conf->set('plugins.TWITTER_HIDE_URL', TWEET_HIDE_URL);
    }

    if (!s2t_is_config_valid($conf)) {
        return ['Please set up your Twitter API and token keys in plugin administration page.'];
    }
}

/**
 * Add the CSS file for editlink page
 *
 * @param array $data - header data.
 *
 * @return mixed - header data with s2t CSS file added.
 */
function hook_shaarli2twitter_render_includes($data)
{
    if ($data['_PAGE_'] == TemplatePage::EDIT_LINK) {
        $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/shaarli2twitter/shaarli2twitter.css';
    }

    return $data;
}

/**
 * Add the JS file: disable the tweet button if the link is set to private.
 *
 * @param array         $data New link values.
 * @param ConfigManager $conf instance.
 *
 * @return array $data with the JS file.
 */
function hook_shaarli2twitter_render_footer($data, $conf)
{
    if ($data['_PAGE_'] == TemplatePage::EDIT_LINK) {
        $data['js_files'][] = PluginManager::$PLUGINS_PATH . '/shaarli2twitter/shaarli2twitter.js';
    }

    return $data;
}

/**
 * Hook save link: will automatically publish a tweet when a new public link is shaared.
 *
 * @param array         $data New link values.
 * @param ConfigManager $conf instance.
 *
 * @return array $data not altered.
 */
function hook_shaarli2twitter_save_link($data, $conf)
{
    // No tweet without config, for private links, or on edit.
    if (!s2t_is_config_valid($conf)
        || (isset($data['updated']) && $data['updated'] != false)
        || $data['private']
        || !isset($_POST['tweet'])
    ) {
        return $data;
    }

    // We make sure not to alter data
    $link = $data;

    // We will use an array to generate hashtags, then restore original shaare tags.
    $data['tags'] = array_values(array_filter(explode(' ', $data['tags'])));
    for ($i = 0; $i < count($data['tags']); $i++) {
        // Keep tags strictly alphanumerical because Twitter only allows that.
        $data['tags'][$i] = s2t_get_tagify($data['tags'][$i]);
    }


    $data['permalink'] = index_url($_SERVER) . 'shaare/' . $data['shorturl'];

    // In case of note, we use permalink
    if (s2t_is_link_note($data)) {
        $data['url'] = $data['permalink'];
    }

    $hide = $conf->get('plugins.TWITTER_HIDE_URL', TWEET_HIDE_URL);
    if (! empty($_POST['s2t-content'])) {
        $format = escape($_POST['s2t-content']);
    } else {
        $format = $conf->get('plugins.TWITTER_TWEET_FORMAT', TWEET_DEFAULT_FORMAT);
    }
    $tweet = s2t_format_tweet($data, $format, $hide);
    $response = s2t_tweet($conf, $tweet);
    $response = json_decode($response, true);
    // If an error has occurred, not blocking: just log it.
    if (isset($response['errors'])) {
        foreach ($response['errors'] as $error) {
            error_log('Twitter error ' . $error['code'] . ': ' . $error['message']);
            error_log('Tweet: "' . $tweet . '"');
        }
{'fraction_non_alphanumeric': 0.1540048126503953, 'fraction_numerical': 0.006646041022115274, 'mean_word_length': 3.3229321446260527, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '652558', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2865, 'n_words': 748}