Use Guava’s predicate instead of utils’ filter.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / SearchPage.java
1 /*
2  * Sone - SearchPage.java - Copyright © 2010–2012 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 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;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
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;
53
54 import com.google.common.base.Predicate;
55 import com.google.common.collect.Collections2;
56
57 /**
58  * This page lets the user search for posts and replies that contain certain
59  * words.
60  *
61  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
62  */
63 public class SearchPage extends SoneTemplatePage {
64
65         /** The logger. */
66         private static final Logger logger = Logging.getLogger(SearchPage.class);
67
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>>>() {
70
71                 @Override
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());
77                         }
78                         return new DefaultCacheItem<Set<Hit<Post>>>(getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator()));
79                 }
80
81         }, new TimedMap<List<Phrase>, CacheItem<Set<Hit<Post>>>>(300000));
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         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");
108                 }
109
110                 List<Phrase> phrases = parseSearchPhrases(query);
111                 if (phrases.isEmpty()) {
112                         throw new RedirectException("index.html");
113                 }
114
115                 /* check for a couple of shortcuts. */
116                 if (phrases.size() == 1) {
117                         String phrase = phrases.get(0).getPhrase();
118
119                         /* is it a Sone ID? */
120                         redirectIfNotNull(getSoneId(phrase), "viewSone.html?sone=");
121
122                         /* is it a post ID? */
123                         redirectIfNotNull(getPostId(phrase), "viewPost.html?post=");
124
125                         /* is it a reply ID? show the post. */
126                         redirectIfNotNull(getReplyPostId(phrase), "viewPost.html?post=");
127
128                         /* is it an album ID? */
129                         redirectIfNotNull(getAlbumId(phrase), "imageBrowser.html?album=");
130
131                         /* is it an image ID? */
132                         redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
133                 }
134
135                 Set<Sone> sones = webInterface.getCore().getSones();
136                 Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
137
138                 Collection<Hit<Post>> postHits;
139                 try {
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();
145                 }
146
147                 /* now filter. */
148                 soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER);
149                 postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
150
151                 /* now sort. */
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);
156
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>());
160
161                 /* pagination. */
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));
164
165                 templateContext.set("sonePagination", sonePagination);
166                 templateContext.set("soneHits", sonePagination.getItems());
167                 templateContext.set("postPagination", postPagination);
168                 templateContext.set("postHits", postPagination.getItems());
169         }
170
171         //
172         // PRIVATE METHODS
173         //
174
175         /**
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
180          * returned.
181          *
182          * @param <T>
183          *            The type of the objects
184          * @param objects
185          *            The objects to search over
186          * @param phrases
187          *            The phrases to search for
188          * @param stringGenerator
189          *            The string generator for the objects
190          * @return The hits for the given phrases
191          */
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));
198                 }
199                 return hits;
200         }
201
202         /**
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}.
208          *
209          * @param query
210          *            The query to parse
211          * @return The parsed phrases
212          */
213         private static List<Phrase> parseSearchPhrases(String query) {
214                 List<String> parsedPhrases = null;
215                 try {
216                         parsedPhrases = StringEscaper.parseLine(query);
217                 } catch (TextException te1) {
218                         /* invalid query. */
219                         return Collections.emptyList();
220                 }
221
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));
227                                 } else {
228                                         phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
229                                 }
230                         } else if (phrase.startsWith("-")) {
231                                 if (phrase.length() > 1) {
232                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
233                                 } else {
234                                         phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
235                                 }
236                         } else {
237                                 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
238                         }
239                 }
240                 return phrases;
241         }
242
243         /**
244          * Calculates the score for the given expression when using the given
245          * phrases.
246          *
247          * @param phrases
248          *            The phrases to search for
249          * @param expression
250          *            The expression to search
251          * @return The score of the expression
252          */
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) {
262                                 ++requiredPhrases;
263                         }
264                         int matches = 0;
265                         int index = 0;
266                         double score = 0;
267                         while (index < expression.length()) {
268                                 int position = expression.toLowerCase().indexOf(phraseString, index);
269                                 if (position == -1) {
270                                         break;
271                                 }
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));
275                                 ++matches;
276                         }
277                         logger.log(Level.FINEST, String.format("Score: %f", score));
278                         if (matches == 0) {
279                                 continue;
280                         }
281                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
282                                 requiredHits += score;
283                         }
284                         if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
285                                 optionalHits += score;
286                         }
287                         if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
288                                 forbiddenHits += matches;
289                         }
290                 }
291                 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
292         }
293
294         /**
295          * Throws a
296          * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
297          * if the given object is not {@code null}, appending the object to the
298          * given target URL.
299          *
300          * @param object
301          *            The object on which to redirect
302          * @param target
303          *            The target of the redirect
304          * @throws RedirectException
305          *             if {@code object} is not {@code null}
306          */
307         private static void redirectIfNotNull(String object, String target) throws RedirectException {
308                 if (object != null) {
309                         throw new RedirectException(target + object);
310                 }
311         }
312
313         /**
314          * If the given phrase contains a Sone ID (optionally prefixed by
315          * “sone://”), returns said Sone ID, otherwise return {@code null}.
316          *
317          * @param phrase
318          *            The phrase that maybe is a Sone ID
319          * @return The Sone ID, or {@code null}
320          */
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;
324         }
325
326         /**
327          * If the given phrase contains a post ID (optionally prefixed by
328          * “post://”), returns said post ID, otherwise return {@code null}.
329          *
330          * @param phrase
331          *            The phrase that maybe is a post ID
332          * @return The post ID, or {@code null}
333          */
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;
337         }
338
339         /**
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}.
343          *
344          * @param phrase
345          *            The phrase that maybe is a reply ID
346          * @return The reply’s post ID, or {@code null}
347          */
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;
351         }
352
353         /**
354          * If the given phrase contains an album ID (optionally prefixed by
355          * “album://”), returns said album ID, otherwise return {@code null}.
356          *
357          * @param phrase
358          *            The phrase that maybe is an album ID
359          * @return The album ID, or {@code null}
360          */
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;
364         }
365
366         /**
367          * If the given phrase contains an image ID (optionally prefixed by
368          * “image://”), returns said image ID, otherwise return {@code null}.
369          *
370          * @param phrase
371          *            The phrase that maybe is an image ID
372          * @return The image ID, or {@code null}
373          */
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;
377         }
378
379         /**
380          * Converts a given object into a {@link String}.
381          *
382          * @param <T>
383          *            The type of the objects
384          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
385          */
386         private static interface StringGenerator<T> {
387
388                 /**
389                  * Generates a {@link String} for the given object.
390                  *
391                  * @param object
392                  *            The object to generate the {@link String} for
393                  * @return The generated {@link String}
394                  */
395                 public String generateString(T object);
396
397         }
398
399         /**
400          * Generates a {@link String} from a {@link Sone}, concatenating the name of
401          * the Sone and all {@link Profile} {@link Field} values.
402          *
403          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
404          */
405         private static class SoneStringGenerator implements StringGenerator<Sone> {
406
407                 /** A static instance of a complete Sone string generator. */
408                 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
409
410                 /**
411                  * A static instance of a Sone string generator that will only use the
412                  * name of the Sone.
413                  */
414                 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
415
416                 /** Whether to generate a string from all data of a Sone. */
417                 private final boolean complete;
418
419                 /**
420                  * Creates a new Sone string generator.
421                  *
422                  * @param complete
423                  *            {@code true} to use the profile’s fields, {@code false} to
424                  *            not to use the profile‘s fields
425                  */
426                 private SoneStringGenerator(boolean complete) {
427                         this.complete = complete;
428                 }
429
430                 /**
431                  * {@inheritDoc}
432                  */
433                 @Override
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());
440                         }
441                         if (soneProfile.getMiddleName() != null) {
442                                 soneString.append(' ').append(soneProfile.getMiddleName());
443                         }
444                         if (soneProfile.getLastName() != null) {
445                                 soneString.append(' ').append(soneProfile.getLastName());
446                         }
447                         if (complete) {
448                                 for (Field field : soneProfile.getFields()) {
449                                         soneString.append(' ').append(field.getValue());
450                                 }
451                         }
452                         return soneString.toString();
453                 }
454
455         }
456
457         /**
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.
461          *
462          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
463          */
464         private class PostStringGenerator implements StringGenerator<Post> {
465
466                 /**
467                  * {@inheritDoc}
468                  */
469                 @Override
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()));
475                         }
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());
479                         }
480                         return postString.toString();
481                 }
482
483         }
484
485         /**
486          * A search phrase.
487          *
488          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
489          */
490         private static class Phrase {
491
492                 /**
493                  * The optionality of a search phrase.
494                  *
495                  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
496                  *         Roden</a>
497                  */
498                 public enum Optionality {
499
500                         /** The phrase is optional. */
501                         OPTIONAL,
502
503                         /** The phrase is required. */
504                         REQUIRED,
505
506                         /** The phrase is forbidden. */
507                         FORBIDDEN
508
509                 }
510
511                 /** The phrase to search for. */
512                 private final String phrase;
513
514                 /** The optionality of the phrase. */
515                 private final Optionality optionality;
516
517                 /**
518                  * Creates a new phrase.
519                  *
520                  * @param phrase
521                  *            The phrase to search for
522                  * @param optionality
523                  *            The optionality of the phrase
524                  */
525                 public Phrase(String phrase, Optionality optionality) {
526                         this.optionality = optionality;
527                         this.phrase = phrase;
528                 }
529
530                 /**
531                  * Returns the phrase to search for.
532                  *
533                  * @return The phrase to search for
534                  */
535                 public String getPhrase() {
536                         return phrase;
537                 }
538
539                 /**
540                  * Returns the optionality of the phrase.
541                  *
542                  * @return The optionality of the phrase
543                  */
544                 public Optionality getOptionality() {
545                         return optionality;
546                 }
547
548                 //
549                 // OBJECT METHODS
550                 //
551
552                 /**
553                  * {@inheritDoc}
554                  */
555                 @Override
556                 public int hashCode() {
557                         return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
558                 }
559
560                 /**
561                  * {@inheritDoc}
562                  */
563                 @Override
564                 public boolean equals(Object object) {
565                         if (!(object instanceof Phrase)) {
566                                 return false;
567                         }
568                         Phrase phrase = (Phrase) object;
569                         return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
570                 }
571
572         }
573
574         /**
575          * A hit consists of a searched object and the score it got for the phrases
576          * of the search.
577          *
578          * @see SearchPage#calculateScore(List, String)
579          * @param <T>
580          *            The type of the searched object
581          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
582          */
583         private static class Hit<T> {
584
585                 /** Filter for {@link Hit}s with a score of more than 0. */
586                 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
587
588                         @Override
589                         public boolean apply(Hit<?> hit) {
590                                 return hit.getScore() > 0;
591                         }
592
593                 };
594
595                 /** Comparator that sorts {@link Hit}s descending by score. */
596                 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
597
598                         @Override
599                         public int compare(Hit<?> leftHit, Hit<?> rightHit) {
600                                 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
601                         }
602
603                 };
604
605                 /** The object that was searched. */
606                 private final T object;
607
608                 /** The score of the object. */
609                 private final double score;
610
611                 /**
612                  * Creates a new hit.
613                  *
614                  * @param object
615                  *            The object that was searched
616                  * @param score
617                  *            The score of the object
618                  */
619                 public Hit(T object, double score) {
620                         this.object = object;
621                         this.score = score;
622                 }
623
624                 /**
625                  * Returns the object that was searched.
626                  *
627                  * @return The object that was searched
628                  */
629                 public T getObject() {
630                         return object;
631                 }
632
633                 /**
634                  * Returns the score of the object.
635                  *
636                  * @return The score of the object
637                  */
638                 public double getScore() {
639                         return score;
640                 }
641
642         }
643
644         /**
645          * Extracts the object from a {@link Hit}.
646          *
647          * @param <T>
648          *            The type of the object to extract
649          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
650          */
651         public static class HitMapper<T> implements Mapper<Hit<T>, T> {
652
653                 /**
654                  * {@inheritDoc}
655                  */
656                 @Override
657                 public T map(Hit<T> input) {
658                         return input.getObject();
659                 }
660
661         }
662
663 }