You are here

function hook_mostpopular_service in Drupal Most Popular 6

Defines hook_mostpopular_service() and provides an empty implementation.

Parameters

string $op: The operation to perform.

  • list: returns an array containing definitions of each available service.
  • refresh: invokes one particular service and returns an array of results.
  • throttles: returns a list of the default throttles to use for the given set of intervals.
  • config: returns a form with specific elements for configuring the service.

mixed $delta: Identifiers which service to use, if this function defines more than one.

array $options: Values associated with this $op.

  • For 'refresh': $options is an array containing:

    • sid: The internal service ID of the running service.
    • iid: The internal interval ID of the running interval.
    • ts: The timestamp from which to start the search.
    • last_run: The timestamp of the last time this service was run for the particular interval in question.
    • max: The maximum number of results to retrieve.
  • For 'throttles': $options is an array of strtotime() values representing the current list of intervals.

Return value

mixed The return values depends on $op.

  • For 'list': returns an array where each delta is a key and each value is an array containing:

    • name: The unique name of the service.
    • title: The default title to show users for this service.
  • For 'refresh': returns an array of most popular items, where each item is an array containing:

    • title: The page title.
    • url: The URL of the page within Drupal.
    • count: The number of times the page was accessed.
    • nid: If this is a node, the ID of the node.
  • For 'config': returns a Form API contribution to the configuration form for the service.
  • For 'throttles': returns an array where the keys are the same interval IDs provided in the $options array, and the values are strings parseable by strtotime() which will be applied to the last time the service was run to see whether it can run again. These are defaults only; administrators can adjust these throttles.
3 functions implement hook_mostpopular_service()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

mostpopular_addthis_mostpopular_service in modules/mostpopular_addthis/mostpopular_addthis.module
Implements hook_mostpopular_service().
mostpopular_drupal_mostpopular_service in modules/mostpopular_drupal/mostpopular_drupal.module
Implements hook_mostpopular_service().
mostpopular_ga_mostpopular_service in modules/mostpopular_ga/mostpopular_ga.module
Implements hook_mostpopular_service().
4 invocations of hook_mostpopular_service()
MostPopularLastRun::getDefaultThrottles in classes/lastrun.php
Invokes hook_mostpopular_service('throttles') on the given service to get the default throttles to use for the currently-configured intervals.
MostPopularService::fetchAvailable in classes/services.php
Fetches all of the available services from the enabled modules that define them.
mostpopular_refresh in ./mostpopular.api.php
Refreshes data from each service by invoking hook_mostpopular_service('refresh').
mostpopular_service_config_form in ./mostpopular.admin.inc

File

./mostpopular.api.php, line 71
Provides functions for other modules to use to interact with the Most Popular data.

Code

function hook_mostpopular_service($op, $delta = 0, $options = array()) {
  switch ($op) {

    // Returns a list of all of the services defined by this module
    case 'list':
      return array(
        'viewed' => array(
          // The delta to use
          'name' => t('My Service'),
          // Unique name for administrators
          'title' => t('Viewed'),
        ),
      );
      break;

    // Connects to some internal or external query to get the list of
    // most popular items.
    case 'refresh':
      switch ($delta) {
        case 'viewed':

          // Get a list of page URLs
          $pages = get_urls();
          foreach ($pages as $page) {
            $url = $page['url'];
            $count = $page['count'];

            /*
             * Optionally, match each returned URL to a node.
             *
             * We typically only care about the most popular nodes, so if
             * a service returns URLs to pages that aren't nodes, we shouldn't
             * include them.  Your service can call this API function to help
             * you out.   However, if you do this, you should make sure you
             * request more than the max number of results from the service,
             * so that you can still end up with the right number even after
             * some of them have been filtered out.
             */
            $obj = mostpopular_match_result_nodes($url, $count);
            if (isset($obj)) {
              $out[] = $obj;
            }

            // Once we've found the max number of results, we're done!
            if (count($out) >= $options['max']) {
              break;
            }
          }
          return $out;
      }
      return FALSE;

    // Indicates there are no results
    // Returns a part of the configuration form for this service
    case 'config':
      $form = array();
      $form['auth'] = array(
        '#type' => 'fieldset',
        '#title' => t('Service login credentials'),
        'myservice_username' => array(
          '#type' => 'textfield',
          '#title' => t('User Name'),
          '#default_value' => variable_get('myservice_username', ''),
        ),
        'myservice_password' => array(
          '#type' => 'textfield',
          '#title' => t('Password'),
          '#default_value' => variable_get('myservice_password', ''),
        ),
      );
      return $form;

    // Returns a list of default throttles for this service
    case 'throttles':
      switch ($delta) {
        case 'viewed':
          $out = array();
          foreach ($options as $iid => $ts) {

            // Determine the interval based on the starting timestamp
            if ($ts >= strtotime('-1 day -10 minutes')) {

              // add a buffer
              $out[$iid] = '1 hour';
            }
            elseif ($ts <= strtotime('-1 year')) {
              $out[$iid] = '1 week';
            }
            else {
              $out[$iid] = '1 day';
            }
          }
          return $out;
      }
  }
}