[icinga-checkins] icinga.org: icinga-web/next: Migrate old filter to new ones

git at icinga.org git at icinga.org
Thu Apr 18 15:57:16 CEST 2013


Module: icinga-web
Branch: next
Commit: e3e04331874afbbf9bf2dcdc7a690c691117a3b0
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=e3e04331874afbbf9bf2dcdc7a690c691117a3b0

Author: Marius Hein <marius.hein at netways.de>
Date:   Thu Apr 18 15:53:24 2013 +0200

Migrate old filter to new ones

Implements phing build task which uses old filter
structs in state of custom cronks and convert it
into new format (refs #3260). So filter can be
used beyond version.

fixes #3981

---

 etc/build.xml                      |    5 ++
 etc/lib/CronkStruct.php            |   96 ++++++++++++++++++++++++++++++++++-
 etc/lib/CronkUpgradeFilterTask.php |   42 ++++++++++++++++
 3 files changed, 140 insertions(+), 3 deletions(-)

diff --git a/etc/build.xml b/etc/build.xml
index 456cd40..ff1c31c 100644
--- a/etc/build.xml
+++ b/etc/build.xml
@@ -158,4 +158,9 @@
         <taskdef name="cronkdroplayout" classname="etc.lib.CronkDropLayoutTask"/>
         <cronkdroplayout />
     </target>
+
+    <target name="upgrade-cronk-filter">
+        <taskdef name="cronkupgradefilter" classname="etc.lib.CronkUpgradeFilterTask" />
+        <cronkupgradefilter />
+    </target>
 </project>
diff --git a/etc/lib/CronkStruct.php b/etc/lib/CronkStruct.php
index ffcde93..8343e90 100644
--- a/etc/lib/CronkStruct.php
+++ b/etc/lib/CronkStruct.php
@@ -37,7 +37,7 @@ class CronkStruct {
 
     /**
      * Json state data
-     * @var stdClass
+     * @var array
      */
     private $json;
 
@@ -59,6 +59,13 @@ class CronkStruct {
      */
     private $name;
 
+    private static $operatorMap = array(
+        // AppKitSQLConstants::SQL_OP_IS -> Special case because IS can be 'is' or '='
+        AppKitSQLConstants::SQL_OP_CONTAIN      => 'contain',
+        AppKitSQLConstants::SQL_OP_GREATERTHAN  => '>',
+        AppKitSQLConstants::SQL_OP_LESSTHAN     => '<'
+    );
+
     public function __construct(Cronk $record=null)
     {
         $this->dom = new DOMDocument('1.0', 'utf-8');
@@ -182,7 +189,7 @@ class CronkStruct {
 
     /**
      * Returns parameter value from xml
-     * @param $name Parameter name
+     * @param string $name Parameter name
      * @return null|string
      */
     public function getXmlParam($name)
@@ -394,4 +401,87 @@ class CronkStruct {
         unset($this->json['colModel']);
         unset($this->json['nativeState']);
     }
-}
\ No newline at end of file
+
+    /**
+     * Converter for array data
+     *
+     * Takes the two array from JSON state as arguments and
+     * returns a ready to use array for new json filter
+     * formats
+     *
+     * @param array $params Filter struct 'filter_params'
+     * @param array $types Filter struct 'filter_types#
+     * @return array Array for new filter feature
+     */
+    private function normalizeFilterData(array $params, array $types)
+    {
+        $struct = array();
+        $m = array();
+        $key = null;
+
+        foreach ($params as $key => $value) {
+            if (preg_match('/^f\[([^-]+)-(value|operator)\]$/', $key, $m)) {
+                if (!isset($struct[$m[1]])) {
+                    $struct[$m[1]] = array();
+                }
+
+                if ($m[2] === 'operator') {
+                    $value = (int)$value;
+                }
+
+                $struct[$m[1]][$m[2]] = $value;
+            }
+        }
+
+        foreach ($struct as $key => &$values) {
+            if ($values['operator'] === AppKitSQLConstants::SQL_OP_IS) {
+                if (is_numeric($values['value'])) {
+                    $values['operator'] = '=';
+                } else {
+                    $values['operator'] = 'is';
+                }
+            } elseif (array_key_exists($values['operator'], self::$operatorMap)) {
+                $values['operator'] = self::$operatorMap[$values['operator']];
+            } else {
+                /*
+                 * Fallback always contain if we can not find an appropriate operator
+                 */
+                $values['operator'] = 'contain';
+            }
+
+            if (array_key_exists($key, $types) && isset($types[$key]['fLabel'])) {
+                $values['label'] = $types[$key]['fLabel'];
+            } else {
+                $values['label'] = $key;
+            }
+
+            $values['field'] = $key;
+        }
+        return $struct;
+    }
+
+    /**
+     * Upgrade old filter structs
+     *
+     * Convert old filter format from custom cronks
+     * into the new superduper format with AND and OR
+     * and things like that
+     */
+    public function upgradeFilter()
+    {
+        if (array_key_exists('filter', $this->json) === false && array_key_exists('filter_params', $this->json)) {
+            $params = $this->normalizeFilterData(
+                $this->json['filter_params'],
+                $this->json['filter_types']
+            );
+
+            $jsonStruct = array(
+                'AND' => array_values($params)
+            );
+
+            $this->json['filter'] = json_encode($jsonStruct);
+            unset($this->json['filter_params']);
+            unset($this->json['filter_types']);
+        }
+    }
+}
diff --git a/etc/lib/CronkUpgradeFilterTask.php b/etc/lib/CronkUpgradeFilterTask.php
new file mode 100644
index 0000000..820e25f
--- /dev/null
+++ b/etc/lib/CronkUpgradeFilterTask.php
@@ -0,0 +1,42 @@
+<?php
+// {{{ICINGA_LICENSE_CODE}}}
+// -----------------------------------------------------------------------------
+// This file is part of icinga-web.
+// 
+// Copyright (c) 2009-2013 Icinga Developer Team.
+// All rights reserved.
+// 
+// icinga-web is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// 
+// icinga-web is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with icinga-web.  If not, see <http://www.gnu.org/licenses/>.
+// -----------------------------------------------------------------------------
+// {{{ICINGA_LICENSE_CODE}}}
+
+require 'CronkUpgradeAbstract.php';
+
+/**
+ * Class cronkUpgradeTask
+ *
+ * Specific caller class to drop state from cronks
+ */
+class CronkUpgradeFilterTask extends CronkUpgradeAbstract {
+
+    /**
+     * Drops the layout state from each cronk
+     *
+     * @param CronkStruct $struct
+     * @return mixed|void
+     */
+    protected function upgradeMethod(CronkStruct $struct) {
+        $struct->upgradeFilter();
+    }
+}





More information about the icinga-checkins mailing list