<?php  if (!defined('PmWiki')) exit();

/*

  SearchCloud - This script makes a list of the terms used to search the site and
     inludes a markup (:searchcloud:) to list those terms.

 *****
 *
 *  Copyright 2018 Carlos A. Bonamigo <cabsec.pmwiki@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *****

Solved all problems posted to the recipe page

*/

$RecipeInfo['SearchCloud']['Version']  =  '20180317';

SDV($EnableSearchCloudClean , '1');
SDV($SearchCloudCleanAction , 'clearcloud');
SDV($SearchCloudFile , ".searchcloud");
SDV($SearchCloudReps , array('link=','/','?','$','`','"',"'"));
SDV($SearchCloudStyles , "
   .searchclouditem > a{
      text-decoration:none;
      color:black;
   }
   .searchclouditem > a:hover{
     text-decoration:underline
   }
"
);

$SCrq = $_REQUEST['q'];
$SCrq = stripmagic($SCrq);
$SCrq = str_replace(
   $SearchCloudReps,'',$SCrq);

$HTMLStylesFmt['searchcloud'] = $SearchCloudStyles;

Markup_e("searchcloud", "directives", '/\\(:searchcloud:\\)/i', "SearchCloudList()");

function SCStrToArr($x){

if($x == '') return false;
if(is_array($x)) $xa = $x;
  if (!is_array($x)){
     if (strpos($x,' ') !== false){
       $xa = explode(' ',$x); # multi ;
       foreach($xa as $k => $v){
         $xo[$v] = @$xo[$v] + 1;
       }
     }else{ $xo = array("$x" => '1');} # single
  }
return $xo;
}

function SearchCloudIn($x){
global $WorkDir, $SearchCloudFile, $action;

  if($action != 'search') return;
  $SCfile = "$WorkDir/$SearchCloudFile";
  $SCtxt = file_get_contents($SCfile);
  $SCtxt = unserialize($SCtxt);
  $SCin = SCStrToArr($x);
    foreach($SCin as $k => $v){
      $SCout[$k] = $v;
    }
    if($SCtxt != ''){
      foreach($SCtxt as $k => $v){
        if($SCout[$k] != '') $SCout[$k] = $v + 1;
        if($SCout[$k] == '') $SCout[$k] = $v;
      }
    }
  file_put_contents($SCfile,serialize($SCout));
}

function SearchCloudClean(){
global $action,$WorkDir, 
  $EnableSearchCloudClean, 
  $SearchCloudFile;

  if($action != $SearchCloudCleanAction) return;
  if($EnableSearchCloudClean == false) return;
  $SCfile = "$WorkDir/$SearchCloudFile";
  @unlink($SCfile);
}

function SearchCloudList() {
global $action, $WorkDir, $SearchCloudFile;

$SCfile = "$WorkDir/$SearchCloudFile";
  if(!file_exists($SCfile)) touch($SearchCloudFile);
  elseif(file_exists($SCfile)){
    $SCtext= file_get_contents($SCfile);
    $SCitens = unserialize($SCtext);

    $output = '';

    foreach ($SCitens as $k => $v){
      if($k!="0" && $k != "")
	$output=$output.'<span class="searchclouditem"'.
             ' style="font-size:'.($v+10).'px;font-weight:'.($v+500).'">'.
	     '<a class="searchcloudlink" href="'.$ScriptUrl.
             '?action=search&amp;q='.$k.'">'.$k.'</a></span> &nbsp; ';
      }

    return $output;

  }else{

    return;

  }
}

If ($EnableSearchCloudClean == 1 &&
     $action == $SearchCloudCleanAction) SearchCloudClean();

# tip from Holger decided to str_replace instead of stristr
if ($action == "search" && $q != "") SearchCloudIn($SCrq);