示例#1
0
        PhpValue /*object|array|WP_Error*/ PluginsApi(PhpValue result, string action, object args)
        {
            var arr = (PhpArray)PhpValue.FromClass(args);
            var log = NuGet.Common.NullLogger.Instance;

            switch (action)
            {
            case "query_plugins":

                int page     = (int)arr["page"] - 1;
                int per_page = (int)arr["per_page"];

                var searchFilter = new SearchFilter(true);

                // arr[browse|search|author|tag]
                var browse     = arr["browse"].AsString();
                var searchTerm = arr["search"].AsString() ?? arr["author"].AsString() ?? arr["tag"].AsString() ?? string.Empty;

                if (browse != null)
                {
                    switch (browse)
                    {
                    case "beta":
                    case "featured":
                    //case "popular":
                    case "recommended":         // = premium
                    default:
                        break;
                    }

                    searchFilter = new SearchFilter(includePrerelease: browse == "beta");
                }

                searchFilter.PackageTypes = WpPluginPackageType;

                //var results = PackageSearchResource.SearchAsync(
                //    "", new SearchFilter(true),
                //    skip: page * per_page, take: per_page, log: null, cancellationToken: CancellationToken.None).Result.ToList();

                // TODO: list versions that are compatible with current wpdotnet ?

                var raw     = RawSearchResourceV3.SearchPage(searchTerm, searchFilter, page * per_page, per_page, log, CancellationToken.None).Result;
                var results = (raw[JsonProperties.Data] as JArray ?? Enumerable.Empty <JToken>())
                              .OfType <JObject>()
                              .Select(PackageSearchMetadataFromJObject)
                              .ToList();
                var totalHits = raw["totalHits"].ToObject <int>();

                return(PhpValue.FromClass(new QueryPluginsResult
                {
                    info = new PhpArray()
                    {
                        { "page", page },
                        { "pages", (totalHits / per_page) + ((totalHits % per_page) == 0 ? 0 : 1) },
                        { "results", results.Count },
                    },
                    plugins = new PhpArray(results.Select(_ => new PluginResult(_))),
                }));

            case "plugin_information":

                var p = RegistrationResourceV3.GetPackageMetadata(PluginResult.SlugToId(arr["slug"].ToString()), true, true, log, CancellationToken.None)
                        .Result
                        .Select(PackageSearchMetadataFromJObject)
                        .FirstOrDefault();

                //var p = PackageMetadataResource.GetMetadataAsync(new PackageIdentity(arr["slug"].ToString(), new NuGet.Versioning.NuGetVersion("")), log, CancellationToken.None).Result;
                if (p != null)
                {
                    var packageBaseAddress = ServiceIndexResourceV3.GetServiceEntryUri(ServiceTypes.PackageBaseAddress)?.AbsoluteUri;
                    var id      = p.Identity.Id.ToLowerInvariant();
                    var version = p.Identity.Version.ToNormalizedString().ToLowerInvariant();
                    var url     = $"{packageBaseAddress}/{id}/{version}/{id}.{version}.nupkg";

                    var plugin = new PluginResult(p);
                    plugin.download_link = url;
                    return(PhpValue.FromClass(plugin));
                }
                else
                {
                    return(PhpValue.Null);
                }

            case "hot_tags":
            case "hot_categories":
                return(PhpValue.Null);

            default:
                throw new ArgumentException(nameof(action));
            }
        }