Related Stories section for Geeklog

In my view a related stories section is the only lacking feature of Geeklog, how hard is it to implement?
- Well let's find out

Lots of bugs fixed in the getStoryRelations method (2011-03-16)

Why bother?

If a user ends up on your site from a search engine or an external link, this will bring attention to the fact that you have other content that might prove useful.
It also may increase the ranking from various search engines. Links equals traffic.

Listing the stories

Before you start modifying anything, make sure you've read this entire article at least once.

The idea is simple, when adding a story in geeklog you can specify meta keywords, why not use those to find other stories with similar keywords and list those stories as relevant? It shouldn't be too hard to implement.

Let's also insert a crude "score" system for sorting the related topics by relevance. This is how I imagined it

  • Separate all keywords of the current story into an array
  • For each keyword, select all stories with the keyword in it.
  • If the story is already listed because of another keyword, increase the "relevance score"
  • Sounds simple enough so let's code!

     

    Coding

    I won't bother making this as a plugin or block, in fact I'm not a very good php programmer and don't know much about geeklog at all. Instead I will modify some of the core files in GeekLog. I'd much rather use a plugin but as I couldn't find any and all I want is to get my site up and running with a minimum of effort I'll just do it the "dirty" and "quick" way. Feel free to use this article as a reference or hint for developing a plugin. Don't worry if you spot a bug or performance issue, they are.. intended, but feel free to correct them and let me know of better ways to do it.

    Files

    I use the professional theme, in case you're running another theme you may have to adapt some of the filenames mentioned here.

    Download the following files

  • layout/professional/storytext.thtml
  • layout/professional/featuredstorytext.thtml
  • system/lib-story.php
  • Actually those are the only files I modified..

     

    lib-story.php

    I really can't explain the code, it's a lot of quirks and a severe case of "happy hacking".
    Paste the following code in an empty spot within this file:

     

     function array_sort($array, $on, $order=SORT_ASC)
    {
        $new_array = array();
        $sortable_array = array();
    
        if (count($array) > 0) {
            foreach ($array as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $k2 => $v2) {
                        if ($k2 == $on) {
                            $sortable_array[$k] = $v2;
                        }
                    }
                } else {
                    $sortable_array[$k] = $v;
                }
            }
    
            switch ($order) {
                case SORT_ASC:
                    asort($sortable_array);
                break;
                case SORT_DESC:
                    arsort($sortable_array);
                break;
            }
    
            foreach ($sortable_array as $k => $v) {
                $new_array[$k] = $array[$k];
            }
        }
    
        return $new_array;
    }
    
     function getStoryRelations($currentStoryId, $keyWords)
     { 
      global $_CONF, $_TABLES;
      $return = "";
      $keyWordArray = explode(",", $keyWords);
      $storyArray = null;
      $keyWord = "";  
      foreach ($keyWordArray as $keyWord)
      {	
    	$keyWord = trim($keyWord);			
    	if ($keyWord != "") {
    		$result = DB_query("select sid, `title` as storytitle from ".$_TABLES['stories']. " where sid<>'".$currentStoryId."' and draft_flag=0 and meta_keywords like '%".$keyWord."%' limit 0, 5");			
    		while ($dataRow = DB_fetchArray($result, true))
    		{
    		  $currentRow = $dataRow;	  
    		  $relStoryId = $currentRow['sid'];	  	  
    		  $relStoryTitle = $currentRow['storytitle'];	  	  
    		  $relStoryInfo = null;	  
    		  if (!isset($storyArray[$relStoryId]))
    		  { $relStoryInfo = array('title'=>$relStoryTitle, 'score'=>0, 'storyid'=>$relStoryId); }
    		  else
    		  {  $relStoryInfo = $storyArray[$relStoryId]; }
    		  $relStoryInfo['score'] = $relStoryInfo['score'] + 1;	     
    		  $storyArray[$relStoryId] = $relStoryInfo;	  	  	  
    		} 
    	} 
      }
      $storyArray = array_sort($storyArray, 'score', SORT_DESC); 
      $maxItems = 5;
      if (count($storyArray) > 0) {
          $return = '

    Related Articles

     

    • '; foreach($storyArray as $storyInfo) { if ($maxItems == 0) { break; } $maxItems --; $articleUrl = COM_buildUrl($_CONF['site_url'] . '/article.php?story='. $storyInfo['storyid']); $score = $storyInfo['score']; $return.='
    • '.$storyInfo['title']."
    • "; } $return.="

     

    "; } return $return; }

     

    Now you have the basic functionality in place and we only need to add the call to this method and set a variable on the $article object. Locate the line that says:

    if( $_CONF['hideviewscount'] != 1 ) {
    Inside the method STORY_renderArticle and insert a blank line above it.
    Insert the following line of code in the new blank line:
    $article->set_var( 'story_relations', getStoryRelations($story->getSid(), $story->DisplayElements('meta_keywords')) );

     

    Modifying the templates storytext.thml and featuredstorytext.html

    In my case I just want to list the relevant articles in direct connection with the body text of the story, as if I had entered them manually. With both of these files, locate the line that says

    {story_text_no_br}
    And insert this line:
    {story_relations}
    Directly in the line below.

     

    Done, upload the files to their original place and test it. Feel free to modify, improve, use and abuse this code.

    p.s

    You DID take a backup of the original files, right?

Page 1 /

 Internet Explorer 9, JScript Error c00c023f

Ajax and Error c00c023f

After updating to Internet Explorer 9, a previously working ajax driven site of mine stopped working sporadically.

In this short article you will see how to reproduce, and how to solve the problem.

 Has NSA released the smoking gun regarding UFO's?

I’ve seen a lot of bloggers and web pages lately, claiming that the NSA has released a document containing hard proof that radio signals of extraterrestrial origin has been received. The document in question actually DOES state that signals has been observed, and it also contains a visual description of the signal in question.

 InstallerExplorer shows hidden installed products

InstallerExplorer is a small lightning fast utility that allows you to list, view detailed information and uninstall software installed on your machine without your knowledge. Some products mark themselves as “System Components” to hide from windows internal list in add/remove programs. This application also allows you to see where in the registry the product was found, and where the product is installed. Many users are not aware that windows installer packages at the moment of installation are cached in a windows folder using a rather cryptic filename, this utility takes care of those also.

 Nasa plans landing on asteroid in 2025

By the year 2025 Nasa plans to land astronauts on an asteroid, anyone thinking of the movie Armageddon yet?

 Would this interest you?

Page 3 / 11