public virtual async Task <ActionResult> ListPackages(string q, int page = 1) { if (page < 1) { page = 1; } q = (q ?? "").Trim(); var searchFilter = SearchAdaptor.GetSearchFilter(q, page, sortOrder: null, context: SearchFilter.UISearchContext); var results = await _searchService.Search(searchFilter); int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, results.IndexTimestampUtc, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return(View(viewModel)); }
public virtual ActionResult ListPackages(string q, string sortOrder = Constants.PopularitySortOrder, int page = 1) { if (page < 1) { page = 1; } IQueryable <Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true); q = (q ?? "").Trim(); if (GetIdentity().IsAuthenticated) { // Only show listed packages. For unlisted packages, only show them if the owner is viewing it. packageVersions = packageVersions.Where(p => p.Listed || p.PackageRegistration.Owners.Any(owner => owner.Username == User.Identity.Name)); } else { packageVersions = packageVersions.Where(p => p.Listed); } int totalHits; if (!String.IsNullOrEmpty(q)) { if (sortOrder.Equals(Constants.RelevanceSortOrder, StringComparison.OrdinalIgnoreCase)) { packageVersions = searchSvc.SearchWithRelevance(packageVersions, q, take: page * Constants.DefaultPackageListPageSize, totalHits: out totalHits); if (page == 1 && !packageVersions.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } } else { packageVersions = searchSvc.Search(packageVersions, q) .SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } } else { packageVersions = packageVersions.SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } var viewModel = new PackageListViewModel(packageVersions, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return(View(viewModel)); }
public virtual async Task <ActionResult> ListPackages(string curatedFeedName, string q, int page = 1, bool prerel = true) { if (page < 1) { page = 1; } q = (q ?? string.Empty).Trim(); var searchFilter = SearchAdaptor.GetSearchFilter( q, page, includePrerelease: prerel, sortOrder: null, context: SearchFilter.UISearchContext, semVerLevel: SemVerLevelKey.SemVerLevel2); searchFilter.CuratedFeed = CuratedFeedService.GetFeedByName(curatedFeedName, includePackages: false); if (searchFilter.CuratedFeed == null) { return(HttpNotFound()); } SearchResults results = await SearchService.Search(searchFilter); int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, GetCurrentUser(), results.IndexTimestampUtc, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url, curatedFeedName, includePrerelease: prerel); ViewBag.SearchTerm = q; return(View("ListPackages", viewModel)); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1) { if (page < 1) { page = 1; } IQueryable <Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true); q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } int totalHits; if (!String.IsNullOrEmpty(q)) { var searchFilter = GetSearchFilter(q, sortOrder, page); packageVersions = searchSvc.Search(packageVersions, searchFilter, out totalHits); if (page == 1 && !packageVersions.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } } else { packageVersions = packageVersions.SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } var viewModel = new PackageListViewModel(packageVersions, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return(View(viewModel)); }
public virtual ActionResult ListPackages(string curatedFeedName, string q, string sortOrder = null, int page = 1, bool prerelease = false) { if (page < 1) { page = 1; } q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } var searchFilter = SearchAdaptor.GetSearchFilter(q, sortOrder, page, prerelease); searchFilter.CuratedFeedKey = CuratedFeedService.GetKey(curatedFeedName); if (searchFilter.CuratedFeedKey == 0) { return(HttpNotFound()); } int totalHits; IQueryable <Package> packageVersions = SearchService.Search(searchFilter, out totalHits); if (page == 1 && !packageVersions.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( packageVersions, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url, prerelease); ViewBag.SearchTerm = q; return(View("~/Views/Packages/ListPackages.cshtml", viewModel)); }
public virtual ActionResult ListPackages(string q, string sortOrder = Const.DefaultPackageListSortOrder, int page = 1) { if (page < 1) { page = 1; } IQueryable <Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true); q = (q ?? "").Trim(); if (!String.IsNullOrEmpty(q)) { packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true).Search(q); } if (GetIdentity().IsAuthenticated) { // Only show listed packages. For unlisted packages, only show them if the owner is viewing it. packageVersions = packageVersions.Where(p => p.Listed || p.PackageRegistration.Owners.Any(owner => owner.Username == User.Identity.Name)); } else { packageVersions = packageVersions.Where(p => p.Listed); } var viewModel = new PackageListViewModel(packageVersions, q, sortOrder, page - 1, Const.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return(View(viewModel)); }
public virtual async Task<ActionResult> ListPackages(string q, int page = 1) { if (page < 1) { page = 1; } q = (q ?? string.Empty).Trim(); SearchResults results; // fetch most common query from cache to relieve load on the search service if (string.IsNullOrEmpty(q) && page == 1) { var cachedResults = HttpContext.Cache.Get("DefaultSearchResults"); if (cachedResults == null) { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); // note: this is a per instance cache HttpContext.Cache.Add( "DefaultSearchResults", results, null, DateTime.UtcNow.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } else { // default for /packages view results = (SearchResults)cachedResults; } } else { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); } int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, results.IndexTimestampUtc, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1) { if (page < 1) { page = 1; } IQueryable<Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true); q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } int totalHits; if (!String.IsNullOrEmpty(q)) { var searchFilter = GetSearchFilter(q, sortOrder, page); packageVersions = searchSvc.Search(packageVersions, searchFilter, out totalHits); if (page == 1 && !packageVersions.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } } else { packageVersions = packageVersions.SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } var viewModel = new PackageListViewModel(packageVersions, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1, bool prerelease = true, bool moderatorQueue = false) { if (page < 1) page = 1; IQueryable<Package> packageVersions = packageSvc.GetPackagesForListing(prerelease); IEnumerable<Package> packagesToShow = new List<Package>(); if (moderatorQueue) { var unknownStatus = PackageStatusType.Unknown.GetDescriptionOrValue(); //This is going to be fun. Unknown status ones would be listed, but sometimes a few might slip through the cracks if a maintainer unlists a package. // A user can just email us to catch those though. packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase == unknownStatus || p.StatusForDatabase == null); } q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } int totalHits = 0; int updatedPackagesCount = 0; int unreviewedPackagesCount = 0; int waitingPackagesCount = 0; var searchFilter = GetSearchFilter(q, sortOrder, page, prerelease); if (moderatorQueue) { var submittedPackages = packageSvc.GetSubmittedPackages().ToList(); var updatedStatus = PackageSubmittedStatusType.Updated.ToString(); var readyStatus = PackageSubmittedStatusType.Ready.ToString(); var waitingStatus = PackageSubmittedStatusType.Waiting.ToString(); //var resubmittedPackages = submittedPackages.Where(p => p.ReviewedDate.HasValue && p.Published > p.ReviewedDate).OrderBy(p => p.Published).ToList(); var resubmittedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == updatedStatus).OrderBy(p => p.Published).ToList(); updatedPackagesCount = resubmittedPackages.Count; var unreviewedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == readyStatus || p.SubmittedStatusForDatabase == null).OrderBy(p => p.Published).ToList(); unreviewedPackagesCount = unreviewedPackages.Count; //var waitingForMaintainerPackages = submittedPackages.Where(p => p.ReviewedDate >= p.Published).OrderByDescending(p => p.ReviewedDate).ToList(); var waitingForMaintainerPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == waitingStatus).OrderByDescending(p => p.ReviewedDate).ToList(); waitingPackagesCount = waitingForMaintainerPackages.Count; packagesToShow = resubmittedPackages.Union(unreviewedPackages).Union(waitingForMaintainerPackages); totalHits = packagesToShow.Count() + packageVersions.Count(); if ((searchFilter.Skip + searchFilter.Take) >= packagesToShow.Count()) packagesToShow = packagesToShow.Union(packageVersions.OrderByDescending(pv => pv.DownloadCount)); packagesToShow = packagesToShow.Skip(searchFilter.Skip).Take(searchFilter.Take); } else packagesToShow = searchSvc.Search(packageVersions, searchFilter, out totalHits).ToList(); if (page == 1 && !packagesToShow.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( packagesToShow, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url, prerelease, moderatorQueue, updatedPackagesCount, unreviewedPackagesCount, waitingPackagesCount); ViewBag.SearchTerm = q; return View("~/Views/Packages/ListPackages.cshtml", viewModel); }
public virtual async Task <ActionResult> ListPackages(string q, int page = 1) { if (page < 1) { page = 1; } q = (q ?? string.Empty).Trim(); // We are not going to SQL here anyway, but our request logs do show some attempts to SQL injection. // The below code just fails out those requests early. if (q.ToLowerInvariant().Contains("char(") || q.ToLowerInvariant().Contains("union select") || q.ToLowerInvariant().Contains("/*") || q.ToLowerInvariant().Contains("--")) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } SearchResults results; // fetch most common query from cache to relieve load on the search service if (string.IsNullOrEmpty(q) && page == 1) { var cachedResults = HttpContext.Cache.Get("DefaultSearchResults"); if (cachedResults == null) { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); // note: this is a per instance cache HttpContext.Cache.Add( "DefaultSearchResults", results, null, DateTime.UtcNow.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } else { // default for /packages view results = (SearchResults)cachedResults; } } else { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); } int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, results.IndexTimestampUtc, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return(View(viewModel)); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1) { if (page < 1) { page = 1; } IQueryable<Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true); q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } if (GetIdentity().IsAuthenticated) { // Only show listed packages. For unlisted packages, only show them if the owner is viewing it. packageVersions = packageVersions.Where(p => p.Listed || p.PackageRegistration.Owners.Any(owner => owner.Username == User.Identity.Name)); } else { packageVersions = packageVersions.Where(p => p.Listed); } int totalHits; if (!String.IsNullOrEmpty(q)) { if (sortOrder.Equals(Constants.RelevanceSortOrder, StringComparison.OrdinalIgnoreCase)) { packageVersions = searchSvc.SearchWithRelevance(packageVersions, q, take: page * Constants.DefaultPackageListPageSize, totalHits: out totalHits); if (page == 1 && !packageVersions.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } } else { packageVersions = searchSvc.Search(packageVersions, q) .SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } } else { packageVersions = packageVersions.SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } var viewModel = new PackageListViewModel(packageVersions, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual async Task<ActionResult> ListPackages(string q, int page = 1) { if (page < 1) { page = 1; } q = (q ?? "").Trim(); var searchFilter = SearchAdaptor.GetSearchFilter(q, page, sortOrder: null, context: SearchFilter.UISearchContext); var results = await _searchService.Search(searchFilter); int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, results.IndexTimestampUtc, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1, bool prerelease = true, bool moderatorQueue = false) { if (page < 1) { page = 1; } IQueryable <Package> packageVersions = packageSvc.GetPackagesForListing(prerelease); IEnumerable <Package> packagesToShow = new List <Package>(); if (moderatorQueue) { var unknownStatus = PackageStatusType.Unknown.GetDescriptionOrValue(); //This is going to be fun. Unknown status ones would be listed, but sometimes a few might slip through the cracks if a maintainer unlists a package. // A user can just email us to catch those though. packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase == unknownStatus || p.StatusForDatabase == null); } q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } int totalHits = 0; int updatedPackagesCount = 0; int unreviewedPackagesCount = 0; int waitingPackagesCount = 0; var searchFilter = GetSearchFilter(q, sortOrder, page, prerelease); if (moderatorQueue) { var submittedPackages = packageSvc.GetSubmittedPackages().ToList(); var updatedStatus = PackageSubmittedStatusType.Updated.ToString(); var readyStatus = PackageSubmittedStatusType.Ready.ToString(); var waitingStatus = PackageSubmittedStatusType.Waiting.ToString(); //var resubmittedPackages = submittedPackages.Where(p => p.ReviewedDate.HasValue && p.Published > p.ReviewedDate).OrderBy(p => p.Published).ToList(); var resubmittedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == updatedStatus).OrderBy(p => p.Published).ToList(); updatedPackagesCount = resubmittedPackages.Count; var unreviewedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == readyStatus || p.SubmittedStatusForDatabase == null).OrderBy(p => p.Published).ToList(); unreviewedPackagesCount = unreviewedPackages.Count; //var waitingForMaintainerPackages = submittedPackages.Where(p => p.ReviewedDate >= p.Published).OrderByDescending(p => p.ReviewedDate).ToList(); var waitingForMaintainerPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == waitingStatus).OrderByDescending(p => p.ReviewedDate).ToList(); waitingPackagesCount = waitingForMaintainerPackages.Count; packagesToShow = resubmittedPackages.Union(unreviewedPackages).Union(waitingForMaintainerPackages); totalHits = packagesToShow.Count() + packageVersions.Count(); if ((searchFilter.Skip + searchFilter.Take) >= packagesToShow.Count()) { packagesToShow = packagesToShow.Union(packageVersions.OrderByDescending(pv => pv.DownloadCount)); } packagesToShow = packagesToShow.Skip(searchFilter.Skip).Take(searchFilter.Take); } else { packagesToShow = searchSvc.Search(packageVersions, searchFilter, out totalHits).ToList(); } if (page == 1 && !packagesToShow.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( packagesToShow, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url, prerelease, moderatorQueue, updatedPackagesCount, unreviewedPackagesCount, waitingPackagesCount); ViewBag.SearchTerm = q; return(View("~/Views/Packages/ListPackages.cshtml", viewModel)); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1, bool prerelease = false, bool moderatorQueue = false) { if (page < 1) page = 1; q = (q ?? string.Empty).Trim(); IQueryable<Package> packageVersions = packageSvc.GetPackagesForListing(prerelease); IEnumerable<Package> packagesToShow = new List<Package>(); if (moderatorQueue) { var unknownStatus = PackageStatusType.Unknown.GetDescriptionOrValue(); //This is going to be fun. Unknown status ones would be listed, but sometimes a few might slip through the cracks if a maintainer unlists a package. // A user can just email us to catch those though. packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase == unknownStatus || p.StatusForDatabase == null); } q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } int totalHits = 0; int updatedPackagesCount = 0; int respondedPackagesCount = 0; int unreviewedPackagesCount = 0; int waitingPackagesCount = 0; var searchFilter = GetSearchFilter(q, sortOrder, page, prerelease); if (moderatorQueue) { var submittedPackages = packageSvc.GetSubmittedPackages().ToList(); var updatedStatus = PackageSubmittedStatusType.Updated.ToString(); var respondedStatus = PackageSubmittedStatusType.Responded.ToString(); var readyStatus = PackageSubmittedStatusType.Ready.ToString(); var pendingStatus = PackageSubmittedStatusType.Pending.ToString(); var waitingStatus = PackageSubmittedStatusType.Waiting.ToString(); //var resubmittedPackages = submittedPackages.Where(p => p.ReviewedDate.HasValue && p.Published > p.ReviewedDate).OrderBy(p => p.Published).ToList(); var resubmittedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == updatedStatus).OrderBy(p => p.Published).ToList(); updatedPackagesCount = resubmittedPackages.Count; var respondedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == respondedStatus).OrderBy(p => p.LastUpdated).ToList(); respondedPackagesCount = respondedPackages.Count; var unreviewedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == readyStatus).OrderBy(p => p.Published).ToList(); unreviewedPackagesCount = unreviewedPackages.Count; var pendingAutoReviewPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == pendingStatus || p.SubmittedStatusForDatabase == null).OrderBy(p => p.Published).ToList(); unreviewedPackagesCount += pendingAutoReviewPackages.Count; //var waitingForMaintainerPackages = submittedPackages.Where(p => p.ReviewedDate >= p.Published).OrderByDescending(p => p.ReviewedDate).ToList(); var waitingForMaintainerPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == waitingStatus).OrderByDescending(p => p.ReviewedDate).ToList(); waitingPackagesCount = waitingForMaintainerPackages.Count; packagesToShow = resubmittedPackages.Union(respondedPackages).Union(unreviewedPackages).Union(pendingAutoReviewPackages).Union(waitingForMaintainerPackages); if (!string.IsNullOrWhiteSpace(q)) { packagesToShow = packagesToShow.AsQueryable().Search(q).ToList(); } switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; default: //do not change the search order break; } totalHits = packagesToShow.Count() + packageVersions.Count(); if ((searchFilter.Skip + searchFilter.Take) >= packagesToShow.Count() & string.IsNullOrWhiteSpace(q)) packagesToShow = packagesToShow.Union(packageVersions.OrderByDescending(pv => pv.PackageRegistration.DownloadCount).ToList()); packagesToShow = packagesToShow.Skip(searchFilter.Skip).Take(searchFilter.Take); } else { SearchResults results; // fetch most common query from cache to relieve load on the search service if (string.IsNullOrEmpty(q) && page == 1) { results = Cache.Get( string.Format( "searchResults-{0}-{1}-{2}-{3}-{4}", searchFilter.SearchTerm, searchFilter.IncludePrerelease, searchFilter.Skip, searchFilter.SortProperty.to_string(), searchFilter.SortDirection), DateTime.UtcNow.AddMinutes(10), () => searchSvc.Search(searchFilter)); } else { results = Cache.Get( string.Format( "searchResults-{0}-{1}-{2}-{3}-{4}", searchFilter.SearchTerm, searchFilter.IncludePrerelease, searchFilter.Skip, searchFilter.SortProperty.to_string(), searchFilter.SortDirection), DateTime.UtcNow.AddSeconds(30), () => searchSvc.Search(searchFilter)); } totalHits = results.Hits; packagesToShow = results.Data.ToList(); } if (page == 1 && !packagesToShow.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( packagesToShow, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url, prerelease, moderatorQueue, updatedPackagesCount, unreviewedPackagesCount, waitingPackagesCount, respondedPackagesCount); ViewBag.SearchTerm = q; return View("~/Views/Packages/ListPackages.cshtml", viewModel); }
public virtual ActionResult ListPackages(string q, string sortOrder = "", int page = 1) { if (page < 1) { page = 1; } IQueryable<Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true); q = (q ?? "").Trim(); if (GetIdentity().IsAuthenticated) { // Only show listed packages. For unlisted packages, only show them if the owner is viewing it. packageVersions = packageVersions.Where(p => p.Listed || p.PackageRegistration.Owners.Any(owner => owner.Username == User.Identity.Name)); } else { packageVersions = packageVersions.Where(p => p.Listed); } int totalHits; if (!String.IsNullOrEmpty(q)) { if (String.IsNullOrEmpty(sortOrder)) { packageVersions = searchSvc.SearchWithRelevance(packageVersions, q, take: page * Constants.DefaultPackageListPageSize, totalHits: out totalHits); } else { packageVersions = searchSvc.Search(packageVersions, q) .SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } } else { packageVersions = packageVersions.SortBy(GetSortExpression(sortOrder)); totalHits = packageVersions.Count(); } var viewModel = new PackageListViewModel(packageVersions, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual ActionResult ListPackages(string q, string sortOrder = Const.DefaultPackageListSortOrder, int page = 1) { if (page < 1) { page = 1; } IQueryable<Package> packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true); if (!String.IsNullOrEmpty(q)) { packageVersions = packageSvc.GetLatestPackageVersions(allowPrerelease: true).Search(q); } // Only show listed packages. For unlisted packages, only show them if the owner is viewing it. packageVersions = packageVersions.Where(p => p.Listed || p.PackageRegistration.Owners.Any(owner => owner.Username == User.Identity.Name)); var viewModel = new PackageListViewModel(packageVersions, q, sortOrder, page - 1, Const.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual async Task <ActionResult> ListPackages(string q, int page = 1) { if (page < 1) { page = 1; } q = (q ?? string.Empty).Trim(); SearchResults results; // fetch most common query from cache to relieve load on the search service if (string.IsNullOrEmpty(q) && page == 1) { var cachedResults = HttpContext.Cache.Get("DefaultSearchResults"); if (cachedResults == null) { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); // note: this is a per instance cache HttpContext.Cache.Add( "DefaultSearchResults", results, null, DateTime.UtcNow.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } else { // default for /packages view results = (SearchResults)cachedResults; } } else { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); } int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, results.IndexTimestampUtc, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return(View(viewModel)); }
public virtual async Task<ActionResult> ListPackages(string q, int page = 1) { if (page < 1) { page = 1; } q = (q ?? string.Empty).Trim(); // We are not going to SQL here anyway, but our request logs do show some attempts to SQL injection. // The below code just fails out those requests early. if (q.ToLowerInvariant().Contains("char(") || q.ToLowerInvariant().Contains("union select") || q.ToLowerInvariant().Contains("/*") || q.ToLowerInvariant().Contains("--")) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } SearchResults results; // fetch most common query from cache to relieve load on the search service if (string.IsNullOrEmpty(q) && page == 1) { var cachedResults = HttpContext.Cache.Get("DefaultSearchResults"); if (cachedResults == null) { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); // note: this is a per instance cache HttpContext.Cache.Add( "DefaultSearchResults", results, null, DateTime.UtcNow.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } else { // default for /packages view results = (SearchResults)cachedResults; } } else { var searchFilter = SearchAdaptor.GetSearchFilter(q, page, null, SearchFilter.UISearchContext); results = await _searchService.Search(searchFilter); } int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, results.IndexTimestampUtc, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1, bool prerelease = false) { if (page < 1) { page = 1; } q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } var searchFilter = SearchAdaptor.GetSearchFilter(q, sortOrder, page, prerelease); int totalHits; IQueryable<Package> packageVersions = _searchService.Search(searchFilter, out totalHits); if (page == 1 && !packageVersions.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( packageVersions, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url, prerelease); ViewBag.SearchTerm = q; return View(viewModel); }
public virtual ActionResult ListPackages(string q, string sortOrder = null, int page = 1, bool prerelease = false, bool moderatorQueue = false, string moderationStatus = null) { if (page < 1) { page = 1; } q = (q ?? string.Empty).Trim(); IQueryable <Package> packageVersions = packageSvc.GetPackagesForListing(prerelease); IEnumerable <Package> packagesToShow = new List <Package>(); if (Request.Cookies["preferenceModView"] != null && string.IsNullOrWhiteSpace(q) && String.IsNullOrEmpty(sortOrder)) { moderatorQueue = true; } if (moderatorQueue) { var unknownStatus = PackageStatusType.Unknown.GetDescriptionOrValue(); //This is going to be fun. Unknown status ones would be listed, but sometimes a few might slip through the cracks if a maintainer unlists a package. // A user can just email us to catch those though. packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase == unknownStatus || p.StatusForDatabase == null); } q = (q ?? "").Trim(); if (String.IsNullOrEmpty(sortOrder)) { // Determine the default sort order. If no query string is specified, then the sortOrder is DownloadCount // If we are searching for something, sort by relevance. sortOrder = q.IsEmpty() ? Constants.PopularitySortOrder : Constants.RelevanceSortOrder; } if (String.IsNullOrEmpty(moderationStatus)) { // If no moderation status is specified, default to All Statuses moderationStatus = Constants.AllModerationStatuses; } int totalHits = 0; int updatedPackagesCount = 0; int respondedPackagesCount = 0; int unreviewedPackagesCount = 0; int waitingPackagesCount = 0; int pendingAutoReviewPackagesCount = 0; int unknownPackagesCount = 0; var searchFilter = GetSearchFilter(q, sortOrder, page, prerelease, moderationStatus); if (moderatorQueue) { var submittedPackages = packageSvc.GetSubmittedPackages(useCache: !Request.IsAuthenticated).ToList(); var updatedStatus = PackageSubmittedStatusType.Updated.ToString(); var respondedStatus = PackageSubmittedStatusType.Responded.ToString(); var readyStatus = PackageSubmittedStatusType.Ready.ToString(); var pendingStatus = PackageSubmittedStatusType.Pending.ToString(); var waitingStatus = PackageSubmittedStatusType.Waiting.ToString(); var unknownStatus = PackageStatusType.Unknown.GetDescriptionOrValue(); //var resubmittedPackages = submittedPackages.Where(p => p.ReviewedDate.HasValue && p.Published > p.ReviewedDate).OrderBy(p => p.Published).ToList(); var resubmittedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == updatedStatus).OrderBy(p => p.Published).ToList(); updatedPackagesCount = resubmittedPackages.Count; var respondedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == respondedStatus).OrderBy(p => p.LastUpdated).ToList(); respondedPackagesCount = respondedPackages.Count; var unreviewedPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == readyStatus).OrderBy(p => p.Published).ToList(); unreviewedPackagesCount = unreviewedPackages.Count; var pendingAutoReviewPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == pendingStatus || p.SubmittedStatusForDatabase == null).OrderBy(p => p.Published).ToList(); unreviewedPackagesCount += pendingAutoReviewPackages.Count; pendingAutoReviewPackagesCount = pendingAutoReviewPackages.Count; //var waitingForMaintainerPackages = submittedPackages.Where(p => p.ReviewedDate >= p.Published).OrderByDescending(p => p.ReviewedDate).ToList(); var waitingForMaintainerPackages = submittedPackages.Where(p => p.SubmittedStatusForDatabase == waitingStatus).OrderByDescending(p => p.ReviewedDate).ToList(); waitingPackagesCount = waitingForMaintainerPackages.Count; var unknownPackages = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase == unknownStatus || p.StatusForDatabase == null).ToList(); unknownPackagesCount = unknownPackages.Count; packagesToShow = resubmittedPackages.Union(respondedPackages).Union(unreviewedPackages).Union(pendingAutoReviewPackages).Union(waitingForMaintainerPackages); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; default: //do not change the search order break; } switch (searchFilter.SortModeration) { case SortModeration.AllStatuses: packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase != unknownStatus && p.StatusForDatabase != null); packagesToShow = resubmittedPackages.Union(respondedPackages).Union(unreviewedPackages).Union(pendingAutoReviewPackages).Union(waitingForMaintainerPackages); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; default: //do not change the search order break; } break; case SortModeration.SubmittedStatus: packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase != unknownStatus && p.StatusForDatabase != null); packagesToShow = submittedPackages.Where(p => p.SubmittedStatusForDatabase == readyStatus || p.SubmittedStatusForDatabase == pendingStatus); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; case SortProperty.DownloadCount: packagesToShow = packagesToShow.OrderByDescending(p => p.DownloadCount); break; default: //do not change the search order break; } break; case SortModeration.UnknownStatus: packagesToShow = submittedPackages.Where(p => p.SubmittedStatusForDatabase != readyStatus && p.SubmittedStatusForDatabase != pendingStatus && p.SubmittedStatusForDatabase != waitingStatus && p.SubmittedStatusForDatabase != respondedStatus && p.SubmittedStatusForDatabase != updatedStatus); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packageVersions.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packageVersions.OrderByDescending(p => p.Published); break; case SortProperty.DownloadCount: packagesToShow = packageVersions.OrderByDescending(p => p.DownloadCount); break; default: //do not change the search order break; } break; case SortModeration.PendingStatus: packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase != unknownStatus && p.StatusForDatabase != null); packagesToShow = submittedPackages.Where(p => p.SubmittedStatusForDatabase == pendingStatus); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; case SortProperty.DownloadCount: packagesToShow = packagesToShow.OrderByDescending(p => p.DownloadCount); break; default: //do not change the search order break; } break; case SortModeration.WaitingStatus: packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase != unknownStatus && p.StatusForDatabase != null); packagesToShow = submittedPackages.Where(p => p.SubmittedStatusForDatabase == waitingStatus); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; case SortProperty.DownloadCount: packagesToShow = packagesToShow.OrderByDescending(p => p.DownloadCount); break; default: //do not change the search order break; } break; case SortModeration.RespondedStatus: packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase != unknownStatus && p.StatusForDatabase != null); packagesToShow = submittedPackages.Where(p => p.SubmittedStatusForDatabase == respondedStatus); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; case SortProperty.DownloadCount: packagesToShow = packagesToShow.OrderByDescending(p => p.DownloadCount); break; default: //do not change the search order break; } break; case SortModeration.ReadyStatus: packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase != unknownStatus && p.StatusForDatabase != null); packagesToShow = submittedPackages.Where(p => p.SubmittedStatusForDatabase == readyStatus); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; case SortProperty.DownloadCount: packagesToShow = packagesToShow.OrderByDescending(p => p.DownloadCount); break; default: //do not change the search order break; } break; case SortModeration.UpdatedStatus: packageVersions = packageVersions.Where(p => !p.IsPrerelease).Where(p => p.StatusForDatabase != unknownStatus && p.StatusForDatabase != null); packagesToShow = submittedPackages.Where(p => p.SubmittedStatusForDatabase == updatedStatus); switch (searchFilter.SortProperty) { case SortProperty.DisplayName: packagesToShow = packagesToShow.OrderBy(p => p.Title); break; case SortProperty.Recent: packagesToShow = packagesToShow.OrderByDescending(p => p.Published); break; case SortProperty.DownloadCount: packagesToShow = packagesToShow.OrderByDescending(p => p.DownloadCount); break; default: //do not change the search order break; } break; default: //do not change the search order break; } if (!string.IsNullOrWhiteSpace(q)) { packagesToShow = packagesToShow.AsQueryable().Search(q).ToList(); } totalHits = packagesToShow.Count() + packageVersions.Count(); if ((searchFilter.Skip + searchFilter.Take) >= packagesToShow.Count() & string.IsNullOrWhiteSpace(q)) { packagesToShow = packagesToShow.Union(packageVersions.OrderByDescending(pv => pv.PackageRegistration.DownloadCount).ToList()); } packagesToShow = packagesToShow.Skip(searchFilter.Skip).Take(searchFilter.Take); } else { var results = searchSvc.Search(searchFilter); var cacheTime = DateTime.UtcNow.AddSeconds(30); // fetch most common query from cache to relieve load on the search service if (string.IsNullOrEmpty(q) && page == 1) { cacheTime = DateTime.UtcNow.AddMinutes(10); } totalHits = 0; int.TryParse(Cache.Get( string.Format( "searchResultsHits-{0}-{1}-{2}-{3}-{4}-{5}", searchFilter.SearchTerm.to_lower(), searchFilter.IncludePrerelease, searchFilter.Skip, searchFilter.SortProperty.to_string(), searchFilter.SortDirection.to_string(), searchFilter.SortModeration.to_string()), cacheTime, () => results.Hits.to_string()), out totalHits); packagesToShow = Cache.Get( string.Format( "searchResults-{0}-{1}-{2}-{3}-{4}-{5}", searchFilter.SearchTerm.to_lower(), searchFilter.IncludePrerelease, searchFilter.Skip, searchFilter.SortProperty.to_string(), searchFilter.SortDirection.to_string(), searchFilter.SortModeration.to_string()), cacheTime, () => results.Data.ToList()); } if (page == 1 && !packagesToShow.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( packagesToShow, q, sortOrder, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url, prerelease, moderatorQueue, updatedPackagesCount, unreviewedPackagesCount, waitingPackagesCount, respondedPackagesCount, pendingAutoReviewPackagesCount, unknownPackagesCount, moderationStatus); ViewBag.SearchTerm = q; return(View("~/Views/Packages/ListPackages.cshtml", viewModel)); }
public virtual async Task<ActionResult> ListPackages(string curatedFeedName, string q, int page = 1) { if (page < 1) { page = 1; } q = (q ?? "").Trim(); var searchFilter = SearchAdaptor.GetSearchFilter(q, page, sortOrder: null); searchFilter.CuratedFeed = CuratedFeedService.GetFeedByName(curatedFeedName, includePackages: false); if (searchFilter.CuratedFeed == null) { return HttpNotFound(); } SearchResults results = await SearchService.Search(searchFilter); int totalHits = results.Hits; if (page == 1 && !results.Data.Any()) { // In the event the index wasn't updated, we may get an incorrect count. totalHits = 0; } var viewModel = new PackageListViewModel( results.Data, q, totalHits, page - 1, Constants.DefaultPackageListPageSize, Url); ViewBag.SearchTerm = q; return View("ListPackages", viewModel); }