Change visibility of hit mapper.
[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.concurrent.TimeUnit;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Profile;
34 import net.pterodactylus.sone.data.Profile.Field;
35 import net.pterodactylus.sone.data.Reply;
36 import net.pterodactylus.sone.data.Sone;
37 import net.pterodactylus.sone.web.page.FreenetRequest;
38 import net.pterodactylus.util.collection.Pagination;
39 import net.pterodactylus.util.logging.Logging;
40 import net.pterodactylus.util.number.Numbers;
41 import net.pterodactylus.util.template.Template;
42 import net.pterodactylus.util.template.TemplateContext;
43 import net.pterodactylus.util.text.StringEscaper;
44 import net.pterodactylus.util.text.TextException;
45
46 import com.google.common.base.Function;
47 import com.google.common.base.Predicate;
48 import com.google.common.cache.CacheBuilder;
49 import com.google.common.cache.CacheLoader;
50 import com.google.common.cache.LoadingCache;
51 import com.google.common.collect.Collections2;
52 import com.google.common.collect.FluentIterable;
53 import com.google.common.collect.Ordering;
54
55 /**
56  * This page lets the user search for posts and replies that contain certain
57  * words.
58  *
59  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60  */
61 public class SearchPage extends SoneTemplatePage {
62
63         /** The logger. */
64         private static final Logger logger = Logging.getLogger(SearchPage.class);
65
66         /** Short-term cache. */
67         private final LoadingCache<List<Phrase>, Set<Hit<Post>>> hitCache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build(new CacheLoader<List<Phrase>, Set<Hit<Post>>>() {
68
69                 @Override
70                 @SuppressWarnings("synthetic-access")
71                 public Set<Hit<Post>> load(List<Phrase> phrases) {
72                         Set<Post> posts = new HashSet<Post>();
73                         for (Sone sone : webInterface.getCore().getSones()) {
74                                 posts.addAll(sone.getPosts());
75                         }
76                         return getHits(Collections2.filter(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
77                 }
78         });
79
80         /**
81          * Creates a new search page.
82          *
83          * @param template
84          *            The template to render
85          * @param webInterface
86          *            The Sone web interface
87          */
88         public SearchPage(Template template, WebInterface webInterface) {
89                 super("search.html", template, "Page.Search.Title", webInterface);
90         }
91
92         //
93         // SONETEMPLATEPAGE METHODS
94         //
95
96         /**
97          * {@inheritDoc}
98          */
99         @Override
100         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
101                 super.processTemplate(request, templateContext);
102                 String query = request.getHttpRequest().getParam("query").trim();
103                 if (query.length() == 0) {
104                         throw new RedirectException("index.html");
105                 }
106
107                 List<Phrase> phrases = parseSearchPhrases(query);
108                 if (phrases.isEmpty()) {
109                         throw new RedirectException("index.html");
110                 }
111
112                 /* check for a couple of shortcuts. */
113                 if (phrases.size() == 1) {
114                         String phrase = phrases.get(0).getPhrase();
115
116                         /* is it a Sone ID? */
117                         redirectIfNotNull(getSoneId(phrase), "viewSone.html?sone=");
118
119                         /* is it a post ID? */
120                         redirectIfNotNull(getPostId(phrase), "viewPost.html?post=");
121
122                         /* is it a reply ID? show the post. */
123                         redirectIfNotNull(getReplyPostId(phrase), "viewPost.html?post=");
124
125                         /* is it an album ID? */
126                         redirectIfNotNull(getAlbumId(phrase), "imageBrowser.html?album=");
127
128                         /* is it an image ID? */
129                         redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
130                 }
131
132                 Set<Sone> sones = webInterface.getCore().getSones();
133                 Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
134
135                 Collection<Hit<Post>> postHits = hitCache.getUnchecked(phrases);
136
137                 /* now filter. */
138                 soneHits = Collections2.filter(soneHits, Hit.POSITIVE_FILTER);
139                 postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
140
141                 /* now sort. */
142                 List<Hit<Sone>> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits);
143                 List<Hit<Post>> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits);
144
145                 /* extract Sones and posts. */
146                 List<Sone> resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper<Sone>()).toList();
147                 List<Post> resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper<Post>()).toList();
148
149                 /* pagination. */
150                 Pagination<Sone> sonePagination = new Pagination<Sone>(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0));
151                 Pagination<Post> postPagination = new Pagination<Post>(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0));
152
153                 templateContext.set("sonePagination", sonePagination);
154                 templateContext.set("soneHits", sonePagination.getItems());
155                 templateContext.set("postPagination", postPagination);
156                 templateContext.set("postHits", postPagination.getItems());
157         }
158
159         //
160         // PRIVATE METHODS
161         //
162
163         /**
164          * Collects hit information for the given objects. The objects are converted
165          * to a {@link String} using the given {@link StringGenerator}, and the
166          * {@link #calculateScore(List, String) calculated score} is stored together
167          * with the object in a {@link Hit}, and all resulting {@link Hit}s are then
168          * returned.
169          *
170          * @param <T>
171          *            The type of the objects
172          * @param objects
173          *            The objects to search over
174          * @param phrases
175          *            The phrases to search for
176          * @param stringGenerator
177          *            The string generator for the objects
178          * @return The hits for the given phrases
179          */
180         private static <T> Set<Hit<T>> getHits(Collection<T> objects, List<Phrase> phrases, StringGenerator<T> stringGenerator) {
181                 Set<Hit<T>> hits = new HashSet<Hit<T>>();
182                 for (T object : objects) {
183                         String objectString = stringGenerator.generateString(object);
184                         double score = calculateScore(phrases, objectString);
185                         hits.add(new Hit<T>(object, score));
186                 }
187                 return hits;
188         }
189
190         /**
191          * Parses the given query into search phrases. The query is split on
192          * whitespace while allowing to group words using single or double quotes.
193          * Isolated phrases starting with a “+” are
194          * {@link Phrase.Optionality#REQUIRED}, phrases with a “-” are
195          * {@link Phrase.Optionality#FORBIDDEN}.
196          *
197          * @param query
198          *            The query to parse
199          * @return The parsed phrases
200          */
201         private static List<Phrase> parseSearchPhrases(String query) {
202                 List<String> parsedPhrases = null;
203                 try {
204                         parsedPhrases = StringEscaper.parseLine(query);
205                 } catch (TextException te1) {
206                         /* invalid query. */
207                         return Collections.emptyList();
208                 }
209
210                 List<Phrase> phrases = new ArrayList<Phrase>();
211                 for (String phrase : parsedPhrases) {
212                         if (phrase.startsWith("+")) {
213                                 if (phrase.length() > 1) {
214                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.REQUIRED));
215                                 } else {
216                                         phrases.add(new Phrase("+", Phrase.Optionality.OPTIONAL));
217                                 }
218                         } else if (phrase.startsWith("-")) {
219                                 if (phrase.length() > 1) {
220                                         phrases.add(new Phrase(phrase.substring(1), Phrase.Optionality.FORBIDDEN));
221                                 } else {
222                                         phrases.add(new Phrase("-", Phrase.Optionality.OPTIONAL));
223                                 }
224                         } else {
225                                 phrases.add(new Phrase(phrase, Phrase.Optionality.OPTIONAL));
226                         }
227                 }
228                 return phrases;
229         }
230
231         /**
232          * Calculates the score for the given expression when using the given
233          * phrases.
234          *
235          * @param phrases
236          *            The phrases to search for
237          * @param expression
238          *            The expression to search
239          * @return The score of the expression
240          */
241         private static double calculateScore(List<Phrase> phrases, String expression) {
242                 logger.log(Level.FINEST, String.format("Calculating Score for “%s”…", expression));
243                 double optionalHits = 0;
244                 double requiredHits = 0;
245                 int forbiddenHits = 0;
246                 int requiredPhrases = 0;
247                 for (Phrase phrase : phrases) {
248                         String phraseString = phrase.getPhrase().toLowerCase();
249                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
250                                 ++requiredPhrases;
251                         }
252                         int matches = 0;
253                         int index = 0;
254                         double score = 0;
255                         while (index < expression.length()) {
256                                 int position = expression.toLowerCase().indexOf(phraseString, index);
257                                 if (position == -1) {
258                                         break;
259                                 }
260                                 score += Math.pow(1 - position / (double) expression.length(), 2);
261                                 index = position + phraseString.length();
262                                 logger.log(Level.FINEST, String.format("Got hit at position %d.", position));
263                                 ++matches;
264                         }
265                         logger.log(Level.FINEST, String.format("Score: %f", score));
266                         if (matches == 0) {
267                                 continue;
268                         }
269                         if (phrase.getOptionality() == Phrase.Optionality.REQUIRED) {
270                                 requiredHits += score;
271                         }
272                         if (phrase.getOptionality() == Phrase.Optionality.OPTIONAL) {
273                                 optionalHits += score;
274                         }
275                         if (phrase.getOptionality() == Phrase.Optionality.FORBIDDEN) {
276                                 forbiddenHits += matches;
277                         }
278                 }
279                 return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2);
280         }
281
282         /**
283          * Throws a
284          * {@link net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException}
285          * if the given object is not {@code null}, appending the object to the
286          * given target URL.
287          *
288          * @param object
289          *            The object on which to redirect
290          * @param target
291          *            The target of the redirect
292          * @throws RedirectException
293          *             if {@code object} is not {@code null}
294          */
295         private static void redirectIfNotNull(String object, String target) throws RedirectException {
296                 if (object != null) {
297                         throw new RedirectException(target + object);
298                 }
299         }
300
301         /**
302          * If the given phrase contains a Sone ID (optionally prefixed by
303          * “sone://”), returns said Sone ID, otherwise return {@code null}.
304          *
305          * @param phrase
306          *            The phrase that maybe is a Sone ID
307          * @return The Sone ID, or {@code null}
308          */
309         private String getSoneId(String phrase) {
310                 String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase;
311                 return (webInterface.getCore().getSone(soneId, false) != null) ? soneId : null;
312         }
313
314         /**
315          * If the given phrase contains a post ID (optionally prefixed by
316          * “post://”), returns said post ID, otherwise return {@code null}.
317          *
318          * @param phrase
319          *            The phrase that maybe is a post ID
320          * @return The post ID, or {@code null}
321          */
322         private String getPostId(String phrase) {
323                 String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase;
324                 return (webInterface.getCore().getPost(postId, false) != null) ? postId : null;
325         }
326
327         /**
328          * If the given phrase contains a reply ID (optionally prefixed by
329          * “reply://”), returns the ID of the post the reply belongs to, otherwise
330          * return {@code null}.
331          *
332          * @param phrase
333          *            The phrase that maybe is a reply ID
334          * @return The reply’s post ID, or {@code null}
335          */
336         private String getReplyPostId(String phrase) {
337                 String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase;
338                 return (webInterface.getCore().getPostReply(replyId, false) != null) ? webInterface.getCore().getPostReply(replyId, false).getPost().getId() : null;
339         }
340
341         /**
342          * If the given phrase contains an album ID (optionally prefixed by
343          * “album://”), returns said album ID, otherwise return {@code null}.
344          *
345          * @param phrase
346          *            The phrase that maybe is an album ID
347          * @return The album ID, or {@code null}
348          */
349         private String getAlbumId(String phrase) {
350                 String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
351                 return (webInterface.getCore().getAlbum(albumId, false) != null) ? albumId : null;
352         }
353
354         /**
355          * If the given phrase contains an image ID (optionally prefixed by
356          * “image://”), returns said image ID, otherwise return {@code null}.
357          *
358          * @param phrase
359          *            The phrase that maybe is an image ID
360          * @return The image ID, or {@code null}
361          */
362         private String getImageId(String phrase) {
363                 String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase;
364                 return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null;
365         }
366
367         /**
368          * Converts a given object into a {@link String}.
369          *
370          * @param <T>
371          *            The type of the objects
372          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
373          */
374         private static interface StringGenerator<T> {
375
376                 /**
377                  * Generates a {@link String} for the given object.
378                  *
379                  * @param object
380                  *            The object to generate the {@link String} for
381                  * @return The generated {@link String}
382                  */
383                 public String generateString(T object);
384
385         }
386
387         /**
388          * Generates a {@link String} from a {@link Sone}, concatenating the name of
389          * the Sone and all {@link Profile} {@link Field} values.
390          *
391          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
392          */
393         private static class SoneStringGenerator implements StringGenerator<Sone> {
394
395                 /** A static instance of a complete Sone string generator. */
396                 public static final SoneStringGenerator COMPLETE_GENERATOR = new SoneStringGenerator(true);
397
398                 /**
399                  * A static instance of a Sone string generator that will only use the
400                  * name of the Sone.
401                  */
402                 public static final SoneStringGenerator NAME_GENERATOR = new SoneStringGenerator(false);
403
404                 /** Whether to generate a string from all data of a Sone. */
405                 private final boolean complete;
406
407                 /**
408                  * Creates a new Sone string generator.
409                  *
410                  * @param complete
411                  *            {@code true} to use the profile’s fields, {@code false} to
412                  *            not to use the profile‘s fields
413                  */
414                 private SoneStringGenerator(boolean complete) {
415                         this.complete = complete;
416                 }
417
418                 /**
419                  * {@inheritDoc}
420                  */
421                 @Override
422                 public String generateString(Sone sone) {
423                         StringBuilder soneString = new StringBuilder();
424                         soneString.append(sone.getName());
425                         Profile soneProfile = sone.getProfile();
426                         if (soneProfile.getFirstName() != null) {
427                                 soneString.append(' ').append(soneProfile.getFirstName());
428                         }
429                         if (soneProfile.getMiddleName() != null) {
430                                 soneString.append(' ').append(soneProfile.getMiddleName());
431                         }
432                         if (soneProfile.getLastName() != null) {
433                                 soneString.append(' ').append(soneProfile.getLastName());
434                         }
435                         if (complete) {
436                                 for (Field field : soneProfile.getFields()) {
437                                         soneString.append(' ').append(field.getValue());
438                                 }
439                         }
440                         return soneString.toString();
441                 }
442
443         }
444
445         /**
446          * Generates a {@link String} from a {@link Post}, concatenating the text of
447          * the post, the text of all {@link Reply}s, and the name of all
448          * {@link Sone}s that have replied.
449          *
450          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
451          */
452         private class PostStringGenerator implements StringGenerator<Post> {
453
454                 /**
455                  * {@inheritDoc}
456                  */
457                 @Override
458                 public String generateString(Post post) {
459                         StringBuilder postString = new StringBuilder();
460                         postString.append(post.getText());
461                         if (post.getRecipient() != null) {
462                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient()));
463                         }
464                         for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) {
465                                 postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
466                                 postString.append(' ').append(reply.getText());
467                         }
468                         return postString.toString();
469                 }
470
471         }
472
473         /**
474          * A search phrase.
475          *
476          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
477          */
478         private static class Phrase {
479
480                 /**
481                  * The optionality of a search phrase.
482                  *
483                  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’
484                  *         Roden</a>
485                  */
486                 public enum Optionality {
487
488                         /** The phrase is optional. */
489                         OPTIONAL,
490
491                         /** The phrase is required. */
492                         REQUIRED,
493
494                         /** The phrase is forbidden. */
495                         FORBIDDEN
496
497                 }
498
499                 /** The phrase to search for. */
500                 private final String phrase;
501
502                 /** The optionality of the phrase. */
503                 private final Optionality optionality;
504
505                 /**
506                  * Creates a new phrase.
507                  *
508                  * @param phrase
509                  *            The phrase to search for
510                  * @param optionality
511                  *            The optionality of the phrase
512                  */
513                 public Phrase(String phrase, Optionality optionality) {
514                         this.optionality = optionality;
515                         this.phrase = phrase;
516                 }
517
518                 /**
519                  * Returns the phrase to search for.
520                  *
521                  * @return The phrase to search for
522                  */
523                 public String getPhrase() {
524                         return phrase;
525                 }
526
527                 /**
528                  * Returns the optionality of the phrase.
529                  *
530                  * @return The optionality of the phrase
531                  */
532                 public Optionality getOptionality() {
533                         return optionality;
534                 }
535
536                 //
537                 // OBJECT METHODS
538                 //
539
540                 /**
541                  * {@inheritDoc}
542                  */
543                 @Override
544                 public int hashCode() {
545                         return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0));
546                 }
547
548                 /**
549                  * {@inheritDoc}
550                  */
551                 @Override
552                 public boolean equals(Object object) {
553                         if (!(object instanceof Phrase)) {
554                                 return false;
555                         }
556                         Phrase phrase = (Phrase) object;
557                         return (this.optionality == phrase.optionality) && this.phrase.equals(phrase.phrase);
558                 }
559
560         }
561
562         /**
563          * A hit consists of a searched object and the score it got for the phrases
564          * of the search.
565          *
566          * @see SearchPage#calculateScore(List, String)
567          * @param <T>
568          *            The type of the searched object
569          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
570          */
571         private static class Hit<T> {
572
573                 /** Filter for {@link Hit}s with a score of more than 0. */
574                 public static final Predicate<Hit<?>> POSITIVE_FILTER = new Predicate<Hit<?>>() {
575
576                         @Override
577                         public boolean apply(Hit<?> hit) {
578                                 return hit.getScore() > 0;
579                         }
580
581                 };
582
583                 /** Comparator that sorts {@link Hit}s descending by score. */
584                 public static final Comparator<Hit<?>> DESCENDING_COMPARATOR = new Comparator<Hit<?>>() {
585
586                         @Override
587                         public int compare(Hit<?> leftHit, Hit<?> rightHit) {
588                                 return (rightHit.getScore() < leftHit.getScore()) ? -1 : ((rightHit.getScore() > leftHit.getScore()) ? 1 : 0);
589                         }
590
591                 };
592
593                 /** The object that was searched. */
594                 private final T object;
595
596                 /** The score of the object. */
597                 private final double score;
598
599                 /**
600                  * Creates a new hit.
601                  *
602                  * @param object
603                  *            The object that was searched
604                  * @param score
605                  *            The score of the object
606                  */
607                 public Hit(T object, double score) {
608                         this.object = object;
609                         this.score = score;
610                 }
611
612                 /**
613                  * Returns the object that was searched.
614                  *
615                  * @return The object that was searched
616                  */
617                 public T getObject() {
618                         return object;
619                 }
620
621                 /**
622                  * Returns the score of the object.
623                  *
624                  * @return The score of the object
625                  */
626                 public double getScore() {
627                         return score;
628                 }
629
630         }
631
632         /**
633          * Extracts the object from a {@link Hit}.
634          *
635          * @param <T>
636          *            The type of the object to extract
637          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
638          */
639         private static class HitMapper<T> implements Function<Hit<T>, T> {
640
641                 /**
642                  * {@inheritDoc}
643                  */
644                 @Override
645                 public T apply(Hit<T> input) {
646                         return input.getObject();
647                 }
648
649         }
650
651 }