As mentioned in #1040022: Detailed comparison of Scheduler D6 to D7, the D6 scheduler's hook_nodeapi has been split out into the new D7 hook_node_'op' functions. I'd like to take each one in turn to verify that the conversion is correct. First up is hook_node_load. Attached is a grab from my comparison program, comparing the latest D6 dev (21 Jan) with D7 (28 Jan).

I think this one is OK, but there are some improvements:

  1. Is it worth checking the $types value and only continuing if at least one of the types is enabled for scheduler? The overhead may negate any later gain.
  2. The db_query could be re-written to use the new abstraction layer
  3. The unset of $record->nid is not required, it was only done in D6 because this array was being re-used

If these are not critical then we can leave it for now, but it is worth recording anything here for our future reference.

Comments

jonathan1055’s picture

Just noticed another improvement:

  • 4. instead of doing the foreach ($nodes as $node) loop, we can just use array_keys($nodes) instead of $nids in the SQL.
eric-alexander schaefer’s picture

(1) Could be worth it. To be sure we would need to measure it.
(2) Right now I would rather keep the code if it is working and make it D7ish later
(3) Right. This is not needed anymore.
(4) Definitely.

I will do 3 and 4 right now and we should do (1) and (2) after release 1.0.

eric-alexander schaefer’s picture

Status: Active » Needs review
StatusFileSize
new941 bytes
jonathan1055’s picture

Yes I agree with leaving 1 and 2 for later.
In your patch, I would do it in one line, ie change

  $nids = array_keys($nodes);
  $result = db_query('SELECT * FROM {scheduler} WHERE nid IN (:nids)', array(':nids' => $nids));

into

  $result = db_query('SELECT * FROM {scheduler} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));

to save the overhead of creating the array $nids which is not used anywhere else. This is how it is done in the API docs example

eric-alexander schaefer’s picture

I don't think this adds overhead and I like the code to speak to me. If you inline $nids you have no idea what array_keys() is retrieving. If you assign the result to $nids you will know that it returns a list of node IDs. Also I like statements to be about one thing. The first line is all about getting a list of node IDs while the second line is all about the query. In the inlined version of the code the line is about two things at once. This might not seem to be such a big deal but it usually pays off later, because the code is easier to understand.

jonathan1055’s picture

Yes, that is very good reasoning. In addition, having it as two lines means you can insert a debug line between the two if necessary. I'll test the patch and report back.

jonathan1055’s picture

Your patch is fine, but I think there was another error in the original translation to D7. When $row was created in D6 it was being saved to $node->scheduler which is correct because the D6 version can only load one node at a time. But in D7, multiple nodes can be loaded, and they are stored in $nodes keyed by the node id. So the line

$node->scheduler = $row;

should become

$nodes[$nid]->scheduler = $row;

Otherwise the $row never gets saved and stored, which is half of the point of this function.

Also you could change the function header text to read 'Implements hook_node_load().' as this is the new standard. I know this is quite minor, but this header block is bound to be copied to the other new hook_node_{op} functions so we might as well save ourselves effort later on.

jonathan1055’s picture

Status: Needs review » Needs work

Forgot to set status back to 'needs work'

eric-alexander schaefer’s picture

The values from the scheduler table actually get stored in $nodes[$nid]->publish_on and $nodes[$nid]->unpublish_on ($node-> in D6 and D5). I have no idea why $row was stored that way anyway.

jonathan1055’s picture

Yes the actual values get stored in $nodes[$nid]->publish_on, that is fine. What I'm saying is that the formatted text (which used to be stored in $node->scheduler via $row in D6) is now not saved at all. To match D6 we need the line change I suggested in #7 above.

eric-alexander schaefer’s picture

Yes I understand that. But why create and save it at all? Where is it accessed?

jonathan1055’s picture

StatusFileSize
new89.81 KB

I've just looked back, and the array $node->scheduler actually has all four scheduler values in the D5 and D6 versions. See screen grab from D6. The ->scheduler array was introduced somewhere between 5.x-1.3 (March 2007) and 5.x-1.10 (Feb 2008). I don't know the background why publish_on and unpublish_on are duplicated directly in $node and in the $node->scheduler array but I don't think we should remove either of them in the D7 conversion (atleast not for 7.x-1.0). They are not accessed in any scheduler module code, just provided for others to use, I presume.

So in addition to the line change in #7 above, we also need

    $row['publish_on'] = $record->publish_on;
    $row['unpublish_on'] = $record->unpublish_on;

so that the stored array has all four values just like D6.

eric-alexander schaefer’s picture

Status: Needs work » Fixed

You are probably right. There might be modules or themes that rely on this data so we should keep it.
http://drupal.org/cvs?commit=495054

Status: Fixed » Closed (fixed)

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