updated context, metatag, imagestyleflush, browscap
This commit is contained in:
@@ -20,25 +20,43 @@ to retrieve and update its user agent database.
|
||||
|
||||
Note: Some hosting companies have this capability blocked.
|
||||
|
||||
|
||||
Recommendations
|
||||
---------------
|
||||
|
||||
If you are using MySQL, it is recommended to use InnoDB and not MyISAM as the
|
||||
table storage engine. (InnoDB is the default for Drupal 7.) MyISAM does not
|
||||
support transactions, and without transactions Browscap may not return correct
|
||||
browser information while new Browscap data is being loaded.
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
Browscap can be installed via the standard Drupal installation process.
|
||||
http://drupal.org/node/895232
|
||||
https://drupal.org/node/895232
|
||||
|
||||
|
||||
API
|
||||
---
|
||||
|
||||
Modules can make use of browscap data by calling browscap_get_browser()
|
||||
anywhere they would otherwise call the PHP get_browser()
|
||||
(http://us3.php.net/manual/en/function.get-browser.php) function.
|
||||
(https://secure.php.net/manual/en/function.get-browser.php) function.
|
||||
|
||||
Note: browser_name_regex is not returned.
|
||||
|
||||
|
||||
Drush integration
|
||||
-----------------
|
||||
|
||||
To import the data via drush, use `drush browscap-import`.
|
||||
|
||||
|
||||
Credits
|
||||
-------
|
||||
|
||||
Development of Browscap is sponsored by Acquia (http://www.acquia.com) and the
|
||||
Development of Browscap is sponsored by Acquia (https://www.acquia.com) and the
|
||||
Ontario Ministry of Northern Development and Mines (http://www.mndm.gov.on.ca).
|
||||
|
||||
A special thanks goes out to Gary Keith (http://www.garykeith.com) who provides
|
||||
|
@@ -22,8 +22,8 @@ function browscap_settings_form($form, &$form_state) {
|
||||
$form['browscap_enable_automatic_updates'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Enable automatic updates'),
|
||||
'#default_value' => variable_get('browscap_enable_automatic_updates', TRUE),
|
||||
'#description' => t('Automatically update the user agent detection information.'),
|
||||
'#default_value' => variable_get('browscap_enable_automatic_updates', FALSE),
|
||||
'#description' => t('Automatically update the user agent detection information. <em>Warning: This may require several seconds and possibly up to a minute or more during cron.</em>'),
|
||||
);
|
||||
$form['browscap_automatic_updates_timer'] = array(
|
||||
'#type' => 'select',
|
||||
@@ -54,9 +54,5 @@ function browscap_settings_form($form, &$form_state) {
|
||||
* Submit handler for the refresh browscap button.
|
||||
*/
|
||||
function browscap_refresh_submit($form, &$form_state) {
|
||||
// Update the browscap information.
|
||||
_browscap_import(FALSE);
|
||||
|
||||
// Record when the browscap information was updated.
|
||||
variable_set('browscap_imported', REQUEST_TIME);
|
||||
}
|
||||
|
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Browscap Drush integration.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_drush_command().
|
||||
*/
|
||||
function browscap_drush_command() {
|
||||
$items['browscap-import'] = array(
|
||||
'description' => 'Imports/updates browscap data via HTTP.',
|
||||
);
|
||||
$items['browscap-get-browser'] = array(
|
||||
'description' => dt('Show browser information for a given user agent.'),
|
||||
'arguments' => array(
|
||||
'user agent' => 'Quote-enclosed user agent',
|
||||
),
|
||||
'required-arguments' => TRUE,
|
||||
'examples' => array("drush browscap-get-browser 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9700; en) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.448 Mobile Safari/534.8+'"),
|
||||
'outputformat' => array(
|
||||
'default' => 'json',
|
||||
),
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports/updates browscap data via HTTP.
|
||||
*/
|
||||
function drush_browscap_import() {
|
||||
drush_log(dt('Importing Browscap data...'), 'ok');
|
||||
switch (_browscap_import()) {
|
||||
case BROWSCAP_IMPORT_VERSION_ERROR:
|
||||
drush_set_error('BROWSCAP', dt('Could not check current available Browscap data version.'));
|
||||
break;
|
||||
|
||||
case BROWSCAP_IMPORT_NO_NEW_VERSION:
|
||||
drush_log(dt('No new version of Browscap data to import.'), 'ok');
|
||||
break;
|
||||
|
||||
case BROWSCAP_IMPORT_DATA_ERROR:
|
||||
drush_set_error('BROWSCAP', dt('Failed to retrieve or load Browscap data.'));
|
||||
break;
|
||||
|
||||
case BROWSCAP_IMPORT_OK:
|
||||
drush_log(dt('Imported Browscap data successfully.'), 'ok');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets browser information for Drush output.
|
||||
*
|
||||
* @param string $user_agent
|
||||
* User agent.
|
||||
*
|
||||
* @return array
|
||||
* An array of information about the user agent.
|
||||
*/
|
||||
function drush_browscap_get_browser($user_agent) {
|
||||
return browscap_get_browser($user_agent);
|
||||
}
|
@@ -3,9 +3,9 @@ description = Provides a replacement for PHPs get_browser() function.
|
||||
core = 7.x
|
||||
configure = admin/config/system/browscap
|
||||
|
||||
; Information added by Drupal.org packaging script on 2014-07-09
|
||||
version = "7.x-2.2"
|
||||
; Information added by Drupal.org packaging script on 2016-03-23
|
||||
version = "7.x-2.3"
|
||||
core = "7.x"
|
||||
project = "browscap"
|
||||
datestamp = "1404915228"
|
||||
datestamp = "1458745750"
|
||||
|
||||
|
@@ -29,14 +29,34 @@ function browscap_schema() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
* Implements hook_requirements().
|
||||
*/
|
||||
function browscap_install() {
|
||||
// Update the browscap information.
|
||||
_browscap_import();
|
||||
function browscap_requirements($phase) {
|
||||
$requirements = array();
|
||||
$t = get_t();
|
||||
if ($phase == 'runtime' && user_access('administer browscap')) {
|
||||
$requirement = array(
|
||||
'value' => variable_get('browscap_version', 0) === 0 ? $t('Not installed') : l(variable_get('browscap_version', 0), 'admin/config/system/browscap'),
|
||||
'title' => $t('Browscap version'),
|
||||
);
|
||||
|
||||
// Record when the browscap information was updated.
|
||||
variable_set('browscap_imported', REQUEST_TIME);
|
||||
if (variable_get('browscap_version', 0) === 0) {
|
||||
$requirement += array(
|
||||
'severity' => REQUIREMENT_ERROR,
|
||||
'description' => $t('Browscap data is not imported! See <a href="!settings_url">Browscap settings</a> to import manually.', array(
|
||||
'!settings_url' => url('admin/config/system/browscap'),
|
||||
)),
|
||||
);
|
||||
}
|
||||
else {
|
||||
$requirement += array(
|
||||
'severity' => REQUIREMENT_OK,
|
||||
);
|
||||
}
|
||||
|
||||
$requirements['browscap_version'] = $requirement;
|
||||
}
|
||||
return $requirements;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -48,21 +48,18 @@ function browscap_help($path, $arg) {
|
||||
* Implements hook_cron().
|
||||
*/
|
||||
function browscap_cron() {
|
||||
if (variable_get('browscap_enable_automatic_updates', TRUE) == TRUE) {
|
||||
if (variable_get('browscap_enable_automatic_updates', FALSE)) {
|
||||
// Check the current update timer.
|
||||
$automatic_update_timer = variable_get('browscap_automatic_updates_timer', 604800);
|
||||
|
||||
// Check when the last update occurred.
|
||||
$last_imported = variable_get('browscap_imported', REQUEST_TIME);
|
||||
$last_imported = variable_get('browscap_imported', 0);
|
||||
|
||||
// Update the browscap data if the amount of time specified by the update
|
||||
// timer has passed.
|
||||
if (($last_imported + $automatic_update_timer) < REQUEST_TIME) {
|
||||
// Update the browscap information.
|
||||
_browscap_import();
|
||||
|
||||
// Record when the browscap information was updated.
|
||||
variable_set('browscap_imported', REQUEST_TIME);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,19 +4,33 @@
|
||||
* Browscap data import functions.
|
||||
*/
|
||||
|
||||
define('BROWSCAP_IMPORT_OK', 0);
|
||||
define('BROWSCAP_IMPORT_VERSION_ERROR', 1);
|
||||
define('BROWSCAP_IMPORT_NO_NEW_VERSION', 2);
|
||||
define('BROWSCAP_IMPORT_DATA_ERROR', 3);
|
||||
|
||||
/**
|
||||
* Helper function to update the browscap data.
|
||||
*
|
||||
* @param bool $cron
|
||||
* Optional import environment. If false, display status messages to the user
|
||||
* in addition to logging information with the watchdog.
|
||||
*
|
||||
* @return int
|
||||
* A code indicating the result:
|
||||
* - BROWSCAP_IMPORT_OK: New data was imported.
|
||||
* - BROWSCAP_IMPORT_NO_NEW_VERSION: No new data version was available.
|
||||
* - BROWSCAP_IMPORT_VERSION_ERROR: Checking the current data version failed.
|
||||
* - BROWSCAP_IMPORT_DATA_ERROR: The data could not be downloaded or parsed.
|
||||
*/
|
||||
function _browscap_import($cron = TRUE) {
|
||||
// Check the local browscap data version number.
|
||||
$local_version = variable_get('browscap_version', 0);
|
||||
|
||||
watchdog('browscap', 'Checking for new browscap version...');
|
||||
|
||||
// Retrieve the current browscap data version number using HTTP.
|
||||
$current_version = drupal_http_request('http://www.browscap.org/version-number');
|
||||
$current_version = drupal_http_request('https://www.browscap.org/version-number');
|
||||
|
||||
// Log an error if the browscap version number could not be retrieved.
|
||||
if (isset($current_version->error)) {
|
||||
@@ -28,7 +42,7 @@ function _browscap_import($cron = TRUE) {
|
||||
drupal_set_message(t("Couldn't check version: %error", array('%error' => $current_version->error)), 'error');
|
||||
}
|
||||
|
||||
return;
|
||||
return BROWSCAP_IMPORT_VERSION_ERROR;
|
||||
}
|
||||
|
||||
// Sanitize the returned version number.
|
||||
@@ -45,7 +59,7 @@ function _browscap_import($cron = TRUE) {
|
||||
drupal_set_message(t('No new version of browscap to import'));
|
||||
}
|
||||
|
||||
return;
|
||||
return BROWSCAP_IMPORT_NO_NEW_VERSION;
|
||||
}
|
||||
|
||||
// Set options for downloading data with or without compression.
|
||||
@@ -61,7 +75,7 @@ function _browscap_import($cron = TRUE) {
|
||||
}
|
||||
|
||||
// Retrieve the browscap data using HTTP.
|
||||
$browscap_data = drupal_http_request('http://www.browscap.org/stream?q=PHP_BrowsCapINI', $options);
|
||||
$browscap_data = drupal_http_request('https://www.browscap.org/stream?q=PHP_BrowsCapINI', $options);
|
||||
|
||||
// Log an error if the browscap data could not be retrieved.
|
||||
if (isset($browscap_data->error) || empty($browscap_data)) {
|
||||
@@ -73,7 +87,7 @@ function _browscap_import($cron = TRUE) {
|
||||
drupal_set_message(t("Couldn't retrieve updated browscap: %error", array('%error' => $browscap_data->error)), 'error');
|
||||
}
|
||||
|
||||
return;
|
||||
return BROWSCAP_IMPORT_DATA_ERROR;
|
||||
}
|
||||
|
||||
// Decompress the downloaded data if it is compressed.
|
||||
@@ -123,46 +137,15 @@ function _browscap_import($cron = TRUE) {
|
||||
// The version information is the first entry in the array.
|
||||
$version = array_shift($browscap_data);
|
||||
|
||||
// Store the data available for each user agent.
|
||||
foreach ($browscap_data as $key => $values) {
|
||||
// Store the current value.
|
||||
$e = $values;
|
||||
|
||||
// Create an array to hold the last parent.
|
||||
$last_parent = array();
|
||||
|
||||
// Recurse through the available user agent information.
|
||||
while (isset($values['Parent']) && $values['Parent'] !== $last_parent) {
|
||||
$values = isset($browscap_data[$values['Parent']]) ? $browscap_data[$values['Parent']] : array();
|
||||
$e = array_merge($values, $e);
|
||||
$last_parent = $values;
|
||||
}
|
||||
|
||||
// Replace '*?' with '%_'.
|
||||
$user_agent = strtr($key, '*?', '%_');
|
||||
|
||||
// Change all array keys to lowercase.
|
||||
$e = array_change_key_case($e);
|
||||
|
||||
// Delete all data about the current user agent from the database.
|
||||
db_delete('browscap')
|
||||
->condition('useragent', $user_agent)
|
||||
->execute();
|
||||
|
||||
// Insert all data about the current user agent into the database.
|
||||
db_insert('browscap')
|
||||
->fields(array(
|
||||
'useragent' => $user_agent,
|
||||
'data' => serialize($e),
|
||||
))
|
||||
->execute();
|
||||
}
|
||||
// Save parsed Browscap data.
|
||||
_browscap_save_parsed_data($browscap_data);
|
||||
|
||||
// Clear the browscap data cache.
|
||||
cache_clear_all('*', 'cache_browscap', TRUE);
|
||||
|
||||
// Update the browscap version.
|
||||
// Update the browscap version and imported time.
|
||||
variable_set('browscap_version', $current_version);
|
||||
variable_set('browscap_imported', REQUEST_TIME);
|
||||
|
||||
// Log a message with the watchdog.
|
||||
watchdog('browscap', 'New version of browscap imported: %version', array('%version' => $current_version));
|
||||
@@ -171,5 +154,82 @@ function _browscap_import($cron = TRUE) {
|
||||
if ($cron == FALSE) {
|
||||
drupal_set_message(t('New version of browscap imported: %version', array('%version' => $current_version)));
|
||||
}
|
||||
|
||||
return BROWSCAP_IMPORT_OK;
|
||||
}
|
||||
|
||||
return BROWSCAP_IMPORT_DATA_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves parsed Browscap data.
|
||||
*
|
||||
* The purpose of this function is to perform the queries on the {browscap}
|
||||
* table as a transaction. This vastly improves performance with database
|
||||
* engines such as InnoDB and ensures that queries will work while new data
|
||||
* is being imported.
|
||||
*
|
||||
* @param array $browscap_data
|
||||
* Browscap data that has been parsed with parse_ini_string() or
|
||||
* parse_ini_file().
|
||||
*/
|
||||
function _browscap_save_parsed_data(&$browscap_data) {
|
||||
// Start a transaction. The variable is unused. That's on purpose.
|
||||
$transaction = db_transaction();
|
||||
|
||||
// Delete all data from database.
|
||||
db_delete('browscap')->execute();
|
||||
|
||||
// Prepare the data for insertion.
|
||||
$import_data = array();
|
||||
foreach ($browscap_data as $key => $values) {
|
||||
// Store the current value.
|
||||
$original_values = $values;
|
||||
|
||||
// Replace '*?' with '%_'.
|
||||
$user_agent = strtr($key, '*?', '%_');
|
||||
|
||||
// Remove trailing spaces to prevent "duplicate entry" errors. Databases
|
||||
// such as MySQL do not preserve trailing spaces when storing VARCHARs.
|
||||
$user_agent = rtrim($user_agent);
|
||||
|
||||
// Change all array keys to lowercase.
|
||||
$original_values = array_change_key_case($original_values);
|
||||
|
||||
// Add to array of data to import.
|
||||
$import_data[$user_agent] = $original_values;
|
||||
|
||||
// Remove processed data to reduce memory usage.
|
||||
unset($browscap_data[$key]);
|
||||
}
|
||||
|
||||
// Get user agents to insert, removing case-insensitive duplicates.
|
||||
$user_agents = array_keys($import_data);
|
||||
$user_agents = array_intersect_key($user_agents, array_unique(array_map('strtolower', $user_agents)));
|
||||
|
||||
// Insert all data about user agents into database in chunks.
|
||||
$user_agents_chunks = array_chunk($user_agents, 1000);
|
||||
foreach ($user_agents_chunks as $user_agents_chunk) {
|
||||
$query = db_insert('browscap')
|
||||
->fields(array('useragent', 'data'));
|
||||
foreach ($user_agents_chunk as $user_agent) {
|
||||
$values = $import_data[$user_agent];
|
||||
|
||||
// Recurse through the available user agent information.
|
||||
$previous_parent = NULL;
|
||||
$parent = isset($values['parent']) ? $values['parent'] : FALSE;
|
||||
while ($parent && $parent !== $previous_parent) {
|
||||
$parent_values = isset($import_data[$parent]) ? $import_data[$parent] : array();
|
||||
$values = array_merge($parent_values, $values);
|
||||
$previous_parent = $parent;
|
||||
$parent = isset($parent_values['parent']) ? $parent_values['parent'] : FALSE;
|
||||
}
|
||||
|
||||
$query->values(array(
|
||||
'useragent' => $user_agent,
|
||||
'data' => serialize($values),
|
||||
));
|
||||
}
|
||||
$query->execute();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user