2 * Sone - SearchPage.java - Copyright © 2010–2013 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("Sone.Web.Search");
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 processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
105 super.processTemplate(request, templateContext);
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()).setPage(fromNullable(tryParse(request.getHttpRequest().getParam("sonePage"))).or(0));
155 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(fromNullable(tryParse(request.getHttpRequest().getParam("postPage"))).or(0));
157 templateContext.set("sonePagination", sonePagination);
158 templateContext.set("soneHits", sonePagination.getItems());
159 templateContext.set("postPagination", postPagination);
160 templateContext.set("postHits", postPagination.getItems());
168 * Collects hit information for the given objects. The objects are converted
169 * to a {@link String} using the given {@link StringGenerator}, and the
170 * {@link #calculateScore(List, String) calculated score} is stored together
171 * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
175 * The type of the objects
177 * The objects to search over
179 * The phrases to search for
180 * @param stringGenerator
181 * The string generator for the objects
182 * @return The hits for the given phrases
184 private static <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
185 Set<Hit<T>> hits = new HashSet<Hit<T>>();
186 for (T object : objects) {
187 String objectString = stringGenerator.generateString(object);
188 double score = calculateScore(phrases, objectString);
189 hits.add(new Hit<T>(object, score));
195 * Parses the given query into search phrases. The query is split on
196 * whitespace while allowing to group words using single or double quotes.
197 * Isolated phrases starting with a “+” are
198 * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
199 * {@link Phrase.Optionality#FORBIDDEN}.
203 * @return The parsed phrases
205 private static List<Phrase> parseSearchPhrases(String query) {
206 List<String> parsedPhrases;
208 parsedPhrases = StringEscaper.parseLine(query);
209 } catch (TextException te1) {
211 return Collections.emptyList();
214 List<Phrase> phrases = new ArrayList<Phrase>();
215 for (String phrase : parsedPhrases) {
216 if (phrase.startsWith("+")) {
217 if (phrase.length() > 1) {
218 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
220 phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
222 } else if (phrase.startsWith("-")) {
223 if (phrase.length() > 1) {
224 phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
226 phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
229 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
236 * Calculates the score for the given expression when using the given
240 * The phrases to search for
242 * The expression to search
243 * @return The score of the expression
245 private static double calculateScore(List<Phrase> phrases, String expression) {
246 logger.log(Level.FINEST, String.format("Calculating Score for “%s”…", expression));
247 double optionalHits = 0;
248 double requiredHits = 0;
249 int forbiddenHits = 0;
250 int requiredPhrases = 0;
251 for (Phrase phrase : phrases) {
252 String phraseString = phrase.getPhrase().toLowerCase();
253 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
259 while (index < expression.length()) {
260 int position = expression.toLowerCase().indexOf(phraseString, index);
261 if (position == -1) {
264 score += Math.pow(1 - position / (double) expression.length(), 2);
265 index = position + phraseString.length();
266 logger.log(Level.FINEST, String.format("Got hit at position %d.", position));
269 logger.log(Level.FINEST, String.format("Score: %f", score));
273 if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
274 requiredHits += score;
276 if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
277 optionalHits += score;
279 if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
280 forbiddenHits += matches;
283 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
288 * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
289 * if the given object is not {@code null}, appending the object to the
293 * The object on which to redirect
295 * The target of the redirect
296 * @throws RedirectException
297 * if {@code object} is not {@code null}
299 private static void redirectIfNotNull(String object, String target) throws RedirectException {
300 if (object != null) {
301 throw new RedirectException(target + object);
306 * If the given phrase contains a Sone ID (optionally prefixed by
307 * “sone://”), returns said Sone ID, otherwise return {@code null}.
310 * The phrase that maybe is a Sone ID
311 * @return The Sone ID, or {@code null}
313 private String getSoneId(String phrase) {
314 String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase;
315 return (webInterface.getCore().getSone(soneId).isPresent()) ? soneId : null;
319 * If the given phrase contains a post ID (optionally prefixed by
320 * “post://”), returns said post ID, otherwise return {@code null}.
323 * The phrase that maybe is a post ID
324 * @return The post ID, or {@code null}
326 private String getPostId(String phrase) {
327 String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase;
328 return (webInterface.getCore().getPost(postId).isPresent()) ? postId : null;
332 * If the given phrase contains a reply ID (optionally prefixed by
333 * “reply://”), returns the ID of the post the reply belongs to, otherwise
334 * return {@code null}.
337 * The phrase that maybe is a reply ID
338 * @return The reply’s post ID, or {@code null}
340 private String getReplyPostId(String phrase) {
341 String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
342 Optional<PostReply> postReply = webInterface.getCore().getPostReply(replyId);
343 if (!postReply.isPresent()) {
346 return postReply.get().getPostId();
350 * If the given phrase contains an album ID (optionally prefixed by
351 * “album://”), returns said album ID, otherwise return {@code null}.
354 * The phrase that maybe is an album ID
355 * @return The album ID, or {@code null}
357 private String getAlbumId(String phrase) {
358 String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
359 return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null;
363 * If the given phrase contains an image ID (optionally prefixed by
364 * “image://”), returns said image ID, otherwise return {@code null}.
367 * The phrase that maybe is an image ID
368 * @return The image ID, or {@code null}
370 private String getImageId(String phrase) {
371 String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase;
372 return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null;
376 * Converts a given object into a {@link String}.
379 * The type of the objects
380 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
382 private static interface StringGenerator<T> {
385 * Generates a {@link String} for the given object.
388 * The object to generate the {@link String} for
389 * @return The generated {@link String}
391 public String generateString(T object);
396 * Generates a {@link String} from a {@link Sone}, concatenating the name of
397 * the Sone and all {@link Profile} {@link Field} values.
399 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
401 private static class SoneStringGenerator implements StringGenerator<Sone> {
403 /** A static instance of a complete Sone string generator. */
404 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
407 * A static instance of a Sone string generator that will only use the
410 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
412 /** Whether to generate a string from all data of a Sone. */
413 private final boolean complete;
416 * Creates a new Sone string generator.
419 * {@code true} to use the profile’s fields, {@code false} to
420 * not to use the profile‘s fields
422 private SoneStringGenerator(boolean complete) {
423 this.complete = complete;
430 public String generateString(Sone sone) {
431 StringBuilder soneString = new StringBuilder();
432 soneString.append(sone.getName());
433 Profile soneProfile = sone.getProfile();
434 if (soneProfile.getFirstName() != null) {
435 soneString.append(' ').append(soneProfile.getFirstName());
437 if (soneProfile.getMiddleName() != null) {
438 soneString.append(' ').append(soneProfile.getMiddleName());
440 if (soneProfile.getLastName() != null) {
441 soneString.append(' ').append(soneProfile.getLastName());
444 for (Field field : soneProfile.getFields()) {
445 soneString.append(' ').append(field.getValue());
448 return soneString.toString();
454 * Generates a {@link String} from a {@link Post}, concatenating the text of
455 * the post, the text of all {@link Reply}s, and the name of all
456 * {@link Sone}s that have replied.
458 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
460 private class PostStringGenerator implements StringGenerator<Post> {
466 public String generateString(Post post) {
467 StringBuilder postString = new StringBuilder();
468 postString.append(post.getText());
469 if (post.getRecipient().isPresent()) {
470 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient().get()));
472 for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post.getId()), Reply.FUTURE_REPLY_FILTER)) {
473 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
474 postString.append(' ').append(reply.getText());
476 return postString.toString();
484 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
486 private static class Phrase {
489 * The optionality of a search phrase.
491 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
494 public enum Optionality {
496 /** The phrase is optional. */
499 /** The phrase is required. */
502 /** The phrase is forbidden. */
507 /** The phrase to search for. */
508 private final String phrase;
510 /** The optionality of the phrase. */
511 private final Optionality optionality;
514 * Creates a new phrase.
517 * The phrase to search for
519 * The optionality of the phrase
521 public Phrase(String phrase, Optionality optionality) {
522 this.optionality = optionality;
523 this.phrase = phrase;
527 * Returns the phrase to search for.
529 * @return The phrase to search for
531 public String getPhrase() {
536 * Returns the optionality of the phrase.
538 * @return The optionality of the phrase
540 public Optionality getOptionality() {
552 public int hashCode() {
553 return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
560 public boolean equals(Object object) {
561 if (!(object instanceof Phrase)) {
564 Phrase phrase = (Phrase) object;
565 return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
571 * A hit consists of a searched object and the score it got for the phrases
574 * @see SearchPage#calculateScore(List, String)
576 * The type of the searched object
577 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
579 private static class Hit<T> {
581 /** Filter for {@link Hit}s with a score of more than 0. */
582 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
585 public boolean apply(Hit<?> hit) {
586 return (hit != null) && (hit.getScore() > 0);
591 /** Comparator that sorts {@link Hit}s descending by score. */
592 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
595 public int compare(Hit<?> leftHit, Hit<?> rightHit) {
596 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
601 /** The object that was searched. */
602 private final T object;
604 /** The score of the object. */
605 private final double score;
611 * The object that was searched
613 * The score of the object
615 public Hit(T object, double score) {
616 this.object = object;
621 * Returns the object that was searched.
623 * @return The object that was searched
625 public T getObject() {
630 * Returns the score of the object.
632 * @return The score of the object
634 public double getScore() {
641 * Extracts the object from a {@link Hit}.
644 * The type of the object to extract
645 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
647 private static class HitMapper<T> implements Function<Hit<T>, T> {
653 public T apply(Hit<T> input) {
654 return input.getObject();