2 * Sone - SearchPage.java - Copyright © 2010–2016 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 static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.primitives.Ints.tryParse;
22 import static java.util.logging.Logger.getLogger;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.HashSet;
29 import java.util.List;
31 import java.util.concurrent.TimeUnit;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.PostReply;
37 import net.pterodactylus.sone.data.Profile;
38 import net.pterodactylus.sone.data.Profile.Field;
39 import net.pterodactylus.sone.data.Reply;
40 import net.pterodactylus.sone.data.Sone;
41 import net.pterodactylus.sone.web.page.FreenetRequest;
42 import net.pterodactylus.util.collection.Pagination;
43 import net.pterodactylus.util.template.Template;
44 import net.pterodactylus.util.template.TemplateContext;
45 import net.pterodactylus.util.text.StringEscaper;
46 import net.pterodactylus.util.text.TextException;
48 import com.google.common.base.Function;
49 import com.google.common.base.Optional;
50 import com.google.common.base.Predicate;
51 import com.google.common.cache.CacheBuilder;
52 import com.google.common.cache.CacheLoader;
53 import com.google.common.cache.LoadingCache;
54 import com.google.common.collect.Collections2;
55 import com.google.common.collect.FluentIterable;
56 import com.google.common.collect.Ordering;
59 * This page lets the user search for posts and replies that contain certain
62 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
64 public class SearchPage extends SoneTemplatePage {
67 private static final Logger logger = getLogger(SearchPage.class.getName());
69 /** Short-term cache. */
70 private final LoadingCache<List<Phrase>, Set<Hit<Post>>> hitCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader<List<Phrase>, Set<Hit<Post>>>() {
73 @SuppressWarnings("synthetic-access")
74 public Set<Hit<Post>> load(List<Phrase> phrases) {
75 Set<Post> posts = new HashSet<Post>();
76 for (Sone sone : webInterface.getCore().getSones()) {
77 posts.addAll(sone.getPosts());
79 return getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
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 @SuppressWarnings("synthetic-access")
104 protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
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 Collection<Sone> sones = webInterface.getCore().getSones();
136 Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
138 Collection<Hit<Post>> postHits = hitCache.getUnchecked(phrases);
141 soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER);
142 postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
145 List<Hit<Sone>> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits);
146 List<Hit<Post>> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits);
148 /* extract Sones and posts. */
149 List<Sone> resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper<Sone>()).toList();
150 List<Post> resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper<Post>()).toList();
153 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(fromNullable(tryParse(request.getHttpRequest().getParam("sonePage"))).or(0));
154 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(fromNullable(tryParse(request.getHttpRequest().getParam("postPage"))).or(0));
156 templateContext.set("sonePagination", sonePagination);
157 templateContext.set("soneHits", sonePagination.getItems());
158 templateContext.set("postPagination", postPagination);
159 templateContext.set("postHits", postPagination.getItems());
167 * Collects hit information for the given objects. The objects are converted
168 * to a {@link String} using the given {@link StringGenerator}, and the
169 * {@link #calculateScore(List, String) calculated score} is stored together
170 * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
174 * The type of the objects
176 * The objects to search over
178 * The phrases to search for
179 * @param stringGenerator
180 * The string generator for the objects
181 * @return The hits for the given phrases
183 private static <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
184 Set<Hit<T>> hits = new HashSet<Hit<T>>();
185 for (T object : objects) {
186 String objectString = stringGenerator.generateString(object);
187 double score = calculateScore(phrases, objectString);
188 hits.add(new Hit<T>(object, score));
194 * Parses the given query into search phrases. The query is split on
195 * whitespace while allowing to group words using single or double quotes.
196 * Isolated phrases starting with a “+” are
197 * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
198 * {@link Phrase.Optionality#FORBIDDEN}.
202 * @return The parsed phrases
204 private static List<Phrase> parseSearchPhrases(String query) {
205 List<String> parsedPhrases;
207 parsedPhrases = StringEscaper.parseLine(query);
208 } catch (TextException te1) {
210 return Collections.emptyList();
213 List<Phrase> phrases = new ArrayList<Phrase>();
214 for (String phrase : parsedPhrases) {
215 if (phrase.startsWith("+")) {
216 if (phrase.length() > 1) {
217 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
219 phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
221 } else if (phrase.startsWith("-")) {
222 if (phrase.length() > 1) {
223 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
225 phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
228 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
235 * Calculates the score for the given expression when using the given
239 * The phrases to search for
241 * The expression to search
242 * @return The score of the expression
244 private static double calculateScore(List<Phrase> phrases, String expression) {
245 logger.log(Level.FINEST, String.format("Calculating Score for “%s”…", expression));
246 double optionalHits = 0;
247 double requiredHits = 0;
248 int forbiddenHits = 0;
249 int requiredPhrases = 0;
250 for (Phrase phrase : phrases) {
251 String phraseString = phrase.getPhrase().toLowerCase();
252 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
258 while (index < expression.length()) {
259 int position = expression.toLowerCase().indexOf(phraseString, index);
260 if (position == -1) {
263 score += Math.pow(1 - position / (double) expression.length(), 2);
264 index = position + phraseString.length();
265 logger.log(Level.FINEST, String.format("Got hit at position %d.", position));
268 logger.log(Level.FINEST, String.format("Score: %f", score));
272 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
273 requiredHits += score;
275 if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
276 optionalHits += score;
278 if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
279 forbiddenHits += matches;
282 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
287 * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
288 * if the given object is not {@code null}, appending the object to the
292 * The object on which to redirect
294 * The target of the redirect
295 * @throws RedirectException
296 * if {@code object} is not {@code null}
298 private static void redirectIfNotNull(String object, String target) throws RedirectException {
299 if (object != null) {
300 throw new RedirectException(target + object);
305 * If the given phrase contains a Sone ID (optionally prefixed by
306 * “sone://”), returns said Sone ID, otherwise return {@code null}.
309 * The phrase that maybe is a Sone ID
310 * @return The Sone ID, or {@code null}
312 private String getSoneId(String phrase) {
313 String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase;
314 return (webInterface.getCore().getSone(soneId).isPresent()) ? soneId : null;
318 * If the given phrase contains a post ID (optionally prefixed by
319 * “post://”), returns said post ID, otherwise return {@code null}.
322 * The phrase that maybe is a post ID
323 * @return The post ID, or {@code null}
325 private String getPostId(String phrase) {
326 String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase;
327 return (webInterface.getCore().getPost(postId).isPresent()) ? postId : null;
331 * If the given phrase contains a reply ID (optionally prefixed by
332 * “reply://”), returns the ID of the post the reply belongs to, otherwise
333 * return {@code null}.
336 * The phrase that maybe is a reply ID
337 * @return The reply’s post ID, or {@code null}
339 private String getReplyPostId(String phrase) {
340 String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
341 Optional<PostReply> postReply = webInterface.getCore().getPostReply(replyId);
342 if (!postReply.isPresent()) {
345 return postReply.get().getPostId();
349 * If the given phrase contains an album ID (optionally prefixed by
350 * “album://”), returns said album ID, otherwise return {@code null}.
353 * The phrase that maybe is an album ID
354 * @return The album ID, or {@code null}
356 private String getAlbumId(String phrase) {
357 String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
358 return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null;
362 * If the given phrase contains an image ID (optionally prefixed by
363 * “image://”), returns said image ID, otherwise return {@code null}.
366 * The phrase that maybe is an image ID
367 * @return The image ID, or {@code null}
369 private String getImageId(String phrase) {
370 String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase;
371 return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null;
375 * Converts a given object into a {@link String}.
378 * The type of the objects
379 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
381 private static interface StringGenerator<T> {
384 * Generates a {@link String} for the given object.
387 * The object to generate the {@link String} for
388 * @return The generated {@link String}
390 public String generateString(T object);
395 * Generates a {@link String} from a {@link Sone}, concatenating the name of
396 * the Sone and all {@link Profile} {@link Field} values.
398 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
400 private static class SoneStringGenerator implements StringGenerator<Sone> {
402 /** A static instance of a complete Sone string generator. */
403 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
406 * A static instance of a Sone string generator that will only use the
409 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
411 /** Whether to generate a string from all data of a Sone. */
412 private final boolean complete;
415 * Creates a new Sone string generator.
418 * {@code true} to use the profile’s fields, {@code false} to
419 * not to use the profile‘s fields
421 private SoneStringGenerator(boolean complete) {
422 this.complete = complete;
429 public String generateString(Sone sone) {
430 StringBuilder soneString = new StringBuilder();
431 soneString.append(sone.getName());
432 Profile soneProfile = sone.getProfile();
433 if (soneProfile.getFirstName() != null) {
434 soneString.append(' ').append(soneProfile.getFirstName());
436 if (soneProfile.getMiddleName() != null) {
437 soneString.append(' ').append(soneProfile.getMiddleName());
439 if (soneProfile.getLastName() != null) {
440 soneString.append(' ').append(soneProfile.getLastName());
443 for (Field field : soneProfile.getFields()) {
444 soneString.append(' ').append(field.getValue());
447 return soneString.toString();
453 * Generates a {@link String} from a {@link Post}, concatenating the text of
454 * the post, the text of all {@link Reply}s, and the name of all
455 * {@link Sone}s that have replied.
457 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
459 private class PostStringGenerator implements StringGenerator<Post> {
465 public String generateString(Post post) {
466 StringBuilder postString = new StringBuilder();
467 postString.append(post.getText());
468 if (post.getRecipient().isPresent()) {
469 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient().get()));
471 for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post.getId()), Reply.FUTURE_REPLY_FILTER)) {
472 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
473 postString.append(' ').append(reply.getText());
475 return postString.toString();
483 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
485 private static class Phrase {
488 * The optionality of a search phrase.
490 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
493 public enum Optionality {
495 /** The phrase is optional. */
498 /** The phrase is required. */
501 /** The phrase is forbidden. */
506 /** The phrase to search for. */
507 private final String phrase;
509 /** The optionality of the phrase. */
510 private final Optionality optionality;
513 * Creates a new phrase.
516 * The phrase to search for
518 * The optionality of the phrase
520 public Phrase(String phrase, Optionality optionality) {
521 this.optionality = optionality;
522 this.phrase = phrase;
526 * Returns the phrase to search for.
528 * @return The phrase to search for
530 public String getPhrase() {
535 * Returns the optionality of the phrase.
537 * @return The optionality of the phrase
539 public Optionality getOptionality() {
551 public int hashCode() {
552 return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
559 public boolean equals(Object object) {
560 if (!(object instanceof Phrase)) {
563 Phrase phrase = (Phrase) object;
564 return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
570 * A hit consists of a searched object and the score it got for the phrases
573 * @see SearchPage#calculateScore(List, String)
575 * The type of the searched object
576 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
578 private static class Hit<T> {
580 /** Filter for {@link Hit}s with a score of more than 0. */
581 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
584 public boolean apply(Hit<?> hit) {
585 return (hit != null) && (hit.getScore() > 0);
590 /** Comparator that sorts {@link Hit}s descending by score. */
591 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
594 public int compare(Hit<?> leftHit, Hit<?> rightHit) {
595 return Double.compare(rightHit.getScore(), leftHit.getScore());
600 /** The object that was searched. */
601 private final T object;
603 /** The score of the object. */
604 private final double score;
610 * The object that was searched
612 * The score of the object
614 public Hit(T object, double score) {
615 this.object = object;
620 * Returns the object that was searched.
622 * @return The object that was searched
624 public T getObject() {
629 * Returns the score of the object.
631 * @return The score of the object
633 public double getScore() {
640 * Extracts the object from a {@link Hit}.
643 * The type of the object to extract
644 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
646 private static class HitMapper<T> implements Function<Hit<T>, T> {
652 public T apply(Hit<T> input) {
653 return input.getObject();