diff --git a/core/modules/tracker/lib/Drupal/tracker/Access/OwnTrackingInformation.php b/core/modules/tracker/lib/Drupal/tracker/Access/OwnTrackingInformation.php new file mode 100644 index 0000000..c674379 --- /dev/null +++ b/core/modules/tracker/lib/Drupal/tracker/Access/OwnTrackingInformation.php @@ -0,0 +1,32 @@ +getRequirements()); + } + + /** + * Implements AccessCheckInterface::access(). + */ + public function access(Route $route, Request $request) { + return ($account = user_uid_optional_load(arg(1))) && $account->uid && ($GLOBALS['user']->uid == $account->uid) && user_access('access content'); + } +} diff --git a/core/modules/tracker/lib/Drupal/tracker/Access/UserTab.php b/core/modules/tracker/lib/Drupal/tracker/Access/UserTab.php new file mode 100644 index 0000000..9e25492 --- /dev/null +++ b/core/modules/tracker/lib/Drupal/tracker/Access/UserTab.php @@ -0,0 +1,32 @@ +getRequirements()); + } + + /** + * Implements AccessCheckInterface::access(). + */ + public function access(Route $route, Request $request) { + return ($account = user_load(arg(1), TRUE)) && $account->access('view') && user_access('access content'); + } +} diff --git a/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerController.php b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerController.php new file mode 100644 index 0000000..1e99a5d --- /dev/null +++ b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerController.php @@ -0,0 +1,197 @@ +get('database'), + $container->get('plugin.manager.entity') + ); + } + + /** + * Constructs a TrackerController object. + * + * @param \Drupal\Core\Database\Connection $database + * The database connection object. + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The entity manager. + */ + public function __construct(Connection $database, EntityManager $entity_manager) { + $this->database = $database; + $this->entityManager = $entity_manager; + } + + /** + * Generates a page of tracked nodes for the site. + * + * Queries the database for info, adds RDFa info if applicable, and generates + * the render array that will be used to render the page. + * + * @param \Drupal\user\Plugin\Core\Entity\User|null $account + * A user object or NULL. + * @param bool $set_title + * TRUE if the title of the page should be replaced by the username, + * else FALSE. Defaults to FALSE. + * + * + * @return array + * A renderable array. + * + */ + public function trackerPage($account = NULL, $set_title = FALSE) { + if ($account) { + $query = $this->database->select('tracker_node', 't') + ->extend('Drupal\Core\Database\Query\PagerSelectExtender') + ->condition('t.uid', $account->uid); + + if ($set_title) { + // When viewed from user/%user/track, display the name of the user + // as page title -- the tab title remains Track so this needs to be done + // here and not in the menu definition. + drupal_set_title(user_format_name($account)); + } + } + else { + $query = $this->database->select('tracker_node', 't', array('target' => 'slave')) + ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); + } + + // This array acts as a placeholder for the data selected later + // while keeping the correct order. + $tracker_data = $query + ->addTag('node_access') + ->addMetaData('base_table', 'tracker_node') + ->fields('t', array('nid', 'changed')) + ->condition('t.published', 1) + ->orderBy('t.changed', 'DESC') + ->limit(25) + ->execute() + ->fetchAllAssoc('nid'); + + $rows = array(); + if (!empty($tracker_data)) { + $nids = array_keys($tracker_data); + $nodes = $this->entityManager + ->getStorageController('node') + ->load($nids); + // Now, get the data and put into the placeholder array. + $select = $this->database->select('node', 'n', array('target' => 'slave')); + $select->join('node_comment_statistics', 'l', 'n.nid = l.nid'); + $select->join('users', 'u', 'n.uid = u.uid'); + $select->addField('n', 'nid'); + $select->addField('l', 'comment_count'); + $select->condition('n.nid', $nids, 'IN'); + $result = $select->execute()->fetchAllKeyed(); + foreach ($result as $nid => $comment_count) { + $nodes[$nid]->last_activity = $tracker_data[$nid]->changed; + $nodes[$nid]->comment_count = $comment_count; + } + + // Display the data. + foreach ($nodes as $node) { + // Determine the number of comments. + $comments = 0; + if ($node->comment_count) { + $comments = $node->comment_count; + + if ($new = comment_num_new($node->nid)) { + $comments .= '
'; + $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->nid, array('fragment' => 'new')); + } + } + + $row = array( + 'type' => check_plain(node_get_type_label($node)), + // Do not use $node->label(), because $node comes from the database. + 'title' => array('data' => l($node->title, 'node/' . $node->nid) . ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed)))), + 'author' => array('data' => theme('username', array('account' => $node))), + 'replies' => array('class' => array('replies'), 'data' => $comments), + 'last updated' => array('data' => t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))), + ); + + // Adds extra RDFa markup to the $row array if the RDF module is enabled. + if (function_exists('rdf_mapping_load')) { + // Each node is not loaded for performance reasons, as a result we need + // to retrieve the RDF mapping for each node type. + $mapping = rdf_mapping_load('node', $node->type); + // Adds RDFa markup to the title of the node. Because the RDFa markup is + // added to the td tag which might contain HTML code, we specify an + // empty datatype to ensure the value of the title read by the RDFa + // parsers is a plain literal. + $row['title'] += rdf_rdfa_attributes($mapping['title']) + array('datatype' => ''); + // Annotates the td tag containing the author of the node. + $row['author'] += rdf_rdfa_attributes($mapping['uid']); + // Annotates the td tag containing the number of replies. We add the + // content attribute to ensure that only the comment count is used as + // the value for 'num_replies'. Otherwise, other text such as a link + // to the number of new comments could be included in the 'num_replies' + // value. + $row['replies'] += rdf_rdfa_attributes($mapping['comment_count']); + $row['replies'] += array('content' => $node->comment_count); + // If the node has no comments, we assume the node itself was modified + // and apply 'changed' in addition to 'last_activity'. If there are + // comments present, we cannot infer whether the node itself was + // modified or a comment was posted, so we use only 'last_activity'. + $mapping_last_activity = rdf_rdfa_attributes($mapping['last_activity'], $node->last_activity); + if ($node->comment_count == 0) { + $mapping_changed = rdf_rdfa_attributes($mapping['changed'], $node->last_activity); + $mapping_last_activity['property'] = array_merge($mapping_last_activity['property'], $mapping_changed['property']); + } + $row['last updated'] += $mapping_last_activity; + + // We need to add the about attribute on the tr tag to specify which + // node the RDFa annotations above apply to. We move the content of + // $row to a 'data' sub array so we can specify attributes for the row. + $row = array('data' => $row); + $row['about'] = url('node/' . $node->nid); + } + $rows[] = $row; + } + } + + $page['tracker'] = array( + '#rows' => $rows, + '#header' => array(t('Type'), t('Title'), t('Author'), t('Replies'), t('Last updated')), + '#theme' => 'table', + '#empty' => t('No content available.'), + ); + $page['pager'] = array( + '#theme' => 'pager', + '#quantity' => 25, + '#weight' => 10, + ); + $page['#sorted'] = TRUE; + + return $page; + } +} diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module index 125b5da..22d2728 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -34,10 +34,8 @@ function tracker_help($path, $arg) { function tracker_menu() { $items['tracker'] = array( 'title' => 'Recent content', - 'page callback' => 'tracker_page', - 'access arguments' => array('access content'), + 'route_name' => 'tracker_page', 'weight' => 1, - 'file' => 'tracker.pages.inc', ); $items['tracker/all'] = array( 'title' => 'All recent content', @@ -45,22 +43,13 @@ function tracker_menu() { ); $items['tracker/%user_uid_optional'] = array( 'title' => 'My recent content', - 'page callback' => 'tracker_page', - 'access callback' => '_tracker_myrecent_access', - 'access arguments' => array(1), - 'page arguments' => array(1), + 'route_name' => 'tracker_users_recent_content', 'type' => MENU_LOCAL_TASK, - 'file' => 'tracker.pages.inc', ); - $items['user/%user/track'] = array( 'title' => 'Track', - 'page callback' => 'tracker_page', - 'page arguments' => array(1, TRUE), - 'access callback' => '_tracker_user_access', - 'access arguments' => array(1), + 'route_name' => 'tracker_user_tab', 'type' => MENU_LOCAL_TASK, - 'file' => 'tracker.pages.inc', ); $items['user/%user/track/content'] = array( 'title' => 'Track content', @@ -156,39 +145,6 @@ function tracker_cron() { } /** - * Access callback: Determines access permission for a user's own account. - * - * @param int $account - * The account ID to check. - * - * @return boolean - * TRUE if a user is accessing tracking info for their own account and - * has permission to access the content. - * - * @see tracker_menu() - */ -function _tracker_myrecent_access($account) { - // This path is only allowed for authenticated users looking at their own content. - return $account->uid && ($GLOBALS['user']->uid == $account->uid) && user_access('access content'); -} - -/** - * Access callback: Determines access permission for an account. - * - * @param int $account - * The user account ID to track. - * - * @return boolean - * TRUE if a user has permission to access the account for $account and - * has permission to access the content. - * - * @see tracker_menu() - */ -function _tracker_user_access($account) { - return $account->access('view') && user_access('access content'); -} - -/** * Implements hook_node_insert(). * * Adds new tracking information for this node since it's new. diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc deleted file mode 100644 index a801ad7..0000000 --- a/core/modules/tracker/tracker.pages.inc +++ /dev/null @@ -1,137 +0,0 @@ -extend('Drupal\Core\Database\Query\PagerSelectExtender'); - $query->condition('t.uid', $account->uid); - - if ($set_title) { - // When viewed from user/%user/track, display the name of the user - // as page title -- the tab title remains Track so this needs to be done - // here and not in the menu definition. - drupal_set_title(user_format_name($account)); - } - } - else { - $query = db_select('tracker_node', 't', array('target' => 'slave')) - ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); - } - - // This array acts as a placeholder for the data selected later - // while keeping the correct order. - $tracker_data = $query - ->addTag('node_access') - ->addMetaData('base_table', 'tracker_node') - ->fields('t', array('nid', 'changed')) - ->condition('t.published', 1) - ->orderBy('t.changed', 'DESC') - ->limit(25) - ->execute() - ->fetchAllAssoc('nid'); - - $rows = array(); - if (!empty($tracker_data)) { - $nids = array_keys($tracker_data); - $nodes = entity_load_multiple('node', $nids); - // Now, get the data and put into the placeholder array. - $result = db_query('SELECT n.nid, l.comment_count FROM {node} n INNER JOIN {node_comment_statistics} l ON n.nid = l.nid INNER JOIN {users} u ON n.uid = u.uid WHERE n.nid IN (:nids)', array(':nids' => $nids), array('target' => 'slave'))->fetchAllKeyed(); - foreach ($result as $nid => $comment_count) { - $nodes[$nid]->last_activity = $tracker_data[$nid]->changed; - $nodes[$nid]->comment_count = $comment_count; - } - - // Display the data. - foreach ($nodes as $node) { - // Determine the number of comments. - $comments = 0; - if ($node->comment_count) { - $comments = $node->comment_count; - - if ($new = comment_num_new($node->nid)) { - $comments .= '
'; - $comments .= l(format_plural($new, '1 new', '@count new'), 'node/' . $node->nid, array('fragment' => 'new')); - } - } - - $row = array( - 'type' => check_plain(node_get_type_label($node)), - // Do not use $node->label(), because $node comes from the database. - 'title' => array('data' => l($node->title, 'node/' . $node->nid) . ' ' . theme('mark', array('type' => node_mark($node->nid, $node->changed)))), - 'author' => array('data' => theme('username', array('account' => $node))), - 'replies' => array('class' => array('replies'), 'data' => $comments), - 'last updated' => array('data' => t('!time ago', array('!time' => format_interval(REQUEST_TIME - $node->last_activity)))), - ); - - // Adds extra RDFa markup to the $row array if the RDF module is enabled. - if (function_exists('rdf_mapping_load')) { - // Each node is not loaded for performance reasons, as a result we need - // to retrieve the RDF mapping for each node type. - $mapping = rdf_mapping_load('node', $node->type); - // Adds RDFa markup to the title of the node. Because the RDFa markup is - // added to the td tag which might contain HTML code, we specify an - // empty datatype to ensure the value of the title read by the RDFa - // parsers is a plain literal. - $row['title'] += rdf_rdfa_attributes($mapping['title']) + array('datatype' => ''); - // Annotates the td tag containing the author of the node. - $row['author'] += rdf_rdfa_attributes($mapping['uid']); - // Annotates the td tag containing the number of replies. We add the - // content attribute to ensure that only the comment count is used as - // the value for 'num_replies'. Otherwise, other text such as a link - // to the number of new comments could be included in the 'num_replies' - // value. - $row['replies'] += rdf_rdfa_attributes($mapping['comment_count']); - $row['replies'] += array('content' => $node->comment_count); - // If the node has no comments, we assume the node itself was modified - // and apply 'changed' in addition to 'last_activity'. If there are - // comments present, we cannot infer whether the node itself was - // modified or a comment was posted, so we use only 'last_activity'. - $mapping_last_activity = rdf_rdfa_attributes($mapping['last_activity'], $node->last_activity); - if ($node->comment_count == 0) { - $mapping_changed = rdf_rdfa_attributes($mapping['changed'], $node->last_activity); - $mapping_last_activity['property'] = array_merge($mapping_last_activity['property'], $mapping_changed['property']); - } - $row['last updated'] += $mapping_last_activity; - - // We need to add the about attribute on the tr tag to specify which - // node the RDFa annotations above apply to. We move the content of - // $row to a 'data' sub array so we can specify attributes for the row. - $row = array('data' => $row); - $row['about'] = url('node/' . $node->nid); - } - $rows[] = $row; - } - } - - $page['tracker'] = array( - '#rows' => $rows, - '#header' => array(t('Type'), t('Title'), t('Author'), t('Replies'), t('Last updated')), - '#theme' => 'table', - '#empty' => t('No content available.'), - ); - $page['pager'] = array( - '#theme' => 'pager', - '#quantity' => 25, - '#weight' => 10, - ); - $page['#sorted'] = TRUE; - - return $page; -} diff --git a/core/modules/tracker/tracker.routing.yml b/core/modules/tracker/tracker.routing.yml new file mode 100644 index 0000000..f591b8d --- /dev/null +++ b/core/modules/tracker/tracker.routing.yml @@ -0,0 +1,20 @@ +tracker_page: + pattern: '/tracker' + defaults: + _content: '\Drupal\tracker\Controller\TrackerController::trackerPage' + requirements: + _permission: 'access content' + +tracker_users_recent_content: + pattern: '/tracker/{user_uid_optional}' + defaults: + _content: '\Drupal\tracker\Controller\TrackerController::trackerPage' + requirements: + _access_tracker_own_information: 'TRUE' + +tracker_user_tab: + pattern: '/user/{user}/track' + defaults: + _content: '\Drupal\tracker\Controller\TrackerController::trackerPage' + requirements: + _access_tracker_user_tab: 'TRUE' \ No newline at end of file diff --git a/core/modules/tracker/tracker.services.yml b/core/modules/tracker/tracker.services.yml new file mode 100644 index 0000000..90d1ce3 --- /dev/null +++ b/core/modules/tracker/tracker.services.yml @@ -0,0 +1,9 @@ +services: + access_check.tracker.own_information: + class: Drupal\tracker\Access\OwnTrackingInformation + tags: + - { name: access_check } + access_check.tracker.user_tab: + class: Drupal\tracker\Access\UserTab + tags: + - { name: access_check }