示例#1
0
 /**
  * Initializes Profiling via XHProf.
  * See: https://github.com/piwik/piwik/blob/master/tests/README.xhprof.md
  */
 public static function setupProfilerXHProf($mainRun = false)
 {
     if (SettingsServer::isTrackerApiRequest()) {
         // do not profile Tracker
         return;
     }
     $path = PIWIK_INCLUDE_PATH . '/tests/lib/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php';
     if (!file_exists($path)) {
         return;
     }
     if (!function_exists('xhprof_enable')) {
         return;
     }
     if (!is_writable(ini_get("xhprof.output_dir"))) {
         throw new \Exception("The profiler output dir '" . ini_get("xhprof.output_dir") . "' should exist and be writable.");
     }
     require_once $path;
     require_once PIWIK_INCLUDE_PATH . '/tests/lib/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php';
     if (!function_exists('xhprof_error')) {
         function xhprof_error($out)
         {
             echo substr($out, 0, 300) . '...';
         }
     }
     $currentGitBranch = SettingsPiwik::getCurrentGitBranch();
     $profilerNamespace = "piwik";
     if ($currentGitBranch != 'master') {
         $profilerNamespace .= "." . $currentGitBranch;
     }
     xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
     if ($mainRun) {
         self::setProfilingRunIds(array());
     }
     register_shutdown_function(function () use($profilerNamespace, $mainRun) {
         $xhprofData = xhprof_disable();
         $xhprofRuns = new \XHProfRuns_Default();
         $runId = $xhprofRuns->save_run($xhprofData, $profilerNamespace);
         if (empty($runId)) {
             die('could not write profiler run');
         }
         $runs = self::getProfilingRunIds();
         $runs[] = $runId;
         //            $weights = array_fill(0, count($runs), 1);
         //            $aggregate = xhprof_aggregate_runs($xhprofRuns, $runs, $weights, $profilerNamespace);
         //            $runId = $xhprofRuns->save_run($aggregate, $profilerNamespace);
         if ($mainRun) {
             $runIds = implode(',', $runs);
             $out = "\n\n";
             $baseUrl = "http://" . @$_SERVER['HTTP_HOST'] . "/" . @$_SERVER['REQUEST_URI'];
             $baseUrlStored = SettingsPiwik::getPiwikUrl();
             if (strlen($baseUrlStored) > strlen($baseUrl)) {
                 $baseUrl = $baseUrlStored;
             }
             $baseUrl = "\n" . $baseUrl . "tests/lib/xhprof-0.9.4/xhprof_html/?source={$profilerNamespace}&run=";
             $out .= "Profiler report is available at:";
             $out .= $baseUrl . $runId;
             if ($runId != $runIds) {
                 $out .= "\n\nProfiler Report aggregating all runs triggered from this process: ";
                 $out .= $baseUrl . $runIds;
             }
             $out .= "\n\n";
             echo $out;
         } else {
             self::setProfilingRunIds($runs);
         }
     });
 }
示例#2
0
 /**
  * Get file integrity information (in PIWIK_INCLUDE_PATH).
  *
  * @return array(bool, string, ...) Return code (true/false), followed by zero or more error messages
  */
 public static function getFileIntegrityInformation()
 {
     $messages = array();
     $messages[] = true;
     $manifest = PIWIK_INCLUDE_PATH . '/config/manifest.inc.php';
     if (file_exists($manifest)) {
         require_once $manifest;
     }
     if (!class_exists('Piwik\\Manifest')) {
         $git = SettingsPiwik::getCurrentGitBranch();
         if (empty($git)) {
             $messages[] = Piwik::translate('General_WarningFileIntegrityNoManifest') . " If you are deploying Piwik from Git, this message is normal.";
         }
         return $messages;
     }
     $files = \Piwik\Manifest::$files;
     $hasMd5file = function_exists('md5_file');
     $hasMd5 = function_exists('md5');
     foreach ($files as $path => $props) {
         $file = PIWIK_INCLUDE_PATH . '/' . $path;
         if (!file_exists($file)) {
             $messages[] = Piwik::translate('General_ExceptionMissingFile', $file);
         } else {
             if (filesize($file) != $props[0]) {
                 if (!$hasMd5 || in_array(substr($path, -4), array('.gif', '.ico', '.jpg', '.png', '.swf'))) {
                     // files that contain binary data (e.g., images) must match the file size
                     $messages[] = Piwik::translate('General_ExceptionFilesizeMismatch', array($file, $props[0], filesize($file)));
                 } else {
                     // convert end-of-line characters and re-test text files
                     $content = @file_get_contents($file);
                     $content = str_replace("\r\n", "\n", $content);
                     if (strlen($content) != $props[0] || @md5($content) !== $props[1]) {
                         $messages[] = Piwik::translate('General_ExceptionFilesizeMismatch', array($file, $props[0], filesize($file)));
                     }
                 }
             } else {
                 if ($hasMd5file && @md5_file($file) !== $props[1]) {
                     $messages[] = Piwik::translate('General_ExceptionFileIntegrity', $file);
                 }
             }
         }
     }
     if (count($messages) > 1) {
         $messages[0] = false;
     }
     if (!$hasMd5file) {
         $messages[] = Piwik::translate('General_WarningFileIntegrityNoMd5file');
     }
     return $messages;
 }