....................................../////.===Shadow-Here===./////................................................ > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < > < ------------------------------------------------------------------------------------------------------------------- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// RIFF¤ WEBPVP8 ˜ ðÑ *ôô>‘HŸK¥¤"§£±¨àð enü¹%½_F‘åè¿2ºQú³íªú`N¿­3ÿƒügµJžaÿ¯ÿ°~¼ÎùnúîÞÖô•òíôÁÉß®Sm¥Ü/ ‡ó˜f£Ùà<˜„xëJ¢Ù€SO3x<ªÔ©4¿+ç¶A`q@Ì“Úñè™ÍÿJÌ´ª-˜ÆtÊÛL]Ïq*‘Ý”ì#ŸÌÏãY]@ê`¿ /ªfkØB4·®£ó z—Üw¥Pxù–ÞLШKÇN¾AkÙTf½è'‰g gÆv›Øuh~ a˜Z— ïj*á¥t d£“uÒ ¨`K˜¹ßþ]b>˜]_ÏÔ6W—è2r4x•íÖ…"ƒÖNîä!¦å Ú}ýxGøÌ —@ ;ÆÚŠ=ɾ1ý8lªË¥ô ^yf®Œ¢u&2©nÙÇ›ñÂñŒ³ aPo['½»øFùà­+4ê“$!lövlüÞ=;N®3ð‚õ›DÉKòÞ>ÄÍ ¥ˆuߤ#ˆ$6ù™¥îЇy’ÍB¼ çxÛ;X"WL£R÷͝*ó-¶Zu}º.s¸sšXqù–DþÿvªhüïwyŸ ¯é³lÀ:KCûÄ£Ëá\…­ ~—ýóî ¼ûûÜTÓüÇy…ŽÆvc»¾×U ñ¸žþоP÷¦ó:Ò¨¨5;Ð#&#ÖúñläÿÁœ GxÉ­/ñ‡áQðìYÉtÒw޼GÔ´zàÒò ð*ëzƒ•4~H]Ø‹f ñÓÈñ`NåWçs'ÆÏW^ø¹!XžµmQ5ÃËoLœÎ: ÞËÍ¥J ù…î èo£ßPÎñ¶ž8.Œ]ʵ~5›ÙË-ù*8ÙÖß±~ ©¹rÓê‚j¶d¸{^Q'˜±Crß ÚH—#¥¥QlÀ×ëã‡DÜ«èî þ&Çæžî;ŽÏºò6ÒLÃXy&ZŒ'j‚¢Ù€IßÚù+–MGi‰*jE€‘JcÜ ÓÌ EÏÚj]o˜ Þr <¾U ûŪæÍ/šÝH¥˜b”¼ ÁñßX GP›ï2›4WŠÏà×£…íÓk†¦H·ÅíMh–*nó÷à]ÁjCº€b7<ب‹¨5車bp2:Á[UªM„QŒçiNMa#<5›áËó¸HýÊ"…×Éw¹¦ì2º–x<›»a±¸3Weü®FÝ⑱ö–î–³|LPÈ~çð~Çå‡|º kD¢µÏàÆAI %1À% ¹Ò – ”ϝS¦‰4&¶£°à Öý”û_Ò Áw°A«Å€?mÇÛgHÉ/8)á¾ÛìáöŽP í¨PŸNÙµº¦‡§Ùš"ÿ«>+ªÕ`Ê÷‡‚ß Õû˜þãÇ-PÍ.¾XV‘€ dÜ"þ4¹ ±Oú‘©t¥¦FªÄÃÄ•b‚znýu½—#cDs˜ÃiÑOˆñ×QO=*IAÊ,¶ŽZƒ;‡wøXè%EÐk:F±Ú” .Ѽ+Áu&Ç`."pÈÉw o&¿dE6‘’EqTuK@Ì¥ã™À(Êk(h‰,H}RÀIXÛš3µ1©_OqÚÒJAñ$ÊÙÜ;D3çŒ[þùœh¬Ã³™ö6ç†NY".Ú‰ï[ªŸŒ '²Ð öø_¨ÂÉ9ué¶³ÒŠõTàîMØ#û¯gN‡bÙ놚X„ö …ÉeüÌ^J ‹€.œ$Æ)βÄeæW#óüßĺŸ€ ÀzwV 9oä»f4V*uB «Ë†¹ì¯žR霓æHXa=&“I4K;¯ç‹h×·"UŠ~<•╪Vêª&ÍSÃÆÅ?ÔqÎ*mTM ˜›µwêd#[C¡©§‘D<©àb†–ÁœøvH/,í:¯( ²£|4-„Æövv„Yͼ™^Á$ˆ„¢Û[6yB.åH*V¨æ?$=˜Ñ€•ñ·­(VlŸ‘ nÀt8W÷´Bûba?q9ú¶Xƒl«ÿ\ù¶’þòUÐj/õ¢Ìµ³g$ƒÎR!¸»|Oߍë’BhîÚÑ¢ñåŒJ„®„£2Ð3•ô02Nt…!£Í]Ïc½Qÿ?ˆ<&ÃA¾Ú,JˆijÌ#5yz„‰Î|ÊŽ5QÏ:‹ÐaóVÔxW—CpeÏzÐïíçôÿÅ_[hãsÐ_/ŽTÝ?BîˆííV$<¿i>²F¬_Eß¿ †bÊŒº­ÿ®Z H“C}”¬,Mp ý/Bá£w>˜YV°aƒúh+cŠ- r/[%|üUMHäQ°X»|û/@|°¥Ð !BÔ Ç¢Ä©š+Õì D«7ìN¶ŽðÔ " ƶ’ÖçtA‰Û×}{tþz­¾GÍ›k¹OEJR$ Â׃ «ëÁ"oÉôž$oUK(Ä)Ãz³Ê-‹êN[Ò3Œñbï8P 4ƒ×q¢bo|?<ÛX¬òÄͰL–±›(™ûG?ýË©ÚÄ–ÂDØÐ_Ç¡ô ¾–ÄÏø ×e8Ë©$ÄF¹Å‹ì[©óìl:F¾f´‹‹Xì²ï®\¬ôùƒ ÿat¥óèÒùHß0äe‚;ü×h:ÆWðHž=Ã8骣"kœ'Y?³}Tûè€>?0l›e1Lòñ„aæKÆw…hÖŠùW…ÈÆÄ0ši·›[pcwËþñiêíY/~-Á5˜!¿†A›™Mÿþ(±“t@â“ö2­´TG5yé]çå僳 .·ÍïçÝ7UÚ±Ð/Nè»,_Ï ùdj7\ï Wì4›„»c¸àešg#ÒÊ⥭áØo5‘?ÌdÝô¯ ¹kzsƒ=´#ëÉK›Ø´±-¥eW?‡çßtòTã…$Ý+qÿ±ƒ÷_3Ô¥í÷:æ–ž<·Ö‡‰Å¢ š‡%Ô—utÌÈìðžgÖÀz²À—ï÷Óîäõ{K'´È÷³yaÏÁjƒô}ž§®æÊydÕÈë5¯èˆõvÕ©ã*çD„ “z„Ó‡^^xÂ3M§A´JG‚öï 3W'ˆ.OvXè¡ÊÕª?5º7†˜(˜Ç¶#çê’¶!ÌdZK§æ 0fãaN]òY³RV ™î$®K2R¨`W!1Ôó\;Ý ýB%qæK•&ÓÈe9È0êI±žeŸß -ú@žQr¦ ö4»M¼Áè¹µmw 9 EÆE_°2ó„ŸXKWÁ×Hóì^´²GѝF©óäR†¦‰ç"V»eØ<3ùd3ÿÚ¤Žú“Gi" —‘_ÙËÎ~Üö¯¥½Î»üŸEÚŽåmÞþí ;ÞólËΦMzA"Âf(´òá;Éï(/7½ûñÌ­cïÕçлþÝz¾-ÍvÑ“pH­–ðÓj$¸Äû¤‚‘ãUBË-n“2åPkS5&‹Â|+g^œ®Ì͆d!OïäîU«c;{Û!ÅŽ«ëZ9Ókóˆ]¯ƒ›né `ÇÒ+tÆš (ØKá¾—=3œ®•vuMñg²\ï Ec€ 05±d™‡×iÇ×›UúvÌ¢£Èþ¡ÕØô¶ßÎA"ß±#Ö²ˆÊŸ¦*Ä~ij|àø.-¼'»Ú¥£h ofº¦‡VsR=N½„Î v˜Z*SÌ{=jÑB‹tê…;’HžH¯8–îDù8ñ¢|Q•bÛçš–‹m³“ê¨ åÏ^m¬Žãþ©ïêO‡½6] µÆ„Ooòü ²x}N¦Ë3ïé¿»€›HA˜m%çÞ/¿í7Fø“‹léUk)É°Œµ8Q8›:ÀŠeT*šõ~ôڝG6 ¢}`ùH­–”¡k ‰P1>š†®9z11!X wKfmÁ¦xÑ,N1Q”–æB¶M…ÒÃv6SMˆhU¬ÊPŽï‘öj=·CŒ¯u¹ƒVIЃsx4’ömÛýcå¡¶7ßŠß 57^\wÒÐÆ k§h,Œý î«q^R½3]J¸ÇðN ‚çU¬ôº^Áì} ³f©Õœ§ˆã:FÄÈ‚é(€™?àýÓüè1Gô£¼éj‚OÅñ  #>×—ßtà 0G¥Åa뀐kßhc™À_ÉñÞ#±)GD" YîäË-ÿÙ̪ ¹™a¯´¢E\ÝÒö‚;™„ë]_ p8‰o¡ñ+^÷ 3‘'dT4œŽ ðVë½° :¬víÑ«£tßÚS-3¶“þ2 †üüʨòrš¹M{É_¤`Û¨0ìjœøJ‡:÷ÃáZ˜†@GP&œÑDGÏs¡þ¦þDGú‘1Yá9Ôþ¼ ûø…§÷8&–ÜÑnÄ_m®^üÆ`;ÉVÁJ£?â€-ßê}suÍ2sõA NÌúA磸‘îÿÚ»ƒìö·á¿±tÑÐ"Tÿü˜[@/äj¬€uüªìù¥Ý˜á8Ý´sõj 8@rˆð äþZÇD®ÿUÏ2ùôõrBzÆÏÞž>Ì™xœ“ wiÎ×7_… ¸ \#€MɁV¶¥üÕÿPÔ9Z‡ø§É8#H:ƒ5ÀÝå9ÍIŒ5åKÙŠ÷qÄ>1AÈøžj"µÂд/ªnÀ qªã}"iŸBå˜ÓÛŽ¦…&ݧ;G@—³b¯“•"´4í¨ôM¨åñC‹ïùÉó¯ÓsSH2Ý@ßáM‡ˆKÀªÛUeø/4\gnm¥‹ŸŒ qÄ b9ÞwÒNÏ_4Ég³ú=܆‚´ •â¥õeíþkjz>éÚyU«Íӝ݃6"8/ø{=Ô¢»G¥ äUw°W«,ô—¿ãㆅү¢³xŠUû™yŒ (øSópÐ 9\åTâ»—*oG$/×ÍT†Y¿1¤Þ¢_‡ ¼ „±ÍçèSaÓ 3ÛMÁBkxs‰’R/¡¤ˆÙçª(*õ„üXÌ´ƒ E§´¬EF"Ù”R/ÐNyÆÂ^°?™6¡œïJ·±$§?º>ÖüœcNÌù¯G ‹ñ2ЁBB„^·úìaz¨k:#¨Æ¨8LÎõލ£^§S&cŒÐU€ü(‡F±Š¼&P>8ÙÁ ‰ p5?0ÊÆƒZl¸aô š¼¡}gÿ¶zÆC²¹¬ÎÖG*HB¡O<º2#ñŒAƒ–¡B˜´É$¥›É:FÀÔx¾u?XÜÏÓvN©RS{2ʈãk9rmP¼Qq̳ è¼ÐFׄ^¡Öì fE“F4A…!ì/…¦Lƒ… … $%´¾yã@CI¬ á—3PþBÏNÿ<ý°4Ü ËÃ#ØÍ~âW«rEñw‹eùMMHß²`¬Öó½íf³:‹k˜¯÷}Z!ã¿<¥,\#öµÀ¯aÒNÆIé,Ћ–lŽ#Àæ9ÀÒS·I’½-Ïp Äz¤Š Â* ­íÄ9­< h>׍3ZkËU¹§˜ŒŠ±f­’¤º³Q ÏB?‹#µíÃ¥®@(Gs«†vI¥Mµ‹Á©e~2ú³ÁP4ìÕi‚²Ê^ö@-DþÓàlÜOÍ]n"µã:žpsŽ¢:! Aõ.ç~ÓBûH÷JCÌ]õVƒd «ú´QÙEA–¯¯Œ!.ˆˆëQ±ù œ·Ì!Õâ )ùL„ÅÀlÚè5@B…o´Æ¸XÓ&Û…O«˜”_#‡ƒ„ûÈt!¤ÁÏ›ÎÝŠ?c9 â\>lÓÁVÄÑ™£eØY]:fÝ–—ù+p{™ðè û³”g±OƒÚSù£áÁÊ„ä,ï7š²G ÕÌBk)~ÑiCµ|h#u¤¶îK¨² #²vݯGãeÖ϶ú…¾múÀ¶þÔñ‚Š9'^($¤§ò “š½{éúp÷J›ušS¹áªCÂubÃH9™D™/ZöØÁ‡¦ÝÙŸ·kð*_”.C‹{áXó€‡c¡c€§/šò/&éš÷,àéJþ‰X›fµ“C¨œ®r¬"kL‰Â_q…Z–.ÉL~O µ›zn‚¹À¦Öª7\àHµšÖ %»ÇníV[¥*Õ;ƒ#½¾HK-ÖIÊdÏEÚ#=o÷Óò³´Š: Ç?{¾+9›–‘OEáU·S€˜j"ÄaÜ ŒÛWt› á–c#a»pÔZÞdŽtWê=9éöÊ¢µ~ ë ;Öe‡Œ®:bî3±ýê¢wà¼îpêñ¹¾4 zc¾ðÖÿzdêŒÑÒŝÀ‰s6¤í³ÎÙB¿OZ”+F¤á‡3@Ñëäg©·Ž ˆèª<ù@É{&S„œÕúÀA)‰h:YÀ5^ÂÓŒ°õäU\ ùËÍû#²?Xe¬tu‰^zÒÔãë¼ÛWtEtû …‚g¶Úüâî*moGè¨7%u!]PhÏd™Ý%Îx: VÒ¦ôÊD3ÀŽKÛËãvÆî…N¯ä>Eró–ð`5 Œ%u5XkñÌ*NU%¶áœÊ:Qÿú»“úzyÏ6å-၇¾ ´ ÒÊ]y žO‘w2Äøæ…H’²f±ÎÇ.ª|¥'gîV•Ü .̘¯€šòü¤U~Ù†*¢!?ò wý,}´°ÔÞnïoKq5µb!áÓ3"vAßH¡³¡·G(ÐÎ0Îò¼MG!/ài®@—¬04*`…«é8ªøøló“ˆÊ”èù¤…ßÊoÿé'ËuÌÖ5×È¡§ˆˆfŽë9}hìâ_!!¯  B&Ëö¶‰ÀAÙNVŸ Wh›¸®XÑJì¨ú“¿÷3uj²˜¨ÍÎìë±aúŠÝå¯ð*Ó¨ôJ“yºØ)m°WýOè68†ŸÏ2—‰Ïüꪫٚ¥‹l1 ø ÏÄFjêµvÌbü¦èÝx:X±¢H=MÐß—,ˆÉÇ´(9ú¾^ÅÚ4¿m‡$âX‘å%(AlZo@½¨UOÌÕ”1ø¸jÎÀÃÃ_ µ‘Ü.œº¦Ut: Æï’!=¯uwû#,“pþÇúŒø(é@?³ü¥‘Mo §—s@Œ#)§ŒùkL}NOÆêA›¸~r½¼ÙA—HJ«eˆÖ´*¡ÓpÌŸö.m<-"³ûÈ$¬_6­åf£ïÚâj1y§ÕJ½@dÞÁr&Í\Z%D£Íñ·AZ Û³øüd/ªAi†/Й~  ‡âĮҮÏh§°b—›Û«mJžòG'[ÈYýŒ¦9psl ýÁ ®±f¦x,‰½tN ‚Xª9 ÙÖH.«Lo0×?͹m¡å†Ѽ+›2ƒF ±Ê8 7Hցϓ²Æ–m9…òŸï]Â1äN†VLâCˆU .ÿ‰Ts +ÅÎx(%¦u]6AF Š ØF鈄‘ |¢¶c±soŒ/t[a¾–û:s·`i햍ê›ËchÈ…8ßÀUÜewŒðNOƒõD%q#éû\9¤x¹&UE×G¥ Í—™$ð E6-‡¼!ýpãÔM˜ Âsìe¯ñµK¢Ç¡ùôléœ4Ö£”À Š®Ðc ^¨À}ÙËŸ§›ºê{ÊuÉC ×Sr€¤’fÉ*j!úÓ’Gsùìoîßîn%ò· àc Wp÷$¨˜)û»H ×8ŽÒ€Zj¤3ÀÙºY'Ql¦py{-6íÔCeiØp‘‡XÊîÆUߢ܂ž£Xé¼Y8þ©ëgñß}é.ÎógÒ„ÃØËø¯»™§Xýy M%@NŠ À(~áÐvu7&•,Ù˜ó€uP‡^^®=_E„jt’ 403WebShell
403Webshell
Server IP : 66.29.146.187  /  Your IP : 216.73.216.61
Web Server : LiteSpeed
System : Linux premium302.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User : ailwtbdh ( 734)
PHP Version : 8.1.34
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /home/ailwtbdh/www/wp-content/plugins/wpforo/classes/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /home/ailwtbdh/www/wp-content/plugins/wpforo/classes/VectorStorageLocal.php
<?php

namespace wpforo\classes;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Local Vector Storage for AI Embeddings
 *
 * Handles storing and searching embeddings in WordPress MySQL database
 * as an alternative to cloud-based vector storage (gVectors AI Services on AWS Cloud).
 *
 * Features:
 * - Binary packed vector storage (efficient BLOB)
 * - Pre-computed magnitudes for fast cosine similarity
 * - Similarity cache with TTL
 * - PHP-based cosine similarity calculation
 *
 * @since 3.0.0
 */
class VectorStorageLocal {

	/**
	 * Default cache TTL in seconds (1 hour)
	 */
	const CACHE_TTL = 3600;

	/**
	 * Maximum similar items to cache per source
	 */
	const MAX_CACHED_SIMILAR = 20;

	/**
	 * Post count threshold for performance warning
	 */
	const PERFORMANCE_THRESHOLD = 5000000;

	/**
	 * Default vector dimensions
	 */
	const DEFAULT_DIMENSIONS = 1024;

	/**
	 * Constructor
	 */
	public function __construct() {
		// Note: Cron registration moved to VectorStorageManager::register_cron_hooks()
		// This ensures the cleanup callback is available when the cron fires
	}

	/**
	 * Store an embedding vector for a post
	 *
	 * @param int    $topicid          Topic ID
	 * @param int    $postid           Post ID (chunk)
	 * @param int    $forumid          Forum ID
	 * @param int    $userid           User ID
	 * @param array  $vector           Float array of embeddings
	 * @param string $content_hash     MD5 hash of content
	 * @param string $content_preview  Content preview (full chunk text)
	 * @param string $model_name       Model used for embedding
	 * @return int|false Insert ID or false on failure
	 */
	public function store_embedding( $topicid, $postid, $forumid, $userid, $vector, $content_hash, $content_preview = '', $model_name = 'amazon.titan-embed-text-v2', $content_type = 'forum' ) {
		global $wpdb;

		if ( empty( $vector ) || ! is_array( $vector ) ) {
			return false;
		}

		$dimensions = count( $vector );
		$magnitude = $this->calculate_magnitude( $vector );

		// Normalize vector for faster similarity computation
		$normalized_vector = $this->normalize_vector( $vector, $magnitude );
		$binary_vector = $this->pack_vector( $normalized_vector );

		// Check if embedding already exists
		$existing = $wpdb->get_var( $wpdb->prepare(
			"SELECT id FROM " . WPF()->tables->ai_embeddings . " WHERE postid = %d AND model_name = %s",
			$postid,
			$model_name
		) );

		$data = [
			'topicid'           => $topicid,
			'postid'            => $postid,
			'forumid'           => $forumid,
			'userid'            => $userid,
			'embedding_vector'  => $binary_vector,
			'vector_dimensions' => $dimensions,
			'vector_magnitude'  => 1.0, // Normalized vectors have magnitude 1
			'model_name'        => $model_name,
			'content_hash'      => $content_hash,
			'content_preview'   => $content_preview,
			'content_type'      => $content_type,
		];

		if ( $existing ) {
			// Update existing
			$result = $wpdb->update(
				WPF()->tables->ai_embeddings,
				$data,
				[ 'id' => $existing ],
				[ '%d', '%d', '%d', '%d', '%s', '%d', '%f', '%s', '%s', '%s', '%s' ],
				[ '%d' ]
			);

			// Invalidate cache for this post
			$this->invalidate_cache( 'post', $postid );

			return $existing;
		} else {
			// Insert new
			$result = $wpdb->insert(
				WPF()->tables->ai_embeddings,
				$data,
				[ '%d', '%d', '%d', '%d', '%s', '%d', '%f', '%s', '%s', '%s', '%s' ]
			);

			return $result ? $wpdb->insert_id : false;
		}
	}

	/**
	 * Get embedding for a post
	 *
	 * @param int    $postid     Post ID
	 * @param string $model_name Model name
	 * @return array|null Embedding data or null
	 */
	public function get_embedding( $postid, $model_name = 'amazon.titan-embed-text-v2' ) {
		global $wpdb;

		$row = $wpdb->get_row( $wpdb->prepare(
			"SELECT * FROM " . WPF()->tables->ai_embeddings . " WHERE postid = %d AND model_name = %s",
			$postid,
			$model_name
		), ARRAY_A );

		if ( ! $row ) {
			return null;
		}

		$row['vector'] = $this->unpack_vector( $row['embedding_vector'] );
		unset( $row['embedding_vector'] );

		return $row;
	}

	/**
	 * Delete embedding for a post
	 *
	 * @param int $postid Post ID
	 * @return bool Success
	 */
	public function delete_embedding( $postid ) {
		global $wpdb;

		$result = $wpdb->delete(
			WPF()->tables->ai_embeddings,
			[ 'postid' => $postid ],
			[ '%d' ]
		);

		// Also delete from cache
		$this->invalidate_cache( 'post', $postid );

		return $result !== false;
	}

	/**
	 * Delete all embeddings for a topic
	 *
	 * @param int $topicid Topic ID
	 * @return int Number of deleted rows
	 */
	public function delete_topic_embeddings( $topicid ) {
		global $wpdb;

		// Get all postids first for cache invalidation
		$postids = $wpdb->get_col( $wpdb->prepare(
			"SELECT postid FROM " . WPF()->tables->ai_embeddings . " WHERE topicid = %d",
			$topicid
		) );

		$result = $wpdb->delete(
			WPF()->tables->ai_embeddings,
			[ 'topicid' => $topicid ],
			[ '%d' ]
		);

		// Invalidate cache for all posts
		foreach ( $postids as $postid ) {
			$this->invalidate_cache( 'post', $postid );
		}

		return $result;
	}

	/**
	 * Delete WordPress CPT embeddings from local storage.
	 *
	 * @param array|null $post_types Post types to delete (e.g., ['docs', 'page']). Null = all non-forum.
	 * @param array|null $post_ids   Specific post IDs to delete.
	 * @return int Number of deleted rows.
	 */
	public function delete_wp_embeddings( $post_types = null, $post_ids = null ) {
		global $wpdb;

		$table = WPF()->tables->ai_embeddings;

		if ( ! empty( $post_ids ) ) {
			// Delete specific post IDs
			$placeholders = implode( ', ', array_fill( 0, count( $post_ids ), '%d' ) );
			$deleted = $wpdb->query(
				$wpdb->prepare(
					"DELETE FROM $table WHERE postid IN ($placeholders) AND content_type != 'forum'",
					...$post_ids
				)
			);
		} elseif ( ! empty( $post_types ) ) {
			// Delete by content_type
			$placeholders = implode( ', ', array_fill( 0, count( $post_types ), '%s' ) );
			$deleted = $wpdb->query(
				$wpdb->prepare(
					"DELETE FROM $table WHERE content_type IN ($placeholders)",
					...$post_types
				)
			);
		} else {
			// Delete all non-forum embeddings
			$deleted = $wpdb->query(
				"DELETE FROM $table WHERE content_type != 'forum'"
			);
		}

		// Clear the embeddings cache
		$wpdb->query( "DELETE FROM " . WPF()->tables->ai_embeddings_cache );

		return (int) $deleted;
	}

	/**
	 * Semantic search using cosine similarity
	 *
	 * @param array  $query_vector Query embedding vector
	 * @param int    $limit        Maximum results
	 * @param array  $filters      Optional filters: forumid, userid, etc.
	 * @return array Search results with scores
	 */
	public function semantic_search( $query_vector, $limit = 10, $filters = [] ) {
		global $wpdb;

		if ( empty( $query_vector ) ) {
			return [];
		}

		// Extract minimum score threshold from filters (0-1 scale, 0 = no filtering)
		$min_score = isset( $filters['min_score'] ) ? (float) $filters['min_score'] : 0;

		// Normalize query vector
		$query_magnitude = $this->calculate_magnitude( $query_vector );
		$normalized_query = $this->normalize_vector( $query_vector, $query_magnitude );

		// Build WHERE clause for filters
		$where = [];
		$values = [];

		if ( ! empty( $filters['forumid'] ) ) {
			$where[] = 'forumid = %d';
			$values[] = (int) $filters['forumid'];
		}

		if ( ! empty( $filters['forumids'] ) && is_array( $filters['forumids'] ) ) {
			$placeholders = implode( ',', array_fill( 0, count( $filters['forumids'] ), '%d' ) );
			$where[] = "forumid IN ($placeholders)";
			$values = array_merge( $values, array_map( 'intval', $filters['forumids'] ) );
		}

		if ( ! empty( $filters['userid'] ) ) {
			$where[] = 'userid = %d';
			$values[] = (int) $filters['userid'];
		}

		if ( ! empty( $filters['exclude_topicids'] ) && is_array( $filters['exclude_topicids'] ) ) {
			$placeholders = implode( ',', array_fill( 0, count( $filters['exclude_topicids'] ), '%d' ) );
			$where[] = "topicid NOT IN ($placeholders)";
			$values = array_merge( $values, array_map( 'intval', $filters['exclude_topicids'] ) );
		}

		$where_sql = ! empty( $where ) ? 'WHERE ' . implode( ' AND ', $where ) : '';

		// Process embeddings in batches to avoid memory exhaustion on large datasets.
		// Each batch loads ~2000 rows (~9MB with 1024-dim vectors), keeping peak memory safe.
		$batch_size  = 2000;
		$offset      = 0;
		$top_results = [];

		$table = WPF()->tables->ai_embeddings;

		while ( true ) {
			$batch_query  = "SELECT id, topicid, postid, forumid, userid, embedding_vector, vector_dimensions, content_preview, content_type
			                 FROM {$table} {$where_sql}
			                 ORDER BY id ASC LIMIT %d OFFSET %d";
			$batch_values = array_merge( $values, [ $batch_size, $offset ] );
			$batch_query  = $wpdb->prepare( $batch_query, $batch_values );

			$rows = $wpdb->get_results( $batch_query, ARRAY_A );

			if ( empty( $rows ) ) {
				break;
			}

			// Calculate similarity for each embedding in this batch
			foreach ( $rows as $row ) {
				$stored_vector = $this->unpack_vector( $row['embedding_vector'] );
				$similarity    = $this->dot_product( $normalized_query, $stored_vector );

				// Skip results below minimum score threshold
				if ( $min_score > 0 && $similarity < $min_score ) {
					continue;
				}

				$top_results[] = [
					'id'              => $row['id'],
					'topicid'         => $row['topicid'],
					'postid'          => $row['postid'],
					'forumid'         => $row['forumid'],
					'userid'          => $row['userid'],
					'similarity'      => $similarity,
					'content_preview' => $row['content_preview'],
					'content_type'    => $row['content_type'] ?? 'forum',
				];
			}

			// Trim accumulated results to top N to bound memory growth
			if ( count( $top_results ) > $limit * 3 ) {
				usort( $top_results, function( $a, $b ) {
					return $b['similarity'] <=> $a['similarity'];
				} );
				$top_results = array_slice( $top_results, 0, $limit );
			}

			// If fewer rows than batch size, we've processed everything
			if ( count( $rows ) < $batch_size ) {
				break;
			}

			$offset += $batch_size;

			// Free batch memory before loading next batch
			unset( $rows );
		}

		if ( empty( $top_results ) ) {
			return [];
		}

		// Final sort by similarity (descending)
		usort( $top_results, function( $a, $b ) {
			return $b['similarity'] <=> $a['similarity'];
		} );

		// Return top N results
		return array_slice( $top_results, 0, $limit );
	}

	/**
	 * Find similar items with caching
	 *
	 * @param string $source_type 'topic' or 'post'
	 * @param int    $source_id   Source item ID
	 * @param int    $limit       Maximum results
	 * @param bool   $force_refresh Force cache refresh
	 * @return array Similar items with scores
	 */
	public function find_similar( $source_type, $source_id, $limit = 10, $force_refresh = false ) {
		// Check cache first
		if ( ! $force_refresh ) {
			$cached = $this->get_cached_similar( $source_type, $source_id, $limit );
			if ( $cached !== null ) {
				return $cached;
			}
		}

		// Get source embedding
		if ( $source_type === 'topic' ) {
			// Get first post's embedding for topic
			global $wpdb;
			$first_postid = $wpdb->get_var( $wpdb->prepare(
				"SELECT postid FROM " . WPF()->tables->ai_embeddings . " WHERE topicid = %d ORDER BY postid ASC LIMIT 1",
				$source_id
			) );
			if ( ! $first_postid ) {
				return [];
			}
			$embedding = $this->get_embedding( $first_postid );
		} else {
			$embedding = $this->get_embedding( $source_id );
		}

		if ( ! $embedding || empty( $embedding['vector'] ) ) {
			return [];
		}

		// Search for similar items
		$filters = [
			'exclude_topicids' => [ $embedding['topicid'] ], // Exclude self
		];

		$results = $this->semantic_search( $embedding['vector'], self::MAX_CACHED_SIMILAR, $filters );

		// Group by topic (forum) or post (CPT) and take best match per group
		$by_group = [];
		foreach ( $results as $result ) {
			$content_type = $result['content_type'] ?? 'forum';
			if ( $content_type !== 'forum' ) {
				// WordPress CPT: group by postid (topicid is 0 for all CPT)
				$group_key = 'wp_' . $result['postid'];
			} else {
				// Forum: group by topicid
				$group_key = 'topic_' . $result['topicid'];
			}
			if ( ! isset( $by_group[ $group_key ] ) || $result['similarity'] > $by_group[ $group_key ]['similarity'] ) {
				$by_group[ $group_key ] = $result;
			}
		}

		// Re-sort and limit
		$similar = array_values( $by_group );
		usort( $similar, function( $a, $b ) {
			return $b['similarity'] <=> $a['similarity'];
		} );
		$similar = array_slice( $similar, 0, self::MAX_CACHED_SIMILAR );

		// Cache results
		$this->cache_similar( $source_type, $source_id, 'topic', $similar );

		return array_slice( $similar, 0, $limit );
	}

	/**
	 * Get cached similar items
	 *
	 * @param string $source_type Source type
	 * @param int    $source_id   Source ID
	 * @param int    $limit       Maximum results
	 * @return array|null Cached results or null if not cached/expired
	 */
	private function get_cached_similar( $source_type, $source_id, $limit ) {
		global $wpdb;

		$now = current_time( 'mysql' );

		$results = $wpdb->get_results( $wpdb->prepare(
			"SELECT similar_id, similarity_score
			 FROM " . WPF()->tables->ai_embeddings_cache . "
			 WHERE source_type = %s AND source_id = %d AND expires_at > %s
			 ORDER BY rank_position ASC
			 LIMIT %d",
			$source_type,
			$source_id,
			$now,
			$limit
		), ARRAY_A );

		if ( empty( $results ) ) {
			return null;
		}

		// Enrich with topic data
		$enriched = [];
		foreach ( $results as $row ) {
			$enriched[] = [
				'topicid'    => (int) $row['similar_id'],
				'similarity' => (float) $row['similarity_score'],
			];
		}

		return $enriched;
	}

	/**
	 * Cache similar items
	 *
	 * @param string $source_type  Source type
	 * @param int    $source_id    Source ID
	 * @param string $similar_type Similar item type
	 * @param array  $similar      Similar items
	 */
	private function cache_similar( $source_type, $source_id, $similar_type, $similar ) {
		global $wpdb;

		// Delete existing cache for this source
		$wpdb->delete(
			WPF()->tables->ai_embeddings_cache,
			[
				'source_type' => $source_type,
				'source_id'   => $source_id,
			],
			[ '%s', '%d' ]
		);

		// Insert new cache entries
		$expires_at = date( 'Y-m-d H:i:s', time() + self::CACHE_TTL );

		foreach ( $similar as $rank => $item ) {
			$wpdb->insert(
				WPF()->tables->ai_embeddings_cache,
				[
					'source_type'      => $source_type,
					'source_id'        => $source_id,
					'similar_type'     => $similar_type,
					'similar_id'       => $item['topicid'],
					'similarity_score' => $item['similarity'],
					'rank_position'    => $rank + 1,
					'expires_at'       => $expires_at,
				],
				[ '%s', '%d', '%s', '%d', '%f', '%d', '%s' ]
			);
		}
	}

	/**
	 * Invalidate cache for an item
	 *
	 * @param string $type Item type
	 * @param int    $id   Item ID
	 */
	public function invalidate_cache( $type, $id ) {
		global $wpdb;

		// Delete where this item is the source
		$wpdb->delete(
			WPF()->tables->ai_embeddings_cache,
			[
				'source_type' => $type,
				'source_id'   => $id,
			],
			[ '%s', '%d' ]
		);

		// Delete where this item is in similar results
		$wpdb->delete(
			WPF()->tables->ai_embeddings_cache,
			[
				'similar_type' => $type,
				'similar_id'   => $id,
			],
			[ '%s', '%d' ]
		);
	}

	/**
	 * Cleanup expired cache entries
	 */
	public function cleanup_expired_cache() {
		global $wpdb;

		$now = current_time( 'mysql' );

		$wpdb->query( $wpdb->prepare(
			"DELETE FROM " . WPF()->tables->ai_embeddings_cache . " WHERE expires_at < %s",
			$now
		) );
	}

	/**
	 * Get embedding statistics
	 *
	 * @return array Statistics
	 */
	public function get_stats() {
		global $wpdb;

		$stats = [
			'total_embeddings' => 0,
			'total_topics'     => 0,
			'total_posts'      => 0,
			'cache_entries'    => 0,
			'storage_size_mb'  => 0,
			'last_indexed_at'  => null,
		];

		// Combined query: total embeddings, unique topics, unique posts, last indexed
		// (4 queries → 1 on the same table)
		$combined = $wpdb->get_row(
			"SELECT COUNT(*) as total_embeddings,
				COUNT(DISTINCT topicid) as total_topics,
				COUNT(DISTINCT postid) as total_posts,
				MAX(updated_at) as last_indexed_at
			FROM " . WPF()->tables->ai_embeddings,
			ARRAY_A
		);
		if ( $combined ) {
			$stats['total_embeddings'] = (int) $combined['total_embeddings'];
			$stats['total_topics']     = (int) $combined['total_topics'];
			$stats['total_posts']      = (int) $combined['total_posts'];
			$stats['last_indexed_at']  = $combined['last_indexed_at'];
		}

		// Cache entries (separate table)
		$stats['cache_entries'] = (int) $wpdb->get_var(
			"SELECT COUNT(*) FROM " . WPF()->tables->ai_embeddings_cache
		);

		// Storage size (approximate)
		$table_name = WPF()->tables->ai_embeddings;
		$table_status = $wpdb->get_row(
			$wpdb->prepare( "SHOW TABLE STATUS WHERE Name = %s", $table_name )
		);
		if ( $table_status ) {
			$stats['storage_size_mb'] = round( ( $table_status->Data_length + $table_status->Index_length ) / 1024 / 1024, 2 );
		}

		return $stats;
	}

	/**
	 * Get counts of indexed WordPress CPT content grouped by post type.
	 *
	 * Queries the ai_embeddings table for non-forum content and returns
	 * counts keyed by wp_{content_type} (e.g., wp_docs, wp_page, wp_post).
	 * This matches the format returned by the cloud API's /rag/wordpress/status.
	 *
	 * @return array<string, int> Post type => count (e.g., ['wp_docs' => 145, 'wp_page' => 20])
	 */
	public function get_wp_indexed_counts() {
		global $wpdb;

		$results = $wpdb->get_results(
			"SELECT content_type, COUNT(*) as cnt
			 FROM " . WPF()->tables->ai_embeddings . "
			 WHERE content_type != 'forum'
			 GROUP BY content_type",
			ARRAY_A
		);

		$counts = [];
		if ( $results ) {
			foreach ( $results as $row ) {
				$counts[ 'wp_' . $row['content_type'] ] = (int) $row['cnt'];
			}
		}

		return $counts;
	}

	/**
	 * Check if local storage should be recommended based on post count
	 *
	 * @return array Recommendation with status and message
	 */
	public function get_storage_recommendation() {
		$post_count = WPF()->post->get_count();

		if ( $post_count < 10000 ) {
			return [
				'status'  => 'good',
				'message' => wpforo_phrase( 'Excellent choice for your forum size. Local storage will provide fast performance.', false ),
				'icon'    => 'yes-alt',
			];
		} elseif ( $post_count < self::PERFORMANCE_THRESHOLD ) {
			return [
				'status'  => 'good',
				'message' => wpforo_phrase( 'Good choice. Local storage with caching will provide acceptable performance.', false ),
				'icon'    => 'yes',
			];
		} elseif ( $post_count < 100000 ) {
			return [
				'status'  => 'warning',
				'message' => sprintf(
					wpforo_phrase( 'Your forum has %s posts. Local storage may have slower search performance. Consider using cloud storage for better results.', false ),
					number_format( $post_count )
				),
				'icon'    => 'warning',
			];
		} else {
			return [
				'status'  => 'not_recommended',
				'message' => sprintf(
					wpforo_phrase( 'Your forum has %s posts. Cloud storage (gVectors) is recommended for optimal performance.', false ),
					number_format( $post_count )
				),
				'icon'    => 'dismiss',
			];
		}
	}

	// =========================================================================
	// Vector Math Utilities
	// =========================================================================

	/**
	 * Pack float array to binary
	 *
	 * @param array $vector Float array
	 * @return string Binary packed data
	 */
	private function pack_vector( $vector ) {
		return pack( 'f*', ...$vector );
	}

	/**
	 * Unpack binary to float array
	 *
	 * @param string $binary Binary data
	 * @return array Float array
	 */
	private function unpack_vector( $binary ) {
		$floats = unpack( 'f*', $binary );
		return array_values( $floats );
	}

	/**
	 * Calculate vector magnitude
	 *
	 * @param array $vector Float array
	 * @return float Magnitude
	 */
	private function calculate_magnitude( $vector ) {
		$sum = 0;
		foreach ( $vector as $val ) {
			$sum += $val * $val;
		}
		return sqrt( $sum );
	}

	/**
	 * Normalize vector to unit length
	 *
	 * @param array $vector    Float array
	 * @param float $magnitude Pre-computed magnitude (optional)
	 * @return array Normalized vector
	 */
	private function normalize_vector( $vector, $magnitude = null ) {
		if ( $magnitude === null ) {
			$magnitude = $this->calculate_magnitude( $vector );
		}

		if ( $magnitude == 0 ) {
			return $vector;
		}

		return array_map( function( $val ) use ( $magnitude ) {
			return $val / $magnitude;
		}, $vector );
	}

	/**
	 * Calculate dot product of two vectors
	 *
	 * @param array $a Vector A
	 * @param array $b Vector B
	 * @return float Dot product
	 */
	private function dot_product( $a, $b ) {
		$sum = 0;
		$len = min( count( $a ), count( $b ) );

		for ( $i = 0; $i < $len; $i++ ) {
			$sum += $a[ $i ] * $b[ $i ];
		}

		return $sum;
	}

	/**
	 * Calculate cosine similarity between two vectors
	 *
	 * @param array $a Vector A
	 * @param array $b Vector B
	 * @return float Similarity score (0-1)
	 */
	public function cosine_similarity( $a, $b ) {
		$dot = $this->dot_product( $a, $b );
		$mag_a = $this->calculate_magnitude( $a );
		$mag_b = $this->calculate_magnitude( $b );

		if ( $mag_a == 0 || $mag_b == 0 ) {
			return 0;
		}

		return $dot / ( $mag_a * $mag_b );
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit