From 4ab35bb24ca01bccb7d0bfd68c737bb0fcd1b4e8 Mon Sep 17 00:00:00 2001
From: mattab <matthieu.aubry@gmail.com>
Date: Tue, 18 Jun 2013 17:58:32 +1200
Subject: [PATCH] Moving some logic to Piwik_DataAccess_ArchiveTableCreator

---
 core/DataAccess/ArchiveTableCreator.php       | 40 +++++++++++++++++++
 core/Piwik.php                                | 17 --------
 plugins/CoreAdminHome/API.php                 |  7 +---
 plugins/CoreAdminHome/CoreAdminHome.php       |  4 +-
 plugins/Installation/Controller.php           |  7 ++--
 plugins/PrivacyManager/ReportsPurger.php      | 35 +++++++---------
 .../Benchmarks/ArchiveQueryBenchmark.php      |  2 +-
 tests/PHPUnit/IntegrationTestCase.php         |  2 +-
 tests/PHPUnit/Plugins/PrivacyManagerTest.php  |  2 +-
 9 files changed, 65 insertions(+), 51 deletions(-)

diff --git a/core/DataAccess/ArchiveTableCreator.php b/core/DataAccess/ArchiveTableCreator.php
index c932639fdb..cdd7ad19af 100644
--- a/core/DataAccess/ArchiveTableCreator.php
+++ b/core/DataAccess/ArchiveTableCreator.php
@@ -69,4 +69,44 @@ class Piwik_DataAccess_ArchiveTableCreator
     {
         self::$tablesAlreadyInstalled = Piwik::getTablesInstalled($forceReload);
     }
+
+    /**
+     * Returns all table names archive_*
+     *
+     * @return array
+     */
+    static public function getTablesArchivesInstalled()
+    {
+        if (is_null(self::$tablesAlreadyInstalled)) {
+            self::refreshTableList();
+        }
+
+        $archiveTables = array();
+        foreach (self::$tablesAlreadyInstalled as $table) {
+            if (strpos($table, 'archive_numeric_') !== false
+                || strpos($table, 'archive_blob_') !== false ) {
+                $archiveTables[] = $table;
+            }
+        }
+        return $archiveTables;
+    }
+
+    static public function getDateFromTableName($tableName)
+    {
+        $tableName = Piwik_Common::unprefixTable($tableName);
+        $date = str_replace(array('archive_numeric_', 'archive_blob_'), '', $tableName);
+        return $date;
+    }
+
+    static public function getTypeFromTableName($tableName)
+    {
+        if(strpos('archive_numeric_', $tableName) !== false) {
+            return self::NUMERIC_TABLE;
+        }
+        if(strpos('archive_blob_', $tableName) !== false) {
+            return self::BLOB_TABLE;
+        }
+        return false;
+    }
+
 }
\ No newline at end of file
diff --git a/core/Piwik.php b/core/Piwik.php
index 2b082b1d22..b21b501342 100644
--- a/core/Piwik.php
+++ b/core/Piwik.php
@@ -2246,23 +2246,6 @@ class Piwik
         return Piwik_Db_Schema::getInstance()->getTablesInstalled($forceReload);
     }
 
-    /**
-     * Returns all table names archive_*
-     *
-     * @return array
-     */
-    static public function getTablesArchivesInstalled()
-    {
-        $archiveTables = array();
-        $tables = Piwik::getTablesInstalled();
-        foreach ($tables as $table) {
-            if (strpos($table, 'archive_') !== false) {
-                $archiveTables[] = $table;
-            }
-        }
-        return $archiveTables;
-    }
-
     /**
      * Batch insert into table from CSV (or other delimited) file.
      *
diff --git a/plugins/CoreAdminHome/API.php b/plugins/CoreAdminHome/API.php
index eded6d451f..e17fb28467 100644
--- a/plugins/CoreAdminHome/API.php
+++ b/plugins/CoreAdminHome/API.php
@@ -92,9 +92,6 @@ class Piwik_CoreAdminHome_API
             }
         }
 
-        // Lookup archive tables
-        $tables = Piwik::getTablesInstalled();
-        $archiveTables = Piwik::getTablesArchivesInstalled();
 
         // If using the feature "Delete logs older than N days"...
         $logsAreDeletedBeforeThisDate = Piwik_Config::getInstance()->Deletelogs['delete_logs_schedule_lowest_interval'];
@@ -144,10 +141,10 @@ class Piwik_CoreAdminHome_API
 
         // In each table, invalidate day/week/month/year containing this date
         $sqlIdSites = implode(",", $idSites);
+        $archiveTables = Piwik_DataAccess_ArchiveTableCreator::getTablesArchivesInstalled();
         foreach ($archiveTables as $table) {
             // Extract Y_m from table name
-            $suffix = str_replace(array('archive_numeric_', 'archive_blob_'), '', Piwik_Common::unprefixTable($table));
-
+            $suffix = Piwik_DataAccess_ArchiveTableCreator::getDateFromTableName($table);
             if (!isset($datesByMonth[$suffix])) {
                 continue;
             }
diff --git a/plugins/CoreAdminHome/CoreAdminHome.php b/plugins/CoreAdminHome/CoreAdminHome.php
index ee80acceba..1711a02fc7 100644
--- a/plugins/CoreAdminHome/CoreAdminHome.php
+++ b/plugins/CoreAdminHome/CoreAdminHome.php
@@ -111,7 +111,7 @@ class Piwik_CoreAdminHome extends Piwik_Plugin
 
     function purgeOutdatedArchives()
     {
-        $archiveTables = Piwik::getTablesArchivesInstalled();
+        $archiveTables = Piwik_DataAccess_ArchiveTableCreator::getTablesArchivesInstalled();
         foreach ($archiveTables as $table) {
             if (strpos($table, 'numeric') !== false) {
                 Piwik_ArchiveProcessor_Rules::shouldPurgeOutdatedArchives($table);
@@ -121,7 +121,7 @@ class Piwik_CoreAdminHome extends Piwik_Plugin
 
     function optimizeArchiveTable()
     {
-        $archiveTables = Piwik::getTablesArchivesInstalled();
+        $archiveTables = Piwik_DataAccess_ArchiveTableCreator::getTablesArchivesInstalled();
         Piwik_OptimizeTables($archiveTables);
     }
 }
diff --git a/plugins/Installation/Controller.php b/plugins/Installation/Controller.php
index 153991a5d3..253d62dda2 100644
--- a/plugins/Installation/Controller.php
+++ b/plugins/Installation/Controller.php
@@ -253,19 +253,20 @@ class Piwik_Installation_Controller extends Piwik_Controller_Admin
         }
 
         $tablesInstalled = Piwik::getTablesInstalled();
-        $tablesToInstall = Piwik::getTablesNames();
         $view->tablesInstalled = '';
         if (count($tablesInstalled) > 0) {
             // we have existing tables
             $view->tablesInstalled = implode(', ', $tablesInstalled);
             $view->someTablesInstalled = true;
 
+            // remove monthly archive tables
+            $archiveTables = Piwik_DataAccess_ArchiveTableCreator::getTablesArchivesInstalled();
+            $baseTablesInstalled = count($tablesInstalled) - count($archiveTables);
             $minimumCountPiwikTables = 17;
-            $baseTablesInstalled = preg_grep('/archive_numeric|archive_blob/', $tablesInstalled, PREG_GREP_INVERT);
 
             Piwik::createAccessObject();
             Piwik::setUserIsSuperUser();
-            if (count($baseTablesInstalled) >= $minimumCountPiwikTables &&
+            if ($baseTablesInstalled >= $minimumCountPiwikTables &&
                 count(Piwik_SitesManager_API::getInstance()->getAllSitesId()) > 0 &&
                 count(Piwik_UsersManager_API::getInstance()->getUsers()) > 0
             ) {
diff --git a/plugins/PrivacyManager/ReportsPurger.php b/plugins/PrivacyManager/ReportsPurger.php
index 61c650a613..4234b6b8c9 100755
--- a/plugins/PrivacyManager/ReportsPurger.php
+++ b/plugins/PrivacyManager/ReportsPurger.php
@@ -203,24 +203,23 @@ class Piwik_PrivacyManager_ReportsPurger
         // reports whose creation date <= this month will be deleted
         // (NOTE: we ignore how far we are in the current month)
         $toRemoveDate = Piwik_Date::factory('today')->subMonth(1 + $this->deleteReportsOlderThan);
-        $toRemoveYear = (int)$toRemoveDate->toString('Y');
-        $toRemoveMonth = (int)$toRemoveDate->toString('m');
 
         // find all archive tables that are older than N months
         $oldNumericTables = array();
         $oldBlobTables = array();
         foreach (Piwik::getTablesInstalled() as $table) {
-            if (preg_match("/archive_(numeric|blob)_([0-9]+)_([0-9]+)/", $table, $matches)) {
-                $type = $matches[1];
-                $year = (int)$matches[2];
-                $month = (int)$matches[3];
-
-                if (self::shouldReportBePurged($year, $month, $toRemoveDate)) {
-                    if ($type == "numeric") {
-                        $oldNumericTables[] = $table;
-                    } else {
-                        $oldBlobTables[] = $table;
-                    }
+            $type = Piwik_DataAccess_ArchiveTableCreator::getTypeFromTableName($table);
+            if($type === false) {
+                continue;
+            }
+            $date = Piwik_DataAccess_ArchiveTableCreator::getDateFromTableName($table);
+            list($month, $year) = explode('_', $date);
+
+            if (self::shouldReportBePurged($year, $month, $toRemoveDate)) {
+                if ($type == Piwik_DataAccess_ArchiveTableCreator::NUMERIC_TABLE) {
+                    $oldNumericTables[] = $table;
+                } else {
+                    $oldBlobTables[] = $table;
                 }
             }
         }
@@ -285,7 +284,7 @@ class Piwik_PrivacyManager_ReportsPurger
             // if not keeping segments make sure segments w/ kept periods are also deleted
             if (!$this->keepSegmentReports) {
                 $this->findSegmentArchives($oldNumericTables);
-                $archiveIds = $this->segmentArchiveIds[$this->getArchiveTableDate($table)];
+                $archiveIds = $this->segmentArchiveIds[Piwik_DataAccess_ArchiveTableCreator::getDateFromTableName($table)];
 
                 if (!empty($archiveIds)) {
                     $where .= " OR idarchive IN (" . implode(',', $archiveIds) . ")";
@@ -308,7 +307,7 @@ class Piwik_PrivacyManager_ReportsPurger
         }
 
         foreach ($numericTables as $table) {
-            $tableDate = $this->getArchiveTableDate($table);
+            $tableDate = Piwik_DataAccess_ArchiveTableCreator::getDateFromTableName($table);
 
             $maxIdArchive = Piwik_FetchOne("SELECT MAX(idarchive) FROM $table");
 
@@ -326,12 +325,6 @@ class Piwik_PrivacyManager_ReportsPurger
         }
     }
 
-    private function getArchiveTableDate($table)
-    {
-        preg_match("/[a-zA-Z_]+([0-9]+_[0-9]+)/", $table, $matches);
-        return $matches[1];
-    }
-
     /**
      * Utility function. Creates a new instance of ReportsPurger with the supplied array
      * of settings.
diff --git a/tests/PHPUnit/Benchmarks/ArchiveQueryBenchmark.php b/tests/PHPUnit/Benchmarks/ArchiveQueryBenchmark.php
index 225bbc6b3f..414d04d977 100644
--- a/tests/PHPUnit/Benchmarks/ArchiveQueryBenchmark.php
+++ b/tests/PHPUnit/Benchmarks/ArchiveQueryBenchmark.php
@@ -16,7 +16,7 @@ class ArchiveQueryBenchmark extends BenchmarkTestCase
     
     public function setUp()
     {
-        $archivingTables = Piwik::getTablesArchivesInstalled();
+        $archivingTables = Piwik_DataAccess_ArchiveTableCreator::getTablesArchivesInstalled();
         if (empty($archivingTables)) {
             $this->archivingLaunched = true;
             Piwik_VisitsSummary_API::getInstance()->get(
diff --git a/tests/PHPUnit/IntegrationTestCase.php b/tests/PHPUnit/IntegrationTestCase.php
index d87c1970a2..e085147ec4 100755
--- a/tests/PHPUnit/IntegrationTestCase.php
+++ b/tests/PHPUnit/IntegrationTestCase.php
@@ -1105,7 +1105,7 @@ abstract class IntegrationTestCase extends PHPUnit_Framework_TestCase
      */
     public static function deleteArchiveTables()
     {
-        foreach (Piwik::getTablesArchivesInstalled() as $table) {
+        foreach (Piwik_DataAccess_ArchiveTableCreator::getTablesArchivesInstalled() as $table) {
             Piwik_Query("DROP TABLE IF EXISTS $table");
         }
 
diff --git a/tests/PHPUnit/Plugins/PrivacyManagerTest.php b/tests/PHPUnit/Plugins/PrivacyManagerTest.php
index bc9d653551..d227c951d7 100755
--- a/tests/PHPUnit/Plugins/PrivacyManagerTest.php
+++ b/tests/PHPUnit/Plugins/PrivacyManagerTest.php
@@ -250,7 +250,7 @@ class PrivacyManagerTest extends IntegrationTestCase
     public function testPurgeDataDeleteLogsNoData()
     {
         Piwik::truncateAllTables();
-        foreach (Piwik::getTablesArchivesInstalled() as $table) {
+        foreach (Piwik_DataAccess_ArchiveTableCreator::getTablesArchivesInstalled() as $table) {
             Piwik_Exec("DROP TABLE $table");
         }
 
-- 
GitLab