Paged Comments

Working for 1.1.6.0 to 1.1.6.4 (latest)
Slight changes in the code in the vicinity of this modification, nothing serious.
Last edited by MasinAlDujaili:
working versions
Wed, 04 Jun 2008 04:21 UTC [diff]


When the number of comments at the bottom of a page is too large (there is virtually no limit to their number), the comments block becomes user-unfriendly:

I've written a configurable comments pager to address this issue. The pager allows to switch between a "paged" and a "showall" view. The default number of comments per page is set by a $limit_comments session parameter, that can be changed in UserSettings.
Paged comments come in two "flavours".


Screenshots:

Flavour A) - previous/next links
http://wikka.jsnx.com/images/pagedcomments.jpg

Flavour B) - pagelist
http://wikka.jsnx.com/images/pagedcomments2.jpg



Here's the code.

1. Create function LoadCommentPage() in wikka.php

        function LoadCommentPage($tag, $start, $limit) {
            return $this->LoadAll("SELECT * FROM ".$this->config["table_prefix"]."comments WHERE page_tag = '".mysql_real_escape_string($tag)."' ORDER BY time LIMIT ".$start.",".$limit);
        }


2. Modify the comments block in handlers/page/show.php

        if ($this->GetConfigValue("hide_comments") != 1)
        {
            // set default number of comments per page
            $d = ($_SESSION["limit_comments"])? $_SESSION["limit_comments"] : "10";

            // set default visualization to "paged"
            $showall = ($_REQUEST["showall"])? $_REQUEST["showall"] : "0";

            // get total number of comments for current page
            $n = count($this->LoadComments($this->tag));
           
            // calculate starting item of last page
            $lastcomments = ceil($n/$d)*$d;
            $start = ($n > $d)? ($lastcomments-$d) : "0";

            // set starting item and limit
            $s = (isset($_REQUEST["s"]))? $_REQUEST["s"] : $start;
            $l = (isset($_REQUEST["l"]))? $_REQUEST["l"] : $d;         
            // set no limits for showall visualization
            if ($showall == 1) {
                $s = "0";
                $l = "999";
            }

            // Build pager links:

/*
            // FLAVOUR A) - next/previous links

            // show "previous" link if needed
            if($s > 0) {
                $prev = " <a href=\"" .$this->Href("","","l=".$l."&s=".($s-$l))."#comments\">&laquo;</a>  ";
            }

            // show "next" link if needed
            if($n > ($s + $l)) {
                $next = " <a href=\"".$this->Href("","","l=".$l."&s=".($s+$l))."#comments\">&raquo;</a> ";
            }

            // create pager links
            $pager = ($prev || $next)? $prev.($s+1)."-".($s+$l).$next : "";        

*/


            // FLAVOUR B) - pagelist

            if ($showall == 0 && $n > $l) {
                $i = 0;
                while ($i < $n) {
                    if ($i == $s) {
                        $pager .= ($i+1)."-".($i+$l)." | \n";
                    } else {
                        $pager .= "<a href=\"".$this->Href("","","l=".$l."&s=".($i))."#comments\">".($i+1)."-".($i + $l)."</a> | \n";
                    }
                $i = $i + $l;
                }
            }


            // create switcher link
            switch($showall) {

                case "0":
//              FLAVOUR A)
//              $switcher = ($n > $l)? " [<a href=\"".$this->Href("","","showall=1")."#comments\">Show all</a>] " : "";
//              FLAVOUR B)
                $switcher = ($n > $l)? " <a href=\"".$this->Href("","","showall=1")."#comments\">all</a> " : "";
                break;

                case "1":
//              FLAVOUR A)
//              $switcher = ($n > $d)? " [<a href=\"".$this->Href("","","showall=0")."#comments\">Show paged</a>] " : "";
//              FLAVOUR B)
                $switcher = ($n > $d)? " <a href=\"".$this->Href("","","showall=0")."#comments\">show paged</a> " : "";
            }


            // load comments for this page with limits
            $comments = $this->LoadCommentPage($this->tag, $s, $l);

            // store comments display in session
            $tag = $this->GetPageTag();
            if (!isset($_SESSION["show_comments"][$tag]))
                $_SESSION["show_comments"][$tag] = ($this->UserWantsComments() ? "1" : "0");
            if (isset($_REQUEST["show_comments"])){
                switch($_REQUEST["show_comments"])
                {
                case "0":
                    $_SESSION["show_comments"][$tag] = 0;
                    break;
                case "1":
                    $_SESSION["show_comments"][$tag] = 1;
                    break;
                }
            }
            // display comments!
            if ($_SESSION["show_comments"][$tag])
            {
                // display comments header
                ?>
                <div class="commentsheader">
                <span id="comments">&nbsp;</span>Comments <?php echo "(".$n.") ".$pager.$switcher; ?>[<a href="<?php echo $this->href("", "", "show_comments=0") ?>">Hide comments/form</a>]
                </div>
                <?php
                // display comments themselves
                if ($comments)
                {
                    $current_user = $this->GetUserName();
                    foreach ($comments as $comment)
                    {
                        print("<div class=\"comment\">\n");
                        print("<span id=\"comment_".$comment["id"]."\"></span>".$comment["comment"]."\n");
                        print("\t<div class=\"commentinfo\">\n-- ".$this->Format($comment["user"])." (".$comment["time"].")\n");
                        $current_user = $this->GetUserName();
                            if ($this->UserIsOwner() || $current_user == $comment["user"] || ($this->config['anony_delete_own_comments'] && $current_user == $comment["user"]) )
                        {
                        ?>
                        <?php echo $this->FormOpen("delcomment"); ?>
                           <input type="hidden" name="comment_id" value="<?php echo $comment["id"] ?>" />
                           <input type="submit" value="Delete Comment" accesskey="d" />
                        <?php echo $this->FormClose(); ?>
                        <?php
                        }
                        print("\n\t</div>\n");
                        print("</div>\n");
                    }
                    // Show bottom pager link
                    if ($showall == 0 && $n > $l) {
                        print "<div class=\"commentsheader\">".$pager.$switcher."</div>\n";
                    }
                }
                // display comment form
                print("<div class=\"commentform\">\n");
                if ($this->HasAccess("comment"))
                {?>
                        <?php echo $this->FormOpen("addcomment"); ?>
                    <label for="commentbox">Add a comment to this page:<br />
                    <textarea id="commentbox" name="body" rows="6" cols="78"></textarea><br />
                    <input type="submit" value="Add Comment" accesskey="s" />
                        </label>
                    <?php echo $this->FormClose(); ?>
                <?php
                }
                print("</div>\n");
            }
            else
            {
            ?>
                <div class="commentsheader">
                <?php
                switch ($n)
                {
                case 0:
                    print("<p>There are no comments on this page. ");
                    $showcomments_text = "Add comment";
                    break;
                case 1:
                    print("<p>There is one comment on this page. ");
                    $showcomments_text = "Display comment";
                    break;
                default:
                    print("<p>There are ".$n." comments on this page. ");
                    $showcomments_text = "Display comments";
                }
                ?>
                [<a href="<?php echo $this->href("", "", "show_comments=1#comments")."\">$showcomments_text"; ?></a>]</p>
                </div>
                <?php
            }
        }


3. Update actions/recentlycommented.php

Replace the two occurrences of

"show_comments=1"


with

"show_comments=1&showall=1"



4. Update actions/usersettings.php

These modifications allow users to store their preferred number of comments per page in a session parameter.
Modify the two following code blocks:

A)
original
    $this->SetUser($this->LoadUser($user["name"]));
   
    // forward
    $this->SetMessage("User settings stored!");
    $this->Redirect($this->href());


modified
    $this->SetUser($this->LoadUser($user["name"]));
    $_SESSION["limit_comments"] = ($_REQUEST["limit_comments"])? $_REQUEST["limit_comments"] : "10";
   
    // forward
    $this->SetMessage("User settings stored!");
    $this->Redirect($this->href());


B)

original
    <tr>
        <td align="right">Page revisions list limit:</td>
        <td><input name="revisioncount" value="<?php echo htmlspecialchars($user["revisioncount"]) ?>" size="40" /></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Update Settings" /> <input type="button" value="Logout" onClick="document.location='<?php echo $this->href("", "", "action=logout"); ?>'" /></td>
    </tr>


modified
    <tr>
        <td align="right">Page revisions list limit:</td>
        <td><input name="revisioncount" value="<?php echo htmlspecialchars($user["revisioncount"]) ?>" size="40" /></td>
    </tr>
    <tr>
        <td align="right">Comments per page:</td>
        <?php
            $limit_comments = ($_SESSION["limit_comments"])? $_SESSION["limit_comments"] : "10";
        ?>
        <td><input name="limit_comments" value="<?php echo $limit_comments ?>" size="40" /></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Update Settings" /> <input type="button" value="Logout" onClick="document.location='<?php echo $this->href("", "", "action=logout"); ?>'" /></td>
    </tr>



CategoryDevelopmentHandlers
Comments
Comment by ChristianBarthelemy
2004-12-15 19:06:35
Very useful. Anyway, I think that this should be stored in the wikka_users table as well. Any reason not to do it? Is it going to be part of 1.1.6.0?
Comment by JavaWoman
2005-01-04 23:32:05
@DarTar,
Whatever I do to set my max page amount high, eventually it still reverts to 10 again - quite annoying but I haven't figured out what causes it. It doesn't revert "immediately" - just "after a while", and while still being logged in. SOME action probably does it - but I don't know what.
@Christian,
No, it won't be in 1.1.6.0 - it's still very much in beta...
Comment by DarTar
2005-01-04 23:41:38
JW, as we discussed on #wikka, there is much work to do to improve this hack. Still, I can hardly understand where your slowly-revert-to-10-comments bug might come from (looks like a problem with the session). I'll revise and patch everything ASAP. Sorry for uploading something so beta ;-)
Comment by GmBowen
2005-01-04 23:41:55
DarTar....I just noticed that if you're showing "all" and you delete a comment then it reverts to paged again. I'm not sure this is the intended functionality.
Comment by JavaWoman
2005-01-04 23:53:42
DarTar,
It's a beta so we can bang on it and break it. ;-) Good way to do that is to put it up here so it gets some use. I'll try to keep an eye on when the revert-to-10 happens - it's not obvious to me from the code. For now, it's back to 100 again. (I hope.)
Comment by JavaWoman
2005-01-06 10:49:49
OK, once observation:
This morning I found a BSOD on my computer so I had to reboot. Of course that also killed my session cookie and the setting had reverted to 10. I changed it back to 100. That's to be expecetd as long as it's not stored in the user settings.
But just now, I added a new comment (#33) to BannerMaker - and when it appeared it was suddenly paged with 10 to a page again...

Doesn't happen with every comment I add - maybe only when the # of comments is already > 10? (This will be #6 on this page)
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki