Skip to content

Commit 147b874

Browse files
committed
More backports, hack around getTopics() explosion on MySQL.
1 parent 5f217f9 commit 147b874

File tree

7 files changed

+121
-92
lines changed

7 files changed

+121
-92
lines changed

forum/code/controllers/ForumMemberProfile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function doregister($data, $form) {
231231
$member->write();
232232
$member->login();
233233

234-
$forumGroup->Members()->add($member);
234+
$member->Groups()->add($forumGroup);
235235

236236
$member->extend('onForumRegister', $this->request);
237237

forum/code/model/ForumThread.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,17 @@ function incNumViews() {
203203
* @return String
204204
*/
205205
function Link($action = "show", $showID = true) {
206-
$forum = DataObject::get_by_id("Forum", (int)$this->ForumID);
207-
208-
// Prevents a crash and burn if you're subscribed to an impossible thread...
209-
// Happened to my profile after one of SoonDead's CSRF tests. -Colby
210-
if (!$forum)
206+
$forum = DataObject::get_by_id("Forum", (int)$this->ForumID);
207+
if ($forum) {
208+
$baseLink = $forum->Link();
209+
$extra = ($showID) ? '/'.$this->ID : '';
210+
return ($action) ? $baseLink . $action . $extra : $baseLink;
211+
} else {
212+
// user_error("Bad ForumID '$this->ForumID'", E_USER_WARNING);
213+
// Prevents a crash and burn if you're subscribed to an impossible thread...
214+
// Happened to my profile after one of SoonDead's CSRF tests. -Colby
211215
return "#";
212-
213-
$baseLink = $forum->Link();
214-
$extra = ($showID) ? '/'.$this->ID : '';
215-
216-
return ($action) ? $baseLink . $action . $extra : $baseLink;
216+
}
217217
}
218218

219219
/**

forum/code/pagetypes/Forum.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ function getTopics() {
395395

396396
// If we're a moderater, awaiting & approved posts. Otherwise just approved
397397
if(Member::currentUser()==$this->Moderator() && is_numeric($this->ID)) {
398-
$posts = $posts->filter('Status', array('Moderated', 'Awaiting'));
398+
// $posts = $posts->filter('Status', array('Moderated', 'Awaiting'));
399399
} else {
400-
$posts = $posts->filter('Status', 'Moderated');
400+
// $posts = $posts->filter('Status', 'Moderated');
401401
}
402402

403403
// Get the underlying query and change it to return the ThreadID and Max(Created) and Max(ID) for each thread
@@ -700,8 +700,11 @@ function PostMessageForm($addMode = false, $post = false) {
700700

701701
$thread = false;
702702

703-
if($post) $thread = $post->Thread();
704-
else if(isset($this->urlParams['ID'])) $thread = DataObject::get_by_id('ForumThread', $this->urlParams['ID']);
703+
if ($post) {
704+
$thread = $post->Thread();
705+
} else if(isset($this->urlParams['ID']) && is_numeric($this->urlParams['ID'])) {
706+
$thread = DataObject::get_by_id('ForumThread', $this->urlParams['ID']);
707+
}
705708

706709
// Check permissions
707710
$messageSet = array(
@@ -787,6 +790,8 @@ function PostMessageForm($addMode = false, $post = false) {
787790

788791
$form = new Form($this, 'PostMessageForm', $fields, $actions, $required);
789792

793+
$this->extend('updatePostMessageForm', $form, $post);
794+
790795
Requirements::customScript(<<<EX
791796
(function($) {
792797
$(function () {

forum/code/pagetypes/ForumHolder.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ function getNumPosts() {
179179
return DB::query("
180180
SELECT COUNT(\"Post\".\"ID\")
181181
FROM \"Post\"
182-
JOIN \"ForumThread\" ON \"Post\".\"ThreadID\" = \"ForumThread\".\"ID\"
183-
JOIN \"" . ForumHolder::baseForumTable() . "\" AS \"ForumPage\" ON \"ForumThread\".\"ForumID\" = \"ForumPage\".\"ID\"
182+
JOIN \"" . ForumHolder::baseForumTable() . "\" AS \"ForumPage\" ON \"Post\".\"ForumID\" = \"ForumPage\".\"ID\"
184183
WHERE \"ForumPage\".\"ParentID\" = '" . $this->ID . "'")->value();
185184
}
186185

@@ -208,8 +207,7 @@ function getNumAuthors() {
208207
return DB::query("
209208
SELECT COUNT(DISTINCT \"Post\".\"AuthorID\")
210209
FROM \"Post\"
211-
JOIN \"ForumThread\" ON \"Post\".\"ThreadID\" = \"ForumThread\".\"ID\"
212-
JOIN \"" . ForumHolder::baseForumTable() . "\" AS \"ForumPage\" ON \"ForumThread\".\"ForumID\"=\"ForumPage\".\"ID\"
210+
JOIN \"" . ForumHolder::baseForumTable() . "\" AS \"ForumPage\" ON \"Post\".\"ForumID\"=\"ForumPage\".\"ID\"
213211
AND \"ForumPage\".\"ParentID\" = '" . $this->ID . "'")->value();
214212
}
215213

@@ -391,7 +389,7 @@ function getRecentPosts($limit = 50, $forumID = null, $threadID = null, $lastVis
391389
if($lastVisit) $filter[] = "\"Post\".\"Created\" > '". Convert::raw2sql($lastVisit) ."'";
392390

393391
// limit to a forum
394-
if($forumID) $filter[] = "\"ForumThread\".\"ForumID\" = '". Convert::raw2sql($forumID) ."'";
392+
if($forumID) $filter[] = "\"Post\".\"ForumID\" = '". Convert::raw2sql($forumID) ."'";
395393

396394
// limit to a thread
397395
if($threadID) $filter[] = "\"Post\".\"ThreadID\" = '". Convert::raw2sql($threadID) ."'";
@@ -401,9 +399,10 @@ function getRecentPosts($limit = 50, $forumID = null, $threadID = null, $lastVis
401399

402400
$posts = Post::get()
403401
->leftJoin('ForumThread', 'Post.ThreadID = ForumThread.ID')
404-
->leftJoin(ForumHolder::baseForumTable(), 'ForumPage.ID = ForumThread.ForumID', 'ForumPage')
402+
->leftJoin(ForumHolder::baseForumTable(), '"ForumPage"."ID" = "Post"."ForumID"', 'ForumPage')
405403
->limit($limit)
406404
->sort('Post.ID', 'DESC');
405+
407406
foreach ($filter as $value) {
408407
$posts->where($value);
409408
}
@@ -440,16 +439,15 @@ public static function new_posts_available($id, &$data = array(), $lastVisit = n
440439
$filter[] = "\"ForumPage\".\"ParentID\" = '". Convert::raw2sql($id) ."'";
441440
if($lastPostID) $filter[] = "\"Post\".\"ID\" > '". Convert::raw2sql($lastPostID) ."'";
442441
if($lastVisit) $filter[] = "\"Post\".\"Created\" > '". Convert::raw2sql($lastVisit) ."'";
443-
if($forumID) $filter[] = "\"ForumThread\".\"ForumID\" = '". Convert::raw2sql($forumID) ."'";
442+
if($forumID) $filter[] = "\"Post\".\"ForumID\" = '". Convert::raw2sql($forumID) ."'";
444443
if($threadID) $filter[] = "\"ThreadID\" = '". Convert::raw2sql($threadID) ."'";
445444

446445
$filter = implode(" AND ", $filter);
447446

448447
$version = DB::query("
449448
SELECT MAX(\"Post\".\"ID\") AS \"LastID\", MAX(\"Post\".\"Created\") AS \"LastCreated\"
450449
FROM \"Post\"
451-
JOIN \"ForumThread\" ON \"Post\".\"ThreadID\" = \"ForumThread\".\"ID\"
452-
JOIN \"" . ForumHolder::baseForumTable() . "\" AS \"ForumPage\" ON \"ForumThread\".\"ForumID\"=\"ForumPage\".\"ID\"
450+
JOIN \"" . ForumHolder::baseForumTable() . "\" AS \"ForumPage\" ON \"Post\".\"ForumID\"=\"ForumPage\".\"ID\"
453451
WHERE $filter" )->first();
454452

455453
if($version == false) return false;

forum/code/search/ForumDatabaseSearch.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
class ForumDatabaseSearch implements ForumSearchProvider {
10-
10+
1111
/**
1212
* Get the results from the database
1313
*
@@ -23,26 +23,26 @@ public function getResults($forumHolderID, $query, $order, $offset = 0, $limit =
2323
// we need to do escape for the $query string to avoid sql injection
2424
// since $query string has been used more than once below in searching out potential authors and posts in different ways,
2525
// we'd better make the escaping just before its defferent variations putting into a query.
26-
27-
26+
27+
2828
// Search for authors
2929
$SQL_queryParts = Convert::raw2sql(preg_split('/ +/', trim($query)));
30-
foreach($SQL_queryParts as $SQL_queryPart ) {
30+
foreach($SQL_queryParts as $SQL_queryPart ) {
3131
$SQL_clauses[] = "FirstName LIKE '%$SQL_queryPart%' OR Surname LIKE '%$SQL_queryPart' OR Nickname LIKE '%$SQL_queryPart'";
3232
}
3333

3434
$potentialAuthors = DataObject::get('Member', implode(" OR ", $SQL_clauses), '"ID" ASC');
3535
$SQL_authorClause = '';
3636
$SQL_potentialAuthorIDs = array();
37-
37+
3838
if($potentialAuthors) {
3939
foreach($potentialAuthors as $potentialAuthor) {
4040
$SQL_potentialAuthorIDs[] = $potentialAuthor->ID;
4141
}
4242
$SQL_authorList = implode(", ", $SQL_potentialAuthorIDs);
4343
$SQL_authorClause = "OR Post.AuthorID IN ($SQL_authorList)";
4444
}
45-
45+
4646
// Work out what sorting method
4747
switch($order) {
4848
case 'date':
@@ -64,31 +64,31 @@ public function getResults($forumHolderID, $query, $order, $offset = 0, $limit =
6464

6565
$baseSelect = "SELECT Post.ID, Post.Created, Post.LastEdited, Post.ClassName, ForumThread.Title, Post.Content, Post.ThreadID, Post.AuthorID, ForumThread.ForumID";
6666
$baseFrom = "FROM Post JOIN ForumThread ON Post.ThreadID = ForumThread.ID JOIN {ForumHolder::baseForumTable()} ForumPage ON ForumThread.ForumID=ForumPage.ID";
67-
67+
6868
$SQL_query = Convert::raw2sql(trim($query));
69-
// each database engine does its own thing
69+
// each database engine does its own thing
7070
switch(DB::getConn()->getDatabaseServer()) {
7171
case 'postgresql':
7272
$queryString = "
7373
$baseSelect
74-
$baseFrom
74+
$baseFrom
7575
, to_tsquery('english', '$SQL_query') AS q";
76-
76+
7777
$limitString = "LIMIT $limit OFFSET $offset;";
7878
break;
79-
79+
8080
case 'mssql':
8181
$queryString = "
8282
$baseSelect
8383
$baseFrom
8484
WHERE
8585
(CONTAINS(\"ForumThread\".\"Title\", '$SQL_query') OR CONTAINS(\"Post\".\"Content\", '$SQL_query')
8686
AND \"ForumPage\".\"ParentID\"='{$forumHolderID}'";
87-
87+
8888
// @todo fix this to use MSSQL's version of limit/offsetB
8989
$limitString = false;
9090
break;
91-
91+
9292
default:
9393
$queryString = "
9494
$baseSelect,
@@ -105,7 +105,7 @@ public function getResults($forumHolderID, $query, $order, $offset = 0, $limit =
105105

106106
// Find out how many posts that match with no limit
107107
$allPosts = DB::query($queryString);
108-
108+
109109
// Get the 10 posts from the starting record
110110
if($limitString) {
111111
$query = DB::query("
@@ -116,19 +116,19 @@ public function getResults($forumHolderID, $query, $order, $offset = 0, $limit =
116116
else {
117117
$query = $allPosts;
118118
}
119-
119+
120120
$allPostsCount = $allPosts ? $allPosts->numRecords() : 0;
121-
121+
122122
$baseClass = new Post();
123123
$postsSet = $baseClass->buildDataObjectSet($query);
124-
124+
125125
if($postsSet) {
126126
$postsSet->setPageLimits($offset, $limit, $allPostsCount);
127127
}
128-
128+
129129
return $postsSet ? $postsSet: new DataObjectSet();
130130
}
131-
131+
132132
/**
133133
* Callback when this Provider is loaded. For dealing with background processes
134134
*/

0 commit comments

Comments
 (0)