From d4c2eb3f8c211210144908a9a159d45f965f02d7 Mon Sep 17 00:00:00 2001
From: Adam Franco <afranco@middlebury.edu>
Date: Thu, 28 Jul 2011 11:09:22 -0400
Subject: [PATCH] Issue #1232498 Avoid loading all tag data on every page
 load.

Because _nodewords_detect_type_and_ids() makes a call to
_nodewords_get_pages_data(), nodewords_load_tags() gets called for
every single page in the nodewords_custom table. For a site with 1000
entries in the nodewords_custom table this results in an extra 1000
queries on every page load, just to find out if a path in
nodewords_custom matches the current path.

Loading just the content of the nodewords_custom table when
determining path matches will greatly cut down on unneeded queries.
---
 nodewords.module |   28 +++++++++++++++++++++++-----
 1 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/nodewords.module b/nodewords.module
index 7f67a0e..f5f6bf5 100644
--- a/nodewords.module
+++ b/nodewords.module
@@ -1271,18 +1271,16 @@ function _nodewords_detect_type_and_id() {
   }
 
   // Check all of the custom page paths.
-  foreach (_nodewords_get_custom_pages_data() as $page) {
-    $path = $page->path;
-
+  foreach (_nodewords_get_pages_paths() as $pid => $path) {
     // The path is a system path.
     if (drupal_match_path($_GET['q'], $path)) {
-      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $page->pid);
+      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $pid);
     }
 
     // The path is a URL alias.
     $alias = drupal_get_path_alias($_GET['q']);
     if ($alias != $_GET['q'] && drupal_match_path($alias, $path)) {
-      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $page->pid);
+      return array('type' => NODEWORDS_TYPE_PAGE, 'id' => $pid);
     }
   }
 
@@ -1334,6 +1332,26 @@ function _nodewords_get_custom_pages_data($id = NULL) {
 }
 
 /**
+ * Answer a listing of page paths.
+ *
+ */
+function _nodewords_get_pages_paths() {
+  static $paths;
+
+  if (!isset($paths)) {
+    $paths = array();
+    $result = db_query("SELECT pid, path FROM {nodewords_custom} ORDER BY weight ASC");
+
+    while ($page = db_fetch_object($result)) {
+      $paths[$page->pid] = $page->path;
+    }
+  }
+
+  return $paths;
+}
+
+
+/**
  * Load the files in the directory "includes" basing on the enabled modules.
  */
 function _nodewords_load_all_includes() {
-- 
1.7.6.1

