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