Revision [21598]

This is an old revision of HighlightSearchWikka12 made by EmeraldIsland on 2011-10-09 10:21:44.

 

HighlightSearch Handler

See also :
Documentation : HighlightSearchHandlerInfo
Other: SearchHighlighter, HighlightSearch, HighlightSearchWikka12
Works with :
Wikka 1.2
NOT included in any Wikka version

This is the development page for the HighlightSearchWikka12 HighlightSearch handler version Wikka 1.2.
For Wikka version 1.3.1 go HighlightSearchWikka131 here.
For Wikka version 1.3.2 go HighlightSearchWikka132 here.
Previous version for Wikka version < 1.2 is also HighlightSearch available.
 

RolandStens did SearchHighlighter based on Javascript. Here is my contribution in PHP. The work is based on show.php handler and a small part of textsearchexpanded.php action. You can see this new handler in action here.

Installation
- Save the code block below as /plugins/handlers/page/highlightsearch.php
- Give it the same file permissions as the other php files in that directory

Code
1. New /plugins/handlers/page/highlightsearch.php file

<?php
/**
 * Display a page if the user has read access or is an admin.
 *
 * This is the default page handler used by Wikka when no other handler is specified.
 * Depending on user privileges, it displays the page body or an error message. It also
 * displays footer comments and a form to post comments, depending on ACL and general
 * config settings.
 *
 * @package     Handlers
 * @subpackage  Page
 * @version     $Id$
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @filesource
 *
 * @uses        Wakka::Format()
 * @uses        Wakka::FormClose()
 * @uses        Wakka::FormOpen()
 * @uses        Wakka::GetConfigValue()
 * @uses        Wakka::GetPageTag()
 * @uses        Wakka::GetUser()
 * @uses        Wakka::GetUserName()
 * @uses        Wakka::HasAccess()
 * @uses        Wakka::Href()
 * @uses        Wakka::htmlspecialchars_ent()
 * @uses        Wakka::LoadComments()
 * @uses        Wakka::LoadPage()
 * @uses        Wakka::LoadUser()
 * @uses        Wakka::UserIsOwner()
 * @uses        Config::$anony_delete_own_comments
 * @uses        Config::$hide_comments
 *
 * @todo        move <div> to template;
 *          necessary) - #312  
 */


//constants
define('SHOW_OLD_REVISION_SOURCE', 0); # if set to 1 shows by default the source of an old revision instead of the rendered version

//validate URL parameters
$raw = (!empty($_GET['raw']))? (int) $this->GetSafeVar('raw', 'get') : SHOW_OLD_REVISION_SOURCE;

?>
<!-- BEGIN PAGE CONTENT -->
<?php
echo '<div id="content"';
echo (($user = $this->GetUser()) && ($user['doubleclickedit'] == 'N') || !$this->HasAccess('write')) ? '' : ' ondblclick="document.location=\''.$this->Href('edit', '', 'id='.$this->page['id']).'\';" '; #268
echo '>'."\n"; //TODO: move to templating class

if (!$this->HasAccess('read'))
{
    echo '<p><em class="error">You aren\'t allowed to read this page.</em></p>';
    echo "\n".'</div><!--closing page content-->'."\n"; //TODO: move to templating class
}
else
{
    if (!$this->page)
    {
        echo '<p>This page doesn\'t exist yet. Maybe you want to <a href="'.$this->Href('edit').'">create</a> it?</p>';
        echo '</div><!--closing page content-->'."\n"; //TODO: move to templating class
    }
    else
    {
        if ($this->page['latest'] == 'N')
        {
            echo '<div class="revisioninfo">'."\n";
            echo '<h4>Revision ['.$this->page['id'].']</h4>'."\n";
            echo 'This is an <a href="'.$this->Href('revisions').'">old revision</a> of <a href="'.$this->Href().'">'.$this->GetPageTag().'</a> made by '.$this->page['user'].' on <span class="datetime">'.$this->page['time'].'</span>.'."\n";
?>
                <br />
                <?php echo $this->FormOpen('show', '', 'GET', '', 'left') ?>
                <fieldset>
                <input type="hidden" name="time" value="<?php echo $this->GetSafeVar('time', 'get') ?>" />
                <input type="hidden" name="raw" value="<?php echo ($raw == 1)? '0' :'1' ?>" />
                <button type="submit" value="<?php echo ($raw == 1)? 'Show formatted' : 'Show source' ?>"><?php echo ($raw == 1)? 'Show formatted' : 'Show source' ?></button>&nbsp;
                </fieldset>
                <?php echo $this->FormClose(); ?>
<?php
            // if this is an old revision, display some buttons
            if ($this->HasAccess('write'))
            {
                // added if encapsulation : in case where some pages were brutally deleted from database
                if ($latest = $this->LoadPage($this->tag))
                {
?>
                    <?php echo $this->FormOpen('edit') ?>
                    <fieldset>
                    <input type="hidden" name="previous" value="<?php echo $latest['id'] ?>" />
                    <input type="hidden" name="body" value="<?php echo $this->htmlspecialchars_ent($this->page['body']) ?>" />
                    <button type="submit" value="Re-edit this old revision">Re-edit this old revision</button>
                    </fieldset>
                    <?php echo $this->FormClose(); ?>
<?php
                }
            }
            echo '<div class="clear"></div></div>'."\n";
        }
        // display page
        //EMERALDISLAND
        if (isset($_REQUEST['search']) && ($search = $_REQUEST['search'])) {
            $search_re = stripslashes(trim($search));
            if ($search_re) {
                // display page with highlighted string
                $txt_stripped = $this->Format($this->page['body'], 'wakka');
                // The evil REGEXP
                $pattern = '/('.$this->htmlspecialchars_ent($search_re).')(?![^<]*?>)/i';
                $highlightMatch = preg_replace($pattern,'<<$1>>',$txt_stripped,-1);
                $matchText = str_replace(array('<<', '>>'), array('<span class="tse_keywords">', '</span>'), $highlightMatch);
                echo $matchText;
            }
        } else if ($raw == 1)
        {
            echo '<div class="wikisource">'.nl2br($this->htmlspecialchars_ent($this->page["body"], ENT_QUOTES)).'</div>';
            }
        else
        {
            echo $this->Format($this->page['body'], 'wakka', 'page');
        }
        // EMERALDISLAND
?>
<div style="clear: both"></div>
</div><!-- END PAGE CONTENT -->
<?php
        if ($this->GetConfigValue('hide_comments') != 1)
        {
            // load comments for this page
            $comments = $this->LoadComments($this->tag);

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

2. In textsearch.php action, change only one line :
Old textsearch.php source code (line 85) :
$result_page_list .= ($i+1).". ".$this->Link($page["tag"],"<br />\n";

New textsearch.php source code (line 85) :
$result_page_list .= ($i+1).". ".$this->Link($page["tag"],'highlightsearch&search='.$phrase)."<br />\n";

3. That's all !

Remark :
For a better integration in your wiki, you can also replace 'show' handler.
- rename or save handlers/page/show.php in handlers/page/show_ref.php
- copy and rename plugins/handlers/page/highlightsearch.php in handlers/page/show.php
- change 'highlightsearch' by 'show' in actions/textsearch.php (step 2. above)

CategoryUserContributions - CategoryDevelopmentHandlers
There is one comment on this page. [Display comment]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki