diff options
author | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:39 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:40 +0400 |
commit | ae4b03163792f0b6e933933e5d37df87dc3fd566 (patch) | |
tree | d1d7510a9728f587d3d63ebd03e4ecf3d904838b /plugins/VisitorGenerator | |
parent | 158c2150f5f2e13ece459b8d131244c11b763997 (diff) |
Mass conversion of all files to the newly agreed coding standard: PSR 1/2
Converting Piwik core source files, PHP, JS, TPL, CSS
More info: http://piwik.org/participate/coding-standards/
Diffstat (limited to 'plugins/VisitorGenerator')
-rw-r--r-- | plugins/VisitorGenerator/Controller.php | 278 | ||||
-rw-r--r-- | plugins/VisitorGenerator/VisitorGenerator.php | 50 | ||||
-rw-r--r-- | plugins/VisitorGenerator/templates/generate.tpl | 19 | ||||
-rw-r--r-- | plugins/VisitorGenerator/templates/index.tpl | 58 |
4 files changed, 205 insertions, 200 deletions
diff --git a/plugins/VisitorGenerator/Controller.php b/plugins/VisitorGenerator/Controller.php index ec75c39ace..54ebc3aea0 100644 --- a/plugins/VisitorGenerator/Controller.php +++ b/plugins/VisitorGenerator/Controller.php @@ -15,144 +15,142 @@ */ class Piwik_VisitorGenerator_Controller extends Piwik_Controller_Admin { - public function index() - { - Piwik::checkUserIsSuperUser(); - - $sitesList = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(); - - $view = Piwik_View::factory('index'); - $this->setBasicVariablesView($view); - $view->assign('sitesList', $sitesList); - $view->nonce = Piwik_Nonce::getNonce('Piwik_VisitorGenerator.generate'); - $view->countActionsPerRun = count($this->getAccessLog()); - $view->accessLogPath = $this->getAccessLogPath(); - $view->menu = Piwik_GetAdminMenu(); - echo $view->render(); - } - private function getAccessLogPath() - { - return PIWIK_INCLUDE_PATH . "/plugins/VisitorGenerator/data/access.log"; - } - private function getAccessLog() - { - $log = file($this->getAccessLogPath()); - return $log; - } - - public function generate() - { - Piwik::checkUserIsSuperUser(); - $nonce = Piwik_Common::getRequestVar('form_nonce', '', 'string', $_POST); - if(Piwik_Common::getRequestVar('choice', 'no') != 'yes' || - !Piwik_Nonce::verifyNonce('Piwik_VisitorGenerator.generate', $nonce)) - { - Piwik::redirectToModule('VisitorGenerator', 'index'); - } - Piwik_Nonce::discardNonce('Piwik_VisitorGenerator.generate'); - - $daysToCompute = Piwik_Common::getRequestVar('daysToCompute', 1, 'int'); - - // get idSite from POST with fallback to GET - $idSite = Piwik_Common::getRequestVar('idSite', false, 'int', $_GET); - $idSite = Piwik_Common::getRequestVar('idSite', $idSite, 'int', $_POST); - - Piwik::setMaxExecutionTime(0); - - $timer = new Piwik_Timer; - $time = time() - ($daysToCompute-1)*86400; - - $nbActionsTotal = 0; - $dates = array(); - while($time <= time()) - { - $nbActionsTotalThisDay = $this->generateVisits($time, $idSite); - $dates[] = date("Y-m-d", $time); - $time += 86400; - $nbActionsTotal += $nbActionsTotalThisDay; - } - - $api = Piwik_CoreAdminHome_API::getInstance(); - $api->invalidateArchivedReports($idSite, implode($dates, ",")); - - $browserArchiving = Piwik_ArchiveProcessing::isBrowserTriggerArchivingEnabled(); - - // Init view - $view = Piwik_View::factory('generate'); - $this->setBasicVariablesView($view); - $view->menu = Piwik_GetAdminMenu(); - $view->assign('browserArchivingEnabled', $browserArchiving); - $view->assign('timer', $timer); - $view->assign('days', $daysToCompute); - $view->assign('nbActionsTotal', $nbActionsTotal); - $view->assign('nbRequestsPerSec', round($nbActionsTotal / $timer->getTime(),0)); - $view->assign('siteName', Piwik_Site::getNameFor($idSite)); - echo $view->render(); - } - - private function generateVisits($time = false, $idSite = 1) - { - $logs = $this->getAccessLog(); - if(empty($time)) $time = time(); - $date = date("Y-m-d", $time); - - $acceptLanguages = array( - "el,fi;q=0.5", - "de-de,de;q=0.8,en-us", - "pl,en-us;q=0.7,en;q=", - "zh-cn", - "fr-ca", - "en-us", - "en-gb", - "fr-be", - "fr,de-ch;q=0.5", - "fr", - "fr-ch", - "fr", - ); - $prefix = Piwik_Url::getCurrentUrlWithoutFileName() . "piwik.php"; - $count = 0; - foreach($logs as $log) - { - if (!preg_match('/^(\S+) \S+ \S+ \[(.*?)\] "GET (\S+.*?)" \d+ \d+ "(.*?)" "(.*?)"/', $log, $m)) { - continue; - } - $ip = $m[1]; - $time = $m[2]; - $url = $m[3]; - $referrer = $m[4]; - $ua = $m[5]; - - $start = strpos($url, 'piwik.php?') + strlen('piwik.php?'); - $url = substr($url, $start, strrpos($url, " ")-$start); - $datetime = $date . " " . Piwik_Date::factory($time)->toString("H:i:s"); - $ip = strlen($ip) < 10 ? "13.5.111.3" : $ip; - - // Force date/ip & authenticate - $url .= "&cdt=" . urlencode($datetime); - if(strpos($url, 'cip') === false) - { - $url .= "&cip=" . $ip; - } - $url .= "&token_auth=" . Piwik::getCurrentUserTokenAuth(); - $url = $prefix . "?" . $url; - - // Make order IDs unique per day - $url = str_replace("ec_id=", "ec_id=$date-", $url); - - // Disable provider plugin - $url .= "&dp=1"; - - // Replace idsite - $url = preg_replace("/idsite=[0-9]+/", "idsite=$idSite", $url); - - $acceptLanguage = $acceptLanguages[$count % count($acceptLanguages)]; - - if($output = Piwik_Http::sendHttpRequest($url, $timeout = 5, $ua, $path = null, $follow = 0, $acceptLanguage)) - { - $count++; - } - } - return $count; - } + public function index() + { + Piwik::checkUserIsSuperUser(); + + $sitesList = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(); + + $view = Piwik_View::factory('index'); + $this->setBasicVariablesView($view); + $view->assign('sitesList', $sitesList); + $view->nonce = Piwik_Nonce::getNonce('Piwik_VisitorGenerator.generate'); + $view->countActionsPerRun = count($this->getAccessLog()); + $view->accessLogPath = $this->getAccessLogPath(); + $view->menu = Piwik_GetAdminMenu(); + echo $view->render(); + } + + private function getAccessLogPath() + { + return PIWIK_INCLUDE_PATH . "/plugins/VisitorGenerator/data/access.log"; + } + + private function getAccessLog() + { + $log = file($this->getAccessLogPath()); + return $log; + } + + public function generate() + { + Piwik::checkUserIsSuperUser(); + $nonce = Piwik_Common::getRequestVar('form_nonce', '', 'string', $_POST); + if (Piwik_Common::getRequestVar('choice', 'no') != 'yes' || + !Piwik_Nonce::verifyNonce('Piwik_VisitorGenerator.generate', $nonce) + ) { + Piwik::redirectToModule('VisitorGenerator', 'index'); + } + Piwik_Nonce::discardNonce('Piwik_VisitorGenerator.generate'); + + $daysToCompute = Piwik_Common::getRequestVar('daysToCompute', 1, 'int'); + + // get idSite from POST with fallback to GET + $idSite = Piwik_Common::getRequestVar('idSite', false, 'int', $_GET); + $idSite = Piwik_Common::getRequestVar('idSite', $idSite, 'int', $_POST); + + Piwik::setMaxExecutionTime(0); + + $timer = new Piwik_Timer; + $time = time() - ($daysToCompute - 1) * 86400; + + $nbActionsTotal = 0; + $dates = array(); + while ($time <= time()) { + $nbActionsTotalThisDay = $this->generateVisits($time, $idSite); + $dates[] = date("Y-m-d", $time); + $time += 86400; + $nbActionsTotal += $nbActionsTotalThisDay; + } + + $api = Piwik_CoreAdminHome_API::getInstance(); + $api->invalidateArchivedReports($idSite, implode($dates, ",")); + + $browserArchiving = Piwik_ArchiveProcessing::isBrowserTriggerArchivingEnabled(); + + // Init view + $view = Piwik_View::factory('generate'); + $this->setBasicVariablesView($view); + $view->menu = Piwik_GetAdminMenu(); + $view->assign('browserArchivingEnabled', $browserArchiving); + $view->assign('timer', $timer); + $view->assign('days', $daysToCompute); + $view->assign('nbActionsTotal', $nbActionsTotal); + $view->assign('nbRequestsPerSec', round($nbActionsTotal / $timer->getTime(), 0)); + $view->assign('siteName', Piwik_Site::getNameFor($idSite)); + echo $view->render(); + } + + private function generateVisits($time = false, $idSite = 1) + { + $logs = $this->getAccessLog(); + if (empty($time)) $time = time(); + $date = date("Y-m-d", $time); + + $acceptLanguages = array( + "el,fi;q=0.5", + "de-de,de;q=0.8,en-us", + "pl,en-us;q=0.7,en;q=", + "zh-cn", + "fr-ca", + "en-us", + "en-gb", + "fr-be", + "fr,de-ch;q=0.5", + "fr", + "fr-ch", + "fr", + ); + $prefix = Piwik_Url::getCurrentUrlWithoutFileName() . "piwik.php"; + $count = 0; + foreach ($logs as $log) { + if (!preg_match('/^(\S+) \S+ \S+ \[(.*?)\] "GET (\S+.*?)" \d+ \d+ "(.*?)" "(.*?)"/', $log, $m)) { + continue; + } + $ip = $m[1]; + $time = $m[2]; + $url = $m[3]; + $referrer = $m[4]; + $ua = $m[5]; + + $start = strpos($url, 'piwik.php?') + strlen('piwik.php?'); + $url = substr($url, $start, strrpos($url, " ") - $start); + $datetime = $date . " " . Piwik_Date::factory($time)->toString("H:i:s"); + $ip = strlen($ip) < 10 ? "13.5.111.3" : $ip; + + // Force date/ip & authenticate + $url .= "&cdt=" . urlencode($datetime); + if (strpos($url, 'cip') === false) { + $url .= "&cip=" . $ip; + } + $url .= "&token_auth=" . Piwik::getCurrentUserTokenAuth(); + $url = $prefix . "?" . $url; + + // Make order IDs unique per day + $url = str_replace("ec_id=", "ec_id=$date-", $url); + + // Disable provider plugin + $url .= "&dp=1"; + + // Replace idsite + $url = preg_replace("/idsite=[0-9]+/", "idsite=$idSite", $url); + + $acceptLanguage = $acceptLanguages[$count % count($acceptLanguages)]; + + if ($output = Piwik_Http::sendHttpRequest($url, $timeout = 5, $ua, $path = null, $follow = 0, $acceptLanguage)) { + $count++; + } + } + return $count; + } } diff --git a/plugins/VisitorGenerator/VisitorGenerator.php b/plugins/VisitorGenerator/VisitorGenerator.php index 6249ab4df5..ebeb94e976 100644 --- a/plugins/VisitorGenerator/VisitorGenerator.php +++ b/plugins/VisitorGenerator/VisitorGenerator.php @@ -10,32 +10,36 @@ */ /** - * + * * @package Piwik_VisitorGenerator */ -class Piwik_VisitorGenerator extends Piwik_Plugin { +class Piwik_VisitorGenerator extends Piwik_Plugin +{ - public function getInformation() { - return array( - 'description' => Piwik_Translate('VisitorGenerator_PluginDescription'), - 'author' => 'Piwik', - 'author_homepage' => 'http://piwik.org/', - 'version' => Piwik_Version::VERSION, - ); - } + public function getInformation() + { + return array( + 'description' => Piwik_Translate('VisitorGenerator_PluginDescription'), + 'author' => 'Piwik', + 'author_homepage' => 'http://piwik.org/', + 'version' => Piwik_Version::VERSION, + ); + } - public function getListHooksRegistered() { - return array( - 'AdminMenu.add' => 'addMenu', - ); - } + public function getListHooksRegistered() + { + return array( + 'AdminMenu.add' => 'addMenu', + ); + } - public function addMenu() { - Piwik_AddAdminSubMenu( - 'CoreAdminHome_MenuDiagnostic', 'VisitorGenerator_VisitorGenerator', - array('module' => 'VisitorGenerator', 'action' => 'index'), - Piwik::isUserIsSuperUser(), - $order = 20 - ); - } + public function addMenu() + { + Piwik_AddAdminSubMenu( + 'CoreAdminHome_MenuDiagnostic', 'VisitorGenerator_VisitorGenerator', + array('module' => 'VisitorGenerator', 'action' => 'index'), + Piwik::isUserIsSuperUser(), + $order = 20 + ); + } } diff --git a/plugins/VisitorGenerator/templates/generate.tpl b/plugins/VisitorGenerator/templates/generate.tpl index 12d13fdac3..370d40b794 100644 --- a/plugins/VisitorGenerator/templates/generate.tpl +++ b/plugins/VisitorGenerator/templates/generate.tpl @@ -2,16 +2,17 @@ <h2>{'VisitorGenerator_VisitorGenerator'|translate}</h2> -Generated visits for {$siteName} and for {'General_LastDays'|translate:$days}.<br /> -Generated {'General_NbActions'|translate}: {$nbActionsTotal}<br /> -{'VisitorGenerator_NbRequestsPerSec'|translate}: {$nbRequestsPerSec}<br /> +Generated visits for {$siteName} and for {'General_LastDays'|translate:$days}.<br/> +Generated {'General_NbActions'|translate}: {$nbActionsTotal}<br/> +{'VisitorGenerator_NbRequestsPerSec'|translate}: {$nbRequestsPerSec}<br/> {$timer}</p> <p><strong> -{if $browserArchivingEnabled} -The reports will be reprocessed the next time you visit the Piwik reports, it might take a few minutes. -{else} -Please re-run the archive.php Piwik script in the crontab to refresh the reports. <a href="http://piwik.org/docs/setup-auto-archiving/">See "How to Set up Auto-Archiving of Your Reports"</a> -{/if} -</strong> + {if $browserArchivingEnabled} + The reports will be reprocessed the next time you visit the Piwik reports, it might take a few minutes. + {else} + Please re-run the archive.php Piwik script in the crontab to refresh the reports. + <a href="http://piwik.org/docs/setup-auto-archiving/">See "How to Set up Auto-Archiving of Your Reports"</a> + {/if} + </strong> </p> {include file="CoreAdminHome/templates/footer.tpl"} diff --git a/plugins/VisitorGenerator/templates/index.tpl b/plugins/VisitorGenerator/templates/index.tpl index 944b82dee2..f3477285ac 100644 --- a/plugins/VisitorGenerator/templates/index.tpl +++ b/plugins/VisitorGenerator/templates/index.tpl @@ -2,36 +2,38 @@ <h2>{'VisitorGenerator_VisitorGenerator'|translate}</h2> <p>{'VisitorGenerator_PluginDescription'|translate}</p> -<p>You can overwrite the log file that is used to generate fake visits (change {$accessLogPath}). This is a log file of requests to "piwik.php" in the format "Apache combined log".</p> +<p>You can overwrite the log file that is used to generate fake visits (change {$accessLogPath}). This is a log file of requests to "piwik.php" in the format + "Apache combined log".</p> <form method="POST" action="{url module=VisitorGenerator action=generate}"> -<table class="adminTable" style="width: 600px;"> -<tr> - <td><label for="idSite">{'General_ChooseWebsite'|translate}</label></td> - <td> - {include file="CoreHome/templates/sites_selection.tpl" - showAllSitesItem=false showSelectedSite=true switchSiteOnSelect=false inputName=idSite} - </td> -</tr> -<tr> - <td><label for="daysToCompute">{'VisitorGenerator_DaysToCompute'|translate}</label></td> - <td><input type="text" value="1" name="daysToCompute" /></td> -</tr> -</table> -{'VisitorGenerator_Warning'|translate}<br /> -{'VisitorGenerator_NotReversible'|translate:'<b>':'</b>'}<br /><br /> -<p><strong>This will generate approximately {$countActionsPerRun} fake actions on this site for each day</strong>.<br/> -</p> -{'VisitorGenerator_AreYouSure'|translate}<br /><br/> -<input type="checkbox" name="choice" id="choice" value="yes" /> <label for="choice">{'VisitorGenerator_ChoiceYes'|translate}</label> -<br /> -<input type="hidden" value="{$token_auth}" name="token_auth" /> -<input type="hidden" value="{$nonce}" name="form_nonce" /> -<br/> + <table class="adminTable" style="width: 600px;"> + <tr> + <td><label for="idSite">{'General_ChooseWebsite'|translate}</label></td> + <td> + {include file="CoreHome/templates/sites_selection.tpl" + showAllSitesItem=false showSelectedSite=true switchSiteOnSelect=false inputName=idSite} + </td> + </tr> + <tr> + <td><label for="daysToCompute">{'VisitorGenerator_DaysToCompute'|translate}</label></td> + <td><input type="text" value="1" name="daysToCompute"/></td> + </tr> + </table> + {'VisitorGenerator_Warning'|translate}<br/> + {'VisitorGenerator_NotReversible'|translate:'<b>':'</b>'}<br/><br/> - NOTE: It might take a few minutes to generate visits and actions, please be patient...<br/> - There is also a faster tool that will import large test data in Piwik, see the <a href='https://github.com/piwik/piwik/tree/master/tests#testing-data'>README</a>.</p> - <br/> -<input type="submit" value="{'VisitorGenerator_Submit'|translate}" name="submit" class="submit" /> + <p><strong>This will generate approximately {$countActionsPerRun} fake actions on this site for each day</strong>.<br/> + </p> + {'VisitorGenerator_AreYouSure'|translate}<br/><br/> + <input type="checkbox" name="choice" id="choice" value="yes"/> <label for="choice">{'VisitorGenerator_ChoiceYes'|translate}</label> + <br/> + <input type="hidden" value="{$token_auth}" name="token_auth"/> + <input type="hidden" value="{$nonce}" name="form_nonce"/> + <br/> + + NOTE: It might take a few minutes to generate visits and actions, please be patient...<br/> + There is also a faster tool that will import large test data in Piwik, see the <a href='https://github.com/piwik/piwik/tree/master/tests#testing-data'>README</a>.</p> + <br/> + <input type="submit" value="{'VisitorGenerator_Submit'|translate}" name="submit" class="submit"/> </form> {include file="CoreAdminHome/templates/footer.tpl"} |