<commit_before> /* Yet Another Forum.NET
 * Copyright (C) 2003-2005 <NAME>
 * Copyright (C) 2006-2013 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * http://www.yetanotherforum.net/
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

namespace YAF.Classes
{
    #region Using

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web.Script.Serialization;
    using System.Web.Script.Services;
    using System.Web.Security;
    using System.Web.Services;

    using YAF.Classes.Data;
    using YAF.Core;
    using YAF.Core.Services;
    using YAF.Types;
    using YAF.Types.Constants;
    using YAF.Types.Extensions;
    using YAF.Types.Interfaces;
    using YAF.Types.Objects;
    using YAF.Utils.Helpers;
    using YAF.Utils.Helpers.StringUtils;

    #endregion

    /// <summary>
    /// Class for JS jQuery  Ajax Methods
    /// </summary>
    [WebService(Namespace = "http://yetanotherforum.net/yafajax")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class YafAjax : WebService, IHaveServiceLocator
    {
        #region Properties

        /// <summary>
        ///   Gets ServiceLocator.
        /// </summary>
        public IServiceLocator ServiceLocator
        {
            get
            {
                return YafContext.Current.ServiceLocator;
            }
        }

        #endregion

        /// <summary>
        /// Gets the topics by forum.
        /// </summary>
        /// <param name="forumID">The forum identifier.</param>
        /// <param name="page">The page.</param>
        /// <param name="searchTerm">The search term.</param>
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void GetTopics(int forumID, int page, string searchTerm)
        {
            var serializer = new JavaScriptSerializer();

            if (!YafContext.Current.IsAdmin && !YafContext.Current.IsForumModerator)
            {
                this.Context.Response.Write("{ 'No Access' }");
                return;
            }
            
            if (searchTerm.IsSet())
            {
                var topics = LegacyDb.topic_list(
                                 forumID,
                                 null,
                                 DateTimeHelper.SqlDbMinTime(),
                                 DateTime.UtcNow,
                                 0,
                                 30000,
                                 false,
                                 false,
                                 false);

                var topicsList = (from DataRow topic in topics.Rows
                                  where topic["Subject"].ToString().ToLower().Contains(searchTerm.ToLower())
                                   select
                                       new SelectOptions
                                           {
                                               text = topic["Subject"].ToString(),
                                               id = topic["TopicID"].ToString()
                                           }).ToList();

                var pagedTopics = new SelectPagedOptions { Total = 0, Results = topicsList };

                this.Context.Response.Write(serializer.Serialize(pagedTopics));
            }
            else
            {
                var topics = LegacyDb.topic_list(
                                 forumID,
                                 null,
                                 DateTimeHelper.SqlDbMinTime(),<commit_msg>caching topics_list here to avoid db calls after each key input<commit_after> /* Yet Another Forum.NET
 * Copyright (C) 2003-2005 <NAME>
 * Copyright (C) 2006-2013 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * http://www.yetanotherforum.net/
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

namespace YAF.Classes
{
    #region Using

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web.Script.Serialization;
    using System.Web.Script.Services;
    using System.Web.Security;
    using System.Web.Services;

    using YAF.Classes.Data;
    using YAF.Core;
    using YAF.Core.Services;
    using YAF.Types;
    using YAF.Types.Constants;
    using YAF.Types.Extensions;
    using YAF.Types.Interfaces;
    using YAF.Types.Objects;
    using YAF.Utils.Helpers;
    using YAF.Utils.Helpers.StringUtils;

    #endregion

    /// <summary>
    /// Class for JS jQuery  Ajax Methods
    /// </summary>
    [WebService(Namespace = "http://yetanotherforum.net/yafajax")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class YafAjax : WebService, IHaveServiceLocator
    {
        #region Properties

        /// <summary>
        ///   Gets ServiceLocator.
        /// </summary>
        public IServiceLocator ServiceLocator
        {
            get
            {
                return YafContext.Current.ServiceLocator;
            }
        }

        #endregion

        /// <summary>
        /// Gets the topics by forum.
        /// </summary>
        /// <param name="forumID">The forum identifier.</param>
        /// <param name="page">The page.</param>
        /// <param name="searchTerm">The search term.</param>
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void GetTopics(int forumID, int page, string searchTerm)
        {
            var serializer = new JavaScriptSerializer();

            if (!YafContext.Current.IsAdmin && !YafContext.Current.IsForumModerator)
            {
                this.Context.Response.Write("{ 'No Access' }");
                return;
            }
            
            if (searchTerm.IsSet())
            {
                var topics = this.Get<IDataCache>()
                    .GetOrSet(
                        "TopicsList_{0}".FormatWith(forumID),
                        () =>
                        LegacyDb.topic_list(
                            forumID,
                            null,
                            DateTimeHelper.SqlDbMinTime(),
                            DateTime.UtcNow,
                            0,
                            30000,
                            false,
                            false,
                            false),
                        TimeSpan.FromMinutes(5));

                var topicsList = (from DataRow topic in topics.Rows
                                  where topic["Subject"].ToString().ToLower().Contains(searchTerm.ToLower())
                                   select
                                       new SelectOptions
                                           {
                                               text = topic["Subject"].ToString(),
                                               id = topic["TopicID"].ToString()
                                           }).ToList();

                var pagedTopics = new SelectPagedOptions { Total = 0, Results = topicsList };

                this.Context.Response.Write(serializer.Serialize(pagedTopics));
            }
            else
            {
                var topics = LegacyDb.topic_list(
                                 forumID,
                                 null,
                                 DateTimeHelper.SqlDbMinTime(),
{'fraction_non_alphanumeric': 0.08808696626695058, 'fraction_numerical': 0.00851731480443797, 'mean_word_length': 1.4733924611973392, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3304961', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2098, 'n_words': 591}