< */ %% Create the following new action directory/file: **##actions/title/title.php##** %%(php) AddCustomHeader("".$string.""); ?> %% **Usage** %% {{title string="My page title with important keywords"}} %% ===##meta## action=== Requires the ##AddCustomHeader()## method, a relatively new addition to Wikka. Check your ##libs/Wakka.php## function library if in doubt. **##actions/meta/meta.php##** %%(php) AddCustomHeader(""); ?> %% **Usage** %% {{meta name="description" content="Specializing in machining and finishing of fine widgets"}} %% ===Display of search term frequencies in the ##referrers## handler page=== Create the following new directory/handler. Be sure the plugin path precedes the handlers path in your ##wikka_handler_path## in ##wikka.config.php##: **##plugins/handlers/referrers/referrers.php##** %%(php) = $max) { // add $max to the list; then break out of the loop $opts[] = $max; break; } // when $limit is reached, it becomes the new start and increment for the next 'range' $inc = $limit; } return $opts; } // constants define('DEBUG',FALSE); # @@@ set TRUE to generate debugging output define('SEARCH_LIKE','LIKE'); # search string operator define('SEARCH_UNLIKE','NOT LIKE'); # search string operator define('HITS_DEFAULT', '1'); # (was 0 for referrers, 1 for sites) define('HITS_MIN_OPTION', '>='); define('HITS_MAX_OPTION', '<='); define('HOURS_LIMIT',2); # days expressed as hours @@@ could be made configurable define('DAYS_MAX', $this->GetConfigValue('referrers_purge_time')); define('DAYS_DEFAULT', '7'); # default period to retrieve @@@ make configurable $days_limits = array(7,30,90,365); # ranges for days dropdown @@@ make configurable // ------------------------------------- // initialize parameters $qs = NULL; # search string sanitized for SQL query $qx = ''; # search string sanitized for XHTML $qo = 1; # search string option $h = HITS_DEFAULT; # hits number $ho = 1; # hits option $days = DAYS_DEFAULT; # period selection $global = FALSE; # global (site) or this page only $sites = FALSE; # referrers or referring sites $refdel = NULL; # referrer records deleted $bladd = NULL; # blacklist records added $r = 1; # row counter // ------------------------------------- // initialize internal variables $string_option = SEARCH_LIKE; # LIKE or NOT LIKE $hits_option = HITS_MIN_OPTION; # MIN (>=) or MAX (<=) $tag = $this->GetPageTag(); $isAdmin = $this->IsAdmin(); $loggedin = ($isAdmin) ? TRUE : (bool)$this->GetUser(); $pre = $this->config['table_prefix']; $par = ''; $query = ''; $rows = 0; // ------------------------------------- // User-interface strings define('NAME_GLOBAL',$this->GetConfigValue('wakka_name')); define('TITLE_REFERRERS','External pages linking to %s'); define('TITLE_SITES','Domains linking to %s'); define('TITLE_SEARCH_TERMS','(Search term rankings)'); define('REPORT_BLACKLIST','Referrer records removed: %d; blacklist records added: %d'); define('TOTAL_REFERRERS','Total: %d referrers linking to %s'); define('TOTAL_SITES','Total: %d referrers linking to %s'); // current target # you can use NAME_GLOBAL instead of 'this site' if the site name is short enough # @@@ JW: choice between 'this site' and NAME_GLOBAL could be set via configuration (later) define('TARGET_GLOBAL','this site'); define('TARGET_PAGE', $tag); // menus don't use current target but *possible* targets define('MENU_REFERRERS','Referrers to %s'); define('MENU_SITES','Domains linking to %s'); define('MENU_REFERRERS_PAGE',sprintf(MENU_REFERRERS,TARGET_PAGE)); define('MENU_SITES_PAGE',sprintf(MENU_SITES,TARGET_PAGE)); define('MENU_REFERRERS_GLOBAL',sprintf(MENU_REFERRERS,TARGET_GLOBAL)); define('MENU_SITES_GLOBAL',sprintf(MENU_SITES,TARGET_GLOBAL)); define('MENU_BLACKLIST','Blacklisted sites'); define('FORM_LEGEND','Filter view:'); define('FORM_URL_OPT_REFERRERS','URL:'); define('FORM_URL_OPT_SITES','Domain:'); define('FORM_URL_OPT_TITLE','Select search option'); define('FORM_URL_OPT_1','containing'); define('FORM_URL_OPT_0','not containing'); define('FORM_URL_STRING_LABEL','string'); define('FORM_URL_STRING_TITLE','Enter a search string'); define('FORM_HITS_OPT_LABEL','Hits:'); define('FORM_HITS_OPT_TITLE','Select filter option'); define('FORM_HITS_OPT_1','at least'); define('FORM_HITS_OPT_0','no more than'); define('FORM_HITS_NUM_LABEL','hits'); define('FORM_HITS_NUM_TITLE','Enter number of hits'); define('FORM_DAYS_OPT_LABEL','Period:'); define('FORM_DAYS_OPT_TITLE','Select period in days'); define('FORM_DAYS_NUM_LABEL','days'); define('FORM_SUBMIT_URLS','Show referrers'); define('FORM_SUBMIT_SEARCH_TERMS','Rank_search_terms'); define('FORM_SUBMIT_SEARCH_TERMS_LABEL','Rank search terms'); define('FORM_SUBMIT_SITES','Show referring domains'); define('LIST_PERIOD_HOURS',' (last %d hours)'); define('LIST_PERIOD_DAYS',' (last %d days)'); define('LIST_SUMMARY_REFERRERS','Filtered list of referrers, with hits%s, sorted by number of hits'); define('LIST_SUMMARY_SITES','Filtered list of referring sites, with hits%s, sorted by number of hits'); define('LIST_HEAD_HITS','Hits'); define('LIST_HEAD_ACTION','Action'); define('LIST_HEAD_LIST_REFERRERS','Referrers'); define('LIST_HEAD_SEARCH_TERM', 'Search term'); define('LIST_HEAD_FREQ', 'Frequency'); define('LIST_HEAD_LIST_SITES','Referring hosts'); define('LIST_REF_UNKNOWN','unknown'); # make sure the *exact* same string is used in the whitelist definition (delete_referrer.php) define('LIST_ACTION_DESC',' and links to blacklist spammers'); define('LIST_ACTION_BLACKLIST','Blacklist'); define('LIST_ACTION_BLACKLIST_TITLE','Blacklist this domain'); define('LOGIN_NOTE','You need to login to display referrers.'); // show result counts for target define('LIST_RESULT_COUNTER_REFERRERS','Filtered result: %d referrers matching these criteria'); # @@@ does not take account of singular define('LIST_RESULT_COUNTER_SITES','Filtered result: %d domains matching these criteria'); # @@@ does not take account of singular define('LIST_RESULT_NONE','Filtered result:'); // show 'no result' summary for target define('NONE_NOTE_REFERRERS','No referrers found matching these criteria'); define('NONE_NOTE_SITES','No domains found matching these criteria'); // ------------------------------------- // fetch and validate parameters // get query string and comparison method if (isset($_POST['q'])) { $tq = trim(strip_tags($_POST['q'])); if ('' != $tq) { $qs = mysql_real_escape_string($tq); $qx = $this->htmlspecialchars_ent($tq); if (isset($_POST['qo'])) { $qo = ($_POST['qo'] == '1') ? 1 : 0; $string_option = ($qo == 1) ? SEARCH_LIKE : SEARCH_UNLIKE; } } } // get hits and min or max criteria if (isset($_POST['h'])) { $h = (is_numeric($_POST['h'])) ? abs((int)$_POST['h']) : HITS_DEFAULT; # cast to positive integer if numeric } if (isset($_POST['ho'])) { $ho = ($_POST['ho'] == '1') ? 1 : 0; $hits_option = ($ho == 1) ? HITS_MIN_OPTION : HITS_MAX_OPTION; } // get period, not longer than purge time if (isset($_POST['days'])) { $days = (is_numeric($_POST['days'])) ? min(abs((int)$_POST['days']),DAYS_MAX) : DAYS_DEFAULT; } // get search target: page or site (global) if (isset($_POST['global'])) { $global = (bool)$_POST['global']; } elseif (isset($_GET['global'])) { $global = (bool)$_GET['global']; } $iglobal = (int)$global; // get precision: URLS (referrers) or referring sites (domains) if (isset($_POST['sites'])) { $sites = (bool)$_POST['sites']; } elseif (isset($_GET['sites'])) { $sites = (bool)$_GET['sites']; } $isites = (int)$sites; //get reported values (no validation needed, just cast to integer) if (isset($_GET['refdel'])) { $refdel = (int)$_GET['refdel']; $bladd = (isset($_GET['bladd'])) ? $bladd = (int)$_GET['bladd'] : 0; } // derive parameters for 'current' links if (1 == $global) { if ('' != $par) $par .= '&'; $par .= 'global=1'; } if (1 == $sites) { if ('' != $par) $par .= '&'; $par .= 'sites=1'; } // ------------------------------------- // build query from chunks depending on criteria chosen if ($loggedin) { $query = 'SELECT referrer'; if ($sites) { // add 'host' = domain extracted from referrring URL using this algorithm: // find first char after http:// : LOCATE('//',referrer)+2 // find first / after this: LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1 // calculate length: (LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1) - (LOCATE('//',referrer)+2) // get host (standard): SUBSTRING(referrer FROM (LOCATE('//',referrer)+2) FOR ((LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1) - (LOCATE('//',referrer)+2))) // *or* // get host (MySQL-specific): SUBSTRING(SUBSTRING_INDEX(referrer,'/',3) FROM (LOCATE('//',referrer)+1)) $protocol_host = 'SUBSTRING_INDEX(referrer,"/",3)'; # protocol and host: everything before first single / $start_host = 'LOCATE("//",referrer)+2'; # start position of host: after // $query .= ', SUBSTRING('.$protocol_host.' FROM ('.$start_host.')) AS host'; // NOTE: COUNT() cannot use a derived column name but it *can* take an expression $query .= ', COUNT(SUBSTRING('.$protocol_host.' FROM ('.$start_host.'))) AS num'; $query .= ' FROM '.$pre.'referrers'; if (!$global) { $query .= " WHERE page_tag = '".mysql_real_escape_string($tag)."'"; } #if ($days != $max_days) if ($days != DAYS_MAX) { $query .= (!strpos($query,'WHERE')) ? ' WHERE' : ' AND'; $query .= ' TO_DAYS(NOW()) - TO_DAYS(time) <= '.$days; # filter by period } $query .= ' GROUP BY host '; if (isset($qs)) { $query .= ' HAVING host '.$string_option." '%".$qs."%'"; # filter by string (derived column so we use HAVING) } if ($hits_option != HITS_MIN_OPTION || $h != 1) { $query .= (!strpos($query,'HAVING')) ? ' HAVING' : ' AND'; $query .= ' num '.$hits_option.' '.$h; # filter by hits number (derived column so we use HAVING) } } else { $query = 'SELECT referrer'; $query .= ', COUNT(referrer) AS num'; $query .= ' FROM '.$pre.'referrers'; if (!$global) { $query .= " WHERE page_tag = '".mysql_real_escape_string($tag)."'"; } if (isset($qs)) { $query .= (!strpos($query,'WHERE')) ? ' WHERE' : ' AND'; $query .= ' referrer '.$string_option." '%".$qs."%'"; # filter by string } #if ($days != $max_days) if ($days != DAYS_MAX) { $query .= (!strpos($query,'WHERE')) ? ' WHERE' : ' AND'; $query .= ' TO_DAYS(NOW()) - TO_DAYS(time) <= '.$days; # filter by period } $query .= ' GROUP BY referrer '; if ($hits_option != HITS_MIN_OPTION || $h != 1) { $query .= ' HAVING num '.$hits_option.' '.$h; # filter by hits number (derived column so we use HAVING) } } $query .= ' ORDER BY num DESC, referrer ASC'; # set order // get total number of referrers (NOT records!) $query_refcount = 'SELECT COUNT(DISTINCT(referrer)) AS total'; # @@@ referrer column should be indexed to make this really efficient $query_refcount .= ' FROM '.$pre.'referrers'; if (!$global) { $query_refcount .= " WHERE page_tag = '".mysql_real_escape_string($tag)."'"; } } // ------------------------------------- // execute query (if logged in) // @@@ NOTE: we don't use LoadReferrers any more since the query is now completely dynamically built if ($loggedin) { // execute query $referrers = $this->LoadAll($query); $totalrefs = $this->LoadSingle($query_refcount); } // ------------------------------------- // build UI elements // define current target $target = ($global) ? TARGET_GLOBAL : $this->Link(TARGET_PAGE); // title $title = ($sites) ? sprintf(TITLE_SITES,$target) : sprintf(TITLE_REFERRERS,$target); $title .= ($days <= HOURS_LIMIT) ? sprintf(LIST_PERIOD_HOURS,24*$days) : sprintf(LIST_PERIOD_DAYS,$days); if ($isAdmin) { if (isset($refdel)) $rptblacklisted = sprintf(REPORT_BLACKLIST,$refdel,$bladd); } if ($loggedin) { // results $tot = $totalrefs['total']; $total = ($sites) ? sprintf(TOTAL_SITES,$tot,$target) : sprintf(TOTAL_REFERRERS,$tot,$target); $creferrers = count($referrers); if ($creferrers > 0) { $result = ($sites) ? sprintf(LIST_RESULT_COUNTER_SITES,$creferrers) : sprintf(LIST_RESULT_COUNTER_REFERRERS,$creferrers); } else { $result = LIST_RESULT_NONE; } // menu elements: prevent wrapping within element (these *don't* use current target! $menu_referrers_page = str_replace(' ',' ',MENU_REFERRERS_PAGE); $menu_sites_page = str_replace(' ',' ',MENU_SITES_PAGE); $menu_referrers_global = str_replace(' ',' ',MENU_REFERRERS_GLOBAL); $menu_sites_global = str_replace(' ',' ',MENU_SITES_GLOBAL); $menu_blacklist = str_replace(' ',' ',MENU_BLACKLIST); //menu links $m_referrers_page = ''.$menu_referrers_page.''; $m_sites_page =''.$menu_sites_page.''; $m_referrers_global = ''.$menu_referrers_global.''; $m_sites_global = ''.$menu_sites_global.''; $m_blacklist = ''.$menu_blacklist.''; //build menu $menu = ''."\n"; // days dropdown content $daysopts = optionRanges($days_limits,DAYS_MAX); // form $form = $this->FormOpen('referrers','','POST'); # @@@ add parameter for id $form .= ''."\n"; $form .= '
'."\n"; $form .= ''.FORM_LEGEND.''."\n"; $form .= ' '."\n"; $form .= ' '."\n"; $form .= ' '."\n"; $form .= ''; $form .= '
'."\n"; $form .= ' '."\n"; $form .= ' '."\n"; $form .= ''."\n"; $form .= ' '; $form .= '
'."\n"; $form .= ' '."\n"; $form .= ' '."\n"; $form .= ' '."\n"; $form .= '
'."\n"; $form .= ''."\n"; $form .= ''."\n"; $form .= $this->FormClose(); // referrers list with admin link for blacklisting if ($sites) { $summary = ($isAdmin) ? sprintf(LIST_SUMMARY_SITES,LIST_ACTION_DESC) : sprintf(LIST_SUMMARY_SITES,''); $refshead = LIST_HEAD_LIST_SITES; } else { $summary = ($isAdmin) ? sprintf(LIST_SUMMARY_REFERRERS,LIST_ACTION_DESC) : sprintf(LIST_SUMMARY_REFERRERS,''); $refshead = LIST_HEAD_LIST_REFERRERS; } if ($isAdmin && !isset($_POST[FORM_SUBMIT_SEARCH_TERMS])) { $redir = ($global||$sites) ? $this->GetHandler().'&'.$par : $this->GetHandler(); # ensure we return to the same view $par = ($sites) ? 'spam_site' : 'spam_link'; $blacklisturl = $this->Href('delete_referrer','',$par.'=').'%s&redirect=%s'; $blacklink = ''.LIST_ACTION_BLACKLIST.''; } // ids - use constant for variable-content heading $idTotal = $this->makeId('hn','total'); $idResult = $this->makeId('hn','result'); } $idTitle = $this->makeId('hn','title'); // ------------------------------------- // show user interface (pre-template) if (isset($_POST[FORM_SUBMIT_SEARCH_TERMS])) // Modify title { $title .= sprintf(TITLE_SEARCH_TERMS); } echo '
'."\n"; echo '

'.$title.'

'."\n"; # debug if (DEBUG) { echo 'Query (ref): '.$query.'
'; echo 'Query (sites): '.$query_sites.'
'; echo ($global) ? 'Global: TRUE
' : 'Global: FALSE
'; echo ($sites) ? 'Sites: TRUE
' : 'Sites: FALSE
'; } # debug if ($loggedin) { if ($isAdmin && isset($refdel)) echo '

'.$rptblacklisted.'

'; echo $menu."\n"; echo '
'."\n"; echo '

'.$total.'

'."\n"; echo '
'.$form.'
'."\n"; # @@@ kluge until FormOpen() is adapted: id should actually be on form itself and div not necessary! if(isset($_POST[FORM_SUBMIT_SEARCH_TERMS])) { echo '

'.$result.'

'."\n"; echo ''."\n"; echo ''; echo ''; echo ''."\n"; echo ''."\n"; echo ''."\n"; $search_terms = array(); foreach ($referrers as $referrer) { $hits = $referrer['num']; if ($sites) { $ref = $this->htmlspecialchars_ent($referrer['host']); } else { $ref = $this->htmlspecialchars_ent($referrer['referrer']); } // Parse q= attributes from Google refs //if (0 == preg_match('/http:\/\/.*google.*?\//', $ref)) if (0 == preg_match('/q=(.*?)[& ]/', $ref)) { continue; } else { $results = array(); $num_results = preg_match('/q=(.*?)[& ]/', $ref, $results); if(0 == $num_results) { continue; } else { $results2 = explode("+", strtolower($results[1])); foreach($results2 as $result) { $search_terms[$result] += $hits; } } } } arsort($search_terms); // Output search terms foreach($search_terms as $key=>$val) { echo ''."\n"; #enable alternate row color echo ''; echo ''; echo ''; echo ''."\n"; $r++; } echo ''."\n"; echo '
'.LIST_HEAD_SEARCH_TERM.''.LIST_HEAD_FREQ.'
'.$hits.''.$key.''.$val.'
'."\n"; } else if ($creferrers != 0) { echo '

'.$result.'

'."\n"; echo ''."\n"; echo ''; echo ''; if ($isAdmin) echo ''; echo ''."\n"; echo ''."\n"; echo ''."\n"; foreach ($referrers as $referrer) { $hits = $referrer['num']; if ($sites) { $ref = $this->htmlspecialchars_ent($referrer['host']); } else { $ref = $this->htmlspecialchars_ent($referrer['referrer']); } echo ''."\n"; #enable alternate row color echo ''; echo ''; if ($isAdmin) echo ''; if ($sites) { echo ''; } else { echo ''; } echo ''."\n"; $r++; } echo ''."\n"; echo '
'.LIST_HEAD_HITS.''.LIST_HEAD_ACTION.''.$refshead.'
'.$hits.''.$hits.''.sprintf($blacklink,$ref,$redir).''.$ref.''.$ref.'
'."\n"; } else { echo '

'.$result.'

'."\n"; echo '

'.(($sites) ? sprintf(NONE_NOTE_SITES) : sprintf(NONE_NOTE_REFERRERS)).'

'."\n"; } } else { echo '

'.LOGIN_NOTE.'

'."\n"; } echo '
'."\n"; echo '
'."\n"; ?> %% **Usage** Point your browser to ##HomePage/referrers##. Select the ##Referrers to this site## tab. Modify the search parameters to your preference. Click the ##Rank Search Terms## button for a list of search terms sorted by frequency.