I have some modules creating nodes calling directly node_save() and now when trying to rebuild permissions table drupal fails at a random node.
I think this is caused because of some bad created nodes, and I think I have found a way to fix them; but it fail because of memory limit.
Is there a way to make node_load() not store nodes in cache? (I have around 30k nodes)
Thanks
<?php
function mymodule_update_6008() {
?$ret=array();
?$res=db_query("SELECT nid, type FROM {node}");
?while ($row=db_fetch_array($res)){
? ?$prenode['type']=$row['type'];
? ?$prenode=(object)$prenode;
? ?$node_new=node_object_prepare($prenode);
? ?$node_old=node_load($row['nid']);
? ?foreach($node_old AS $key => $value) {
? ? ?$node_new->$key = $value;
? ?}
? ?node_save($node_new);
?}
}
>>> >> >>> ?>
1 year 41 weeks ago
You can use Drupal's Batch API to process large sets of objects like this. Documentation for the API is here:
http://api.drupal.org/api/group/batch
And it includes an example for iterating over all nodes.
Hope that helps!
Regards
Steven Jones
ComputerMinds ltd - Perfect Drupal Websites
My problem right now is node_save itself: uid and timestamps are changed by node_save. Is there a way to avoid that? Or should I reset these fields with db_query("UPDATE ...")?
Thanks
Llu
Try changing $node->uid.. The timestamps will have to change when you update a node.. it is necessary for many modules to be identified of the change like search and xmlsitemap
I just discovered another problem, I am using an access control module, so I had to add 2 functions so my user could load/save any node:
function mymodule_loadAnyNode($nid) {
?if ($nid==0) return FALSE;
?global $user;
?static $orig_user = array();
?array_push($orig_user, $user);
?$user = user_load(1);
?$node=node_load($nid);
?$user = array_pop($orig_user);
?return $node;
}
function mymodule_saveAnyNode($node) {
?if ($nid==0) return FALSE;
?global $user;
?static $orig_user = array();
?array_push($orig_user, $user);
?$user = user_load(1);
?$return=node_save($node);
?$user = array_pop($orig_user);
?return $return;
}
Thanks
Llu
this reminds me that there is a weird issue with drupal... the access all content permissions saves the configuration in node_access with nid = 0 which should not be the case.. so its best if you never interact with
node_access table and stay out of the problem... not that it is relevant with this problem, just wanted to share :)
i'm not sure if i follow the use of the 2 functions.. did it work for you? the permission for the user to be able to save the node can be defined using drupal permissions.. no? even in case you are using any access control module, your node_save should work independently.. the access control module should handle the permissions of saving the node..
Cheers
Mukesh
you are right, as long as uid=1 is logged everything should be ok.
I am using a module which allow users to create/load/edit nodes only under certain circumstances (similar to OG, but more complex), using hook_node_access_records() and hook_node_grants(). In this particular case, just uid=1 will call the function, so I could avoid them; but for some other admin tasks not performed by uid=1 I need these
functions.
Thanks
Llu
Due to a quirk in node_save(), $node->uid makes no difference. Use $node->name and the user name of the node creator.
Never write directly to {node_access}. Use the APIs (like node_save) and this will take care of itself.
Cheers,
Ken
Post Comment