I was pretty surprised that there was no related-pages action that allowed the wiki to "guess" which pages were related to a certain page and link to them. After creating my wiki pages and linking manually to other pages, I realized quickly that it would get out of hand if I did not do something to automate the process - which is detailed below. What this action does is it "guesses" which wiki pages are related to the current page and it will produce a list of links to them.

The way it works is through categories: the action will first determine which pages are categories, and then which categories the current page belongs to. It will then select an X amount of pages from those categories and link to them (X amount - default is 5, though it can be specified). As just stated, you can specify the amount and also "template" which changes the way the action spits out the list; the two current methods are through just regular new lines and through <li> elements (bullet points) in HTML. Default is through new lines.

The action is called through: {{related}}, or with parameters: {{related max="10" template="li"}}
"Max" can be specified to any number - if left blank, it will spit out the default amount of 5; "template" can be set to "li" for bullet points, or it can be left blank for new lines.

This action also requires the use of the modified PageTitle() function in wikka.php, which was edited in the action: LinkUsingPageTitle. The edited PageTitle() function will be posted below as well. Because it uses the modified PageTitle function, the outputted links will be displayed as Page Title as opposed to PageTitle.


Create and save to /actions/related.php
<?
    $unwanted_categories = '';

    $tag = $this->getPageTag( );
    $tag = $this->LoadPage( $tag );
    $tag = $tag['tag'];
   
    $max = ( empty( $vars['max'] ) ) ? 5 : $vars['max'];

    $cats_query = 'SELECT * FROM `' . $this->config['table_prefix'] . 'pages` WHERE `body`=\'\{\{category\}\}\'' . $unwanted_categories;
    $cats_query = mysql_query( $cats_query );

    while( $a = mysql_fetch_array( $cats_query ) )
    {
                 
        if( $i == 0 )
        {
            $query_build = 'WHERE `to_tag`=\'' . $a[1] . '\' AND `from_tag`=\'' . $tag . '\'';
        }
            else
            {   
                $query_build .= ' OR `to_tag`=\'' . $a[1] . '\' AND `from_tag`=\'' . $tag . '\'';   
            }
        $i++;
    }

    $page_query = 'SELECT * FROM `' . $this->config['table_prefix'] . 'links` ' . $query_build;
    $page_query = mysql_query( $page_query );

    while( $q = mysql_fetch_array( $page_query ) )
    {
        $cats[] = $q[1];
    }

    for( $i = 0; $i < count( $cats ); $i++ )
    {   
        if( $i == 0 ) { $query_build = 'WHERE `to_tag`=\'' . $cats[ $i ] . '\''; }
        else{ $query_build .= ' OR `to_tag`=\'' . $cats[ $i ] . '\''; }
    }

    $page_links = 'SELECT * FROM `' . $this->config['table_prefix'] . 'links` ' . $query_build . ' AND `from_tag`!=\'' . $tag . '\' LIMIT ' . $max;
    $page_links = mysql_query( $page_links );

    while( $h = mysql_fetch_array( $page_links ) )
    {
        $site_tag = $h[0];
        $page_info = $this->LoadPage( $site_tag );

        $text = $this->PageTitle($page_info['body'], 'yes');
                if($text == '') $text = $site_tag;

   
        switch( $vars['template'] )
        {
            default:
                $link .= $this->Link( $site_tag, '', $text ) . '<br />';
            break;

            case 'li':
                $link .= '<li>' . $this->Link( $site_tag, '', $text ) .'</li>';
            break;
        }
       
    }

    echo $link;
?>


As stated above, the above action uses a modified PageTitle() function in /libs/Wakka.class.php. Find and replace function PageTitle() with:
    function PageTitle($text='', $link='no') {
        $title = "";
        // Edit by Stefan Koopmanschap: argument should be possible
        if($text=='') {
            $pagecontent = $this->page["body"];
        } else {
            $pagecontent = $text;
        }
        if (ereg( "(=){3,5}([^=\n]+)(=){3,5}", $pagecontent, $title)) {
            $formatting_tags = array("**", "//", "__", "##", "''", "++", "#%", "@@", "\"\"");
            $title = str_replace($formatting_tags, "", $title[2]);
        }
//        if ($title) return strip_tags($this->Format($title));                # fix for forced links in heading
        if ($title) return $title;
        else {
            if($link=='no') {
                return $this->GetPageTag();
            } else {
                return '';
            }
        }
    }

Again, this modified PageTitle() function is originally from LinkUsingPageTitle.

Blocking Categories from Being Listed

On my Wiki, I have a few categories that I didn't want to be listed by this action. This is how I barred the action from listing pages in those categories:
    $unwanted_categories = ' AND `tag`!=\'CategoryUnfinished\' AND `tag`!=\'CategoryReviewRequest\'';

The variable is defined as a continuing mysql query.

--SocksFan

CategoryUserContributions
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki