Skip to content
Snippets Groups Projects
Commit 439ff584 authored by Demian Katz's avatar Demian Katz
Browse files

More refactoring in preparation for statistics driver plugin loader.

parent 7c459a69
No related merge requests found
...@@ -119,7 +119,7 @@ abstract class AbstractBase implements ServiceLocatorAwareInterface ...@@ -119,7 +119,7 @@ abstract class AbstractBase implements ServiceLocatorAwareInterface
if (count($setting) > 1 && !$getAll) { if (count($setting) > 1 && !$getAll) {
// If we only want global drivers, we don't want anything with // If we only want global drivers, we don't want anything with
// limits. // limits.
if (is_null($source)) { if (null === $source) {
continue; continue;
} }
...@@ -137,7 +137,9 @@ abstract class AbstractBase implements ServiceLocatorAwareInterface ...@@ -137,7 +137,9 @@ abstract class AbstractBase implements ServiceLocatorAwareInterface
// When we construct the driver, we pass the name of the data source; // When we construct the driver, we pass the name of the data source;
// we use the special value "global" to represent global writer // we use the special value "global" to represent global writer
// requests (the special null case): // requests (the special null case):
$drivers[] = new $class(is_null($source) ? 'global' : $source); $newDriver = new $class();
$newDriver->setSource(null === $source ? 'global' : $source);
$drivers[] = $newDriver;
} }
} }
......
...@@ -38,17 +38,28 @@ namespace VuFind\Statistics\Driver; ...@@ -38,17 +38,28 @@ namespace VuFind\Statistics\Driver;
*/ */
abstract class AbstractBase abstract class AbstractBase
{ {
protected $source = null;
/**
* Get the source that is using the statistics.
*
* @return string
*/
public function getSource()
{
return $this->source;
}
/** /**
* Constructor * Set the source that is using the statistics.
* *
* @param string $source Which class this writer belongs to * @param string $source Name of source.
* *
* @return void * @return void
*/ */
public function __construct($source) public function setSource($source)
{ {
// This doesn't currently do anything, $this->source = $source;
// just in case we need it here in the future.
} }
/** /**
......
...@@ -40,19 +40,20 @@ use VuFind\Db\Table\UserStats as UserStatsTable, ...@@ -40,19 +40,20 @@ use VuFind\Db\Table\UserStats as UserStatsTable,
*/ */
class Db extends AbstractBase class Db extends AbstractBase
{ {
protected $statsTable; protected $statsTable = null;
/** /**
* Constructor * Get an object representing the statistics table.
* *
* @param string $source Which class this writer belongs to * @return \VuFind\Db\Table\UserStatsFields
*
* @return void
*/ */
public function __construct($source) protected function getStatsTable()
{ {
// Create a table object if (null === $this->statsTable) {
$this->statsTable = new UserStatsFieldsTable(); // Create a table object
$this->statsTable = new UserStatsFieldsTable();
}
return $this->statsTable;
} }
/** /**
...@@ -65,7 +66,7 @@ class Db extends AbstractBase ...@@ -65,7 +66,7 @@ class Db extends AbstractBase
*/ */
public function write($data, $userData) public function write($data, $userData)
{ {
$this->statsTable->save($data, $userData); $this->getStatsTable()->save($data, $userData);
} }
/** /**
...@@ -79,7 +80,7 @@ class Db extends AbstractBase ...@@ -79,7 +80,7 @@ class Db extends AbstractBase
public function getTopList($field, $listLength = 5) public function getTopList($field, $listLength = 5)
{ {
// Use the model // Use the model
return $this->statsTable->getTop($field, $listLength); return $this->getStatsTable()->getTop($field, $listLength);
} }
/** /**
...@@ -93,7 +94,7 @@ class Db extends AbstractBase ...@@ -93,7 +94,7 @@ class Db extends AbstractBase
public function getFullList($field, $value = array()) public function getFullList($field, $value = array())
{ {
// Use the model // Use the model
return $this->statsTable->getFields($field, $value)->toArray(); return $this->getStatsTable()->getFields($field, $value)->toArray();
} }
/** /**
......
...@@ -39,21 +39,20 @@ use VuFind\Config\Reader as ConfigReader; ...@@ -39,21 +39,20 @@ use VuFind\Config\Reader as ConfigReader;
*/ */
class File extends AbstractBase class File extends AbstractBase
{ {
protected $folder; protected $folder = null;
protected $file;
/** /**
* Constructor * Get the name of the folder for storing statistics.
* *
* @param string $source Which class this writer belongs to * @return string
*
* @return void
*/ */
public function __construct($source) protected function getFolder()
{ {
$configs = ConfigReader::getConfig(); if (null === $this->folder) {
$this->folder = $configs->Statistics->file; $configs = ConfigReader::getConfig();
$this->file = strtolower($source); $this->folder = $configs->Statistics->file;
}
return $this->folder;
} }
/** /**
...@@ -67,16 +66,17 @@ class File extends AbstractBase ...@@ -67,16 +66,17 @@ class File extends AbstractBase
public function write($data, $userData) public function write($data, $userData)
{ {
$xml = $this->getSaveXML(array_merge($data, $userData), 1); $xml = $this->getSaveXML(array_merge($data, $userData), 1);
$filename = rtrim($this->folder, '/'); $filename = rtrim($this->getFolder(), '/');
if (!file_exists($filename)) { if (!file_exists($filename)) {
mkdir($filename); mkdir($filename);
} }
$index = (strrpos($this->file, '.')) ? -1 : strlen($this->file); $file = strtolower($this->getSource());
$filename .= '/' . substr($this->file, 0, $index) . '.xml'; $index = (strrpos($file, '.')) ? -1 : strlen($file);
$filename .= '/' . substr($file, 0, $index) . '.xml';
if (file_exists($filename)) { if (file_exists($filename)) {
$this->file = file_get_contents($filename); $file = file_get_contents($filename);
// remove <xml .. > // remove <xml .. >
$xml .= "\n" . substr($this->file, 39, strlen($this->file)); $xml .= "\n" . substr($file, 39, strlen($file));
} else { } else {
$xml .= "\n</xml>"; $xml .= "\n</xml>";
} }
......
...@@ -29,7 +29,7 @@ namespace VuFind\Statistics\Driver; ...@@ -29,7 +29,7 @@ namespace VuFind\Statistics\Driver;
use VuFind\Connection\Manager as ConnectionManager; use VuFind\Connection\Manager as ConnectionManager;
/** /**
* Writer to put statistics to the SOLR index * Writer to put statistics to the Solr index
* *
* @category VuFind2 * @category VuFind2
* @package Statistics * @package Statistics
...@@ -39,18 +39,19 @@ use VuFind\Connection\Manager as ConnectionManager; ...@@ -39,18 +39,19 @@ use VuFind\Connection\Manager as ConnectionManager;
*/ */
class Solr extends AbstractBase class Solr extends AbstractBase
{ {
protected $solr; protected $solr = null;
/** /**
* Constructor * Get Solr connection.
* *
* @param string $source Which class this writer belongs to * @return \VuFind\Connection\SolrStats
*
* @return void
*/ */
public function __construct($source) protected function getSolr()
{ {
$this->solr = ConnectionManager::connectToIndex('SolrStats'); if (null === $this->solr) {
$this->solr = ConnectionManager::connectToIndex('SolrStats');
}
return $this->solr;
} }
/** /**
...@@ -66,8 +67,8 @@ class Solr extends AbstractBase ...@@ -66,8 +67,8 @@ class Solr extends AbstractBase
if (isset($data['phrase']) && $data['phrase'] == '') { if (isset($data['phrase']) && $data['phrase'] == '') {
$data['phrase'] = '*:*'; $data['phrase'] = '*:*';
} }
$this->solr->saveRecord( $this->getSolr()->saveRecord(
$this->solr->getSaveXML( $this->getSolr()->getSaveXML(
array_merge($data, $userData) array_merge($data, $userData)
) )
); );
...@@ -83,8 +84,8 @@ class Solr extends AbstractBase ...@@ -83,8 +84,8 @@ class Solr extends AbstractBase
*/ */
public function getTopList($field, $listLength = 5) public function getTopList($field, $listLength = 5)
{ {
// Records saved in SOLR // Records saved in Solr
$records = $this->solr->search( $records = $this->getSolr()->search(
array( array(
'facet' => array( 'facet' => array(
'field' => array($field), 'field' => array($field),
...@@ -121,7 +122,7 @@ class Solr extends AbstractBase ...@@ -121,7 +122,7 @@ class Solr extends AbstractBase
$limit = 1000; $limit = 1000;
$data = array(); $data = array();
do { do {
$search = $this->solr->search( $search = $this->getSolr()->search(
array( array(
'fields' => array($field), 'fields' => array($field),
'filter' => array($field.':'.$value['value']), 'filter' => array($field.':'.$value['value']),
...@@ -151,7 +152,7 @@ class Solr extends AbstractBase ...@@ -151,7 +152,7 @@ class Solr extends AbstractBase
$limit = 1000; $limit = 1000;
$hashes = array(); $hashes = array();
do { do {
$result = $this->solr->search( $result = $this->getSolr()->search(
array( array(
'field' => 'browser', 'field' => 'browser',
'group' => array('session'), 'group' => array('session'),
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment