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