示例#1
0
 private function call($id, $url, $params)
 {
     if (!defined('PIWIK_INCLUDE_PATH')) {
         return false;
     }
     if (PIWIK_INCLUDE_PATH === FALSE) {
         return array('result' => 'error', 'message' => __('Could not resolve', 'wp-piwik') . ' "' . htmlentities(self::$settings->getGlobalOption('piwik_path')) . '": ' . __('realpath() returns false', 'wp-piwik') . '.');
     }
     if (file_exists(PIWIK_INCLUDE_PATH . "/index.php")) {
         require_once PIWIK_INCLUDE_PATH . "/index.php";
     }
     if (file_exists(PIWIK_INCLUDE_PATH . "/core/API/Request.php")) {
         require_once PIWIK_INCLUDE_PATH . "/core/API/Request.php";
     }
     if (class_exists('\\Piwik\\Application\\Environment') && !self::$piwikEnvironment) {
         // Piwik 2.14.* compatibility fix
         self::$piwikEnvironment = new \Piwik\Application\Environment(null);
         self::$piwikEnvironment->init();
     }
     if (class_exists('Piwik\\FrontController')) {
         \Piwik\FrontController::getInstance()->init();
     } else {
         return array('result' => 'error', 'message' => __('Class Piwik\\FrontController does not exists.', 'wp-piwik'));
     }
     if (class_exists('Piwik\\API\\Request')) {
         $request = new \Piwik\API\Request($params . '&token_auth=' . self::$settings->getGlobalOption('piwik_token'));
     } else {
         return array('result' => 'error', 'message' => __('Class Piwik\\API\\Request does not exists.', 'wp-piwik'));
     }
     if (isset($request)) {
         $result = $request->process();
     } else {
         $result = null;
     }
     if (!headers_sent()) {
         header("Content-Type: text/html", true);
     }
     $result = $this->unserialize($result);
     if ($GLOBALS['wp-piwik_debug']) {
         self::$debug[$id] = array($params . '&token_auth=...');
     }
     return $result;
 }
    /**
     * execute the piwik archive task
     *
     * @return	boolean  always returns true
     */
    public function execute()
    {
        //set execution time
        ini_set('max_execution_time', 0);
        //find piwik
        $piwikScriptPath = dirname(dirname(__FILE__)) . '/../../piwik/piwik';
        file_put_contents('e:\\log.txt', $piwikScriptPath);
        define('PIWIK_INCLUDE_PATH', $piwikScriptPath);
        define('PIWIK_ENABLE_DISPATCH', FALSE);
        define('PIWIK_ENABLE_ERROR_HANDLER', FALSE);
        define('PIWIK_DISPLAY_ERRORS', FALSE);
        ini_set('display_errors', 0);
        require_once PIWIK_INCLUDE_PATH . '/index.php';
        require_once PIWIK_INCLUDE_PATH . '/core/API/Request.php';
        \Piwik\FrontController::getInstance()->init();
        $piwikConfig = parse_ini_file($piwikScriptPath . '/config/config.ini.php', TRUE);
        //log
        $this->writeLog('EXT:piwikintegration cronjob');
        //get API key
        $request = new \Piwik\API\Request('
			module=API
			&method=UsersManager.getTokenAuth
			&userLogin='******'superuser']['login'] . '
			&md5Password='******'superuser']['password'] . '
			&format=php
			&serialize=0');
        $tokenAuth = $request->process();
        //get all piwik siteid's
        $request = new \Piwik\API\Request('
			module=API
			&method=SitesManager.getSitesWithAdminAccess
			&token_auth=' . $tokenAuth . '
			&format=php
			&serialize=0');
        $piwikSiteIds = $request->process();
        //log
        $this->writeLog('EXT:piwikintegration got ' . count($piwikSiteIds) . ' siteids and piwik token (' . $tokenAuth . '), start archiving ');
        //create Archive in piwik
        $periods = array('day', 'week', 'month', 'year');
        //iterate through sites
        //can be done with allSites, but this cannot create the logentries
        foreach ($periods as $period) {
            foreach ($piwikSiteIds as $siteId) {
                $starttime = microtime(TRUE);
                $request = new \Piwik\API\Request('
				            module=API
							&method=VisitsSummary.getVisits
							&idSite=' . intval($siteId['idsite']) . '
							&period=' . $period . '
							&date=last52
							&format=xml
							&token_auth=' . $tokenAuth . '"
				');
                $request->process();
                //log
                $this->writeLog('EXT:piwikintegration period ' . $period . ' (' . $siteId['idsite'] . ') ' . $siteId['name'] . ' (' . round(microtime(TRUE) - $starttime, 3) . 's)');
            }
        }
        //log
        $this->writeLog('EXT:piwikintegration cronjob ended');
        return TRUE;
    }