' . t('About') . ''; $output .= '
' . t('The Statistics module shows you how often content is viewed. This is useful in determining which pages of your site are most popular. For more information, see the online documentation for the Statistics module.', array('!statistics_do' => 'https://drupal.org/documentation/modules/statistics/')) . '
'; $output .= '' . t('Settings for the statistical information that Drupal will keep about the site.') . '
'; } } /** * Implements hook_permission(). */ function statistics_permission() { return array( 'administer statistics' => array( 'title' => t('Administer statistics'), ), 'view post access counter' => array( 'title' => t('View content hits'), ), ); } /** * Implements hook_node_view(). */ function statistics_node_view(EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) { if (!$node->isNew() && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) { $node->content['statistics_content_counter']['#attached']['library'][] = array('statistics', 'drupal.statistics'); $settings = array('data' => array('nid' => $node->id()), 'url' => url(drupal_get_path('module', 'statistics') . '/statistics.php')); $node->content['statistics_content_counter']['#attached']['js'][] = array( 'data' => array('statistics' => $settings), 'type' => 'setting', ); } } /** * Implements hook_node_links_alter(). */ function statistics_node_links_alter(array &$node_links, NodeInterface $entity, array &$context) { if ($context['view_mode'] != 'rss') { if (\Drupal::currentUser()->hasPermission('view post access counter')) { $statistics = statistics_get($entity->id()); if ($statistics) { $links['statistics_counter']['title'] = format_plural($statistics['totalcount'], '1 view', '@count views'); $node_links['statistics'] = array( '#theme' => 'links__node__statistics', '#links' => $links, '#attributes' => array('class' => array('links', 'inline')), ); } } } } /** * Implements hook_menu_link_defaults(). */ function statistics_menu_link_defaults() { $links['statistics.admin.config.system.statistics'] = array( 'link_title' => 'Statistics', 'description' => 'Control details about what and how your site logs content statistics.', 'route_name' => 'statistics.settings', 'parent' => 'system.admin.config.system', 'weight' => -15, ); return $links; } /** * Implements hook_cron(). */ function statistics_cron() { $statistics_timestamp = \Drupal::state()->get('statistics.day_timestamp') ?: 0; if ((REQUEST_TIME - $statistics_timestamp) >= 86400) { // Reset day counts. db_update('node_counter') ->fields(array('daycount' => 0)) ->execute(); \Drupal::state()->set('statistics.day_timestamp', REQUEST_TIME); } // Calculate the maximum of node views, for node search ranking. \Drupal::state()->set('statistics.node_counter_scale', 1.0 / max(1.0, db_query('SELECT MAX(totalcount) FROM {node_counter}')->fetchField())); } /** * Returns the most viewed content of all time, today, or the last-viewed node. * * @param string $dbfield * The database field to use, one of: * - 'totalcount': Integer that shows the top viewed content of all time. * - 'daycount': Integer that shows the top viewed content for today. * - 'timestamp': Integer that shows only the last viewed node. * @param int $dbrows * The number of rows to be returned. * * @return SelectQuery|FALSE * A query result containing the node ID, title, user ID that owns the node, * and the username for the selected node(s), or FALSE if the query could not * be executed correctly. */ function statistics_title_list($dbfield, $dbrows) { if (in_array($dbfield, array('totalcount', 'daycount', 'timestamp'))) { $query = db_select('node_field_data', 'n'); $query->addTag('node_access'); $query->join('node_counter', 's', 'n.nid = s.nid'); $query->join('users', 'u', 'n.uid = u.uid'); return $query ->fields('n', array('nid', 'title')) ->fields('u', array('uid', 'name')) ->condition($dbfield, 0, '<>') ->condition('n.status', 1) // @todo This should be actually filtering on the desired node status // field language and just fall back to the default language. ->condition('n.default_langcode', 1) ->orderBy($dbfield, 'DESC') ->range(0, $dbrows) ->execute(); } return FALSE; } /** * Retrieves a node's "view statistics". * * @param $nid * The node ID. * * @return * An associative array containing: * - totalcount: Integer for the total number of times the node has been * viewed. * - daycount: Integer for the total number of times the node has been viewed * "today". For the daycount to be reset, cron must be enabled. * - timestamp: Integer for the timestamp of when the node was last viewed. */ function statistics_get($nid) { if ($nid > 0) { // Retrieve an array with both totalcount and daycount. return db_query('SELECT totalcount, daycount, timestamp FROM {node_counter} WHERE nid = :nid', array(':nid' => $nid), array('target' => 'slave'))->fetchAssoc(); } } /** * Generates a link to a path, truncating the displayed text to a given width. * * @param string $path * The path to generate the link for. * @param int $width * The width to set the displayed text of the path. * * @return string * A string as a link, truncated to the width, linked to the given $path. */ function _statistics_link($path, $width = 35) { $title = \Drupal::service('path.alias_manager')->getPathAlias($path); $title = truncate_utf8($title, $width, FALSE, TRUE); return l($title, $path); } /** * Formats an item for display, including both the item title and the link. * * @param string $title * The text to link to a path; will be truncated to a maximum width of 35. * @param string $path * The path to link to; will default to '/'. * * @return string * An HTML string with $title linked to the $path. */ function _statistics_format_item($title, $path) { $path = ($path ? $path : '/'); $output = ($title ? "$title