Merge branch 'release/0.9-rc1'
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
1 /*
2  * Sone - SearchPage.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
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;
23
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;
30 import java.util.Set;
31 import java.util.concurrent.TimeUnit;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
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;
47
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;
57
58 /**
59  * This page lets the user search for posts and replies that contain certain
60  * words.
61  *
62  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
63  */
64 public class SearchPage extends SoneTemplatePage {
65
66         /** The logger. */
67         private static final Logger logger = getLogger("Sone.Web.Search");
68
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>>>() {
71
72                 @Override
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());
78                         }
79                         return getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
80                 }
81         });
82
83         /**
84          * Creates a new search page.
85          *
86          * @param template
87          *            The template to render
88          * @param webInterface
89          *            The Sone web interface
90          */
91         public SearchPage(Template template, WebInterface webInterface) {
92                 super("search.html", template, "Page.Search.Title", webInterface);
93         }
94
95         //
96         // SONETEMPLATEPAGE METHODS
97         //
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
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");
109                 }
110
111                 List<Phrase> phrases = parseSearchPhrases(query);
112                 if (phrases.isEmpty()) {
113                         throw new RedirectException("index.html");
114                 }
115
116                 /* check for a couple of shortcuts. */
117                 if (phrases.size() == 1) {
118                         String phrase = phrases.get(0).getPhrase();
119
120                         /* is it a Sone ID? */
121                         redirectIfNotNull(getSoneId(phrase), "viewSone.html?sone=");
122
123                         /* is it a post ID? */
124                         redirectIfNotNull(getPostId(phrase), "viewPost.html?post=");
125
126                         /* is it a reply ID? show the post. */
127                         redirectIfNotNull(getReplyPostId(phrase), "viewPost.html?post=");
128
129                         /* is it an album ID? */
130                         redirectIfNotNull(getAlbumId(phrase), "imageBrowser.html?album=");
131
132                         /* is it an image ID? */
133                         redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
134                 }
135
136                 Collection<Sone> sones = webInterface.getCore().getSones();
137                 Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
138
139                 Collection<Hit<Post>> postHits = hitCache.getUnchecked(phrases);
140
141                 /* now filter. */
142                 soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER);
143                 postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
144
145                 /* now sort. */
146                 List<Hit<Sone>> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits);
147                 List<Hit<Post>> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits);
148
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();
152
153                 /* pagination. */
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));
156
157                 templateContext.set("sonePagination", sonePagination);
158                 templateContext.set("soneHits", sonePagination.getItems());
159                 templateContext.set("postPagination", postPagination);
160                 templateContext.set("postHits", postPagination.getItems());
161         }
162
163         //
164         // PRIVATE METHODS
165         //
166
167         /**
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
172          * returned.
173          *
174          * @param <T>
175          *            The type of the objects
176          * @param objects
177          *            The objects to search over
178          * @param phrases
179          *            The phrases to search for
180          * @param stringGenerator
181          *            The string generator for the objects
182          * @return The hits for the given phrases
183          */
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));
190                 }
191                 return hits;
192         }
193
194         /**
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}.
200          *
201          * @param query
202          *            The query to parse
203          * @return The parsed phrases
204          */
205         private static List<Phrase> parseSearchPhrases(String query) {
206                 List<String> parsedPhrases;
207                 try {
208                         parsedPhrases = StringEscaper.parseLine(query);
209                 } catch (TextException te1) {
210                         /* invalid query. */
211                         return Collections.emptyList();
212                 }
213
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));
219                                 } else {
220                                         phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
221                                 }
222                         } else if (phrase.startsWith("-")) {
223                                 if (phrase.length() > 1) {
224                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
225                                 } else {
226                                         phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
227                                 }
228                         } else {
229                                 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
230                         }
231                 }
232                 return phrases;
233         }
234
235         /**
236          * Calculates the score for the given expression when using the given
237          * phrases.
238          *
239          * @param phrases
240          *            The phrases to search for
241          * @param expression
242          *            The expression to search
243          * @return The score of the expression
244          */
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) {
254                                 ++requiredPhrases;
255                         }
256                         int matches = 0;
257                         int index = 0;
258                         double score = 0;
259                         while (index < expression.length()) {
260                                 int position = expression.toLowerCase().indexOf(phraseString, index);
261                                 if (position == -1) {
262                                         break;
263                                 }
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));
267                                 ++matches;
268                         }
269                         logger.log(Level.FINEST, String.format("Score: %f", score));
270                         if (matches == 0) {
271                                 continue;
272                         }
273                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
274                                 requiredHits += score;
275                         }
276                         if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
277                                 optionalHits += score;
278                         }
279                         if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
280                                 forbiddenHits += matches;
281                         }
282                 }
283                 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
284         }
285
286         /**
287          * Throws a
288          * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
289          * if the given object is not {@code null}, appending the object to the
290          * given target URL.
291          *
292          * @param object
293          *            The object on which to redirect
294          * @param target
295          *            The target of the redirect
296          * @throws RedirectException
297          *             if {@code object} is not {@code null}
298          */
299         private static void redirectIfNotNull(String object, String target) throws RedirectException {
300                 if (object != null) {
301                         throw new RedirectException(target + object);
302                 }
303         }
304
305         /**
306          * If the given phrase contains a Sone ID (optionally prefixed by
307          * “sone://”), returns said Sone ID, otherwise return {@code null}.
308          *
309          * @param phrase
310          *            The phrase that maybe is a Sone ID
311          * @return The Sone ID, or {@code null}
312          */
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;
316         }
317
318         /**
319          * If the given phrase contains a post ID (optionally prefixed by
320          * “post://”), returns said post ID, otherwise return {@code null}.
321          *
322          * @param phrase
323          *            The phrase that maybe is a post ID
324          * @return The post ID, or {@code null}
325          */
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;
329         }
330
331         /**
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}.
335          *
336          * @param phrase
337          *            The phrase that maybe is a reply ID
338          * @return The reply’s post ID, or {@code null}
339          */
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()) {
344                         return null;
345                 }
346                 return postReply.get().getPostId();
347         }
348
349         /**
350          * If the given phrase contains an album ID (optionally prefixed by
351          * “album://”), returns said album ID, otherwise return {@code null}.
352          *
353          * @param phrase
354          *            The phrase that maybe is an album ID
355          * @return The album ID, or {@code null}
356          */
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;
360         }
361
362         /**
363          * If the given phrase contains an image ID (optionally prefixed by
364          * “image://”), returns said image ID, otherwise return {@code null}.
365          *
366          * @param phrase
367          *            The phrase that maybe is an image ID
368          * @return The image ID, or {@code null}
369          */
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;
373         }
374
375         /**
376          * Converts a given object into a {@link String}.
377          *
378          * @param <T>
379          *            The type of the objects
380          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
381          */
382         private static interface StringGenerator<T> {
383
384                 /**
385                  * Generates a {@link String} for the given object.
386                  *
387                  * @param object
388                  *            The object to generate the {@link String} for
389                  * @return The generated {@link String}
390                  */
391                 public String generateString(T object);
392
393         }
394
395         /**
396          * Generates a {@link String} from a {@link Sone}, concatenating the name of
397          * the Sone and all {@link Profile} {@link Field} values.
398          *
399          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
400          */
401         private static class SoneStringGenerator implements StringGenerator<Sone> {
402
403                 /** A static instance of a complete Sone string generator. */
404                 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
405
406                 /**
407                  * A static instance of a Sone string generator that will only use the
408                  * name of the Sone.
409                  */
410                 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
411
412                 /** Whether to generate a string from all data of a Sone. */
413                 private final boolean complete;
414
415                 /**
416                  * Creates a new Sone string generator.
417                  *
418                  * @param complete
419                  *            {@code true} to use the profile’s fields, {@code false} to
420                  *            not to use the profile‘s fields
421                  */
422                 private SoneStringGenerator(boolean complete) {
423                         this.complete = complete;
424                 }
425
426                 /**
427                  * {@inheritDoc}
428                  */
429                 @Override
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());
436                         }
437                         if (soneProfile.getMiddleName() != null) {
438                                 soneString.append(' ').append(soneProfile.getMiddleName());
439                         }
440                         if (soneProfile.getLastName() != null) {
441                                 soneString.append(' ').append(soneProfile.getLastName());
442                         }
443                         if (complete) {
444                                 for (Field field : soneProfile.getFields()) {
445                                         soneString.append(' ').append(field.getValue());
446                                 }
447                         }
448                         return soneString.toString();
449                 }
450
451         }
452
453         /**
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.
457          *
458          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
459          */
460         private class PostStringGenerator implements StringGenerator<Post> {
461
462                 /**
463                  * {@inheritDoc}
464                  */
465                 @Override
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()));
471                         }
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());
475                         }
476                         return postString.toString();
477                 }
478
479         }
480
481         /**
482          * A search phrase.
483          *
484          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
485          */
486         private static class Phrase {
487
488                 /**
489                  * The optionality of a search phrase.
490                  *
491                  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
492                  *         Roden</a>
493                  */
494                 public enum Optionality {
495
496                         /** The phrase is optional. */
497                         OPTIONAL,
498
499                         /** The phrase is required. */
500                         REQUIRED,
501
502                         /** The phrase is forbidden. */
503                         FORBIDDEN
504
505                 }
506
507                 /** The phrase to search for. */
508                 private final String phrase;
509
510                 /** The optionality of the phrase. */
511                 private final Optionality optionality;
512
513                 /**
514                  * Creates a new phrase.
515                  *
516                  * @param phrase
517                  *            The phrase to search for
518                  * @param optionality
519                  *            The optionality of the phrase
520                  */
521                 public Phrase(String phrase, Optionality optionality) {
522                         this.optionality = optionality;
523                         this.phrase = phrase;
524                 }
525
526                 /**
527                  * Returns the phrase to search for.
528                  *
529                  * @return The phrase to search for
530                  */
531                 public String getPhrase() {
532                         return phrase;
533                 }
534
535                 /**
536                  * Returns the optionality of the phrase.
537                  *
538                  * @return The optionality of the phrase
539                  */
540                 public Optionality getOptionality() {
541                         return optionality;
542                 }
543
544                 //
545                 // OBJECT METHODS
546                 //
547
548                 /**
549                  * {@inheritDoc}
550                  */
551                 @Override
552                 public int hashCode() {
553                         return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
554                 }
555
556                 /**
557                  * {@inheritDoc}
558                  */
559                 @Override
560                 public boolean equals(Object object) {
561                         if (!(object instanceof Phrase)) {
562                                 return false;
563                         }
564                         Phrase phrase = (Phrase) object;
565                         return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
566                 }
567
568         }
569
570         /**
571          * A hit consists of a searched object and the score it got for the phrases
572          * of the search.
573          *
574          * @see SearchPage#calculateScore(List, String)
575          * @param <T>
576          *            The type of the searched object
577          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
578          */
579         private static class Hit<T> {
580
581                 /** Filter for {@link Hit}s with a score of more than 0. */
582                 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
583
584                         @Override
585                         public boolean apply(Hit<?> hit) {
586                                 return (hit != null) && (hit.getScore() > 0);
587                         }
588
589                 };
590
591                 /** Comparator that sorts {@link Hit}s descending by score. */
592                 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
593
594                         @Override
595                         public int compare(Hit<?> leftHit, Hit<?> rightHit) {
596                                 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
597                         }
598
599                 };
600
601                 /** The object that was searched. */
602                 private final T object;
603
604                 /** The score of the object. */
605                 private final double score;
606
607                 /**
608                  * Creates a new hit.
609                  *
610                  * @param object
611                  *            The object that was searched
612                  * @param score
613                  *            The score of the object
614                  */
615                 public Hit(T object, double score) {
616                         this.object = object;
617                         this.score = score;
618                 }
619
620                 /**
621                  * Returns the object that was searched.
622                  *
623                  * @return The object that was searched
624                  */
625                 public T getObject() {
626                         return object;
627                 }
628
629                 /**
630                  * Returns the score of the object.
631                  *
632                  * @return The score of the object
633                  */
634                 public double getScore() {
635                         return score;
636                 }
637
638         }
639
640         /**
641          * Extracts the object from a {@link Hit}.
642          *
643          * @param <T>
644          *            The type of the object to extract
645          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
646          */
647         private static class HitMapper<T> implements Function<Hit<T>, T> {
648
649                 /**
650                  * {@inheritDoc}
651                  */
652                 @Override
653                 public T apply(Hit<T> input) {
654                         return input.getObject();
655                 }
656
657         }
658
659 }