| 1 | # PurpleWiki::Search::MovableType.pm | ||
| 2 | # vi:ai:sm:et:sw=4:ts=4 | ||
| 3 | # | ||
| 4 | # $Id: MovableType.pm 364 2004-05-19 18:15:26Z 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::MovableType; | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use base 'PurpleWiki::Search::Interface'; | ||
| 35 | use Time::Local; | ||
| 36 | |||
| 37 | our $VERSION; | ||
| 38 | $VERSION = sprintf("%d", q$Id: MovableType.pm 364 2004-05-19 18:15:26Z eekim $ =~ /\s(\d+)\s/); | ||
| 39 | |||
| 40 | # Where the searching is done. | ||
| 41 | # Most of this taken from MT::App::Search | ||
| 42 | sub search { | ||
| 43 | my $self = shift; | ||
| 44 | my $query = shift; | ||
| 45 | my @results; | ||
| 46 | my %includedBlogs; | ||
| 47 | |||
| 48 | # initialize movable type library stuff | ||
| 49 | $self->_initMT(); | ||
| 50 | |||
| 51 | # make our hash of blog ids we care about | ||
| 52 | foreach my $id (@{$self->config()->MovableTypeBlogId()}) { | ||
| 53 | $includedBlogs{$id}++; | ||
| 54 | } | ||
| 55 | |||
| 56 | my %terms = (status => MT::Entry::RELEASE()); | ||
| 57 | |||
| 58 | my %args = ('sort' => 'modified_on', direction => 'descend'); | ||
| 59 | my $iter = MT::Entry->load_iter(\%terms, \%args); | ||
| 60 | |||
| 61 | while (my $entry = $iter->()) { | ||
| 62 | my $blog_id = $entry->blog_id; | ||
| 63 | next unless ($includedBlogs{$blog_id}); | ||
| 64 | if ($self->_search_hit($query, $entry)) { | ||
| 65 | my $result = new PurpleWiki::Search::Result(); | ||
| 66 | $result->title($entry->title); | ||
| 67 | $result->url($entry->permalink); | ||
| 68 | $result->modifiedTime($self->_calculateModifiedTime($entry)); | ||
| 69 | $result->summary(substr($entry->text, 0, 99) . '...'); | ||
| 70 | push(@results, $result); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | return @results; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _calculateModifiedTime { | ||
| 78 | my $self = shift; | ||
| 79 | my $entry = shift; | ||
| 80 | |||
| 81 | # In YYYYMMDDHHMMSS format | ||
| 82 | my $timestamp = $entry->modified_on(); | ||
| 83 | my ($year, $month, $day, $hour, $min, $sec) = | ||
| 84 | ($timestamp =~ (/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/)); | ||
| 85 | return timelocal($sec, $min, $hour, $day, $month - 1, $year); | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | sub _search_hit { | ||
| 91 | my $self = shift; | ||
| 92 | my $query = shift; | ||
| 93 | my $entry = shift; | ||
| 94 | |||
| 95 | my @text_elements; | ||
| 96 | |||
| 97 | @text_elements = ($entry->title, $entry->text, $entry->text_more, | ||
| 98 | $entry->keywords); | ||
| 99 | |||
| 100 | # get the comment text too | ||
| 101 | my $comments = $entry->comments; | ||
| 102 | foreach my $comment (@$comments) { | ||
| 103 | push(@text_elements, $comment->text, $comment->author, $comment->url); | ||
| 104 | } | ||
| 105 | |||
| 106 | my $txt = join("\n", map $_ || '', @text_elements); | ||
| 107 | return $txt =~ m/$query/i; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _initMT() { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | unshift @INC, $self->config()->MovableTypeDirectory() . 'lib'; | ||
| 114 | unshift @INC, $self->config()->MovableTypeDirectory() . 'extlib'; | ||
| 115 | |||
| 116 | require MT::Object; | ||
| 117 | require MT::ConfigMgr; | ||
| 118 | require MT::Blog; | ||
| 119 | require MT::Entry; | ||
| 120 | |||
| 121 | # FIXME: this is an ugly uninformed way of doing things | ||
| 122 | my $cfg = MT::ConfigMgr->instance; | ||
| 123 | $cfg->read_config($self->config()->MovableTypeDirectory() . 'mt.cfg') or | ||
| 124 | die $cfg->errstr; | ||
| 125 | |||
| 126 | MT::Object->set_driver($cfg->ObjectDriver); | ||
| 127 | |||
| 128 | return $self; | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; | ||
| 132 | __END__ |