<commit_before><!---
*
* Copyright (C) 2005-2008 Razuna
*
* This file is part of Razuna - Enterprise Digital Asset Management.
*
* Razuna is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Razuna is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with Razuna. If not, see <http://www.gnu.org/licenses/>.
*
* You may restribute this Program with a special exception to the terms
* and conditions of version 3.0 of the AGPL as described in Razuna"s
* FLOSS exception. You should have received a copy of the FLOSS exception
* along with Razuna. If not, see <http://www.razuna.com/licenses/>.
*
--->
<cfcomponent output="false" extends="authentication">

	<!--- Create search collection --->
	<cffunction name="search" access="remote" output="false" returntype="struct" returnformat="json">
		<cfargument name="collection" required="true" type="string">
		<cfargument name="criteria" required="true" type="string">
		<cfargument name="category" required="true" type="string">
		<cfargument name="secret" required="true" type="string">
		<cfargument name="startrow" required="true" type="numeric">
		<cfargument name="maxrows" required="true" type="numeric">
		<cfargument name="folderid" required="true" type="string">
		<cfargument name="search_type" required="true" type="string">
		<cfargument name="search_rendition" required="true" type="string">
		<cfargument name="search_upc" required="false" type="boolean" default="false">
		<!--- Log --->
		<cfset consoleoutput(true)>
		<!--- <cfset console(arguments)> --->
		<cfset console("#now()# ---------------------- Starting Search")>
		<!--- Check login --->
		<cfset auth(arguments.secret)>
		<!--- Param --->
		<cfset r.success = true>
		<cfset r.error = "">
		<cfset r.results = "">
		<!--- Call internal function --->
		<cfinvoke method="_search" returnvariable="r.results">
			<cfinvokeargument name="collection" value="#arguments.collection#" />
			<cfinvokeargument name="criteria" value="#arguments.criteria#" />
			<cfinvokeargument name="arg_category" value="#arguments.category#" />
			<cfinvokeargument name="startrow" value="#arguments.startrow#" />
			<cfinvokeargument name="maxrows" value="#arguments.maxrows#" />
			<cfinvokeargument name="folderid" value="#arguments.folderid#" />
			<cfinvokeargument name="search_type" value="#arguments.search_type#" />
			<cfinvokeargument name="search_rendition" value="#arguments.search_rendition#" />
			<cfinvokeargument name="search_upc" value="#arguments.search_upc#" />
		</cfinvoke>
		<!--- Return --->
		<cfreturn r />
	</cffunction>




	<!--- PRIVATE --->




	<!--- Internal search --->
	<cffunction name="_search" access="private" output="false" returntype="query">
		<cfargument name="collection" required="true" type="string">
		<cfargument name="criteria" required="true" type="string">
		<cfargument name="arg_category" required="true" type="string">
		<cfargument name="startrow" required="true" type="numeric">
		<cfargument name="maxrows" required="true" type="numeric">
		<cfargument name="folderid" required="true" type="string">
		<cfargument name="search_type" required="true" type="string">
		<cfargument name="search_rendition" required="true" type="string">
		<cfargument name="search_upc" required="false" type="boolean" default="false">
		<!--- Param --->
		<cfset var results = querynew("category, categorytree, rank, searchcount")>
		<cfset var folderlist = "" />
		<cfset var _searchcount = 0 />
		<!--- Search --->
		<cftry>
			<!--- Syntax --->
			<cfset var _criteria = _searchSyntax(criteria=arguments.criteria, search_type=arguments.search_type, search_rendition=arguments.search_rendition, host_id=arguments.collection) />
			<!--- if the folderid is not 0 we need to filter by folderid --->
			<cfif arguments.folderid NEQ 0>
				<!--- New list var --->
				<cfset var counter_folderlist = 0>
				<!--- If more than 500 folders do the split, else normal operation --->
				<cfif listlen(arguments.folderid) GTE 500>
					<!--- Create new temp query for lucene results --->
					<cfset var _tmp_results = queryNew("category, categorytree, rank, searchcount")>
					<!--- Create new list --->
					<cfset "variables.folderlist_#counter_folderlist#" = "">
					<!--- Loop over folders --->
					<cfloop list="#arguments.folderid#" index="folderid">
						<!--- If we have more than 200 in the current list create a new list --->
						<cfif listlen(variables["folderlist_" & counter_folderlist], " ") GTE 500>
							<!--- Increase counter --->
							<cfset counter_folderlist++>
							<!--- Create new list with new counter --->
							<cfset "variables.folderlist_#counter_folderlist#" = "">
						</cfif>
						<!--- Append to list --->
						<cfset "variables.folderlist_#counter_folderlist#" = variables["folderlist_" & counter_folderlist] & ' folder:("#folderid#") folder_alias:("#folderid#")'>
					</cfloop>
					<!--- We go the individual folder lists, now loop over it and put together the criteria and search in Lucene --->
					<cfloop from="0" to="#counter_folderlist#" index="n">
						<!--- If the returning _criteria is empty we only tag on the folderlist (with this fix user can search with *) --->
						<cfif _criteria EQ "">
							<cfset "variables.criteria_#n#" = "( " & variables["folderlist_" & n] & " )" />
						<cfelse>
							<cfset "variables.criteria_#n#" = "( #_criteria# ) AND ( " & variables["folderlist_" & n] & " )" />
						</cfif>
						<!--- Call internal function. Search all records --->
						<cfset "variables.results_#n#" = _embeddedSearch(collection=arguments.collection, criteria=variables["criteria_" & n], category=arguments.arg_category, startrow=0, maxrows=0)>
						<!--- Set result in variable as QoQ can't handle complex variables --->
						<cfset var _thisqry = variables["results_" & n]>
						<!--- Add up the searchcount --->
						<cftry>
							<cfset var _searchcount = _searchcount + _thisqry.searchcount>
							<cfcatch type="any"></cfcatch>
						</cftry>
						<!--- Add lucene query to tmp results --->
						<cfquery dbtype="query" name="_tmp_results">
						SELECT category, categorytree, rank, searchcount, folder
						FROM _tmp_results
						UNION
						SELECT category, categorytree, rank, searchcount, folder
						FROM _thisqry
						ORDER BY rank
						</cfquery>
					</cfloop>
					<!--- Loop over the _tmp_results and add only the rows we need to the final set --->
					<cfoutput query="_tmp_results" startrow="#arguments.startrow#" maxrows="#arguments.maxrows#">
						<cfset var _q = structnew()>
						<cfset _q.category = category>
						<cfset _q.categorytree = categorytree>
						<cfset _q.rank = rank><commit_msg>Fixed an issue with searching as a user<commit_after><!---
*
* Copyright (C) 2005-2008 Razuna
*
* This file is part of Razuna - Enterprise Digital Asset Management.
*
* Razuna is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Razuna is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with Razuna. If not, see <http://www.gnu.org/licenses/>.
*
* You may restribute this Program with a special exception to the terms
* and conditions of version 3.0 of the AGPL as described in Razuna"s
* FLOSS exception. You should have received a copy of the FLOSS exception
* along with Razuna. If not, see <http://www.razuna.com/licenses/>.
*
--->
<cfcomponent output="false" extends="authentication">

	<!--- Create search collection --->
	<cffunction name="search" access="remote" output="false" returntype="struct" returnformat="json">
		<cfargument name="collection" required="true" type="string">
		<cfargument name="criteria" required="true" type="string">
		<cfargument name="category" required="true" type="string">
		<cfargument name="secret" required="true" type="string">
		<cfargument name="startrow" required="true" type="numeric">
		<cfargument name="maxrows" required="true" type="numeric">
		<cfargument name="folderid" required="true" type="string">
		<cfargument name="search_type" required="true" type="string">
		<cfargument name="search_rendition" required="true" type="string">
		<cfargument name="search_upc" required="false" type="boolean" default="false">
		<!--- Log --->
		<cfset consoleoutput(true)>
		<!--- <cfset console(arguments)> --->
		<cfset console("#now()# ---------------------- Starting Search")>
		<!--- Check login --->
		<cfset auth(arguments.secret)>
		<!--- Param --->
		<cfset r.success = true>
		<cfset r.error = "">
		<cfset r.results = "">
		<!--- Call internal function --->
		<cfinvoke method="_search" returnvariable="r.results">
			<cfinvokeargument name="collection" value="#arguments.collection#" />
			<cfinvokeargument name="criteria" value="#arguments.criteria#" />
			<cfinvokeargument name="arg_category" value="#arguments.category#" />
			<cfinvokeargument name="startrow" value="#arguments.startrow#" />
			<cfinvokeargument name="maxrows" value="#arguments.maxrows#" />
			<cfinvokeargument name="folderid" value="#arguments.folderid#" />
			<cfinvokeargument name="search_type" value="#arguments.search_type#" />
			<cfinvokeargument name="search_rendition" value="#arguments.search_rendition#" />
			<cfinvokeargument name="search_upc" value="#arguments.search_upc#" />
		</cfinvoke>
		<!--- Return --->
		<cfreturn r />
	</cffunction>




	<!--- PRIVATE --->




	<!--- Internal search --->
	<cffunction name="_search" access="private" output="false" returntype="query">
		<cfargument name="collection" required="true" type="string">
		<cfargument name="criteria" required="true" type="string">
		<cfargument name="arg_category" required="true" type="string">
		<cfargument name="startrow" required="true" type="numeric">
		<cfargument name="maxrows" required="true" type="numeric">
		<cfargument name="folderid" required="true" type="string">
		<cfargument name="search_type" required="true" type="string">
		<cfargument name="search_rendition" required="true" type="string">
		<cfargument name="search_upc" required="false" type="boolean" default="false">
		<!--- Param --->
		<cfset var results = querynew("category, categorytree, rank, searchcount")>
		<cfset var folderlist = "" />
		<cfset var _searchcount = 0 />
		<!--- Search --->
		<cftry>
			<!--- Syntax --->
			<cfset var _criteria = _searchSyntax(criteria=arguments.criteria, search_type=arguments.search_type, search_rendition=arguments.search_rendition, host_id=arguments.collection) />
			<!--- if the folderid is not 0 we need to filter by folderid --->
			<cfif arguments.folderid NEQ 0>
				<!--- New list var --->
				<cfset var counter_folderlist = 0>
				<!--- If more than 500 folders do the split, else normal operation --->
				<cfif listlen(arguments.folderid) GTE 500>
					<!--- Create new temp query for lucene results --->
					<cfset var _tmp_results = queryNew("category, categorytree, rank, searchcount")>
					<!--- Create new list --->
					<cfset "variables.folderlist_#counter_folderlist#" = "">
					<!--- Loop over folders --->
					<cfloop list="#arguments.folderid#" index="folderid">
						<!--- If we have more than 200 in the current list create a new list --->
						<cfif listlen(variables["folderlist_" & counter_folderlist], " ") GTE 500>
							<!--- Increase counter --->
							<cfset counter_folderlist++>
							<!--- Create new list with new counter --->
							<cfset "variables.folderlist_#counter_folderlist#" = "">
						</cfif>
						<!--- Append to list --->
						<cfset "variables.folderlist_#counter_folderlist#" = variables["folderlist_" & counter_folderlist] & ' folder:("#folderid#") folder_alias:("#folderid#")'>
					</cfloop>
					<!--- We go the individual folder lists, now loop over it and put together the criteria and search in Lucene --->
					<cfloop from="0" to="#counter_folderlist#" index="n">
						<!--- If the returning _criteria is empty we only tag on the folderlist (with this fix user can search with *) --->
						<cfif _criteria EQ "">
							<cfset "variables.criteria_#n#" = "( " & variables["folderlist_" & n] & " )" />
						<cfelse>
							<cfset "variables.criteria_#n#" = "( #_criteria# ) AND ( " & variables["folderlist_" & n] & " )" />
						</cfif>
						<!--- Call internal function. Search all records --->
						<cfset "variables.results_#n#" = _embeddedSearch(collection=arguments.collection, criteria=variables["criteria_" & n], category=arguments.arg_category, startrow=0, maxrows=0, search_upc=arguments.search_upc)>
						<!--- Set result in variable as QoQ can't handle complex variables --->
						<cfset var _thisqry = variables["results_" & n]>
						<!--- Add up the searchcount --->
						<cftry>
							<cfset var _searchcount = _searchcount + _thisqry.searchcount>
							<cfcatch type="any"></cfcatch>
						</cftry>
						<!--- Add lucene query to tmp results --->
						<cfquery dbtype="query" name="_tmp_results">
						SELECT category, categorytree, rank, searchcount, folder
						FROM _tmp_results
						UNION
						SELECT category, categorytree, rank, searchcount, folder
						FROM _thisqry
						ORDER BY rank
						</cfquery>
					</cfloop>
					<!--- Loop over the _tmp_results and add only the rows we need to the final set --->
					<cfoutput query="_tmp_results" startrow="#arguments.startrow#" maxrows="#arguments.maxrows#">
						<cfset var _q = structnew()>
						<cfset _q.category = category>
						<cfset _q.categorytree = categorytree>
						<cfset _q.rank = rank>
{'fraction_non_alphanumeric': 0.15605879457064492, 'fraction_numerical': 0.004219706027146775, 'mean_word_length': 3.781439139206456, 'pattern_counts': {'":': 0, '<': 227, '<?xml version=': 0, '>': 227, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3773866', 'n_tokens_mistral': 5041, 'n_tokens_neox': 4180, 'n_words': 1213}