2 * Sone - SearchPage.java - Copyright © 2010–2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.HashSet;
25 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.sone.data.Post;
31 import net.pterodactylus.sone.data.PostReply;
32 import net.pterodactylus.sone.data.Profile;
33 import net.pterodactylus.sone.data.Profile.Field;
34 import net.pterodactylus.sone.data.Reply;
35 import net.pterodactylus.sone.data.Sone;
36 import net.pterodactylus.sone.web.page.FreenetRequest;
37 import net.pterodactylus.util.cache.Cache;
38 import net.pterodactylus.util.cache.CacheException;
39 import net.pterodactylus.util.cache.CacheItem;
40 import net.pterodactylus.util.cache.DefaultCacheItem;
41 import net.pterodactylus.util.cache.MemoryCache;
42 import net.pterodactylus.util.cache.ValueRetriever;
43 import net.pterodactylus.util.collection.Pagination;
44 import net.pterodactylus.util.collection.TimedMap;
45 import net.pterodactylus.util.collection.mapper.Mapper;
46 import net.pterodactylus.util.collection.mapper.Mappers;
47 import net.pterodactylus.util.logging.Logging;
48 import net.pterodactylus.util.number.Numbers;
49 import net.pterodactylus.util.template.Template;
50 import net.pterodactylus.util.template.TemplateContext;
51 import net.pterodactylus.util.text.StringEscaper;
52 import net.pterodactylus.util.text.TextException;
54 import com.google.common.base.Predicate;
55 import com.google.common.collect.Collections2;
58 * This page lets the user search for posts and replies that contain certain
61 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
63 public class SearchPage extends SoneTemplatePage {
66 private static final Logger logger = Logging.getLogger(SearchPage.class);
68 /** Short-term cache. */
69 private final Cache<List<Phrase>, Set<Hit<Post>>> hitCache = new MemoryCache<List<Phrase>, Set<Hit<Post>>>(new ValueRetriever<List<Phrase>, Set<Hit<Post>>>() {
72 @SuppressWarnings("synthetic-access")
73 public CacheItem<Set<Hit<Post>>> retrieve(List<Phrase> phrases) throws CacheException {
74 Set<Post> posts = new HashSet<Post>();
75 for (Sone sone : webInterface.getCore().getSones()) {
76 posts.addAll(sone.getPosts());
78 return new DefaultCacheItem<Set<Hit<Post>>>(getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()));
81 }, new TimedMap<List<Phrase>, CacheItem<Set<Hit<Post>>>>(300000));
84 * Creates a new search page.
87 * The template to render
89 * The Sone web interface
91 public SearchPage(Template template, WebInterface webInterface) {
92 super("search.html", template, "Page.Search.Title", webInterface);
96 // SONETEMPLATEPAGE METHODS
103 protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
104 super.processTemplate(request, templateContext);
105 String query = request.getHttpRequest().getParam("query").trim();
106 if (query.length() == 0) {
107 throw new RedirectException("index.html");
110 List<Phrase> phrases = parseSearchPhrases(query);
111 if (phrases.isEmpty()) {
112 throw new RedirectException("index.html");
115 /* check for a couple of shortcuts. */
116 if (phrases.size() == 1) {
117 String phrase = phrases.get(0).getPhrase();
119 /* is it a Sone ID? */
120 redirectIfNotNull(getSoneId(phrase), "viewSone.html?sone=");
122 /* is it a post ID? */
123 redirectIfNotNull(getPostId(phrase), "viewPost.html?post=");
125 /* is it a reply ID? show the post. */
126 redirectIfNotNull(getReplyPostId(phrase), "viewPost.html?post=");
128 /* is it an album ID? */
129 redirectIfNotNull(getAlbumId(phrase), "imageBrowser.html?album=");
131 /* is it an image ID? */
132 redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
135 Set<Sone> sones = webInterface.getCore().getSones();
136 Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
138 Collection<Hit<Post>> postHits;
140 postHits = hitCache.get(phrases);
141 } catch (CacheException ce1) {
142 /* should never happen. */
143 logger.log(Level.SEVERE, "Could not get search results from cache!", ce1);
144 postHits = Collections.emptySet();
148 soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER);
149 postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
152 List<Hit<Sone>> sortedSoneHits = new ArrayList<Hit<Sone>>(soneHits);
153 Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR);
154 List<Hit<Post>> sortedPostHits = new ArrayList<Hit<Post>>(postHits);
155 Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
157 /* extract Sones and posts. */
158 List<Sone> resultSones = Mappers.mappedList(sortedSoneHits, new HitMapper<Sone>());
159 List<Post> resultPosts = Mappers.mappedList(sortedPostHits, new HitMapper<Post>());
162 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
163 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
165 templateContext.set("sonePagination", sonePagination);
166 templateContext.set("soneHits", sonePagination.getItems());
167 templateContext.set("postPagination", postPagination);
168 templateContext.set("postHits", postPagination.getItems());
176 * Collects hit information for the given objects. The objects are converted
177 * to a {@link String} using the given {@link StringGenerator}, and the
178 * {@link #calculateScore(List, String) calculated score} is stored together
179 * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
183 * The type of the objects
185 * The objects to search over
187 * The phrases to search for
188 * @param stringGenerator
189 * The string generator for the objects
190 * @return The hits for the given phrases
192 private static <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
193 Set<Hit<T>> hits = new HashSet<Hit<T>>();
194 for (T object : objects) {
195 String objectString = stringGenerator.generateString(object);
196 double score = calculateScore(phrases, objectString);
197 hits.add(new Hit<T>(object, score));
203 * Parses the given query into search phrases. The query is split on
204 * whitespace while allowing to group words using single or double quotes.
205 * Isolated phrases starting with a “+” are
206 * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
207 * {@link Phrase.Optionality#FORBIDDEN}.
211 * @return The parsed phrases
213 private static List<Phrase> parseSearchPhrases(String query) {
214 List<String> parsedPhrases = null;
216 parsedPhrases = StringEscaper.parseLine(query);
217 } catch (TextException te1) {
219 return Collections.emptyList();
222 List<Phrase> phrases = new ArrayList<Phrase>();
223 for (String phrase : parsedPhrases) {
224 if (phrase.startsWith("+")) {
225 if (phrase.length() > 1) {
226 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
228 phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
230 } else if (phrase.startsWith("-")) {
231 if (phrase.length() > 1) {
232 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
234 phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
237 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
244 * Calculates the score for the given expression when using the given
248 * The phrases to search for
250 * The expression to search
251 * @return The score of the expression
253 private static double calculateScore(List<Phrase> phrases, String expression) {
254 logger.log(Level.FINEST, String.format("Calculating Score for “%s”…", expression));
255 double optionalHits = 0;
256 double requiredHits = 0;
257 int forbiddenHits = 0;
258 int requiredPhrases = 0;
259 for (Phrase phrase : phrases) {
260 String phraseString = phrase.getPhrase().toLowerCase();
261 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
267 while (index < expression.length()) {
268 int position = expression.toLowerCase().indexOf(phraseString, index);
269 if (position == -1) {
272 score += Math.pow(1 - position / (double) expression.length(), 2);
273 index = position + phraseString.length();
274 logger.log(Level.FINEST, String.format("Got hit at position %d.", position));
277 logger.log(Level.FINEST, String.format("Score: %f", score));
281 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
282 requiredHits += score;
284 if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
285 optionalHits += score;
287 if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
288 forbiddenHits += matches;
291 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
296 * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
297 * if the given object is not {@code null}, appending the object to the
301 * The object on which to redirect
303 * The target of the redirect
304 * @throws RedirectException
305 * if {@code object} is not {@code null}
307 private static void redirectIfNotNull(String object, String target) throws RedirectException {
308 if (object != null) {
309 throw new RedirectException(target + object);
314 * If the given phrase contains a Sone ID (optionally prefixed by
315 * “sone://”), returns said Sone ID, otherwise return {@code null}.
318 * The phrase that maybe is a Sone ID
319 * @return The Sone ID, or {@code null}
321 private String getSoneId(String phrase) {
322 String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase;
323 return (webInterface.getCore().getSone(soneId, false) != null) ? soneId : null;
327 * If the given phrase contains a post ID (optionally prefixed by
328 * “post://”), returns said post ID, otherwise return {@code null}.
331 * The phrase that maybe is a post ID
332 * @return The post ID, or {@code null}
334 private String getPostId(String phrase) {
335 String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase;
336 return (webInterface.getCore().getPost(postId, false) != null) ? postId : null;
340 * If the given phrase contains a reply ID (optionally prefixed by
341 * “reply://”), returns the ID of the post the reply belongs to, otherwise
342 * return {@code null}.
345 * The phrase that maybe is a reply ID
346 * @return The reply’s post ID, or {@code null}
348 private String getReplyPostId(String phrase) {
349 String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
350 return (webInterface.getCore().getPostReply(replyId, false) != null) ? webInterface.getCore().getPostReply(replyId, false).getPost().getId() : null;
354 * If the given phrase contains an album ID (optionally prefixed by
355 * “album://”), returns said album ID, otherwise return {@code null}.
358 * The phrase that maybe is an album ID
359 * @return The album ID, or {@code null}
361 private String getAlbumId(String phrase) {
362 String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
363 return (webInterface.getCore().getAlbum(albumId, false) != null) ? albumId : null;
367 * If the given phrase contains an image ID (optionally prefixed by
368 * “image://”), returns said image ID, otherwise return {@code null}.
371 * The phrase that maybe is an image ID
372 * @return The image ID, or {@code null}
374 private String getImageId(String phrase) {
375 String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase;
376 return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null;
380 * Converts a given object into a {@link String}.
383 * The type of the objects
384 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
386 private static interface StringGenerator<T> {
389 * Generates a {@link String} for the given object.
392 * The object to generate the {@link String} for
393 * @return The generated {@link String}
395 public String generateString(T object);
400 * Generates a {@link String} from a {@link Sone}, concatenating the name of
401 * the Sone and all {@link Profile} {@link Field} values.
403 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
405 private static class SoneStringGenerator implements StringGenerator<Sone> {
407 /** A static instance of a complete Sone string generator. */
408 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
411 * A static instance of a Sone string generator that will only use the
414 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
416 /** Whether to generate a string from all data of a Sone. */
417 private final boolean complete;
420 * Creates a new Sone string generator.
423 * {@code true} to use the profile’s fields, {@code false} to
424 * not to use the profile‘s fields
426 private SoneStringGenerator(boolean complete) {
427 this.complete = complete;
434 public String generateString(Sone sone) {
435 StringBuilder soneString = new StringBuilder();
436 soneString.append(sone.getName());
437 Profile soneProfile = sone.getProfile();
438 if (soneProfile.getFirstName() != null) {
439 soneString.append(' ').append(soneProfile.getFirstName());
441 if (soneProfile.getMiddleName() != null) {
442 soneString.append(' ').append(soneProfile.getMiddleName());
444 if (soneProfile.getLastName() != null) {
445 soneString.append(' ').append(soneProfile.getLastName());
448 for (Field field : soneProfile.getFields()) {
449 soneString.append(' ').append(field.getValue());
452 return soneString.toString();
458 * Generates a {@link String} from a {@link Post}, concatenating the text of
459 * the post, the text of all {@link Reply}s, and the name of all
460 * {@link Sone}s that have replied.
462 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
464 private class PostStringGenerator implements StringGenerator<Post> {
470 public String generateString(Post post) {
471 StringBuilder postString = new StringBuilder();
472 postString.append(post.getText());
473 if (post.getRecipient() != null) {
474 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient()));
476 for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) {
477 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
478 postString.append(' ').append(reply.getText());
480 return postString.toString();
488 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
490 private static class Phrase {
493 * The optionality of a search phrase.
495 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
498 public enum Optionality {
500 /** The phrase is optional. */
503 /** The phrase is required. */
506 /** The phrase is forbidden. */
511 /** The phrase to search for. */
512 private final String phrase;
514 /** The optionality of the phrase. */
515 private final Optionality optionality;
518 * Creates a new phrase.
521 * The phrase to search for
523 * The optionality of the phrase
525 public Phrase(String phrase, Optionality optionality) {
526 this.optionality = optionality;
527 this.phrase = phrase;
531 * Returns the phrase to search for.
533 * @return The phrase to search for
535 public String getPhrase() {
540 * Returns the optionality of the phrase.
542 * @return The optionality of the phrase
544 public Optionality getOptionality() {
556 public int hashCode() {
557 return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
564 public boolean equals(Object object) {
565 if (!(object instanceof Phrase)) {
568 Phrase phrase = (Phrase) object;
569 return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
575 * A hit consists of a searched object and the score it got for the phrases
578 * @see SearchPage#calculateScore(List, String)
580 * The type of the searched object
581 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
583 private static class Hit<T> {
585 /** Filter for {@link Hit}s with a score of more than 0. */
586 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
589 public boolean apply(Hit<?> hit) {
590 return hit.getScore() > 0;
595 /** Comparator that sorts {@link Hit}s descending by score. */
596 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
599 public int compare(Hit<?> leftHit, Hit<?> rightHit) {
600 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
605 /** The object that was searched. */
606 private final T object;
608 /** The score of the object. */
609 private final double score;
615 * The object that was searched
617 * The score of the object
619 public Hit(T object, double score) {
620 this.object = object;
625 * Returns the object that was searched.
627 * @return The object that was searched
629 public T getObject() {
634 * Returns the score of the object.
636 * @return The score of the object
638 public double getScore() {
645 * Extracts the object from a {@link Hit}.
648 * The type of the object to extract
649 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
651 public static class HitMapper<T> implements Mapper<Hit<T>, T> {
657 public T map(Hit<T> input) {
658 return input.getObject();