Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-36741 mod_forum: fixed SQL that was generated when either timed p…

…osts or groups were enabled
  • Loading branch information...
commit 6e726489e7b53d6e5e6008ef101b4c3a76bcfd22 1 parent 6548ce0
Mark Nelson authored November 23, 2012
15  lib/rsslib.php
@@ -207,10 +207,21 @@ function rss_get_file_full_name($componentname, $filename) {
207 207
  *
208 208
  * @param stdClass $instance the instance of the source of the RSS feed
209 209
  * @param string $sql the SQL used to produce the RSS feed
  210
+ * @param array $params the parameters used in the SQL query
210 211
  * @return string the name of the RSS file
211 212
  */
212  
-function rss_get_file_name($instance, $sql) {
213  
-    return $instance->id.'_'.md5($sql);
  213
+function rss_get_file_name($instance, $sql, $params = array()) {
  214
+    if ($params) {
  215
+        // If a parameters array is passed, then we want to
  216
+        // serialize it and then concatenate it with the sql.
  217
+        // The reason for this is to generate a unique filename
  218
+        // for queries using the same sql but different parameters.
  219
+        asort($parms);
  220
+        $serializearray = serialize($params);
  221
+        return $instance->id.'_'.md5($sql . $serializearray);
  222
+    } else {
  223
+        return $instance->id.'_'.md5($sql);
  224
+    }
214 225
 }
215 226
 
216 227
 /**
89  mod/forum/rsslib.php
@@ -56,16 +56,10 @@ function forum_rss_get_feed($context, $args) {
56 56
     }
57 57
 
58 58
     //the sql that will retreive the data for the feed and be hashed to get the cache filename
59  
-    $sql = forum_rss_get_sql($forum, $cm);
  59
+    list($sql, $params) = forum_rss_get_sql($forum, $cm);
60 60
 
61 61
     // Hash the sql to get the cache file name.
62  
-    // If the forum is Q and A then we need to cache the files per user. This can
63  
-    // have a large impact on performance, so we want to only do it on this type of forum.
64  
-    if ($forum->type == 'qanda') {
65  
-        $filename = rss_get_file_name($forum, $sql . $USER->id);
66  
-    } else {
67  
-        $filename = rss_get_file_name($forum, $sql);
68  
-    }
  62
+    $filename = rss_get_file_name($forum, $sql, $params);
69 63
     $cachedfilepath = rss_get_file_full_name('mod_forum', $filename);
70 64
 
71 65
     //Is the cache out of date?
@@ -75,9 +69,9 @@ function forum_rss_get_feed($context, $args) {
75 69
     }
76 70
     //if the cache is more than 60 seconds old and there's new stuff
77 71
     $dontrecheckcutoff = time()-60;
78  
-    if ( $dontrecheckcutoff > $cachedfilelastmodified && forum_rss_newstuff($forum, $cm, $cachedfilelastmodified)) {
  72
+    if ($dontrecheckcutoff > $cachedfilelastmodified && forum_rss_newstuff($forum, $cm, $cachedfilelastmodified)) {
79 73
         //need to regenerate the cached version
80  
-        $result = forum_rss_feed_contents($forum, $sql, $modcontext);
  74
+        $result = forum_rss_feed_contents($forum, $sql, $params, $modcontext);
81 75
         if (!empty($result)) {
82 76
             $status = rss_save_file('mod_forum',$filename,$result);
83 77
         }
@@ -111,10 +105,12 @@ function forum_rss_delete_file($forum) {
111 105
 function forum_rss_newstuff($forum, $cm, $time) {
112 106
     global $DB;
113 107
 
114  
-    $sql = forum_rss_get_sql($forum, $cm, $time);
  108
+    list($sql, $params) = forum_rss_get_sql($forum, $cm, $time);
  109
+    if ($DB->count_records_sql($sql, $params) > 0) {
  110
+        return true;
  111
+    }
115 112
 
116  
-    $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
117  
-    return ($recs && !empty($recs));
  113
+    return false;
118 114
 }
119 115
 
120 116
 /**
@@ -126,17 +122,11 @@ function forum_rss_newstuff($forum, $cm, $time) {
126 122
  * @return string the SQL query to be used to get the Discussion/Post details from the forum table of the database
127 123
  */
128 124
 function forum_rss_get_sql($forum, $cm, $time=0) {
129  
-    $sql = null;
130  
-
131  
-    if (!empty($forum->rsstype)) {
132  
-        if ($forum->rsstype == 1) {    //Discussion RSS
133  
-            $sql = forum_rss_feed_discussions_sql($forum, $cm, $time);
134  
-        } else {                //Post RSS
135  
-            $sql = forum_rss_feed_posts_sql($forum, $cm, $time);
136  
-        }
  125
+    if ($forum->rsstype == 1) { // Discussion RSS
  126
+        return forum_rss_feed_discussions_sql($forum, $cm, $time);
  127
+    } else { // Post RSS
  128
+        return forum_rss_feed_posts_sql($forum, $cm, $time);
137 129
     }
138  
-
139  
-    return $sql;
140 130
 }
141 131
 
142 132
 /**
@@ -155,7 +145,7 @@ function forum_rss_feed_discussions_sql($forum, $cm, $newsince=0) {
155 145
     $modcontext = null;
156 146
 
157 147
     $now = round(time(), -2);
158  
-    $params = array($cm->instance);
  148
+    $params = array();
159 149
 
160 150
     $modcontext = context_module::instance($cm->id);
161 151
 
@@ -172,21 +162,21 @@ function forum_rss_feed_discussions_sql($forum, $cm, $newsince=0) {
172 162
         }
173 163
     }
174 164
 
175  
-    //do we only want new posts?
  165
+    // Do we only want new posts?
176 166
     if ($newsince) {
177  
-        $newsince = " AND p.modified > '$newsince'";
  167
+        $params['newsince'] = $newsince;
  168
+        $newsince = " AND p.modified > :newsince";
178 169
     } else {
179 170
         $newsince = '';
180 171
     }
181 172
 
182  
-    //get group enforcing SQL
183  
-    $groupmode    = groups_get_activity_groupmode($cm);
  173
+    // Get group enforcing SQL.
  174
+    $groupmode = groups_get_activity_groupmode($cm);
184 175
     $currentgroup = groups_get_activity_group($cm);
185  
-    $groupselect = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
  176
+    list($groupselect, $groupparams) = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
186 177
 
187  
-    if ($groupmode && $currentgroup) {
188  
-        $params['groupid'] = $currentgroup;
189  
-    }
  178
+    // Add the groupparams to the params array.
  179
+    $params = array_merge($params, $groupparams);
190 180
 
191 181
     $forumsort = "d.timemodified DESC";
192 182
     $postdata = "p.id AS postid, p.subject, p.created as postcreated, p.modified, p.discussion, p.userid, p.message as postmessage, p.messageformat AS postformat, p.messagetrust AS posttrust";
@@ -199,7 +189,7 @@ function forum_rss_feed_discussions_sql($forum, $cm, $newsince=0) {
199 189
              WHERE d.forum = {$forum->id} AND p.parent = 0
200 190
                    $timelimit $groupselect $newsince
201 191
           ORDER BY $forumsort";
202  
-    return $sql;
  192
+    return array($sql, $params);
203 193
 }
204 194
 
205 195
 /**
@@ -213,19 +203,20 @@ function forum_rss_feed_discussions_sql($forum, $cm, $newsince=0) {
213 203
 function forum_rss_feed_posts_sql($forum, $cm, $newsince=0) {
214 204
     $modcontext = context_module::instance($cm->id);
215 205
 
216  
-    //get group enforcement SQL
217  
-    $groupmode    = groups_get_activity_groupmode($cm);
  206
+    // Get group enforcement SQL.
  207
+    $groupmode = groups_get_activity_groupmode($cm);
218 208
     $currentgroup = groups_get_activity_group($cm);
  209
+    $params = array();
219 210
 
220  
-    $groupselect = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
  211
+    list($groupselect, $groupparams) = forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext);
221 212
 
222  
-    if ($groupmode && $currentgroup) {
223  
-        $params['groupid'] = $currentgroup;
224  
-    }
  213
+    // Add the groupparams to the params array.
  214
+    $params = array_merge($params, $groupparams);
225 215
 
226  
-    //do we only want new posts?
  216
+    // Do we only want new posts?
227 217
     if ($newsince) {
228  
-        $newsince = " AND p.modified > '$newsince'";
  218
+        $params['newsince'] = $newsince;
  219
+        $newsince = " AND p.modified > :newsince";
229 220
     } else {
230 221
         $newsince = '';
231 222
     }
@@ -250,7 +241,7 @@ function forum_rss_feed_posts_sql($forum, $cm, $newsince=0) {
250 241
                 $groupselect
251 242
             ORDER BY p.created desc";
252 243
 
253  
-    return $sql;
  244
+    return array($sql, $params);
254 245
 }
255 246
 
256 247
 /**
@@ -264,6 +255,7 @@ function forum_rss_feed_posts_sql($forum, $cm, $newsince=0) {
264 255
  */
265 256
 function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=null) {
266 257
     $groupselect = '';
  258
+    $params = array();
267 259
 
268 260
     if ($groupmode) {
269 261
         if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $modcontext)) {
@@ -272,7 +264,7 @@ function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=nul
272 264
                 $params['groupid'] = $currentgroup;
273 265
             }
274 266
         } else {
275  
-            //seprate groups without access all
  267
+            // Separate groups without access all.
276 268
             if ($currentgroup) {
277 269
                 $groupselect = "AND (d.groupid = :groupid OR d.groupid = -1)";
278 270
                 $params['groupid'] = $currentgroup;
@@ -282,7 +274,7 @@ function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=nul
282 274
         }
283 275
     }
284 276
 
285  
-    return $groupselect;
  277
+    return array($groupselect, $params);
286 278
 }
287 279
 
288 280
 /**
@@ -290,21 +282,19 @@ function forum_rss_get_group_sql($cm, $groupmode, $currentgroup, $modcontext=nul
290 282
  * It returns false if something is wrong
291 283
  *
292 284
  * @param stdClass $forum the forum object
293  
- * @param string   $sql   The SQL used to retrieve the contents from the database
  285
+ * @param string $sql the SQL used to retrieve the contents from the database
  286
+ * @param array $params the SQL parameters used
294 287
  * @param object $context the context this forum relates to
295 288
  * @return bool|string false if the contents is empty, otherwise the contents of the feed is returned
296 289
  *
297 290
  * @Todo MDL-31129 implement post attachment handling
298 291
  */
299 292
 
300  
-function forum_rss_feed_contents($forum, $sql) {
  293
+function forum_rss_feed_contents($forum, $sql, $params, $context) {
301 294
     global $CFG, $DB, $USER;
302 295
 
303  
-
304 296
     $status = true;
305 297
 
306  
-    $params = array();
307  
-    //$params['forumid'] = $forum->id;
308 298
     $recs = $DB->get_recordset_sql($sql, $params, 0, $forum->rssarticles);
309 299
 
310 300
     //set a flag. Are we displaying discussions or posts?
@@ -316,7 +306,6 @@ function forum_rss_feed_contents($forum, $sql) {
316 306
     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course)) {
317 307
         print_error('invalidcoursemodule');
318 308
     }
319  
-    $context = context_module::instance($cm->id);
320 309
 
321 310
     $formatoptions = new stdClass();
322 311
     $items = array();

0 notes on commit 6e72648

Please sign in to comment.
Something went wrong with that request. Please try again.