| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: save_node.php,v 1.2 2006/01/27 06:45:45 puregin Exp $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Functions supporting bookimport.module.
|
| 8 |
*
|
| 9 |
* Copyright (C) 2006 Djun M. Kim
|
| 10 |
*
|
| 11 |
* This program is free software; you can redistribute it and/or
|
| 12 |
* modify it under the terms of the GNU General Public License as
|
| 13 |
* published by the Free Software Foundation; either version 2 of the
|
| 14 |
* License, or (at your option) any later version.
|
| 15 |
*
|
| 16 |
* See the GNU General Public License version 2 LICENSE file for
|
| 17 |
* full terms and conditions of use.
|
| 18 |
*
|
| 19 |
*/
|
| 20 |
|
| 21 |
// global hash provides mapping from old nid to new
|
| 22 |
$new_nid = array();
|
| 23 |
$parent_of = array();
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Fills in the body of the given node.
|
| 27 |
* Note: @arg $parent is not actually used
|
| 28 |
*/
|
| 29 |
function save($tnode, $nid, $parent, $mode) {
|
| 30 |
global $new_nid;
|
| 31 |
global $parent_of;
|
| 32 |
|
| 33 |
if (DEBUG > 2) {
|
| 34 |
echo "saving node '". $tnode->title . "' ";
|
| 35 |
echo "(id = ". $tnode->id .", mode = $mode)\n";
|
| 36 |
|
| 37 |
// set up metadata
|
| 38 |
$nodeid = preg_replace('@^(node-)(\d+)$@', '\2', $tnode->id);
|
| 39 |
$nidinfo = " nid:$nodeid\n";
|
| 40 |
$nidinfo .= " title:$tnode->title\n";
|
| 41 |
$nidinfo .= " md5:". $tnode->get_md5_body() ."\n";
|
| 42 |
$nidinfo .= " weight:". $tnode->get_weight() ."\n";
|
| 43 |
$nidinfo .= " depth:". $tnode->get_depth() ."\n";
|
| 44 |
$nidinfo .= " parent:". $tnode->get_parent() ."\n";
|
| 45 |
|
| 46 |
echo "<pre style=\"font-family: courier; font-size: 8pt;\">";
|
| 47 |
echo "$nidinfo</pre>";
|
| 48 |
}
|
| 49 |
|
| 50 |
if ($mode == 'update') {
|
| 51 |
_update_book_node($tnode);
|
| 52 |
}
|
| 53 |
else if ($mode == 'insert') {
|
| 54 |
_create_book_node($tnode);
|
| 55 |
}
|
| 56 |
else {
|
| 57 |
// Write node body
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Creates a new node in the database
|
| 63 |
*/
|
| 64 |
function _create_book_node($tnode) {
|
| 65 |
global $user;
|
| 66 |
global $new_nid;
|
| 67 |
global $parent_of;
|
| 68 |
|
| 69 |
$node->type = $tnode->nodetype;
|
| 70 |
$node->title = $tnode->title;
|
| 71 |
$node->uid = $tnode->uid;
|
| 72 |
$node->status = $tnode->status;
|
| 73 |
$node->created = $tnode->created;
|
| 74 |
$node->promote = $tnode->promote;
|
| 75 |
$node->moderate = $tnode->moderatep;
|
| 76 |
$node->changed = $tnode->changed;
|
| 77 |
$node->sticky = $tnode->sticky;
|
| 78 |
|
| 79 |
$node->format = $tnode->format;
|
| 80 |
$node->weight = $tnode->weight;
|
| 81 |
$node->body = $tnode->body;
|
| 82 |
$node->author = $tnode->author;
|
| 83 |
|
| 84 |
// $node->uid = $user->uid;
|
| 85 |
// $node->create = time();
|
| 86 |
|
| 87 |
node_save($node);
|
| 88 |
if (DEBUG > 1) {
|
| 89 |
echo "new nid is: ". $node->nid ."<br />";
|
| 90 |
}
|
| 91 |
// identify newly created nodes by mapping new_id => new_id;
|
| 92 |
// newly created nodes are ones which have an empty $tnode->id
|
| 93 |
if ($tnode->id == '') {
|
| 94 |
$tnode->id = $node->nid;
|
| 95 |
}
|
| 96 |
// get the new nid, store it hashed by old nid:
|
| 97 |
$new_nid[$tnode->id] = $node->nid;
|
| 98 |
// record the parent-child of the node,
|
| 99 |
// based on the existing ids
|
| 100 |
$parent_of[$tnode->id] = $tnode->parent;
|
| 101 |
|
| 102 |
if (DEBUG > 1) {
|
| 103 |
echo "created: nid = ". $node->nid ."\n";
|
| 104 |
};
|
| 105 |
return ($node->nid);
|
| 106 |
}
|
| 107 |
|
| 108 |
function _update_book_node($tnode) {
|
| 109 |
global $parent_of;
|
| 110 |
|
| 111 |
$timestamp = time();
|
| 112 |
$log = "\nInserted on ". date("D F j, Y") ." at ". date("G:i:s T") ."<br />\n";
|
| 113 |
|
| 114 |
$node->type = $tnode->nodetype;
|
| 115 |
$node->title = $tnode->title;
|
| 116 |
$node->uid = $tnode->uid;
|
| 117 |
$node->status = $tnode->status;
|
| 118 |
$node->created = $tnode->created;
|
| 119 |
$node->promote = $tnode->promote;
|
| 120 |
$node->moderate = $tnode->moderatep;
|
| 121 |
$node->changed = $tnode->changed;
|
| 122 |
$node->sticky = $tnode->sticky;
|
| 123 |
|
| 124 |
$node->format = $tnode->format;
|
| 125 |
$node->weight = $tnode->weight;
|
| 126 |
$node->body = $tnode->body;
|
| 127 |
$node->author = $tnode->author;
|
| 128 |
|
| 129 |
$node->nid = $tnode->id;
|
| 130 |
|
| 131 |
/* this saves the node; creates a new revision, etc. */
|
| 132 |
|
| 133 |
node_save($node);
|
| 134 |
if (DEBUG > 1) {
|
| 135 |
echo "new nid is: ". $node->nid ."<br />";
|
| 136 |
}
|
| 137 |
|
| 138 |
// Since we're updating, we don't need to remap nids
|
| 139 |
// record the parent-child of the node,
|
| 140 |
// based on the existing ids
|
| 141 |
$parent_of[$node->nid] = $tnode->parent;
|
| 142 |
|
| 143 |
return ($node->nid);
|
| 144 |
}
|
| 145 |
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Updates a given node in the database
|
| 149 |
*/
|
| 150 |
function update($tnode) {
|
| 151 |
// We need to deal with case where tnode does not have an id.
|
| 152 |
// In this case, create the node rather than updating
|
| 153 |
// an existing node.
|
| 154 |
|
| 155 |
// echo "update: tnode->id = " . $tnode->id. "<br />\n";
|
| 156 |
|
| 157 |
if (!isset($tnode->id) || $tnode->id == 0) {
|
| 158 |
_create_book_node($tnode);
|
| 159 |
}
|
| 160 |
else { // were updating an existing entry
|
| 161 |
_update_book_node($tnode);
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
# at end, if inserting new records iterate over new_nid
|
| 166 |
function make_links() {
|
| 167 |
global $new_nid;
|
| 168 |
global $parent_of;
|
| 169 |
|
| 170 |
|
| 171 |
if (DEBUG > 0) {
|
| 172 |
echo "make_links()<br />";
|
| 173 |
};
|
| 174 |
if (DEBUG > 2) {
|
| 175 |
echo "<pre>\n";
|
| 176 |
print "'Parent of' array:";
|
| 177 |
print_r($parent_of);
|
| 178 |
print "Old nid => New nid:";
|
| 179 |
print_r($new_nid);
|
| 180 |
echo "</pre>\n";
|
| 181 |
};
|
| 182 |
|
| 183 |
/* The book table entry will have been created by
|
| 184 |
the 'node_save()' operation since the type
|
| 185 |
of the node is 'book'. We just need to update
|
| 186 |
the table to get the parents and such right */
|
| 187 |
$book_sql =
|
| 188 |
db_rewrite_sql(
|
| 189 |
"UPDATE {book} ".
|
| 190 |
"SET ".
|
| 191 |
" nid=%d, ".
|
| 192 |
" parent=%d, ".
|
| 193 |
" weight=%d ".
|
| 194 |
"WHERE".
|
| 195 |
" vid='%d' "
|
| 196 |
);
|
| 197 |
|
| 198 |
if (!empty($new_nid)) {
|
| 199 |
foreach ($new_nid as $nid => $new_value) {
|
| 200 |
$node = node_load(array('nid' => $new_value));
|
| 201 |
$node->nid = $new_value;
|
| 202 |
$node->parent = $new_nid[$parent_of[$nid]];
|
| 203 |
|
| 204 |
/*
|
| 205 |
$log = "Imported on " . date("D F j, Y") . " at " . date("G:i:s T") . "<br />";
|
| 206 |
$log .= "parent[$nid ($new_value)] = " .$parent_of[$nid]. " ($this_parent)";
|
| 207 |
$log .= $node->log;
|
| 208 |
*/
|
| 209 |
if (!isset($nid) || $nid == '') {
|
| 210 |
$node->parent = $parent_of[$nid];
|
| 211 |
$nid = $new_value;
|
| 212 |
}
|
| 213 |
|
| 214 |
if (DEBUG > 0) {
|
| 215 |
echo sprintf("UPDATE {book} ".
|
| 216 |
"SET ".
|
| 217 |
" nid=%d, ".
|
| 218 |
" parent=%d, ".
|
| 219 |
" weight=%d ".
|
| 220 |
"WHERE".
|
| 221 |
" vid=%d ",
|
| 222 |
$node->nid,
|
| 223 |
$node->parent,
|
| 224 |
$node->weight,
|
| 225 |
$node->vid
|
| 226 |
);
|
| 227 |
echo "<br />";
|
| 228 |
}
|
| 229 |
$result = db_query($book_sql,
|
| 230 |
$node->nid,
|
| 231 |
$node->parent,
|
| 232 |
$node->weight,
|
| 233 |
$node->vid
|
| 234 |
);
|
| 235 |
|
| 236 |
if (DEBUG > 0) {
|
| 237 |
echo "parent[$nid ($new_value)] = ". $parent_of[$nid] ." ($node->parent)<br />";
|
| 238 |
}
|
| 239 |
}
|
| 240 |
}
|
| 241 |
}
|
| 242 |
|
| 243 |
?>
|