/[drupal]/contributions/modules/bookimport/save_node.php
ViewVC logotype

Diff of /contributions/modules/bookimport/save_node.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Fri Sep 2 17:06:59 2005 UTC revision 1.2, Fri Jan 27 06:45:45 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3    // $Id:$
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    
22  /**  /**
23   * Fills in the body of the given node.   * Fills in the body of the given node.
24   */   */
25  function save($tnode, $new_nid, $parent_of, $mode) {  function save($tnode, $new_nid, $parent_of, $mode) {
26    
27      if (DEBUG > 2) {      if (DEBUG > 2) {
28          echo "saving node '" . $tnode->title . "' ";          echo "saving node '". $tnode->title . "' ";
29          echo "(id = " . $tnode->id .", mode = $mode)\n";          echo "(id = ". $tnode->id .", mode = $mode)\n";
30    
31          // set up metadata          // set up metadata
32          $nodeid = preg_replace('@^(node-)(\d+)$@', '\2', $tnode->id);          $nodeid = preg_replace('@^(node-)(\d+)$@', '\2', $tnode->id);
33          $nidinfo  = "  nid:$nodeid\n";          $nidinfo  = "  nid:$nodeid\n";
34          $nidinfo .= "  title:$tnode->title\n";          $nidinfo .= "  title:$tnode->title\n";
35          $nidinfo .= "  md5:".$tnode->get_md5()."\n";          $nidinfo .= "  md5:". $tnode->get_md5_body() ."\n";
36          $nidinfo .= "  weight:".$tnode->get_weight()."\n";          $nidinfo .= "  weight:". $tnode->get_weight() ."\n";
37          $nidinfo .= "  depth:".$tnode->get_depth()."\n";          $nidinfo .= "  depth:". $tnode->get_depth() ."\n";
38          $nidinfo .= "  parent:".$tnode->get_parent()."\n";          $nidinfo .= "  parent:". $tnode->get_parent() ."\n";
39    
40          echo "<pre style=\"font-family: courier; font-size: 8pt;\">";          echo "<pre style=\"font-family: courier; font-size: 8pt;\">";
41          echo "$nidinfo</pre>";          echo "$nidinfo</pre>";
# Line 34  function save($tnode, $new_nid, $parent_ Line 53  function save($tnode, $new_nid, $parent_
53  }  }
54    
55  /**  /**
56   *   * Creates a new node in the database
57   */   */
58  function create($tnode) {  function create($tnode) {
59      global $user;      global $user;
60      global $parent_of;      global $parent_of;
61      global $new_nid;      global $new_nid;
62    
63      $node->body = $tnode->body;      $node->type = $tnode->nodetype;
64      $node->title = $tnode->title;      $node->title = $tnode->title;
     $node->weight = $tnode->weight;  
   
     $node->nodetype = $tnode->nodetype;  
     $node->author = $tnode->author;  
65      $node->uid = $tnode->uid;      $node->uid = $tnode->uid;
     $node->created = $tnode->created;  
66      $node->status = $tnode->status;      $node->status = $tnode->status;
67      $node->format = $tnode->format;      $node->created = $tnode->created;
     $node->sticky = $tnode->sticky;  
68      $node->promote = $tnode->promote;      $node->promote = $tnode->promote;
69        $node->moderate = $tnode->moderatep;
70        $node->changed = $tnode->changed;
71        $node->sticky = $tnode->sticky;
72    
73        $node->format = $tnode->format;
74        $node->weight = $tnode->weight;
75        $node->body = $tnode->body;
76        $node->author = $tnode->author;
77    
78      // $node->uid = $user->uid;      // $node->uid = $user->uid;
79      // $node->create = time();      // $node->create = time();
80    
81      $node->nid = node_save($node);      node_save($node);
82        if (DEBUG > 1)  {
83            echo "new nid is: ". $node->nid ."<br />";
84        }
85      // get the new nid, store it hashed by old nid:      // get the new nid, store it hashed by old nid:
86      $new_nid[$tnode->id] = $node->nid;      $new_nid[$tnode->id] = $node->nid;
87      // record the parent-child of the node,      // record the parent-child of the node,
88      // based on the existing ids      // based on the existing ids
89      $parent_of[$tnode->id] = $tnode->parent;      $parent_of[$tnode->id] = $tnode->parent;
90    
91      if (DEBUG > 1)  { echo "created: nid = " . $node->nid. "\n"; } ;      if (DEBUG > 1)  {
92            echo "created: nid = ". $node->nid ."\n";
93        };
94        return ($node->nid);
95    }
96    
97    function _update_book_node($tnode) {
98        $timestamp = time();
99        $log = "\nInserted on ". date("D F j, Y") ." at ". date("G:i:s T") ."<br />\n";
100    
101        $node->type = $tnode->nodetype;
102        $node->title = $tnode->title;
103        $node->uid = $tnode->uid;
104        $node->status = $tnode->status;
105        $node->created = $tnode->created;
106        $node->promote = $tnode->promote;
107        $node->moderate = $tnode->moderatep;
108        $node->changed = $tnode->changed;
109        $node->sticky = $tnode->sticky;
110    
111        $node->format = $tnode->format;
112        $node->weight = $tnode->weight;
113        $node->body = $tnode->body;
114        $node->author = $tnode->author;
115    
116        $node->nid = $tnode->id;
117    
118        /* this saves the node; creates a new revision, etc. */
119    
120        node_save($node);
121        if (DEBUG > 1)  {
122            echo "new nid is: ". $node->nid ."<br />";
123        }
124    
125        // Since we're updating, we don't need to remap nids
126        // record the parent-child of the node,
127        // based on the existing ids
128        $parent_of[$node->nid] = $tnode->parent;
129    
130      return ($node->nid);      return ($node->nid);
131  }  }
132    
133    
134  /**  /**
135   *   * Updates a given node in the database
136   */   */
137  function update($tnode) {  function update($tnode) {
138    
# Line 78  function update($tnode) { Line 140  function update($tnode) {
140      // In this case, create the node rather than updating      // In this case, create the node rather than updating
141      // an existing node.      // an existing node.
142    
143      // echo "update: tnode->id = " . $tnode->id. "<br/>\n";      // echo "update: tnode->id = " . $tnode->id. "<br />\n";
144    
145      if (!isset($tnode->id) || $tnode->id == 0) {      if (!isset($tnode->id) || $tnode->id == 0) {
146          $tnode->id = create($tnode);          create($tnode);
         if (DEBUG > 1)  { echo "update: created new node with nid = " . $node->nid. "\n"; };  
         // echo "update: created new node with nid = " . $tnode->id. "<br/>\n";  
   
         $log = "\nInserted on " . date("D F j, Y") . " at " . date("G:i:s T") . "<br/>\n";  
   
         $book_sql =  
             db_rewrite_sql(  
                            "UPDATE {book} ".  
                            "SET ".  
                            "  weight=%d, ".  
                            "  parent=%d, ".  
                            "  log='%s' ".  
                            "WHERE ".  
                            "  nid = %d"  
                            );  
   
         $result = db_query($book_sql,  
                            $tnode->get_weight(),  
                            $tnode->parent,  
                            $tnode->log . $log,  
                            $tnode->id  
                            );  
147      }      }
148      else {      else { // were updating an existing entry
149          $timestamp = time();          _update_book_node($tnode);
         $log = "\nUpdated on " . date("D F j, Y") . " at " . date("G:i:s T") . "<br/>\n";  
         $node_sql =  
             db_rewrite_sql(  
                            "UPDATE {node} ".  
                            "SET".  
                            "  type=%s, ".  
                            "  author=%s, ".  
                            "  uid=%d, ".  
                            "  created=%d, ".  
                            "  status=%d, ".  
                            "  format=%d, ".  
                            "  sticky=%d, ".  
                            "  promote=%d, ".  
                            "  weight=%d, ".  
                            "  title='%s',".  
                            "  body='%s',".  
                            "  changed=%d ".  
                            "WHERE".  
                            "  nid=%d"  
                            );  
   
         $book_sql =  
             db_rewrite_sql(  
                            "UPDATE {book} ".  
                            "SET".  
                            "  weight=%d,".  
                            "  parent=%d,".  
                            "  log='%s' ".  
                            "WHERE".  
                            "  nid=%d "  
                            );  
   
         $result = db_query($node_sql,  
                            $tnode->nodetype,  
                            $tnode->author,  
                            $tnode->uid,  
                            $tnode->created,  
                            $tnode->status,  
                            $tnode->format,  
                            $tnode->sticky,  
                            $tnode->promote,  
                            $tnode->get_weight(),  
                            $tnode->title,  
                            $tnode->body,  
                            $timestamp,  
                            $tnode->id  
                            );  
   
         $result = db_query($book_sql,  
                            $tnode->get_weight(),  
                            $tnode->parent,  
                            $tnode->log . $log,  
                            $tnode->id  
                            );  
   
         // if (TRUE) {  
         if (DEBUG > 2) {  
             echo "<pre style=\"font-family: courier; font-size: 8pt;\">";  
             echo "db_query(\"$node_sql\", ".  
                 "'$tnode->title', ".  
                 "tnode->body, ".  
                 "$timestamp, ".  
                 "$tnode->id)<br/>\n";  
             echo "db_query(\"$book_sql\", ".$tnode->get_weight().", ". $tnode->parent.", $tnode->id)<br/>\n";  
             echo "</pre>\n";  
         }  
150      }      }
151  }  }
152    
153  # at end, if inserting new records iterate over new_nid  # at end, if inserting new records iterate over new_nid
154  function make_links($parent_of, $new_nid) {  function make_links($parent_of, $new_nid) {
   
155      global $parent_of;      global $parent_of;
156      global $new_nid;      global $new_nid;
157    
158      if (DEBUG > 0) { echo "make_links()<br/>"; };      if (DEBUG > 0) {
159      if (DEBUG > 4) {          echo "make_links()<br />";
160        };
161        if (DEBUG > 2) {
162          echo "<pre>\n";          echo "<pre>\n";
163            print "'Parent of' array:";
164          print_r($parent_of);          print_r($parent_of);
165            print "Old nid => New nid:";
166          print_r($new_nid);          print_r($new_nid);
167          echo "</pre>\n";          echo "</pre>\n";
168      };      };
169    
170        /* The book table entry will have been created by
171           the 'node_save()' operation since the type
172           of the node is 'book'.  We just need to update
173           the table to get the parents and such right */
174      $book_sql =      $book_sql =
175          db_rewrite_sql(          db_rewrite_sql(
176                         "UPDATE {book} ".                         "UPDATE {book} ".
177                         "SET".                         "SET ".
178                         "  weight=%d, ".                         "  nid=%d, ".
179                         "  parent=%d, ".                         "  parent=%d, ".
180                         "  log='%s' ".                         "  weight=%d ".
181                         "WHERE".                         "WHERE".
182                         "  nid=%d "                         "  vid='%d' "
183                         );                         );
184    
185    
186      foreach ($new_nid as $nid => $new_value) {      foreach ($new_nid as $nid => $new_value) {
187          $node = node_load(array('nid' => $new_value));          $node = node_load(array('nid' => $new_value));
188          $node->nid = $new_value;          $node->nid = $new_value;
189          $this_parent = $new_nid[$parent_of[$nid]];          $node->parent = $new_nid[$parent_of[$nid]];
         $node->parent = $this_parent;  
190    
191          $log = "Imported on " . date("D F j, Y") . " at " . date("G:i:s T") . "<br/>";          /*
192          $log .= "parent[$nid ($new_value)] = " .$parent_of[$nid]. " ($this_parent)";           $log = "Imported on " . date("D F j, Y") . " at " . date("G:i:s T") . "<br />";
193          $log .= $node->log;           $log .= "parent[$nid ($new_value)] = " .$parent_of[$nid]. " ($this_parent)";
194             $log .= $node->log;
195            */
196            if (!isset($nid) || $nid == '') {
197                $node->parent = $parent_of[$nid];
198                $nid = $new_value;
199            }
200    
201            if (DEBUG > 0) {
202                echo sprintf("UPDATE {book} ".
203                             "SET ".
204                             "  nid=%d, ".
205                             "  parent=%d, ".
206                             "  weight=%d ".
207                             "WHERE".
208                             "  vid='%d' ",
209                             $node->nid,
210                             $node->parent,
211                             $node->weight,
212                             $node->vid
213                             );
214                echo "<br />";
215            }
216          $result = db_query($book_sql,          $result = db_query($book_sql,
217                               $node->nid,
218                               $node->parent,
219                             $node->weight,                             $node->weight,
220                             $this_parent,                             $node->vid
                            $log,  
                            $node->nid  
221                             );                             );
222    
223          if (DEBUG > 0) { echo "parent[$nid ($new_value)] = " .$parent_of[$nid]. " ($this_parent)<br/>"; }          echo "parent[$nid ($new_value)] = ". $parent_of[$nid] ." ($node->parent)<br />";
224    
225    
226      }      }
227  }  }
228    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2