From 3052c49e0924fbe93789dec3760ae8f19685654e Mon Sep 17 00:00:00 2001
From: Gerhard Killesreiter <killes_www_drop_org@227.no-reply.drupal.org>
Date: Sun, 26 Mar 2006 19:35:05 +0000
Subject: [PATCH] #53826, Book export: db_rewrite breaks sql query., patch by
 puregin

---
 modules/book.module      | 51 +++++++++++++---------------------------
 modules/book/book.module | 51 +++++++++++++---------------------------
 2 files changed, 32 insertions(+), 70 deletions(-)

diff --git a/modules/book.module b/modules/book.module
index 5ecd04a83545..70f0ee90c7cf 100644
--- a/modules/book.module
+++ b/modules/book.module
@@ -370,7 +370,16 @@ function book_outline_submit($form_id, $form_values) {
 }
 
 /**
- * Return the path (call stack) to a certain book page.
+ * Given a node, this function returns an array of 'book node' objects 
+ * representing the path in the book tree from the root to the 
+ * parent of the given node.
+ * 
+ * @param node - a book node object for which to compute the path
+ *
+ * @return - an array of book node objects representing the path of
+ * nodes root to parent of the given node. Returns an empty array if
+ * the node does not exist or is not part of a book hierarchy.
+ *
  */
 function book_location($node, $nodes = array()) {
   $parent = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $node->parent));
@@ -505,7 +514,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) {
 }
 
 /**
- * Prepares both the custom breadcrumb trail and the forward/backward
+ * Prepares the links to children (TOC) and forward/backward
  * navigation for a node presented as a book page.
  *
  * @ingroup themeable
@@ -669,7 +678,11 @@ function book_render() {
  */
 function book_export($type = 'html', $nid = 0) {
   $type = drupal_strtolower($type);
-  $depth = _book_get_depth($nid);
+  $node_result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $nid);
+  if (db_num_rows($node_result) > 0) {
+      $node = db_fetch_object($node_result);
+  }
+  $depth = count(book_location($node)) + 1;
   $export_function = 'book_export_' . $type;
 
   if (function_exists($export_function)) {
@@ -743,38 +756,6 @@ function theme_book_export_html($title, $content) {
   return $html;
 }
 
-/**
- * Given a node, this function returns the depth of the node in its hierarchy.
- * A root node has depth 1, and children of a node of depth n have depth (n+1).
- *
- * @param nid
- *   - the nid of the node whose depth to compute.
- * @return
- *   - the depth of the given node in its hierarchy.  Returns 0 if the node
- *  does not exist or is not part of a book hierarchy.
- */
-function _book_get_depth($nid) {
-  $depth = 0;
-  if ($nid) {
-    while ($nid) {
-      $result = db_query(db_rewrite_sql('SELECT b.parent FROM {book} b WHERE b.nid = %d'), $nid);
-      $obj =  db_fetch_object($result);
-      $parent = $obj->parent;
-      if ($nid == $parent->parent) {
-        $nid = 0;
-      }
-      else {
-        $nid = $parent;
-      }
-      $depth++;
-    }
-    return $depth;
-  }
-  else {
-    return 0;
-  }
-}
-
 /**
  * Traverses the book tree.  Applies the $visit_pre() callback to each
  * node, is called recursively for each child of the node (in weight,
diff --git a/modules/book/book.module b/modules/book/book.module
index 5ecd04a83545..70f0ee90c7cf 100644
--- a/modules/book/book.module
+++ b/modules/book/book.module
@@ -370,7 +370,16 @@ function book_outline_submit($form_id, $form_values) {
 }
 
 /**
- * Return the path (call stack) to a certain book page.
+ * Given a node, this function returns an array of 'book node' objects 
+ * representing the path in the book tree from the root to the 
+ * parent of the given node.
+ * 
+ * @param node - a book node object for which to compute the path
+ *
+ * @return - an array of book node objects representing the path of
+ * nodes root to parent of the given node. Returns an empty array if
+ * the node does not exist or is not part of a book hierarchy.
+ *
  */
 function book_location($node, $nodes = array()) {
   $parent = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $node->parent));
@@ -505,7 +514,7 @@ function book_nodeapi(&$node, $op, $teaser, $page) {
 }
 
 /**
- * Prepares both the custom breadcrumb trail and the forward/backward
+ * Prepares the links to children (TOC) and forward/backward
  * navigation for a node presented as a book page.
  *
  * @ingroup themeable
@@ -669,7 +678,11 @@ function book_render() {
  */
 function book_export($type = 'html', $nid = 0) {
   $type = drupal_strtolower($type);
-  $depth = _book_get_depth($nid);
+  $node_result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $nid);
+  if (db_num_rows($node_result) > 0) {
+      $node = db_fetch_object($node_result);
+  }
+  $depth = count(book_location($node)) + 1;
   $export_function = 'book_export_' . $type;
 
   if (function_exists($export_function)) {
@@ -743,38 +756,6 @@ function theme_book_export_html($title, $content) {
   return $html;
 }
 
-/**
- * Given a node, this function returns the depth of the node in its hierarchy.
- * A root node has depth 1, and children of a node of depth n have depth (n+1).
- *
- * @param nid
- *   - the nid of the node whose depth to compute.
- * @return
- *   - the depth of the given node in its hierarchy.  Returns 0 if the node
- *  does not exist or is not part of a book hierarchy.
- */
-function _book_get_depth($nid) {
-  $depth = 0;
-  if ($nid) {
-    while ($nid) {
-      $result = db_query(db_rewrite_sql('SELECT b.parent FROM {book} b WHERE b.nid = %d'), $nid);
-      $obj =  db_fetch_object($result);
-      $parent = $obj->parent;
-      if ($nid == $parent->parent) {
-        $nid = 0;
-      }
-      else {
-        $nid = $parent;
-      }
-      $depth++;
-    }
-    return $depth;
-  }
-  else {
-    return 0;
-  }
-}
-
 /**
  * Traverses the book tree.  Applies the $visit_pre() callback to each
  * node, is called recursively for each child of the node (in weight,
-- 
GitLab