Errors creating / updating CCK nodes
| Project: | Swish-E Indexer |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Not directly an issue with this, but I guess it needs addressing and is best done here.
I'm trying to index files on a CCK node using the filefield module. The module's hook_nodeapi implementation currently looks for $node->file to get the information base on which it populates it's own table, however in the case of these nodes it's $node->field_file (or something similar).
Could be hacked to work easily enough, or made to support both. Alternatively, I thought maybe if it interrogates the file table with the node id to gather the details then that would probably be better and should be compatible with any implementation. Would have to double check the order of actions for nodeapi to make sure that the files table is populated first, but soulds good in theory.
Anyway, I'm going to tackle this in the next few days, but thought I'd raise the issue now to open it up to discussion before I get started.
Cheers,
Jon

#1
OK, so I've rewritten the nodeapi update portion. I'll submit it copy and paste style to save any patch confusion with my other submissions.
case "update":// Check if this node has any references in the files table
// Note: we do this because other attachments might not be found in $node->file
$result = db_query("SELECT filepath FROM {files} WHERE nid = %d", $node->nid);
if (db_num_rows($result)) {
while ($row = db_fetch_object($result)) {
$text = _swish_do_text_extract($row->filepath);
// Do a delete and reinsert, in case anything has gone wrong before and the row isn't there already
db_query("DELETE FROM {swish_fulltext} WHERE filepath = '%s'", $row->filepath);
db_query("INSERT INTO {swish_fulltext} (filepath, nid, `fulltext`) VALUES ('%s',%d,'%s')", $row->filepath, $node->nid, $text);
}
}
break;
In addition to resolving the issue of the module barfing on anything that doesn't have the $node->files var, I think I spotted that some of the sql statements had a few bugs in them, so I'm not entirely sure that it was working as it was before.
Cheers,
Jon
#2
Also noticed that it runs against update, submit and insert - the submit seems superfluous, so I imagine that can be removed.
#3
Have found similar issues with the _swish_do_update function which is called by cron - mal formed sql statements. Have rewritten them to fix them and give them a general tidy.
function _swish_do_update() {$result = db_query("SELECT nid, filepath FROM {files}");
while ($row = db_fetch_object($result)) {
$subresult = db_query("SELECT nid from {swish_fulltext} where filepath = '%s' and nid = %d", $row->filepath, $row->nid);
if (db_num_rows($subresult) == 0) {
$text = _swish_do_text_extract($row->filepath);
db_query("DELETE FROM {swish_fulltext} WHERE filepath = '%s'", $row->filepath);
db_query("INSERT INTO {swish_fulltext} (filepath, nid, `fulltext`) VALUES ('%s',%d,'%s')", $row->filepath, $row->nid, $text);
}
}
}
One thing I noticed though was that in cron, it looks for files with matching filepaths and nids, whereas in the nodeapi hooks, it just matches on filepath, which would potentially clear more than one node if they had the same file attached. In practice I don't think this would be a problem, as if the same file was uploaded it would be saved with a different name (file and file_2 or something like that), however for completeness here's the updated code.
function swish_nodeapi(&$node, $op) {switch($op) {
case "insert":
case "update":
// Check if this node has any references in the files table
// Note: we do this because other attachments might not be found in $node->file
$result = db_query("SELECT filepath FROM {files} WHERE filepath = '%s' and nid = %d", $row->filepath, $row->nid);
if (db_num_rows($result)) {
while ($row = db_fetch_object($result)) {
$text = _swish_do_text_extract($row->filepath);
// Do a delete and reinsert, in case anything has gone wrong before and the row isn't there already
db_query("DELETE FROM {swish_fulltext} WHERE filepath = '%s'", $row->filepath);
db_query("INSERT INTO {swish_fulltext} (filepath, nid, `fulltext`) VALUES ('%s',%d,'%s')", $row->filepath, $node->nid, $text);
}
}
break;
}
}
#4
One more time......
function swish_nodeapi(&$node, $op) {switch($op) {
case "insert":
case "update":
// Check if this node has any references in the files table
// Note: we do this because other attachments might not be found in $node->file
$result = db_query("SELECT nid, filepath FROM {files} WHERE filepath = '%s' and nid = %d", $row->filepath, $row->nid);
if (db_num_rows($result)) {
while ($row = db_fetch_object($result)) {
$text = _swish_do_text_extract($row->filepath);
// Do a delete and reinsert, in case anything has gone wrong before and the row isn't there already
db_query("DELETE FROM {swish_fulltext} WHERE filepath = '%s' and nid = %d", $row->filepath, $row->nid);
db_query("INSERT INTO {swish_fulltext} (filepath, nid, `fulltext`) VALUES ('%s',%d,'%s')", $row->filepath, $node->nid, $text);
}
}
break;
}
}
#5
OK, last time, tested this time and should all be ok
function swish_nodeapi(&$node, $op) {switch($op) {
case "insert":
case "update":
// Check if this node has any references in the files table
// Note: we do this because other attachments might not be found in $node->file
$result = db_query("SELECT nid, filepath FROM {files} WHERE nid = %d", $node->nid);
if (db_num_rows($result)) {
while ($row = db_fetch_object($result)) {
$text = _swish_do_text_extract($row->filepath);
// Do a delete and reinsert, in case anything has gone wrong before and the row isn't there already
db_query("DELETE FROM {swish_fulltext} WHERE filepath = '%s' and nid = %d", $row->filepath, $row->nid);
db_query("INSERT INTO {swish_fulltext} (filepath, nid, `fulltext`) VALUES ('%s',%d,'%s')", $row->filepath, $node->nid, $text);
}
}
break;
case "delete":
db_query("DELETE FROM {swish_fulltext} WHERE nid = %d", $node->nid);
break;
}
}
#6
And it includes an implementation to delete records on node deletion.
#7
Great improvement to the module. Added several months ago, closing out the issue.
#8
Automatically closed -- issue fixed for two weeks with no activity.