"Fossies" - the Fresh Open Source Software Archive

Member "Open-Web-Analytics-1.7.0/modules/base/classes/service.php" (16 Sep 2020, 11819 Bytes) of package /linux/www/Open-Web-Analytics-1.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "service.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.6.9_vs_1.7.0.

    1 <?php 
    2 
    3 //
    4 // Open Web Analytics - An Open Source Web Analytics Framework
    5 //
    6 // Copyright 2008 Peter Adams. All rights reserved.
    7 //
    8 // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
    9 //
   10 // Unless required by applicable law or agreed to in writing, software
   11 // distributed under the License is distributed on an "AS IS" BASIS,
   12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13 // See the License for the specific language governing permissions and
   14 // limitations under the License.
   15 //
   16 // $Id$
   17 //
   18 
   19 require_once(OWA_BASE_CLASS_DIR.'geolocation.php');
   20 
   21 /**
   22  * Service Class
   23  * 
   24  * @author      Peter Adams <peter@openwebanalytics.com>
   25  * @copyright   Copyright &copy; 2008 Peter Adams <peter@openwebanalytics.com>
   26  * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
   27  * @category    owa
   28  * @package     owa
   29  * @version        $Revision$
   30  * @since        owa 1.0.0
   31  */
   32 
   33 
   34 class owa_service extends owa_base {
   35 
   36     var $init = false;
   37     var $request;
   38     var $state;
   39     var $current_user;
   40     var $settings;
   41     var $maps = array();
   42     var $update_required = false;
   43     var $install_required = false;
   44     var $modules_needing_updates = array();
   45     var $modules = array();
   46     var $entities = array();
   47     var $metrics = array();
   48     var $dimensions = array();
   49     var $denormalizedDimensions = array();
   50     var $browscap;
   51     var $geolocation;
   52     var $formatters = array();
   53     var $restApiRoutes = array();
   54 
   55     function __construct() {
   56         owa_coreAPI::profile($this, __FUNCTION__, __LINE__);
   57 
   58     }
   59 
   60     function __destruct() {
   61         owa_coreAPI::profile($this, __FUNCTION__, __LINE__);
   62     }
   63 
   64     function initializeFramework() {
   65 
   66         if (!$this->isInit()) {
   67 
   68             // setup request container
   69             $this->request = owa_coreAPI::requestContainerSingleton();
   70 
   71             $this->_loadModules();
   72             $this->_loadFilters();
   73             $this->_loadEntities();
   74             $this->_loadMetrics();
   75             $this->_loadDimensions();
   76             $this->_loadFormatters();
   77             $this->_loadApiMethods();
   78             $this->_loadEventProcessors();
   79             $this->setInit();
   80 
   81             // setup current user
   82             $this->current_user = owa_coreAPI::supportClassFactory('base', 'serviceUser');
   83             // the 'log_users' config directive relies on this being populated
   84             $this->current_user->setUserData( 'user_id' ,  $this->request->state->get('u') );
   85             // load geolocation obj.
   86             $this->geolocation = owa_geolocation::getInstance();
   87         }
   88 
   89     }
   90 
   91     function setBrowscap($b) {
   92 
   93         $this->browscap = $b;
   94     }
   95 
   96     function getBrowscap( $ua = '') {
   97 
   98         if (empty($this->browscap)) {
   99             
  100             if ( ! $ua ) {
  101                 
  102                 $ua = $this->request->getServerParam('HTTP_USER_AGENT');
  103             }
  104             
  105             $this->browscap = owa_coreAPI::supportClassFactory('base', 'browscap', $ua);
  106         }
  107 
  108         return $this->browscap;
  109     }
  110 
  111     function _loadModules() {
  112 
  113         $present_modules = owa_coreAPI::getPresentModules();
  114         $am = owa_coreAPI::getActiveModules();
  115 
  116         foreach ($am as $k => $v) {
  117             
  118             if ( in_array( $v, $present_modules ) ) {
  119                 $m = owa_coreAPI::moduleClassFactory($v);
  120     
  121                 $this->addModule($m);
  122     
  123                 // check for schema updates
  124                 $check = $m->isSchemaCurrent();
  125     
  126                 if ($check != true) {
  127                     $this->markModuleAsNeedingUpdate($m->name);
  128                 }
  129             }
  130         }
  131 
  132         // set schema update flag
  133         if (!empty($this->modules_needing_updates)) {
  134             $this->setUpdateRequired();
  135         }
  136     }
  137     
  138     function checkForRequiredUpdates() {
  139         owa_coreAPI::debug( owa_coreAPI::configSingleton() );
  140         $am = owa_coreAPI::getActiveModules();
  141         
  142         foreach ($am as $k => $v) {
  143             
  144             // check for schema updates
  145             $check = $this->modules[ $v ]->isSchemaCurrent();
  146 
  147             if ($check != true) {
  148                 $this->markModuleAsNeedingUpdate($this->modules[ $v ]->name);
  149             }
  150         }
  151         
  152         // set schema update flag
  153         if (!empty($this->modules_needing_updates)) {
  154             $this->setUpdateRequired();
  155         }
  156     }
  157 
  158 
  159     function _loadEntities() {
  160 
  161         foreach ($this->modules as $k => $module) {
  162 
  163             foreach ($module->entities as $entity_k => $entity_v) {
  164                 // TODO: remove this to make API stateless
  165                 //$this->entities[] = $module->name.$entity_v;
  166                 // proper call
  167                 $this->addEntity($entity_v, $module->name.'.'.$entity_v);
  168             }
  169         }
  170 
  171         return;
  172     }
  173 
  174     function _loadFilters() {
  175 
  176         foreach ($this->modules as $k => $module) {
  177 
  178             $module->registerFilters();
  179         }
  180     }
  181 
  182     function _loadMetrics() {
  183 
  184         foreach ($this->modules as $k => $module) {
  185 
  186             if (is_array($module->metrics)) {
  187 
  188                 $this->metrics = array_merge_recursive( $this->metrics, $module->metrics);
  189             }
  190         }
  191 
  192         $metricsByEntityMap = array();
  193 
  194         foreach ( $this->metrics as $metric => $implementations ) {
  195 
  196             foreach ( $implementations as $implementation ) {
  197 
  198                 $m = owa_coreAPI::metricFactory( $implementation['class'], $implementation['params']);
  199 
  200                 if ( ! $m->isCalculated() ) {
  201                     $metricsByEntityMap[ $m->getEntityName() ][ $implementation['name'] ] = $implementation;
  202                 }
  203             }
  204         }
  205 
  206         $this->setMap('metricsByEntity', $metricsByEntityMap);
  207     }
  208 
  209     function getAllMetrics() {
  210 
  211         return $this->metrics;
  212     }
  213 
  214     function loadCliCommands() {
  215 
  216         $command_map = array();
  217 
  218         foreach ($this->modules as $k => $module) {
  219 
  220             if (is_array($module->cli_commands)) {
  221                 $command_map = array_merge($command_map, $module->cli_commands);
  222             }
  223         }
  224 
  225         $this->setMap('cli_commands', $command_map);
  226     }
  227 
  228     function _loadApiMethods() {
  229 
  230         $method_map = array();
  231 
  232         foreach ($this->modules as $k => $module) {
  233 
  234             if (is_array($module->api_methods)) {
  235                 $method_map = array_merge($method_map, $module->api_methods);
  236             }
  237         }
  238 
  239         $this->setMap('api_methods', $method_map);
  240     }
  241 
  242     function _loadDimensions() {
  243 
  244         foreach ($this->modules as $k => $module) {
  245 
  246             if (is_array($module->dimensions)) {
  247                 $this->dimensions = array_merge($this->dimensions, $module->dimensions);
  248             }
  249 
  250             if (is_array($module->denormalizedDimensions)) {
  251 
  252                 $this->denormalizedDimensions = array_merge_recursive($this->denormalizedDimensions, $module->denormalizedDimensions);
  253             }
  254 
  255             //print_r($this->denormalizedDimensions);
  256         }
  257     }
  258 
  259     function _loadFormatters() {
  260 
  261         foreach ($this->modules as $k => $module) {
  262 
  263             if (is_array($module->formatters)) {
  264                 $this->formatters = array_merge($this->formatters, $module->formatters);
  265             }
  266         }
  267     }
  268 
  269     function _loadEventProcessors() {
  270 
  271         $processors = array();
  272 
  273         foreach ($this->modules as $k => $module) {
  274 
  275             $processors = array_merge($processors, $module->event_processors);
  276         }
  277 
  278         $this->setMap('event_processors', $processors);
  279 
  280     }
  281 
  282     /**
  283      * @return owa_serviceUser
  284      */
  285     function getCurrentUser() {
  286         if (!$this->isInit()) {
  287             throw new Exception('Current User Object could only be get if framework is initialized');
  288         }
  289         return $this->current_user;
  290     }
  291 
  292     function getRequest() {
  293 
  294         return $this->request;
  295     }
  296     
  297     function getRestApiRoute( $module, $version, $route_name, $request_method ) {
  298         
  299         if ( array_key_exists( $module, $this->restApiRoutes ) ) {
  300 
  301             if ( array_key_exists( $version, $this->restApiRoutes[$module] ) ) {
  302             
  303                 if ( array_key_exists( $route_name, $this->restApiRoutes[$module][ $version ] ) ) {
  304                 
  305                     if ( array_key_exists( $request_method, $this->restApiRoutes[$module][ $version ][ $route_name ] ) ) {
  306             
  307                         return $this->restApiRoutes[$module][ $version ][ $route_name ][ $request_method ] ;
  308                     }
  309                 }   
  310             }
  311         }
  312     }
  313     
  314     function setRestApiRoute( $module, $version, $route_name, $request_method, $value ) {
  315         
  316         $this->restApiRoutes[$module][$version][ $route_name ][ $request_method ] = $value;
  317     }
  318     
  319     function getAllRestApiRoutes() {
  320         
  321         return $this->restApiRoutes;
  322     }
  323 
  324     function getState() {
  325 
  326         return $this->request->state;
  327     }
  328 
  329     function getMapValue($map_name, $name) {
  330 
  331         if (array_key_exists($map_name, $this->maps)) {
  332 
  333             if ( $name && array_key_exists($name, $this->maps[$map_name])) {
  334 
  335                 return $this->maps[$map_name][$name];
  336             } else {
  337 
  338                 return false;
  339             }
  340         } else {
  341 
  342             return false;
  343         }
  344     }
  345 
  346     function getMap($name) {
  347 
  348         if (array_key_exists($name, $this->maps)) {
  349 
  350             return $this->maps[$name];
  351         }
  352 
  353     }
  354 
  355     function setMap($name, $map) {
  356 
  357         $this->maps[$name] = $map;
  358     }
  359 
  360     function setMapValue($map_name, $name, $value) {
  361 
  362         $this->maps[$map_name][$name] = $value;
  363     }
  364 
  365     function setUpdateRequired() {
  366 
  367         $this->update_required = true;
  368         return;
  369     }
  370 
  371     function isUpdateRequired() {
  372 
  373         return $this->update_required;
  374     }
  375 
  376     function addModule($module) {
  377 
  378         $this->modules[$module->name] = $module;
  379     }
  380 
  381     function markModuleAsNeedingUpdate($name) {
  382 
  383         $this->modules_needing_updates[] = $name;
  384     }
  385 
  386     function getModulesNeedingUpdates() {
  387 
  388         return $this->modules_needing_updates;
  389     }
  390 
  391 
  392     function setInstallRequired() {
  393         $this->install_required = true;
  394     }
  395 
  396     function isInstallRequired() {
  397 
  398         return $this->install_required;
  399     }
  400 
  401     function addEntity($entity_name, $class) {
  402 
  403         $this->entities[$entity_name] = $class;
  404     }
  405 
  406     function setInit() {
  407         $this->init = true;
  408     }
  409 
  410     function isInit() {
  411 
  412         return $this->init;
  413     }
  414 
  415     function getModule($name) {
  416 
  417         if (array_key_exists($name, $this->modules)) {
  418             return $this->modules[$name];
  419         } else {
  420             return false;
  421         }
  422 
  423     }
  424 
  425     function getAllModules() {
  426         return $this->modules;
  427     }
  428 
  429     function getMetricClasses($name) {
  430 
  431         if (array_key_exists($name, $this->metrics)) {
  432 
  433             return $this->metrics[$name];
  434         }
  435     }
  436 
  437     function getDimension($name) {
  438 
  439         if (array_key_exists($name, $this->dimensions)) {
  440             return $this->dimensions[$name];
  441         }
  442     }
  443 
  444     function getDenormalizedDimension($name, $entity) {
  445 
  446         //print_r($this->denormalizedDimensions);
  447         if (array_key_exists($name, $this->denormalizedDimensions)) {
  448             if (array_key_exists($entity, $this->denormalizedDimensions[$name])) {
  449                 return $this->denormalizedDimensions[$name][$entity];
  450             }
  451         }
  452     }
  453 
  454     function getFormatter($name) {
  455 
  456         if (array_key_exists($name, $this->formatters)) {
  457             return $this->formatters[$name];
  458         }
  459     }
  460 
  461     function getCliCommandClass($command) {
  462 
  463         return $this->getMapValue('cli_commands', $command);
  464     }
  465 
  466     function setCliCommandClass($command, $class) {
  467 
  468         $this->setMapValue('cli_commands', $command, $class);
  469     }
  470 
  471     function getApiMethodClass($method_name) {
  472 
  473         return $this->getMapValue('api_methods', $method_name);
  474     }
  475 
  476     function setApiMethodClass($method_name, $class) {
  477 
  478         $this->setMapValue('api_methods', $method_name, $class);
  479     }
  480 }
  481 
  482 
  483 ?>