<?php
// trending_on_wordpress.php
// RJM Programming
// April, 2017
$prefix="";
$theswords="";

if (isset($_GET['prefix'])) {
  $prefix=urldecode($_GET['prefix']);
}

if (file_exists($prefix . "search_words.txt")) {
  $theswords=@file_get_contents($prefix . "search_words.txt"); 
}

echo "<!doctype html> \n
<html>\n
<head>\n
<title>Trending Searches on this Blog</title>\n
<sc" . "ript type='text/javascript'>\n
\n
var histogramurl='';  \n
var hwordlist='';  \n
var thecwords='';  \n
var fivething=15; \n
var minwl=3; \n
var thetwords=\"Trending Searches on this Blog\"; \n
var thecwords=\"" . str_replace("\n"," ",$theswords) . "\"; \n
\n
\n
function mcAndThen() {  \n
  if (histogramurl.indexOf(',') != -1) {  \n
    window.open(\"http://www.rjmprogramming.com.au/PHP/HistogramChart/histogram_chart.php?title=Trending Searches (\" + (\"\" + fivething).replace('+','') + '.' + minwl + \") in \" + thetwords + \" ... \" + (hwordlist) + \"&onclick=y&task=Word&desc=Count&data=\" + (histogramurl), \"_blank\", \"top=50,left=50,width=900,height=800\"); \n
    histogramurl=''; \n
    setTimeout(mcAndThen, 2000); \n
  } else {  \n
    alert(hblurbis); \n
  } \n
} \n
\n
function nthMostCommon(ammount) {  /" . "/ thanks to http://stackoverflow.com/questions/6565333/using-javascript-to-find-most-common-words-in-string 
    var wordsArray = thecwords.split(/\s/);
    var wordOccurrences = {}
    for (var i = 0; i < wordsArray.length; i++) {
        wordOccurrences['_'+wordsArray[i]] = ( wordOccurrences['_'+wordsArray[i]] || 0 ) + 1;
    }
    var result = Object.keys(wordOccurrences).reduce(function(acc, currentKey) {
        for (var i = 0; i < ammount; i++) {
            if (!acc[i]) {
                acc[i] = { word: currentKey.slice(1, currentKey.length), occurences: wordOccurrences[currentKey] };
                break;
            } else if (acc[i].occurences < wordOccurrences[currentKey]) {
                acc.splice(i, 0, { word: currentKey.slice(1, currentKey.length), occurences: wordOccurrences[currentKey] });
                if (acc.length > ammount)
                    acc.pop();
                break;
            }
        }
        return acc;
    }, []);
    return result;
}

  var hblurbis='', negoff=0;
  var huhacc=nthMostCommon(eval(100 + fivething));
  var ksofar=0, isofar=0, lasthsuff='';
  histogramurl=' ';
  while (ksofar < fivething && isofar < huhacc.length) {
    negoff=0;   
    if (huhacc[isofar].word.substring(0,1) >= 'A' && huhacc[isofar].word.substring(0,1) <= 'Z') negoff=-1;
    if ((huhacc[isofar].word + '~').indexOf(',~') != -1) huhacc[isofar].word=(huhacc[isofar].word + '~').replace(',~','');
    if (huhacc[isofar].word.trim() != '' && huhacc[isofar].word.indexOf('(') == -1 && huhacc[isofar].word.indexOf('<') == -1 && huhacc[isofar].word.indexOf('&') == -1 && huhacc[isofar].word.indexOf('=') == -1 && huhacc[isofar].word.indexOf('[') == -1 && eval(huhacc[isofar].word.length) >= eval(negoff + minwl)) {
      hblurbis+='\"' + huhacc[isofar].word.replace(/@!@/g,\" \") + '\" occurs ' + huhacc[isofar].occurences + ' times ' + String.fromCharCode(10);
      if (histogramurl != '') {
       lasthsuff=\" (\" + huhacc[isofar].occurences + \")\";
       if (histogramurl == ' ') {
        hwordlist=huhacc[isofar].word.replace(/'/g,\"`\").replace(/@!@/g,\"%20\") + lasthsuff;
        histogramurl=\" , [~\" + huhacc[isofar].word.replace(/'/g,\"`\").replace(/@!@/g,\"%20\") + \"~,\" + huhacc[isofar].occurences + \"]\";
       } else {
        hwordlist=huhacc[isofar].word.replace(/'/g,\"`\").replace(/@!@/g,\"%20\") + \" \" + hwordlist;
        histogramurl+=\" , [~\" + huhacc[isofar].word.replace(/'/g,\"`\").replace(/@!@/g,\"%20\") + \"~,\" + huhacc[isofar].occurences + \"]\";
       }
      }
      ksofar++;
    }
    isofar++;
  }
  hwordlist=hwordlist.replace(\" \", lasthsuff + \" \");
  setTimeout(mcAndThen, 2000);
  
</s" . "cript>\n
</head>\n
<body>\n
</body>\n
</html>\n
";

?>
