From a67d2577941e1b2b00fb1583e8bde4dc944794ae Mon Sep 17 00:00:00 2001
From: Jimmy Berry <jimmy@boombatower.com>
Date: Wed, 13 Apr 2011 05:33:56 -0500
Subject: Support libraries API.

---
 geshifilter.admin.inc     |   39 ++++------------------------
 geshifilter.conflicts.inc |    8 +++---
 geshifilter.inc           |   61 ++------------------------------------------
 geshifilter.info          |    3 +-
 geshifilter.install       |    9 ++++++-
 geshifilter.module        |   34 +++++++++++++++++++++++--
 geshifilter.pages.inc     |   10 +++---
 7 files changed, 59 insertions(+), 105 deletions(-)

diff --git a/geshifilter.admin.inc b/geshifilter.admin.inc
index 1ddd524..29da716 100644
--- a/geshifilter.admin.inc
+++ b/geshifilter.admin.inc
@@ -58,7 +58,7 @@ function geshifilter_admin_general_settings($form, &$form_state) {
   $form = array();
 
   // Try to load GeSHi library and get version if successful.
-  $geshi_library = _geshifilter_check_geshi_library();
+  $geshi_library = libraries_load('geshi');
 
   // GeSHi library settings (constant GESHI_VERSION is defined in GeSHi library)
   $form['geshifilter_library'] = array(
@@ -67,15 +67,8 @@ function geshifilter_admin_general_settings($form, &$form_state) {
     '#description' => t('The GeSHi filter requires the GeSHi library (which needs to be <a href="!geshi">downloaded</a> and installed seperately).',
       array('!geshi' => url('http://qbnz.com/highlighter/'))),
     '#collapsible' => TRUE,
-    '#collapsed' => $geshi_library['success'],
+    '#collapsed' => $geshi_library['loaded'],
   );
-  $form['geshifilter_library']['geshifilter_geshi_dir'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Path to GeSHi library'),
-    '#default_value' => _geshifilter_get_geshi_dir(),
-    '#description' => t('Specify the path to the GeSHi library directory (which contains a file called <em>geshi.php</em>).')
-  );
-  $form['geshifilter_library']['#after_build'] = array('geshifilter_admin_general_settings_after_build');
 
   // If the GeSHi library is loaded, show all the options and settings.
   if ($geshi_library['loaded']) {
@@ -233,17 +226,6 @@ function geshifilter_admin_general_settings($form, &$form_state) {
 }
 
 /**
- * After build function for admin settings
- */
-function geshifilter_admin_general_settings_after_build($form, &$form_state) {
-  $geshi_library = _geshifilter_check_geshi_library(FALSE, $form_state['values']['geshifilter_geshi_dir'], FALSE);
-  if (!$geshi_library['success']) {
-    form_set_error('geshifilter_geshi_dir', $geshi_library['message']);
-  }
-  return $form;
-}
-
-/**
  * Validate function for admin settings
  */
 function geshifilter_admin_general_settings_validate($form, &$form_state) {
@@ -257,15 +239,6 @@ function geshifilter_admin_general_settings_validate($form, &$form_state) {
  * Submit function for admin settings
  */
 function geshifilter_admin_general_settings_submit($form, &$form_state) {
-  // Cache of available languages should be cleared when GeSHi library dir changes
-  // _geshifilter_get_geshi_dir() returns the new value set by
-  // system_settings_form_submit() which runs before
-  // geshifilter_admin_general_settings_submit(),
-  // $form['geshifilter_library']['geshifilter_geshi_dir']['#default_value'] is
-  // the old value
-  if (_geshifilter_get_geshi_dir() != $form['geshifilter_library']['geshifilter_geshi_dir']['#default_value']) {
-    variable_del('geshifilter_available_languages_cache');
-  }
   // Regenerate language_css
   // Note the use of variable_get('geshifilter_css_mode') which is alread set
   // to $form_state['values']['geshifilter_css_mode'] by
@@ -283,9 +256,9 @@ function geshifilter_admin_general_settings_submit($form, &$form_state) {
 function geshifilter_admin_per_language_settings($form_state, $view = 'enabled') {
   $form = array();
   // check if GeSHi library is available
-  $geshi_library = _geshifilter_check_geshi_library();
-  if (!$geshi_library['success']) {
-    drupal_set_message($geshi_library['message'], 'error');
+  $geshi_library = libraries_load('geshi');
+  if (!$geshi_library['loaded']) {
+    drupal_set_message($geshi_library['error message'], 'error');
     return;
   }
   $add_checkbox = TRUE;
@@ -549,7 +522,7 @@ function _geshifilter_clear_filter_cache() {
  */
 function _geshifilter_generate_languages_css_rules() {
   $output = '';
-  $geshi_library = _geshifilter_check_geshi_library();
+  $geshi_library = libraries_load('geshi');
   if ($geshi_library['loaded']) {
     require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.pages.inc';
     $languages = _geshifilter_get_enabled_languages();
diff --git a/geshifilter.conflicts.inc b/geshifilter.conflicts.inc
index 1890449..13b2d1a 100644
--- a/geshifilter.conflicts.inc
+++ b/geshifilter.conflicts.inc
@@ -12,14 +12,14 @@ require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.inc';
  */
 function geshifilter_admin_filter_conflicts($check_only=FALSE) {
   global $user;
-  
+
   // start
   $output = '';
   // check if GeSHi library is available
-  $geshi_library = _geshifilter_check_geshi_library();
-  if (!$geshi_library['success']) {
+  $geshi_library = libraries_load('geshi');
+  if (!$geshi_library['loaded']) {
     if (!$check_only) {
-      drupal_set_message($geshi_library['message'], 'error');
+      drupal_set_message($geshi_library['error message'], 'error');
     }
     return $output;
   }
diff --git a/geshifilter.inc b/geshifilter.inc
index c75fb60..c3b1c96 100644
--- a/geshifilter.inc
+++ b/geshifilter.inc
@@ -5,61 +5,6 @@
  * General GeSHi filter helper functions.
  */
 
-function _geshifilter_get_geshi_dir() {
-  // TODO: change the default value to something like 'sites/all/libraries/geshi' (possibly using the Libraries API module)
-  return variable_get('geshifilter_geshi_dir', drupal_get_path('module', 'geshifilter') .'/geshi');
-}
-
-/**
- * Helper function for loading/checking the GeSHi library v 1.0.x (if not already)
- * Returns an array with keys 'success', 'loaded' and 'message'
- */
-function _geshifilter_check_geshi_library($use_cache=TRUE, $geshi_dir=NULL, $load_when_found=TRUE) {
-  static $geshi_library_cache = NULL;
-  if ($use_cache && $geshi_library_cache !== NULL) {
-    // get from cache
-    $geshi_library = $geshi_library_cache;
-  }
-  else {
-    // initialisation
-    $geshi_library = array('success' => NULL, 'loaded' => FALSE, 'message' => NULL);
-    // no cache
-    if (!$geshi_dir) {
-      $geshi_dir = _geshifilter_get_geshi_dir();
-    }
-    if (!is_dir($geshi_dir)) {
-      $geshi_library['success'] = FALSE;
-      $geshi_library['message'] = t('GeSHi library error: %dir is not a directory.', array('%dir' => $geshi_dir));
-    }
-    elseif (is_file($geshi_dir .'/geshi.php')) {
-      // GeSHi 1.0.x found (probably, we can only be sure by loading it)
-      $geshi_library['success'] = TRUE;
-      if ($load_when_found) {
-        require_once($geshi_dir .'/geshi.php');
-        // check version
-        $geshi_library_version = explode('.', GESHI_VERSION);
-        if (!($geshi_library_version[0] == '1' &&  $geshi_library_version[1] == '0')) {
-          $geshi_library['success'] = FALSE;
-          $geshi_library['loaded'] = FALSE;
-          $geshi_library['message'] = t('GeSHi library error: The detected version of GeSHi library (%version) is not supported. A version from the 1.0.x branch is required.', array('%version' => GESHI_VERSION));
-        }
-        else {
-          $geshi_library['loaded'] = TRUE;
-        }
-      }
-    }
-    else {
-      $geshi_library['success'] = FALSE;
-      $geshi_library['message'] = t('GeSHi library error: Could not find a known version of the GeSHi library in directory %dir.' , array('%dir' => $geshi_dir));
-    }
-    // store in cache if needed
-    if ($use_cache) {
-      $geshi_library_cache = $geshi_library;
-    }
-  }
-  return $geshi_library;
-}
-
 /**
  * List of available languages.
  * @return an array mapping language code to array with the language path and full language name
@@ -69,10 +14,10 @@ function _geshifilter_get_available_languages() {
   $available_languages = variable_get('geshifilter_available_languages_cache', NULL);
   if ($available_languages === NULL) {
     // not in cache: build the array of available_languages
-    $geshi_library = _geshifilter_check_geshi_library();
+    $geshi_library = libraries_load('geshi');
     $available_languages = array();
-    if ($geshi_library['success']) {
-      $dirs = array(_geshifilter_get_geshi_dir() .'/geshi', drupal_get_path('module', 'geshifilter') .'/geshi-extra');
+    if ($geshi_library['loaded']) {
+      $dirs = array($geshi_library['library path'] .'/geshi', drupal_get_path('module', 'geshifilter') .'/geshi-extra');
       foreach ($dirs as $dir) {
         foreach (file_scan_directory($dir, '/.[pP][hH][pP]$/i') as $filename => $fileinfo) {
           // short name
diff --git a/geshifilter.info b/geshifilter.info
index 2417f76..2501851 100644
--- a/geshifilter.info
+++ b/geshifilter.info
@@ -2,4 +2,5 @@ name = "GeSHi Filter"
 description = "Provides a filter to highlight source code using the GeSHi library (Generic Syntax Highlighter)"
 package = "Filters"
 core = 7.x
-files[] = geshifilter.test
\ No newline at end of file
+files[] = geshifilter.test
+dependencies[] = libraries
diff --git a/geshifilter.install b/geshifilter.install
index 9d5b8b8..1486f13 100644
--- a/geshifilter.install
+++ b/geshifilter.install
@@ -28,4 +28,11 @@ function geshifilter_install() {
 function geshifilter_uninstall() {
   db_query("DELETE FROM {variable} WHERE name LIKE 'geshifilter_%'");
   cache_clear_all('variables', 'cache');
-}
\ No newline at end of file
+}
+
+/**
+ * Remove 'geshifilter_geshi_dir' since it is replaced by libraries API.
+ */
+function geshifilter_update_7100() {
+  variable_del('geshifilter_geshi_dir');
+}
diff --git a/geshifilter.module b/geshifilter.module
index 708c49a..c118614 100644
--- a/geshifilter.module
+++ b/geshifilter.module
@@ -228,13 +228,19 @@ function geshifilter_requirements($phase) {
   if ($phase == 'runtime') {
     require_once drupal_get_path('module', 'geshifilter') .'/geshifilter.inc';
     // check if GeSHi library is available
-    $geshi_library = _geshifilter_check_geshi_library();
+    $geshi_library = libraries_load('geshi');
     if (!$geshi_library['loaded']) {
       $requirements[] = array(
         'title' => 'GeSHi filter',
         'value' => t('GeSHi library not found.'),
-        'description' => t('You should install the GeSHi library and set its path in the <a href="!geshisettings">GeSHi settings</a>.',
-          array('!geshisettings' => url('admin/config/content/geshifilter'))),
+        'severity' => REQUIREMENT_ERROR,
+      );
+    }
+    elseif (($version = explode('.', GESHI_VERSION)) && !($version[0] == '1' && $version[1] == '0')) {
+      $requirements[] = array(
+        'title' => 'GeSHi filter',
+        'value' => t('GeSHi library invalid version.'),
+        'description' => t('The detected version of GeSHi library (%version) is not supported. A version from the 1.0.x branch is required.', array('%version' => GESHI_VERSION)),
         'severity' => REQUIREMENT_ERROR,
       );
     }
@@ -272,6 +278,28 @@ function geshifilter_requirements($phase) {
   return $requirements;
 }
 
+/**
+ * Implements hook_libraries_info()
+ */
+function geshifilter_libraries_info() {
+  return array(
+    'geshi' => array(
+      'title' => 'GeSHi - Generic Syntax Highlighter for PHP',
+      'vendor url' => 'http://sourceforge.net/projects/geshi',
+      'download url' => 'http://sourceforge.net/projects/geshi/files/geshi',
+      'version arguments' => array(
+        'file' => 'geshi.php',
+        'pattern' => "/define\('GESHI_VERSION', '(.*)'\);/",
+        'lines' => 50,
+      ),
+      'files' => array(
+        'php' => array(
+          'geshi.php',
+        ),
+      ),
+    ),
+  );
+}
 
 /**
  * Helper function for checking if an automatically managed style sheet is possible
diff --git a/geshifilter.pages.inc b/geshifilter.pages.inc
index 7b3e9c1..1b7a935 100644
--- a/geshifilter.pages.inc
+++ b/geshifilter.pages.inc
@@ -211,9 +211,9 @@ function _geshifilter_prepare_php_callback($match) {
  */
 function _geshifilter_process($format, $text) {
   // load GeSHi library (if not already)
-  $geshi_library = _geshifilter_check_geshi_library();
-  if (!$geshi_library['success']) {
-    drupal_set_message($geshi_library['message'], 'error');
+  $geshi_library = libraries_load('geshi');
+  if (!$geshi_library['loaded']) {
+    drupal_set_message($geshi_library['error message'], 'error');
     return $text;
   }
   // get the available tags
@@ -325,9 +325,9 @@ function geshifilter_process_sourcecode($source_code, $lang, $line_numbering=0,
  */
 function geshifilter_geshi_process($source_code, $lang, $line_numbering=0, $linenumbers_start=1, $inline_mode=FALSE, $title = NULL) {
   // load GeSHi library (if not already)
-  $geshi_library = _geshifilter_check_geshi_library();
+  $geshi_library = libraries_load('geshi');
   if (!$geshi_library['loaded']) {
-    drupal_set_message($geshi_library['message'], 'error');
+    drupal_set_message($geshi_library['error message'], 'error');
     return $source_code;
   }
 
-- 
1.7.4.2

