I am starting a new module called Popularity (similar to the WP plugin Popularity Contest) that will be able to rank nodes based on certain factors like comments, statistics, pingbacks, trackbacks, etc. I already use the xmlsitemap module and noticed that you have some similar functionality to give certain factors a higher rating in the sitemap. I was wondering if we could work together and abstract your ranking code from xmlsitemap into popularity. Then, xmlsitemap could simply integrate with the popularity API to help adjust the priority of items on the sitemap. I'm thinking of creating a function like the following:

/**
 * @param $node A node object or node ID.
 * @param $min The minimum value to scale to, defaults to 0.
 * @param $max The maximum value to scale to, defaults to no limit.
 */
function popularity_get_popularity($node, $min = 0, $max = NULL)

This will give the xmlsitemap_node module less code for you to maintain and will help re-use the priority/popularity code elsewhere (ex. views sorting, etc). I've just started this module, so there's not much code to actually look at yet, but it's a work in progress. Let me know what you think!

Comments

avpaderno’s picture

We can achieve that by defining a custom hook that xmlsitemap_node.module would call to allow external third-party modules to change the priority of a node. In that way, if popularity.module would be present, it could change the node priority in the site map; otherwise xmlsitemap_node.module would proceed as its usual.
I was thinking to a hook_xmlsitemap_priority_alter(&$priority, $type, $id), where $type would be a parameter saying the type of object for which the priority is requested to be changed (node, user, term, etc...), while $id would be the ID for that object (nid for nodes, uid for users, etc...).

I like the idea.

Still, this would not give to xmlsitemap_node.module less code to maintain, as the module must work with, or without popularity.module.

dave reid’s picture

The hook_xmlsitemap_priority_alter sounds like a great idea and something I can definately work with.

But what I'm asking you to consider is removing the automatic node calculations currently in xmlsitemap_node. The module will still work just fine without this since users can still assign default priorities for each node type and override the priority on individual nodes. Let me work up a patch to show exactly the changes I'm thinking about.

dave reid’s picture

Status: Active » Needs review
StatusFileSize
new5.77 KB

Patch attached that shows the changes. Note that the module still works just fine, but if users want 'automatic priority calculation', you would recommend them to install the popularity.module. Anyway, this is still rough, so it's mostly to get an idea for how thing would be changed in xmlsitemap_node.module.

avpaderno’s picture

Status: Active » Needs review

The patch proposed remove one the functionality of automatically calculate the priority of the node links. If the patch would be implemented the administrator of the Drupal site running XML Sitemap would have just two options:

  • The priority of the nodes would be set manually for each node; this also mean he should manually change the priority every time a comment is added or removed, to emulate what the module is currently doing.
  • The administrator should install popularity.module; this should be done even in the case he just need the priority of the node automatically set.

Neither of the solutions are something I would like. xmlsitemap_node.module must work also without any third-party modules; by must work I mean that, in absence of third-party modules, the user doesn't need to manually set the priority of the nodes he wants to appear in the site map.
I would think rather difficult for somebody who is using XML Sitemap (and it's not interested in Popularity) accept that, if he doesn't wants to manually set the node priorities, he must install another module.

The changes I can do on the code of XML Sitemap are:

  • In the code that sets the priority of the link, I can add a call to drupal_alter('xmlsitemap_priority', $priority, $type, $id).
  • In the settings page, I can add some options to permit the administrator to exclude the priority adjustment made from XML Sitemap, and to allow third-party modules to override the priority being set.
avpaderno’s picture

Status: Needs review » Active
Issue tags: +Hook implementations, +custom hooks

If there aren't any further comments, I will change this report to a task to allow third party modules to change the priority assigned to the links added in the site map.

avpaderno’s picture

Title: Integrate node priority rankings with popularity.module API » Allow third party modules to change the priority of the links added in the site map
Component: xmlsitemap_node.module » Code
Assigned: dave reid » Unassigned
Category: feature » task
Status: Needs review » Active

As promised, I am changing the report to a task; this is the only thing I can do, as the feature request cannot be fulfilled.

avpaderno’s picture

Status: Active » Postponed

This will be implemented in the 6.x-2 branch.

avpaderno’s picture

Title: Allow third party modules to change the priority of the links added in the site map » Allow third-party modules to change the priority of the links added in the site map
Status: Postponed » Fixed

The feature has been implemented in CVS. Differently from how stated in my previous comment, third-party modules must implement hook_xmlsitemap_priority_alter(&$priority, $type, $id, $sid), where $sid is a secondary ID associated to the link from the module that added the link in the site map database table.

Feel free to re-open the issue if you think there would be improvements to the code.

avpaderno’s picture

Status: Fixed » Active

After adding the code in CVS, I thought the code could be made as general as possible.

The required hook should be hook_xmlsitemap_data_alter(&link), where $link is an object containing the following properties: loc, module, type, id, sid, changed, changefreq, priority, and url; of those properties, only the following are inserted in the site map (therefore, are the only that is worth to change): changed, changefreq, priority, and url.

The difference between loc, and url is that the first is a relative URL in its canonical form (for nodes, it is in the node/<nid> form, while for users is in user/<uid> form), while url is the absolute URL that could also refer to a path alias.

avpaderno’s picture

Status: Active » Needs review

The code has been changed.

After third-party modules had the chance to change something, the data are validated again; if a module wants to avoid a link is added to the site map, it's enough it sets url to an empty string, or changed to 0, or changefreq to 0, or priority to a value outside the range 0,1 (extremes included).
In no cases the modules should remove properties from the object passed to them.

I am not sure if the changes in the data should be written back to the database table (xmlsitemap).

avpaderno’s picture

Status: Needs review » Fixed

At the moment, the code will stay as it is.

Status: Fixed » Closed (fixed)
Issue tags: -Hook implementations, -custom hooks

Automatically closed -- issue fixed for 2 weeks with no activity.