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.pages;
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.utils.Pagination;
42 import net.pterodactylus.sone.web.WebInterface;
43 import net.pterodactylus.sone.web.page.FreenetRequest;
44 import net.pterodactylus.util.template.Template;
45 import net.pterodactylus.util.template.TemplateContext;
46 import net.pterodactylus.util.text.StringEscaper;
47 import net.pterodactylus.util.text.TextException;
49 import com.google.common.base.Function;
50 import com.google.common.base.Optional;
51 import com.google.common.base.Predicate;
52 import com.google.common.cache.CacheBuilder;
53 import com.google.common.cache.CacheLoader;
54 import com.google.common.cache.LoadingCache;
55 import com.google.common.collect.Collections2;
56 import com.google.common.collect.FluentIterable;
57 import com.google.common.collect.Ordering;
60 * This page lets the user search for posts and replies that contain certain
63 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
65 public class SearchPage extends SoneTemplatePage {
68 private static final Logger logger = getLogger(SearchPage.class.getName());
70 /** Short-term cache. */
71 private final LoadingCache<List<Phrase>, Set<Hit<Post>>> hitCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader<List<Phrase>, Set<Hit<Post>>>() {
74 @SuppressWarnings("synthetic-access")
75 public Set<Hit<Post>> load(List<Phrase> phrases) {
76 Set<Post> posts = new HashSet<Post>();
77 for (Sone sone : webInterface.getCore().getSones()) {
78 posts.addAll(sone.getPosts());
80 return getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
85 * Creates a new search page.
88 * The template to render
90 * The Sone web interface
92 public SearchPage(Template template, WebInterface webInterface) {
93 super("search.html", template, "Page.Search.Title", webInterface);
97 // SONETEMPLATEPAGE METHODS
104 @SuppressWarnings("synthetic-access")
105 protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
106 String query = request.getHttpRequest().getParam("query").trim();
107 if (query.length() == 0) {
108 throw new RedirectException("index.html");
111 List<Phrase> phrases = parseSearchPhrases(query);
112 if (phrases.isEmpty()) {
113 throw new RedirectException("index.html");
116 /* check for a couple of shortcuts. */
117 if (phrases.size() == 1) {
118 String phrase = phrases.get(0).getPhrase();
120 /* is it a Sone ID? */
121 redirectIfNotNull(getSoneId(phrase), "viewSone.html?sone=");
123 /* is it a post ID? */
124 redirectIfNotNull(getPostId(phrase), "viewPost.html?post=");
126 /* is it a reply ID? show the post. */
127 redirectIfNotNull(getReplyPostId(phrase), "viewPost.html?post=");
129 /* is it an album ID? */
130 redirectIfNotNull(getAlbumId(phrase), "imageBrowser.html?album=");
132 /* is it an image ID? */
133 redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
136 Collection<Sone> sones = webInterface.getCore().getSones();
137 Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
139 Collection<Hit<Post>> postHits = hitCache.getUnchecked(phrases);
142 soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER);
143 postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
146 List<Hit<Sone>> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits);
147 List<Hit<Post>> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits);
149 /* extract Sones and posts. */
150 List<Sone> resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper<Sone>()).toList();
151 List<Post> resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper<Post>()).toList();
154 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage());
155 sonePagination.setPage(fromNullable(tryParse(request.getHttpRequest().getParam("sonePage"))).or(0));
156 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage());
157 postPagination.setPage(fromNullable(tryParse(request.getHttpRequest().getParam("postPage"))).or(0));
159 templateContext.set("sonePagination", sonePagination);
160 templateContext.set("soneHits", sonePagination.getItems());
161 templateContext.set("postPagination", postPagination);
162 templateContext.set("postHits", postPagination.getItems());
170 * Collects hit information for the given objects. The objects are converted
171 * to a {@link String} using the given {@link StringGenerator}, and the
172 * {@link #calculateScore(List, String) calculated score} is stored together
173 * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
177 * The type of the objects
179 * The objects to search over
181 * The phrases to search for
182 * @param stringGenerator
183 * The string generator for the objects
184 * @return The hits for the given phrases
186 private static <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
187 Set<Hit<T>> hits = new HashSet<Hit<T>>();
188 for (T object : objects) {
189 String objectString = stringGenerator.generateString(object);
190 double score = calculateScore(phrases, objectString);
191 hits.add(new Hit<T>(object, score));
197 * Parses the given query into search phrases. The query is split on
198 * whitespace while allowing to group words using single or double quotes.
199 * Isolated phrases starting with a “+” are
200 * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
201 * {@link Phrase.Optionality#FORBIDDEN}.
205 * @return The parsed phrases
207 private static List<Phrase> parseSearchPhrases(String query) {
208 List<String> parsedPhrases;
210 parsedPhrases = StringEscaper.parseLine(query);
211 } catch (TextException te1) {
213 return Collections.emptyList();
216 List<Phrase> phrases = new ArrayList<Phrase>();
217 for (String phrase : parsedPhrases) {
218 if (phrase.startsWith("+")) {
219 if (phrase.length() > 1) {
220 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
222 phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
224 } else if (phrase.startsWith("-")) {
225 if (phrase.length() > 1) {
226 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
228 phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
231 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
238 * Calculates the score for the given expression when using the given
242 * The phrases to search for
244 * The expression to search
245 * @return The score of the expression
247 private static double calculateScore(List<Phrase> phrases, String expression) {
248 logger.log(Level.FINEST, String.format("Calculating Score for “%s”…", expression));
249 double optionalHits = 0;
250 double requiredHits = 0;
251 int forbiddenHits = 0;
252 int requiredPhrases = 0;
253 for (Phrase phrase : phrases) {
254 String phraseString = phrase.getPhrase().toLowerCase();
255 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
261 while (index < expression.length()) {
262 int position = expression.toLowerCase().indexOf(phraseString, index);
263 if (position == -1) {
266 score += Math.pow(1 - position / (double) expression.length(), 2);
267 index = position + phraseString.length();
268 logger.log(Level.FINEST, String.format("Got hit at position %d.", position));
271 logger.log(Level.FINEST, String.format("Score: %f", score));
275 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
276 requiredHits += score;
278 if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
279 optionalHits += score;
281 if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
282 forbiddenHits += matches;
285 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
290 * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
291 * if the given object is not {@code null}, appending the object to the
295 * The object on which to redirect
297 * The target of the redirect
298 * @throws RedirectException
299 * if {@code object} is not {@code null}
301 private static void redirectIfNotNull(String object, String target) throws RedirectException {
302 if (object != null) {
303 throw new RedirectException(target + object);
308 * If the given phrase contains a Sone ID (optionally prefixed by
309 * “sone://”), returns said Sone ID, otherwise return {@code null}.
312 * The phrase that maybe is a Sone ID
313 * @return The Sone ID, or {@code null}
315 private String getSoneId(String phrase) {
316 String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase;
317 return (webInterface.getCore().getSone(soneId).isPresent()) ? soneId : null;
321 * If the given phrase contains a post ID (optionally prefixed by
322 * “post://”), returns said post ID, otherwise return {@code null}.
325 * The phrase that maybe is a post ID
326 * @return The post ID, or {@code null}
328 private String getPostId(String phrase) {
329 String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase;
330 return (webInterface.getCore().getPost(postId).isPresent()) ? postId : null;
334 * If the given phrase contains a reply ID (optionally prefixed by
335 * “reply://”), returns the ID of the post the reply belongs to, otherwise
336 * return {@code null}.
339 * The phrase that maybe is a reply ID
340 * @return The reply’s post ID, or {@code null}
342 private String getReplyPostId(String phrase) {
343 String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
344 Optional<PostReply> postReply = webInterface.getCore().getPostReply(replyId);
345 if (!postReply.isPresent()) {
348 return postReply.get().getPostId();
352 * If the given phrase contains an album ID (optionally prefixed by
353 * “album://”), returns said album ID, otherwise return {@code null}.
356 * The phrase that maybe is an album ID
357 * @return The album ID, or {@code null}
359 private String getAlbumId(String phrase) {
360 String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
361 return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null;
365 * If the given phrase contains an image ID (optionally prefixed by
366 * “image://”), returns said image ID, otherwise return {@code null}.
369 * The phrase that maybe is an image ID
370 * @return The image ID, or {@code null}
372 private String getImageId(String phrase) {
373 String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase;
374 return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null;
378 * Converts a given object into a {@link String}.
381 * The type of the objects
382 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
384 private static interface StringGenerator<T> {
387 * Generates a {@link String} for the given object.
390 * The object to generate the {@link String} for
391 * @return The generated {@link String}
393 public String generateString(T object);
398 * Generates a {@link String} from a {@link Sone}, concatenating the name of
399 * the Sone and all {@link Profile} {@link Field} values.
401 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
403 private static class SoneStringGenerator implements StringGenerator<Sone> {
405 /** A static instance of a complete Sone string generator. */
406 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
409 * A static instance of a Sone string generator that will only use the
412 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
414 /** Whether to generate a string from all data of a Sone. */
415 private final boolean complete;
418 * Creates a new Sone string generator.
421 * {@code true} to use the profile’s fields, {@code false} to
422 * not to use the profile‘s fields
424 private SoneStringGenerator(boolean complete) {
425 this.complete = complete;
432 public String generateString(Sone sone) {
433 StringBuilder soneString = new StringBuilder();
434 soneString.append(sone.getName());
435 Profile soneProfile = sone.getProfile();
436 if (soneProfile.getFirstName() != null) {
437 soneString.append(' ').append(soneProfile.getFirstName());
439 if (soneProfile.getMiddleName() != null) {
440 soneString.append(' ').append(soneProfile.getMiddleName());
442 if (soneProfile.getLastName() != null) {
443 soneString.append(' ').append(soneProfile.getLastName());
446 for (Field field : soneProfile.getFields()) {
447 soneString.append(' ').append(field.getValue());
450 return soneString.toString();
456 * Generates a {@link String} from a {@link Post}, concatenating the text of
457 * the post, the text of all {@link Reply}s, and the name of all
458 * {@link Sone}s that have replied.
460 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
462 private class PostStringGenerator implements StringGenerator<Post> {
468 public String generateString(Post post) {
469 StringBuilder postString = new StringBuilder();
470 postString.append(post.getText());
471 if (post.getRecipient().isPresent()) {
472 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient().get()));
474 for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post.getId()), Reply.FUTURE_REPLY_FILTER)) {
475 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
476 postString.append(' ').append(reply.getText());
478 return postString.toString();
486 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
488 private static class Phrase {
491 * The optionality of a search phrase.
493 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
496 public enum Optionality {
498 /** The phrase is optional. */
501 /** The phrase is required. */
504 /** The phrase is forbidden. */
509 /** The phrase to search for. */
510 private final String phrase;
512 /** The optionality of the phrase. */
513 private final Optionality optionality;
516 * Creates a new phrase.
519 * The phrase to search for
521 * The optionality of the phrase
523 public Phrase(String phrase, Optionality optionality) {
524 this.optionality = optionality;
525 this.phrase = phrase;
529 * Returns the phrase to search for.
531 * @return The phrase to search for
533 public String getPhrase() {
538 * Returns the optionality of the phrase.
540 * @return The optionality of the phrase
542 public Optionality getOptionality() {
554 public int hashCode() {
555 return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
562 public boolean equals(Object object) {
563 if (!(object instanceof Phrase)) {
566 Phrase phrase = (Phrase) object;
567 return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
573 * A hit consists of a searched object and the score it got for the phrases
576 * @see SearchPage#calculateScore(List, String)
578 * The type of the searched object
579 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
581 private static class Hit<T> {
583 /** Filter for {@link Hit}s with a score of more than 0. */
584 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
587 public boolean apply(Hit<?> hit) {
588 return (hit != null) && (hit.getScore() > 0);
593 /** Comparator that sorts {@link Hit}s descending by score. */
594 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
597 public int compare(Hit<?> leftHit, Hit<?> rightHit) {
598 return Double.compare(rightHit.getScore(), leftHit.getScore());
603 /** The object that was searched. */
604 private final T object;
606 /** The score of the object. */
607 private final double score;
613 * The object that was searched
615 * The score of the object
617 public Hit(T object, double score) {
618 this.object = object;
623 * Returns the object that was searched.
625 * @return The object that was searched
627 public T getObject() {
632 * Returns the score of the object.
634 * @return The score of the object
636 public double getScore() {
643 * Extracts the object from a {@link Hit}.
646 * The type of the object to extract
647 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
649 private static class HitMapper<T> implements Function<Hit<T>, T> {
655 public T apply(Hit<T> input) {
656 return input.getObject();