View source
<?php
class SiteAuditCheckInsightsAnalyze extends SiteAuditCheckAbstract {
public function getLabel() {
return dt('Report');
}
public function getDescription() {
return dt('Analysis by the Google PageSpeed Insights service');
}
public function getResultFail() {
if (!empty($this->registry['errors'])) {
if (drush_get_option('html')) {
$ret_val = '<ul><li>' . implode('</li><li>', $this->registry['errors']) . '</li></ul>';
}
else {
$ret_val = implode(PHP_EOL, $this->registry['errors']);
}
return $ret_val;
}
$this
->getResultPass();
}
public function getResultInfo() {
}
public function getResultPass() {
if ($this->abort) {
return;
}
$ret_val = '';
if (drush_get_option('detail')) {
$stats = array();
foreach ($this->registry['json_result']->pageStats as $stat_name => $count) {
$formatted_stat_name = ucfirst(preg_replace('/(?<!^)((?<![[:upper:]])[[:upper:]]|[[:upper:]](?![[:upper:]]))/', ' $1', $stat_name));
if (stripos($stat_name, 'bytes') !== FALSE) {
$stats[$formatted_stat_name] = round($count / 1024, 2) . 'kB';
}
else {
$stats[$formatted_stat_name] = $count;
}
}
if (drush_get_option('html')) {
$ret_val .= '<h3>' . dt('Page stats') . '</h3>';
$ret_val .= '<dl class="dl-horizontal">';
foreach ($stats as $name => $count) {
$ret_val .= '<dt>' . $name . '</dt>';
$ret_val .= '<dd>' . $count . '</dd>';
}
$ret_val .= '</dl>';
}
else {
$ret_val .= dt('Page stats');
foreach ($stats as $name => $count) {
$ret_val .= PHP_EOL;
if (!drush_get_option('json')) {
$ret_val .= str_repeat(' ', 6);
}
$ret_val .= '- ' . $name . ': ' . $count;
}
}
$impact_filter = drush_get_option('impact');
if (drush_get_option('html')) {
$ret_val .= '<h3>' . dt('Detailed results') . '</h3>';
}
else {
$ret_val .= PHP_EOL . str_repeat(' ', 6) . dt('Detailed results:');
}
$rendered_result_count = 0;
foreach ($this->registry['json_result']->formattedResults->ruleResults as $resultValues) {
rtrim($ret_val);
if ($resultValues->ruleImpact < $impact_filter) {
continue;
}
$rendered_result_count++;
$impact = '';
if ($resultValues->ruleImpact >= 3) {
$impact = dt('(HIGH impact: @ruleImpact)', array(
'@ruleImpact' => $resultValues->ruleImpact,
));
}
elseif ($resultValues->ruleImpact > 0) {
$impact = dt('(low impact: @ruleImpact)', array(
'@ruleImpact' => $resultValues->ruleImpact,
));
}
$rule_score_impact = dt('@localizedRuleName: @ruleScore @impact', array(
'@localizedRuleName' => $resultValues->localizedRuleName,
'@ruleScore' => $resultValues->ruleScore,
'@impact' => $impact,
));
if (drush_get_option('html')) {
$ret_val .= '<div class="alert alert-block ';
if ($resultValues->ruleScore >= 80) {
$ret_val .= 'alert-success';
}
elseif ($resultValues->ruleScore >= 60) {
$ret_val .= 'alert-warning';
}
else {
$ret_val .= 'alert-danger';
}
$ret_val .= '">' . $rule_score_impact . '</div>';
}
else {
$ret_val .= PHP_EOL;
if (!drush_get_option('json')) {
$ret_val .= str_repeat(' ', 8);
}
$ret_val .= $rule_score_impact;
}
if (isset($resultValues->urlBlocks)) {
foreach ($resultValues->urlBlocks as $block) {
if (!isset($block->header->args)) {
$header = google_json_text_replacement($block->header->format);
}
else {
$header = google_json_text_replacement($block->header->format, $block->header->args);
}
$limit = drush_get_option('limit', 0);
if ($limit > 0 && isset($block->urls) && $limit != count($block->urls) && $limit < count($block->urls)) {
$header .= ' ' . dt('Showing @limit out of @total total:', array(
'@limit' => $limit,
'@total' => count($block->urls),
));
}
if (drush_get_option('html')) {
$ret_val .= '<blockquote>' . $header;
}
else {
$ret_val .= PHP_EOL;
if (!drush_get_option('json')) {
$ret_val .= str_repeat(' ', 10);
}
$ret_val .= $header;
}
if (isset($block->urls) && !empty($block->urls)) {
$urls = array();
$count = 0;
foreach ($block->urls as $url) {
if ($limit > 0) {
if (++$count > $limit) {
continue;
}
}
$urls[] = google_json_text_replacement($url->result->format, $url->result->args);
}
if (drush_get_option('html')) {
$ret_val .= '<small>' . dt('URLs:');
$ret_val .= '<ul><li>' . implode('</li><li>', $urls) . '</li></ul>';
$ret_val .= '</small>';
}
else {
foreach ($urls as $url) {
$ret_val .= PHP_EOL;
if (!drush_get_option('json')) {
$ret_val .= str_repeat(' ', 12);
}
$ret_val .= $url;
}
}
}
if (drush_get_option('html')) {
$ret_val .= '</blockquote>';
}
}
}
}
if ($rendered_result_count == 0) {
if ($impact_filter) {
$ret_val .= dt('Nice, no problems to report!');
}
else {
$ret_val .= dt('No results, which is unusual...');
}
}
}
return $ret_val;
}
public function getResultWarn() {
return $this
->getResultPass();
}
public function getAction() {
if ($this->score !== SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL) {
return dt('Full report at https://developers.google.com/speed/pagespeed/insights');
}
}
public function calculateScore() {
$pso_url = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed';
$pso_url .= '?url=' . $this->registry['url'];
$pso_url .= '&key=' . $this->registry['key'];
$request = drupal_http_request($pso_url);
$result = $request->data;
$this->registry['json_result'] = json_decode($result);
if (isset($this->registry['json_result']->error)) {
$this->abort = TRUE;
$this->registry['errors'] = array();
foreach ($this->registry['json_result']->error->errors as $error) {
$this->registry['errors'][] = dt('@message (@domain - @reason)', array(
'@message' => $error->message,
'@domain' => $error->domain,
'@reason' => $error->reason,
));
}
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
}
if (isset($this->registry['json_result']->responseCode) && $this->registry['json_result']->responseCode != 200) {
$this->abort = TRUE;
$this->registry['errors'] = array();
$this->registry['errors'][] = dt('@id is not accessible by PageSpeed Insights - response code (@responsecode)', array(
'@id' => $this->registry['json_result']->id,
'@responsecode' => $this->registry['json_result']->responseCode,
));
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
}
if ($this->registry['json_result']->score > 80) {
$this->percentOverride = $this->registry['json_result']->score;
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
}
elseif ($this->registry['json_result']->score > 60) {
$this->percentOverride = $this->registry['json_result']->score;
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
}
return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
}
}
function google_json_text_replacement($format, $args = array()) {
if (!$args || empty($args)) {
return $format;
}
$format_sprintf = preg_replace('/\\$\\d/', '%s', $format);
$values = array();
foreach ($args as $arg) {
$values[] = $arg->value;
}
return vsprintf($format_sprintf, $values);
}