(dev/drupal/10) Make civicrm-version.php self-sufficient to fetch Civi version and...
authordeb.monish <monish.deb@jmaconsulting.biz>
Sun, 22 Apr 2018 18:23:30 +0000 (23:53 +0530)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Thu, 3 May 2018 07:56:27 +0000 (13:26 +0530)
.gitignore
CRM/Core/CodeGen/Main.php
CRM/Core/CodeGen/Version.php [deleted file]
CRM/Utils/System.php
civicrm-version.php [new file with mode: 0755]

index f7aa94f6599339f81e8ea8ee15b0ab86f6578605..4e204a5b202cbd51a87a7ac5488723ef064993a9 100644 (file)
@@ -7,7 +7,6 @@ CRM/Case/xml/configuration
 CRM/Core/DAO/.listAll.php
 CRM/Core/DAO/listAll.php
 bin/setup.conf
-civicrm-version.php
 civicrm-version.txt
 civicrm.config.php
 node_modules
index f85deb401bbe8d6bdf8eedae48409d60d9c2f6ce..9edd7b7818179acc929e796120835212ee1d7223 100644 (file)
@@ -110,7 +110,6 @@ Alternatively you can get a version of CiviCRM that matches your PHP version
 
     $tasks = array();
     $tasks[] = new CRM_Core_CodeGen_Config($this);
-    $tasks[] = new CRM_Core_CodeGen_Version($this);
     $tasks[] = new CRM_Core_CodeGen_Reflection($this);
     $tasks[] = new CRM_Core_CodeGen_Schema($this);
     foreach (array_keys($this->tables) as $name) {
diff --git a/CRM/Core/CodeGen/Version.php b/CRM/Core/CodeGen/Version.php
deleted file mode 100644 (file)
index e98e38e..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<?php
-
-/**
- * Generate configuration files
- */
-class CRM_Core_CodeGen_Version extends CRM_Core_CodeGen_BaseTask {
-  public function run() {
-    echo "Generating civicrm-version file\n";
-
-    $template = new CRM_Core_CodeGen_Util_Template('php');
-    $template->assign('db_version', $this->config->db_version);
-    $template->assign('cms', ucwords($this->config->cms));
-    $template->run('civicrm_version.tpl', $this->config->phpCodePath . "civicrm-version.php");
-  }
-
-}
index e2d72f5303244d9eb8c031c82696994bedd3f47f..394dde66392b3f2ca956fb60eb1f092e8abe017d 100644 (file)
@@ -1081,14 +1081,9 @@ class CRM_Utils_System {
     static $version;
 
     if (!$version) {
-      $verFile = implode(DIRECTORY_SEPARATOR,
-        array(dirname(__FILE__), '..', '..', 'xml', 'version.xml')
-      );
-      if (file_exists($verFile)) {
-        $str = file_get_contents($verFile);
-        $xmlObj = simplexml_load_string($str);
-        $version = (string) $xmlObj->version_no;
-      }
+      $verFile = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', '..', 'civicrm-version.php']);
+      require_once $verFile;
+      $version = \Civi\Version::findVersion();
 
       // pattern check
       if (!CRM_Utils_System::isVersionFormatValid($version)) {
diff --git a/civicrm-version.php b/civicrm-version.php
new file mode 100755 (executable)
index 0000000..fccb203
--- /dev/null
@@ -0,0 +1,112 @@
+<?php
+
+namespace Civi;
+
+class Version {
+
+  /**
+   * Get the CiviCRM version
+   */
+  public static function findVersion() {
+    $verFile = implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), 'xml', 'version.xml']);
+    if (file_exists($verFile)) {
+      $str = file_get_contents($verFile);
+      $xmlObj = simplexml_load_string($str);
+      return (string) $xmlObj->version_no;
+    }
+
+    trigger_error("Unknown version", E_USER_ERROR);
+    exit();
+  }
+
+  /**
+   * Get the CMS name
+   */
+  public static function findCMS() {
+    if (defined('CIVICRM_UF')) {
+      return CIVICRM_UF;
+    }
+    elseif (function_exists('drupal_bootstrap') && version_compare(VERSION, '6.0', '>=') && version_compare(VERSION, '7.0', '<')) {
+      return 'Drupal6';
+    }
+    elseif (function_exists('drupal_bootstrap') && version_compare(VERSION, '7.0', '>=') && version_compare(VERSION, '8.0', '<')) {
+      return 'Drupal';
+    }
+    else {
+      // guess CMS name from the current path
+      list($cmsType,) = self::findCMSRootPath();
+
+      if (!empty($cmsType)) {
+        return $cmsType;
+      }
+    }
+  }
+
+  /**
+   * Get the CMS root path and CMS name
+   */
+  public static function findCMSRootPath() {
+    $cmsPatterns = array(
+      'Wordpress' => array(
+        'wp-includes/version.php',
+        // Future? 'vendor/civicrm/wordpress/civicrm.php' => 'wp',
+      ),
+      'Joomla' => array(
+        'administrator/components/com_civicrm/civicrm/civicrm-version.php',
+      ),
+      'Drupal' => array(
+        'modules/system/system.module', // D7
+      ),
+      'Drupal8' => array(
+        'core/core.services.yml', // D8
+      ),
+      'Backdrop' => array(
+        'core/modules/layout/layout.module',
+      ),
+    );
+
+    $parts = explode('/', str_replace('\\', '/', self::getSearchDir()));
+    while (!empty($parts)) {
+      $basePath = implode('/', $parts);
+
+      foreach ($cmsPatterns as $cmsType => $relPaths) {
+        foreach ($relPaths as $relPath) {
+          $matches = glob("$basePath/$relPath");
+          if (!empty($matches)) {
+            return [$cmsType, $basePath];
+          }
+        }
+      }
+
+      array_pop($parts);
+    }
+  }
+
+  /**
+   * Get the current path
+   */
+  public static function getSearchDir() {
+    // getenv('PWD') works better with symlinked source trees, but it's
+    // not portable to Windows.
+    if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
+      return getcwd();
+    }
+    else {
+      return getenv('PWD');
+    }
+  }
+
+}
+
+/**
+ * Get the CiviCRM version.
+ * TODO : For now this function is not included in \Civi\Version class so not to break any code
+ *   which directly call civicrmVersion(). So those call need to replaced with \Civi\Version::civicrmVersion()
+ *   when included in the class
+ */
+function civicrmVersion() {
+  return [
+    'version' => \Civi\Version::findVersion(),
+    'cms' => \Civi\Version::findCMS(),
+  ];
+}