📄 generator.php
字号:
<?php/** * Piwik - Open source web analytics * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later * @version $Id: Generator.php 492 2008-05-23 01:08:12Z matt $ * * @package Piwik_Tracker *//** * Class used to generate fake visits. * Useful to test performances, general functional testing, etc. * * Objective: * Generate thousands of visits / actions per visitor using * a single request to misc/generateVisits.php * * Requirements of the visits generator script. Fields that can be edited: * - url => campaigns * - newsletter * - partner * - campaign CPC * - referer * - search engine * - misc site * - same website * - url => multiple directories, page names * - multiple idsite * - multiple settings configurations * - action_name * - HTML title * * * @package Piwik_Tracker * @subpackage Piwik_Tracker_Generator * * "Le Generator, il est trop Fort!" * - Random fan */class Piwik_Tracker_Generator{ /** * GET parameters array of values to be used for the current visit * * @var array ('res' => '1024x768', 'urlref' => 'http://google.com/search?q=piwik', ...) */ protected $currentget = array(); /** * Array of all the potential values for the visit parameters * Values of 'resolution', 'urlref', etc. will be randomly read from this array * * @var array ( * 'res' => array('1024x768','800x600'), * 'urlref' => array('google.com','intel.com','amazon.com'), * ....) */ protected $allget = array(); /** * See @see setMaximumUrlDepth * * @var int */ protected $maximumUrlDepth = 1; /** * Unix timestamp to use for the generated visitor * * @var int Unix timestamp */ protected $timestampToUse; /** * See @see disableProfiler() * The profiler is enabled by default * * @var bool */ protected $profiling = true; /** * If set to true, this will TRUNCATE the profiling tables at every new generated visit * @see initProfiler() * * @var bool */ public $reinitProfilingAtEveryRequest = true; /** * Hostname used to prefix all the generated URLs * we could make this variable dynamic so that a visitor can make hit on several hosts and * only the good ones should be kept (feature not yet implemented in piwik) * * @var string */ public $host = 'http://localhost'; /** * IdSite to generate visits for (@see setIdSite()) * * @var int */ public $idSite = 1; /** * Overwrite the global GET/POST/COOKIE variables and set the fake ones @see setFakeRequest() * Reads the configuration file but disables write to this file * Creates the database object & enable profiling by default (@see disableProfiler()) * */ public function __construct() { $_COOKIE = $_GET = $_REQUEST = $_POST = array(); // init GET and REQUEST to the empty array $this->setFakeRequest(); require_once "core/Piwik.php"; Piwik::createConfigObject('../config/config.ini.php'); Zend_Registry::get('config')->doWriteFileWhenUpdated = false; // setup database Piwik::createDatabaseObject(); Piwik_Tracker_Db::enableProfiling(); $this->timestampToUse = time(); } /** * Sets the depth level of the generated URLs * value = 1 => path OR path/page1 * value = 2 => path OR path/pageRand OR path/dir1/pageRand * * @param int Depth */ public function setMaximumUrlDepth($value) { $this->maximumUrlDepth = (int)$value; } /** * Set the timestamp to use as the starting time for the visitors times * You have to call this method for every day you want to generate data * * @param int Unix timestamp */ public function setTimestampToUse($timestamp) { $this->timestampToUse = $timestamp; } /** * Returns the timestamp to be used as the visitor timestamp * * @return int Unix timestamp */ public function getTimestampToUse() { return $this->timestampToUse; } /** * Set the idsite to generate the visits for * To be called before init() * * @param int idSite */ public function setIdSite($idSite) { $this->idSite = $idSite; } /** * Add a value to the GET global array. * The generator script will then randomly read a value from this array. * * For example, $name = 'res' $aValue = '1024x768' * * @param string Name of the parameter _GET[$name] * @param array|mixed Value of the parameter * @return void */ protected function addParam( $name, $aValue) { if(is_array($aValue)) { $this->allget[$name] = array_merge( $aValue, (array)@$this->allget[$name]); } else { $this->allget[$name][] = $aValue; } } /** * TRUNCATE all logs related tables to start a fresh logging database. * Be careful, any data deleted this way is deleted forever * * @return void */ public function emptyAllLogTables() { $db = Zend_Registry::get('db'); $db->query('TRUNCATE TABLE '.Piwik::prefixTable('log_action')); $db->query('TRUNCATE TABLE '.Piwik::prefixTable('log_visit')); $db->query('TRUNCATE TABLE '.Piwik::prefixTable('log_link_visit_action')); } /** * Call this method to disable the SQL query profiler */ public function disableProfiler() { $this->profiling = false; Piwik_Tracker_Db::disableProfiling(); } /** * This is called at the end of the Generator script. * Calls the Profiler output if the profiler is enabled. * * @return void */ public function end() { Piwik_Tracker::disconnectDatabase(); if($this->profiling) { Piwik::printSqlProfilingReportTracker(); } } /** * Init the Generator script: * - init the SQL profiler * - init the random generator * - setup the different possible values for parameters such as 'resolution', * 'color', 'hour', 'minute', etc. * - load from DataFiles and setup values for the other parameters such as UserAgent, Referers, AcceptedLanguages, etc. * @see /misc/generateVisitsData/ * * @return void */ public function init() { Piwik::createLogObject(); $this->initProfiler(); /* * Init the random number generator */ function make_seed() { list($usec, $sec) = explode(' ', microtime()); return (float) $sec + ((float) $usec * 100000); } mt_srand(make_seed()); /* * Sets values for: resolutions, colors, idSite, times */ $common = array( 'res' => array('1289x800','1024x768','800x600','564x644','200x100','50x2000',), 'col' => array(24,32,16), 'idsite'=> $this->idSite, 'h' => range(0,23), 'm' => range(0,59), 's' => range(0,59), ); foreach($common as $label => $values) { $this->addParam($label,$values); } /* * Sets values for: outlinks, downloads, campaigns */ // we get the name of the Download/outlink variables $downloadOrOutlink = array( Piwik_Tracker_Config::getInstance()->Tracker['download_url_var_name'], Piwik_Tracker_Config::getInstance()->Tracker['outlink_url_var_name'], ); // we have a 20% chance to add a download or outlink variable to the URL $this->addParam('piwik_downloadOrOutlink', $downloadOrOutlink); $this->addParam('piwik_downloadOrOutlink', array_fill(0,8,'')); // we get the variables name for the campaign parameters $campaigns = array( Piwik_Tracker_Config::getInstance()->Tracker['campaign_var_name'], Piwik_Tracker_Config::getInstance()->Tracker['newsletter_var_name'], Piwik_Tracker_Config::getInstance()->Tracker['partner_var_name'], ); // we generate a campaign in the URL in 3/18 % of the generated URls $this->addParam('piwik_vars_campaign', $campaigns); $this->addParam('piwik_vars_campaign', array_fill(0,15,'')); /* * Sets values for: Referers, user agents, accepted languages */ // we load some real referers to be used by the generator $referers = array(); require_once "misc/generateVisitsData/Referers.php"; $this->addParam('urlref',$referers); // and we add 2000 empty referers so that some visitors don't come using a referer (direct entry) $this->addParam('urlref',array_fill(0,2000,'')); // load some user agent and accept language $userAgent = $acceptLanguages = array(); require_once "misc/generateVisitsData/UserAgent.php"; require_once "misc/generateVisitsData/AcceptLanguage.php"; $this->userAgents=$userAgent; $this->acceptLanguage=$acceptLanguages; } /** * If the SQL profiler is enabled and if the reinit at every request is set to true, * then we TRUNCATE the profiling information so that we only profile one visitor at a time * * @return void */ protected function initProfiler() { /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -