| 1 | # PurpleWiki::Search::Wiki.pm | ||
| 2 | # vi:ai:sm:et:sw=4:ts=4 | ||
| 3 | # | ||
| 4 | # $Id: Wiki.pm 438 2004-08-01 03:22:53Z eekim $ | ||
| 5 | # | ||
| 6 | # Copyright (c) Blue Oxen Associates 2002-2004. All rights reserved. | ||
| 7 | # | ||
| 8 | # This file is part of PurpleWiki. PurpleWiki is derived from: | ||
| 9 | # | ||
| 10 | # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001 | ||
| 11 | # AtisWiki v0.3 (c) Markus Denker 1998 | ||
| 12 | # CVWiki CVS-patches (c) Peter Merel 1997 | ||
| 13 | # The Original WikiWikiWeb (c) Ward Cunningham | ||
| 14 | # | ||
| 15 | # PurpleWiki is free software; you can redistribute it and/or modify | ||
| 16 | # it under the terms of the GNU General Public License as published by | ||
| 17 | # the Free Software Foundation; either version 2 of the License, or | ||
| 18 | # (at your option) any later version. | ||
| 19 | # | ||
| 20 | # This program is distributed in the hope that it will be useful, | ||
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 23 | # GNU General Public License for more details. | ||
| 24 | # | ||
| 25 | # You should have received a copy of the GNU General Public License | ||
| 26 | # along with this program; if not, write to the | ||
| 27 | # Free Software Foundation, Inc. | ||
| 28 | # 59 Temple Place, Suite 330 | ||
| 29 | # Boston, MA 02111-1307 USA | ||
| 30 | |||
| 31 | package PurpleWiki::Search::Wiki; | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use base 'PurpleWiki::Search::Interface'; | ||
| 35 | use PurpleWiki::Search::Result; | ||
| 36 | use PurpleWiki::Database; | ||
| 37 | use PurpleWiki::Page; | ||
| 38 | |||
| 39 | our $VERSION; | ||
| 40 | $VERSION = sprintf("%d", q$Id: Wiki.pm 438 2004-08-01 03:22:53Z eekim $ =~ /\s(\d+)\s/); | ||
| 41 | |||
| 42 | # Where the searching is done. | ||
| 43 | sub search { | ||
| 44 | my $self = shift; | ||
| 45 | my $query = shift; | ||
| 46 | my @results; | ||
| 47 | |||
| 48 | my $nameHash; | ||
| 49 | |||
| 50 | foreach $nameHash (PurpleWiki::Database::AllPagesList()) { | ||
| 51 | my $name = $nameHash->{pageName}; | ||
| 52 | if ($name =~ /$query/i) { | ||
| 53 | my $page = $self->_getAndOpenPage($name); | ||
| 54 | push(@results, $self->_getResult($page)); | ||
| 55 | } else { | ||
| 56 | my $page = $self->_getAndOpenPage($name); | ||
| 57 | my $text = $page->getText(); | ||
| 58 | if ($text->getText() =~ /$query/i) { | ||
| 59 | push(@results, $self->_getResult($page, $text)); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | @results = sort {$b->modifiedTime() <=> $a->modifiedTime()} | ||
| 65 | @results; | ||
| 66 | |||
| 67 | return @results; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _getAndOpenPage { | ||
| 71 | my $self = shift; | ||
| 72 | my $name = shift; | ||
| 73 | |||
| 74 | my $page = new PurpleWiki::Database::Page(id => $name, now => time); | ||
| 75 | $page->openPage(); | ||
| 76 | |||
| 77 | return $page; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _getResult { | ||
| 81 | my $self = shift; | ||
| 82 | my $page = shift; | ||
| 83 | my $text = shift || $page->getText(); | ||
| 84 | my $name = $page->getID(); | ||
| 85 | |||
| 86 | my $result = new PurpleWiki::Search::Result(); | ||
| 87 | $result->title($name); | ||
| 88 | $result->modifiedTime($page->getTS()); | ||
| 89 | $result->url(PurpleWiki::Page::getWikiWordLink($name)); | ||
| 90 | $result->summary(substr($text->getText(), 0, 99) . '...'); | ||
| 91 | |||
| 92 | return $result; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | __END__ |