From 9c72a6b0ce50824d73896478e2fc84ef1459eebc Mon Sep 17 00:00:00 2001
From: mattpiwik <matthieu.aubry@gmail.com>
Date: Thu, 26 Mar 2009 03:13:42 +0000
Subject: [PATCH] - fixing broken unit tests and previously broken period
 archiving.

git-svn-id: http://dev.piwik.org/svn/trunk@1028 59fd770c-687e-43c8-a1e3-f5a4ff64c105
---
 core/ArchiveProcessing.php                    | 16 +++----
 core/ArchiveProcessing/Period.php             |  9 +++-
 core/DataTable.php                            |  8 ++--
 core/DataTable/Filter/AddSummaryRow.php       |  5 ++-
 core/DataTable/Filter/Sort.php                |  4 ++
 core/ViewDataTable/GenerateGraphData.php      |  2 +-
 plugins/Actions/Actions.php                   | 22 +++++-----
 plugins/Goals/templates/release_notes.tpl     |  8 ++--
 plugins/Live/Live.php                         |  1 +
 plugins/Provider/Provider.php                 |  2 -
 plugins/Referers/Referers.php                 | 21 +++++----
 tests/core/ReleaseCheckList.test.php          |  4 +-
 tests/core/SitesManager.test.php              | 44 +------------------
 .../resources/test-download-outlink-page.txt  |  8 ++++
 14 files changed, 68 insertions(+), 86 deletions(-)
 create mode 100644 tests/resources/test-download-outlink-page.txt

diff --git a/core/ArchiveProcessing.php b/core/ArchiveProcessing.php
index da16e443c4..a728c98003 100644
--- a/core/ArchiveProcessing.php
+++ b/core/ArchiveProcessing.php
@@ -453,18 +453,19 @@ abstract class Piwik_ArchiveProcessing
 	/**
 	 * @param string $name
 	 * @param int|float $value
-	 * @return void
+	 * @return Piwik_ArchiveProcessing_Record_Numeric
 	 */
 	public function insertNumericRecord($name, $value)
 	{
 		$record = new Piwik_ArchiveProcessing_Record_Numeric($name, $value);
 		$this->insertRecord($record);
+		return $record;
 	}
 	
 	/**
 	 * @param string $name
 	 * @param string|array of string $aValues
-	 * @return void
+	 * @return true
 	 */
 	public function insertBlobRecord($name, $value)
 	{
@@ -475,12 +476,11 @@ abstract class Piwik_ArchiveProcessing
 			{
 				$this->insertRecord($record);
 			}
+			return true;
 		}
-		else
-		{
-			$record = new Piwik_ArchiveProcessing_Record_Blob($name, $value);
-			$this->insertRecord($record);
-		}
+		$record = new Piwik_ArchiveProcessing_Record_Blob($name, $value);
+		$this->insertRecord($record);
+		return true;
 	}
 	
 	/**
@@ -488,7 +488,7 @@ abstract class Piwik_ArchiveProcessing
 	 *
 	 * @param Piwik_ArchiveProcessing_Record $record
 	 */
-	private function insertRecord($record)
+	protected function insertRecord($record)
 	{
 		// table to use to save the data
 		if(Piwik::isNumeric($record->value))
diff --git a/core/ArchiveProcessing/Period.php b/core/ArchiveProcessing/Period.php
index f0a35dbac6..3b13a97551 100644
--- a/core/ArchiveProcessing/Period.php
+++ b/core/ArchiveProcessing/Period.php
@@ -105,6 +105,7 @@ class Piwik_ArchiveProcessing_Period extends Piwik_ArchiveProcessing
 													$name, 
 													$value
 												);
+			$this->insertRecord($records[$name]);
 		}
 		
 		// if asked for only one field to sum
@@ -140,7 +141,10 @@ class Piwik_ArchiveProcessing_Period extends Piwik_ArchiveProcessing
 	 *  				nameTable2 => number of rows,
 	 * 				)
 	 */
-	public function archiveDataTable( $aRecordName, $maximumRowsInDataTableLevelZero = null, $maximumRowsInSubDataTable = null )
+	public function archiveDataTable(	$aRecordName, 
+										$maximumRowsInDataTableLevelZero = null, 
+										$maximumRowsInSubDataTable = null,
+										$columnToSortByBeforeTruncation = null )
 	{
 		if(!is_array($aRecordName))
 		{
@@ -155,7 +159,8 @@ class Piwik_ArchiveProcessing_Period extends Piwik_ArchiveProcessing
 			$nameToCount[$recordName]['level0'] =  $table->getRowsCount();
 			$nameToCount[$recordName]['recursive'] =  $table->getRowsCountRecursive();
 			
-			$record = new Piwik_ArchiveProcessing_RecordArray($recordName, $table->getSerialized( $maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable ));
+			$blob = $table->getSerialized( $maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable, $columnToSortByBeforeTruncation );
+			$this->insertBlobRecord($recordName, $blob);
 		}
 		return $nameToCount;
 	}
diff --git a/core/DataTable.php b/core/DataTable.php
index f48f5999ce..132a940d64 100644
--- a/core/DataTable.php
+++ b/core/DataTable.php
@@ -779,7 +779,9 @@ class Piwik_DataTable
 	 * 					);
 	 * 
 	 */
-	public function getSerialized( $maximumRowsInDataTable = null, $maximumRowsInSubDataTable = null )
+	public function getSerialized(	$maximumRowsInDataTable = null, 
+									$maximumRowsInSubDataTable = null,
+									$columnToSortByBeforeTruncation = null )
 	{
 		static $depth = 0;
 		
@@ -789,7 +791,7 @@ class Piwik_DataTable
 		}
 		if( !is_null($maximumRowsInDataTable) )
 		{
-			$filter = new Piwik_DataTable_Filter_AddSummaryRow($this, $maximumRowsInDataTable - 1);
+			$filter = new Piwik_DataTable_Filter_AddSummaryRow($this, $maximumRowsInDataTable - 1, Piwik_DataTable::LABEL_SUMMARY_ROW, $columnToSortByBeforeTruncation);
 		}
 		
 		// For each row, get the serialized row
@@ -802,7 +804,7 @@ class Piwik_DataTable
 			{
 				$subTable = Piwik_DataTable_Manager::getInstance()->getTable($idSubTable);
 				$depth++;
-				$aSerializedDataTable = $aSerializedDataTable + $subTable->getSerialized( $maximumRowsInSubDataTable, $maximumRowsInSubDataTable );
+				$aSerializedDataTable = $aSerializedDataTable + $subTable->getSerialized( $maximumRowsInSubDataTable, $maximumRowsInSubDataTable, $columnToSortByBeforeTruncation );
 				$depth--;
 			}
 		}
diff --git a/core/DataTable/Filter/AddSummaryRow.php b/core/DataTable/Filter/AddSummaryRow.php
index a62c5dc2da..a14797ba60 100644
--- a/core/DataTable/Filter/AddSummaryRow.php
+++ b/core/DataTable/Filter/AddSummaryRow.php
@@ -27,7 +27,10 @@
  */
 class Piwik_DataTable_Filter_AddSummaryRow extends Piwik_DataTable_Filter
 {
-	public function __construct( $table, $startRowToSummarize, $labelSummaryRow = Piwik_DataTable::LABEL_SUMMARY_ROW, $columnToSortByBeforeTruncating = Piwik_Archive::INDEX_NB_VISITS )
+	public function __construct(	$table, 
+									$startRowToSummarize, 
+									$labelSummaryRow = Piwik_DataTable::LABEL_SUMMARY_ROW, 
+									$columnToSortByBeforeTruncating = null )
 	{
 		parent::__construct($table);
 		$this->startRowToSummarize = $startRowToSummarize;
diff --git a/core/DataTable/Filter/Sort.php b/core/DataTable/Filter/Sort.php
index cc15213095..5e4e324c7e 100644
--- a/core/DataTable/Filter/Sort.php
+++ b/core/DataTable/Filter/Sort.php
@@ -23,6 +23,10 @@ class Piwik_DataTable_Filter_Sort extends Piwik_DataTable_Filter
 	public function __construct( $table, $columnToSort, $order = 'desc', $naturalSort = false )
 	{
 		parent::__construct($table);
+		if(empty($columnToSort))
+		{
+			return;
+		}
 		$this->columnToSort = $columnToSort;
 		$this->naturalSort = $naturalSort;
 		$this->setOrder($order);
diff --git a/core/ViewDataTable/GenerateGraphData.php b/core/ViewDataTable/GenerateGraphData.php
index 37ccae22c4..2ddf1c6210 100644
--- a/core/ViewDataTable/GenerateGraphData.php
+++ b/core/ViewDataTable/GenerateGraphData.php
@@ -72,7 +72,7 @@ abstract class Piwik_ViewDataTable_GenerateGraphData extends Piwik_ViewDataTable
 		// we load the data with the filters applied
 		$this->loadDataTableFromAPI();
 		$offsetStartSummary = $this->getGraphLimit() - 1;
-		$this->dataTable->queueFilter('Piwik_DataTable_Filter_AddSummaryRow', array($offsetStartSummary, Piwik_Translate('General_Others')));
+		$this->dataTable->queueFilter('Piwik_DataTable_Filter_AddSummaryRow', array($offsetStartSummary, Piwik_Translate('General_Others'), Piwik_Archive::INDEX_NB_VISITS));
 		$this->dataAvailable = $this->dataTable->getRowsCount() != 0;
 		
 		if(!$this->dataAvailable)
diff --git a/plugins/Actions/Actions.php b/plugins/Actions/Actions.php
index 20f1f312b1..d212d703ab 100644
--- a/plugins/Actions/Actions.php
+++ b/plugins/Actions/Actions.php
@@ -17,6 +17,9 @@ class Piwik_Actions extends Piwik_Plugin
 {
 	static protected $actionCategoryDelimiter = null;
 	static protected $limitLevelSubCategory = 10;
+	protected $maximumRowsInDataTableLevelZero;
+	protected $maximumRowsInSubDataTable;
+	protected $columnToSortByBeforeTruncation;
 	
 	public function getInformation()
 	{
@@ -41,10 +44,12 @@ class Piwik_Actions extends Piwik_Plugin
 		);
 		return $hooks;
 	}
-	
 	public function __construct()
 	{
 		self::$actionCategoryDelimiter =  Zend_Registry::get('config')->General->action_category_delimiter;
+		$this->columnToSortByBeforeTruncation = 'nb_visits';
+		$this->maximumRowsInDataTableLevelZero = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_actions;
+		$this->maximumRowsInSubDataTable = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_subtable_actions;
 	}
 	
 	function addWidgets()
@@ -64,16 +69,12 @@ class Piwik_Actions extends Piwik_Plugin
 	function archivePeriod( $notification )
 	{
 		$archiveProcessing = $notification->getNotificationObject();
-		
 		$dataTableToSum = array( 
 				'Actions_actions',
 				'Actions_downloads',
 				'Actions_outlink',
 		);
-		
-		$maximumRowsInDataTableLevelZero = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_actions;
-		$maximumRowsInSubDataTable = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_subtable_actions;
-		$archiveProcessing->archiveDataTable($dataTableToSum, $maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable);
+		$archiveProcessing->archiveDataTable($dataTableToSum, $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation);
 	}
 	
 	/**
@@ -183,19 +184,16 @@ class Piwik_Actions extends Piwik_Plugin
 
 	protected function archiveDayRecordInDatabase($archiveProcessing)
 	{
-		$maximumRowsInDataTableLevelZero = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_actions;
-		$maximumRowsInSubDataTable = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_subtable_actions;
-
 		$dataTable = Piwik_ArchiveProcessing_Day::generateDataTable($this->actionsTablesByType[Piwik_Tracker_Action::TYPE_ACTION]);
-		$s = $dataTable->getSerialized( $maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable );
+		$s = $dataTable->getSerialized( $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation );
 		$archiveProcessing->insertBlobRecord('Actions_actions', $s);
 
 		$dataTable = Piwik_ArchiveProcessing_Day::generateDataTable($this->actionsTablesByType[Piwik_Tracker_Action::TYPE_DOWNLOAD]);
-		$s = $dataTable->getSerialized($maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable );
+		$s = $dataTable->getSerialized($this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation );
 		$archiveProcessing->insertBlobRecord('Actions_downloads', $s);
 		
 		$dataTable = Piwik_ArchiveProcessing_Day::generateDataTable($this->actionsTablesByType[Piwik_Tracker_Action::TYPE_OUTLINK]);
-		$s = $dataTable->getSerialized( $maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable );
+		$s = $dataTable->getSerialized( $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation );
 		$archiveProcessing->insertBlobRecord('Actions_outlink', $s);
 		
 		unset($this->actionsTablesByType);
diff --git a/plugins/Goals/templates/release_notes.tpl b/plugins/Goals/templates/release_notes.tpl
index b002f4a4b6..a7118eeace 100644
--- a/plugins/Goals/templates/release_notes.tpl
+++ b/plugins/Goals/templates/release_notes.tpl
@@ -6,14 +6,13 @@ The Goal Tracking Plugin is in alpha release. There is more coming soon!
 - The Goal Report page will display conversion table by search engines, country, keyword, campaign, etc.
 - The Goal Overview page will link to a Goal Report page with a "(more)" link that will ajax reload the page
 - Goals could be triggered using javascript event, with custom revenue
-- internationalization of all strings
-- provide documentation, screenshots, blog post + add screenshot and inline help in "Add a New Goal"
 - provide widgets for the dashboard, general goal overview, and one widget for each goal. With: graph evolution, sparklines. Widget with top segments for each goal.
 - add visits with conversion sparkline in VisitsSummary overview
 - link under goal conversion to full goal reports (optional display)
 - N/A% should be n/a
-- GeoIp compatibility, archive goals by city, country? see archiveDayAggregateGoals
 - documentation, eg. http://feedproxy.google.com/~r/WebAnalyticsWorld/~3/1g5Z7k7jDKQ/10-must-track-google-analytics-goals.html
+- internationalization of all strings i18n
+- provide documentation, screenshots, blog post + add screenshot and inline help in "Add a New Goal"
 
 Known bugs
 - Your top converting keyword include keyword without conversions
@@ -24,6 +23,9 @@ Known bugs
 - lines with 0 visits and no conversion should not appear
 - bug = http://forum.piwik.org/index.php?showtopic=150
 
+In V2
+- GeoIp compatibility, archive goals by city, country? see archiveDayAggregateGoals
+
 Give us Feedback!
 If you find any other bug, or if you have suggestions, please send us a message using the "Give us feedback" link at the top of the Piwik pages.
 
diff --git a/plugins/Live/Live.php b/plugins/Live/Live.php
index a4b5f6388d..a8078b4c45 100644
--- a/plugins/Live/Live.php
+++ b/plugins/Live/Live.php
@@ -6,6 +6,7 @@
 	- api propre
 	- html
 	- jquery spy
+	- make sure only one query is launched at once or what if requests takes more than 10s to succeed?
 	- simple stats above in TEXT
 	- Security review
 	- blog post, push version
diff --git a/plugins/Provider/Provider.php b/plugins/Provider/Provider.php
index 5bbdfecf08..e9f555f9c1 100644
--- a/plugins/Provider/Provider.php
+++ b/plugins/Provider/Provider.php
@@ -80,11 +80,9 @@ class Piwik_Provider extends Piwik_Plugin
 	function archivePeriod( $notification )
 	{
 		$archiveProcessing = $notification->getNotificationObject();
-		
 		$dataTableToSum = array( 
 				'Provider_hostnameExt',
 		);
-		
 		$archiveProcessing->archiveDataTable($dataTableToSum);
 	}
 		
diff --git a/plugins/Referers/Referers.php b/plugins/Referers/Referers.php
index e165f5ff79..c1ed78bb0b 100644
--- a/plugins/Referers/Referers.php
+++ b/plugins/Referers/Referers.php
@@ -15,6 +15,9 @@
 class Piwik_Referers extends Piwik_Plugin
 {	
 	public $archiveProcessing;
+	protected $columnToSortByBeforeTruncation;
+	protected $maximumRowsInDataTableLevelZero;
+	protected $maximumRowsInSubDataTable;
 	public function getInformation()
 	{
 		$info = array(
@@ -39,6 +42,13 @@ class Piwik_Referers extends Piwik_Plugin
 		return $hooks;
 	}
 
+	function __construct()
+	{
+		$this->columnToSortByBeforeTruncation = Piwik_Archive::INDEX_NB_VISITS;
+		$this->maximumRowsInDataTableLevelZero = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_referers;
+		$this->maximumRowsInSubDataTable = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_subtable_referers;
+	}
+	
 	function addWidgets()
 	{
 		Piwik_AddWidget( 'Referers', 'getKeywords', Piwik_Translate('Referers_WidgetKeywords'));
@@ -68,11 +78,7 @@ class Piwik_Referers extends Piwik_Plugin
 			'Referers_keywordByCampaign',
 			'Referers_urlByWebsite',
 		);
-		
-		$maximumRowsInDataTableLevelZero = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_referers;
-		$maximumRowsInSubDataTable = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_subtable_referers;
-		
-		$nameToCount = $archiveProcessing->archiveDataTable($dataTableToSum, $maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable);
+		$nameToCount = $archiveProcessing->archiveDataTable($dataTableToSum, $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation);
 		
 		$mappingFromArchiveName = array(
 			'Referers_distinctSearchEngines' => 
@@ -293,9 +299,6 @@ class Piwik_Referers extends Piwik_Plugin
 		$data = $archiveProcessing->getDataTableSerialized($this->interestByType);
 		$archiveProcessing->insertBlobRecord('Referers_type', $data);
 		
-		$maximumRowsInDataTableLevelZero = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_referers;
-		$maximumRowsInSubDataTable = Zend_Registry::get('config')->General->datatable_archiving_maximum_rows_subtable_referers;
-		
 		$blobRecords = array(
 			'Referers_keywordBySearchEngine' => $archiveProcessing->getDataTableWithSubtablesFromArraysIndexedByLabel($this->interestBySearchEngineAndKeyword, $this->interestBySearchEngine),
 			'Referers_searchEngineByKeyword' => $archiveProcessing->getDataTableWithSubtablesFromArraysIndexedByLabel($this->interestByKeywordAndSearchEngine, $this->interestByKeyword),
@@ -305,7 +308,7 @@ class Piwik_Referers extends Piwik_Plugin
 		
 		foreach($blobRecords as $recordName => $table )
 		{
-			$dataToRecord = $table->getSerialized($maximumRowsInDataTableLevelZero, $maximumRowsInSubDataTable);
+			$dataToRecord = $table->getSerialized($this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $this->columnToSortByBeforeTruncation);
 			$archiveProcessing->insertBlobRecord($recordName, $dataToRecord);
 		}
 	}
diff --git a/tests/core/ReleaseCheckList.test.php b/tests/core/ReleaseCheckList.test.php
index 9bfe6b6f23..c429dfc336 100644
--- a/tests/core/ReleaseCheckList.test.php
+++ b/tests/core/ReleaseCheckList.test.php
@@ -15,8 +15,8 @@ class Test_Piwik_ReleaseCheckList extends UnitTestCase
     {
     	$this->globalConfig = parse_ini_file(PATH_TEST_TO_ROOT . '/config/global.ini.php', true);
 //    	var_dump($globalConfig);
-    	$this->checkEqual(array('Debug' => 'always_archive_data'), '');
-    	$this->checkEqual(array('Debug' => 'enable_sql_profiler'), '');
+    	$this->checkEqual(array('Debug' => 'always_archive_data'), '0');
+    	$this->checkEqual(array('Debug' => 'enable_sql_profiler'), '0');
     	$this->checkEqual(array('General' => 'time_before_archive_considered_outdated'), '10');
     	$this->checkEqual(array('General' => 'enable_browser_archiving_triggering'), '1');
     	$this->checkEqual(array('General' => 'default_language'), 'en');
diff --git a/tests/core/SitesManager.test.php b/tests/core/SitesManager.test.php
index 77c9b84bba..b48c21de2d 100755
--- a/tests/core/SitesManager.test.php
+++ b/tests/core/SitesManager.test.php
@@ -280,54 +280,13 @@ class Test_Piwik_SitesManager extends Test_Database
     	
     	$this->assertEqual($shouldHave, $siteUrlsAfter);
     }
-    
-    /**
-     * wrong format urls => exception
-     */
-    public function test_addSiteUrls_wrongUrlsFormat1()
-    {
-    	
-    	$idsite = $this->test_addSite();
-    	
-    	$toAdd = array("http://pi''.com");
-    	
-    	try {
-    		$insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd);
-    	}
-    	catch (Exception $expected) {
-            return;
-        }
-        $this->fail("Exception not raised.");
-    }
-    
-    /**
-     * wrong format urls => exception
-     */
-    public function test_addSiteUrls_wrongUrlsFormat2()
-    {
-    	
-    	$idsite = $this->test_addSite();
-    	
-    	$toAdd = array("http://pi^.com");
-    	
-    	try {
-    		$insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd);
-    	}
-    	catch (Exception $expected) {
-            return;
-        }
-        $this->fail("Exception not raised.");
-    }
     /**
      * wrong format urls => exception
      */
     public function test_addSiteUrls_wrongUrlsFormat3()
     {
-    	
     	$idsite = $this->test_addSite();
-    	
-    	$toAdd = array("http://pigeq.com/test{}");
-    	
+    	$toAdd = array("htt{}p://pigeq.com/test");
     	try {
     		$insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd);
     	}
@@ -337,7 +296,6 @@ class Test_Piwik_SitesManager extends Test_Database
         $this->fail("Exception not raised.");
     }
     
-    
     /**
      * wrong idsite => no exception because simply no access to this resource
      */
diff --git a/tests/resources/test-download-outlink-page.txt b/tests/resources/test-download-outlink-page.txt
new file mode 100644
index 0000000000..4ad8d83200
--- /dev/null
+++ b/tests/resources/test-download-outlink-page.txt
@@ -0,0 +1,8 @@
+downloads
+piwik.php?idsite=1&download=http%3A//forum.piwik.org/mma mai ****/test.zip&rand=0.10591157392713457&redirect=0&name=my outlink name
+piwik.php?idsite=1&download=http%3A//forum.piwik.org/test.zip&rand=0.10591157392713457&redirect=0
+
+outlinks
+piwik.php?idsite=1&link=http%3A//www.example.org2/subcategory/subpage/&rand=0.10591157392713457&redirect=0
+piwik.php?idsite=1&link=http%3A//example.org/subcategory/latest.zip/test/&rand=0.10591157392713457&redirect=0
+piwik.php?idsite=1&link=mailto%3Atest@test.com&rand=0.15968200434074042&redirect=0
-- 
GitLab