Revision [15694]
This is an old revision of HighlightSearch made by EmeraldIsland on 2006-11-25 20:50:13.
HighlightSearch Handler
This is the development page for the HighlightSearch handler.
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.
You just need to create the new file highlightsearch.php in your /handlers/page directory and copy this code :
Remark : if you prefer, you can replace 'show' handler.In that case, don't forget to also change 'highlightsearch' by 'show' in textsearch.php
<?php
/* HighlightSearch:
* tagname/highlightsearch&search=wanted_string
* Highlight the string passed in argument. Use it with textSearch action
*
* EMERALDISLAND november 2006
* source code based on show handler and textsearchexpanded action
*/
?>
<div class="page" <?php echo (($user = $this->GetUser()) && ($user['doubleclickedit'] == 'N')) ? '' : 'ondblclick="document.location=\''.$this->href('edit').'\';" ' ?>>
<?php
if (!$this->HasAccess('read'))
{
echo '<p><em class="error">You aren\'t allowed to read this page.</em></p></div>';
}
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></div>';
}
else
{
if ($this->page['latest'] == 'N')
{
echo '<div class="revisioninfo">This is an old revision of <a href="'.$this->Href().'">'.$this->GetPageTag().'</a> from '.$this->page['time'].'.</div>';
}
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 { echo $this->Format($this->page['body'], 'wakka'); }
// if this is an old revision, display some buttons
if ($this->page['latest'] == 'N' && $this->HasAccess('write'))
{
// added if encapsulation : in case where some pages were brutally deleted from database
if ($latest = $this->LoadPage($this->tag))
{
?>
<br />
<?php echo $this->FormOpen('edit') ?>
<input type="hidden" name="previous" value="<?php echo $latest['id'] ?>" />
<input type="hidden" name="body" value="<?php echo $this->htmlspecialchars_ent($this->page['body']) ?>" />
<input type="submit" value="Re-edit this old revision" />
<?php echo $this->FormClose(); ?>
<?php
}
}
echo '</div>'."\n";
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($_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"> </span>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();
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");
?>
<input type="hidden" name="comment_id" value="<?php echo $comment['id'] ?>" />
<input type="submit" value="Delete Comment" />
<?php
echo $this->FormClose();
}
echo "\n\t".'</div>'."\n";
echo '</div>'."\n";
}
}
// display comment form
echo '<div class="commentform">'."\n";
if ($this->HasAccess('comment'))
{?>
<?php echo $this->FormOpen('addcomment'); ?>
<label for="commentbox">Add a comment to this page:<br />
<textarea class="commentbox" id="commentbox" name="body"></textarea><br />
<input type="submit" value="Add Comment" accesskey="s" />
</label>
<?php echo $this->FormClose(); ?>
<?php
}
echo '</div>'."\n";
}
else
{
?>
<div class="commentsheader">
<?php
switch (count($comments))
{
case 0:
echo '<div>There are no comments on this page. ';
$showcomments_text = 'Add comment';
break;
case 1:
echo '<div>There is one comment on this page. ';
$showcomments_text = 'Display comment';
break;
default:
echo '<div>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>]</div>
</div>
<?php
}
}
}
}
?>
/* HighlightSearch:
* tagname/highlightsearch&search=wanted_string
* Highlight the string passed in argument. Use it with textSearch action
*
* EMERALDISLAND november 2006
* source code based on show handler and textsearchexpanded action
*/
?>
<div class="page" <?php echo (($user = $this->GetUser()) && ($user['doubleclickedit'] == 'N')) ? '' : 'ondblclick="document.location=\''.$this->href('edit').'\';" ' ?>>
<?php
if (!$this->HasAccess('read'))
{
echo '<p><em class="error">You aren\'t allowed to read this page.</em></p></div>';
}
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></div>';
}
else
{
if ($this->page['latest'] == 'N')
{
echo '<div class="revisioninfo">This is an old revision of <a href="'.$this->Href().'">'.$this->GetPageTag().'</a> from '.$this->page['time'].'.</div>';
}
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 { echo $this->Format($this->page['body'], 'wakka'); }
// if this is an old revision, display some buttons
if ($this->page['latest'] == 'N' && $this->HasAccess('write'))
{
// added if encapsulation : in case where some pages were brutally deleted from database
if ($latest = $this->LoadPage($this->tag))
{
?>
<br />
<?php echo $this->FormOpen('edit') ?>
<input type="hidden" name="previous" value="<?php echo $latest['id'] ?>" />
<input type="hidden" name="body" value="<?php echo $this->htmlspecialchars_ent($this->page['body']) ?>" />
<input type="submit" value="Re-edit this old revision" />
<?php echo $this->FormClose(); ?>
<?php
}
}
echo '</div>'."\n";
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($_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"> </span>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();
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");
?>
<input type="hidden" name="comment_id" value="<?php echo $comment['id'] ?>" />
<input type="submit" value="Delete Comment" />
<?php
echo $this->FormClose();
}
echo "\n\t".'</div>'."\n";
echo '</div>'."\n";
}
}
// display comment form
echo '<div class="commentform">'."\n";
if ($this->HasAccess('comment'))
{?>
<?php echo $this->FormOpen('addcomment'); ?>
<label for="commentbox">Add a comment to this page:<br />
<textarea class="commentbox" id="commentbox" name="body"></textarea><br />
<input type="submit" value="Add Comment" accesskey="s" />
</label>
<?php echo $this->FormClose(); ?>
<?php
}
echo '</div>'."\n";
}
else
{
?>
<div class="commentsheader">
<?php
switch (count($comments))
{
case 0:
echo '<div>There are no comments on this page. ';
$showcomments_text = 'Add comment';
break;
case 1:
echo '<div>There is one comment on this page. ';
$showcomments_text = 'Display comment';
break;
default:
echo '<div>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>]</div>
</div>
<?php
}
}
}
}
?>
For a better integration in your wiki, you will need to change only one line in textsearch.php action :
Old textsearch.php source code (line 50) :
New textsearch.php source code (line 50) :
$result_page_list .= ($i+1).". ".$this->Link($page["tag"],'highlightsearch&search='.$phrase)."<br />\n";
That's all !
CategoryUserContributions - CategoryDevelopmentHandlers