[Mediawiki-l] Taxonomy, navigation and hacking

Gibbons, David David.Gibbons at CaliberCollision.com
Tue Sep 27 16:32:34 UTC 2005


Got the link and I do have some code to build the dropdowns, it's more
of a case of editing this code to pull sub-categories to add to the drop
down.  Again, I'm very new to PHP and the wiki DB.

<?php
/*

 Purpose:       outputs a bulleted list of most recent
                items residing in a category, or a union
                of several categories.


 Contributors: n:en:User:IlyaHaykinson n:en:User:Amgine
 http://en.wikinews.org/wiki/User:Amgine
 http://en.wikinews.org/wiki/User:IlyaHaykinson

 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.
 http://www.gnu.org/copyleft/gpl.html

 Current feature request list
     1. Unset cached of calling page
     2. Alternative formatting (not just unordered list)
     3. Configurable sort order, ascending/descending
     4. RSS feed output?

 To install, add following to LocalSettings.php
   include("extensions/dynamicpagelist.php");

*/

   $wgDLPminCategories = 1;                // Minimum number of
categories to look for
   $wgDLPmaxCategories = 3;                // Maximum number of
categories to look for
   $wgDLPMinResultCount = 1;               // Minimum number of results
to allow
   $wgDLPMaxResultCount = 50;              // Maximum number of results
to allow
   $wgDLPAllowUnlimitedResults = true;     // Allow unlimited results
   $wgDLPAllowUnlimitedCategories = false; // Allow unlimited categories

/**
 * Name: DynamicPageList 1.5 for MW 1.4.6
 * ***
 * Purpose: To support MW 1.4.x
 * ***
 * @package Mediawiki
 * @subpackage DynamicPageList
 * @author n:User:IlyaHaykinson
 * @author m:User:Amgine
 * @version $Alpha 0.1$
 * *** */


$wgExtensionFunctions[] = "wfDynamicPageList";


/**
 * Name: wfDynamicPageList
 * ***
 * Purpose: Establishes DynamicPageList extension
 * ***
 * @return NULL
 * *** */ 
function wfDynamicPageList() {
    global $wgParser, $wgMessageCache;
   
    $wgMessageCache->addMessages( array(
                                        'dynamicpagelist_toomanycats' =>
'DynamicPageList: Too many categories!',
                                        'dynamicpagelist_toofewcats' =>
'DynamicPageList: Too few categories!',
                                        'dynamicpagelist_noresults' =>
'DynamicPageList: No results!',
                                        )
                                  );


    $wgParser->setHook( "DynamicPageList", "RenDynamicPageList" );
    $wgParser->replaceVariables($text, end( $wgParser->mArgStack ));
}
 
/**
 * Name: DynamicPageList
 * ***
 * Purpose: The callback function for converting the input parameters to
HTML output
 * ***
 * @param string $input cr/nl delimited parameter list consisting of
key=value.
 * @return string $output raw html inserted into page.
 * *** */
function RenDynamicPageList( $input ) {
    global $wgUser, $wgContLang;
    global $wgDLPminCategories,
$wgDLPmaxCategories,$wgDLPMinResultCount, $wgDLPMaxResultCount;
    global $wgDLPAllowUnlimitedResults, $wgDLPAllowUnlimitedCategories;
    global $wgMessageCache, $wgOutputPage;
    global $wgTitle, $wgUseTidy;

    // disable cache
    $dbw =& wfGetDB( DB_MASTER );
    $dbw->update( 'cur', array( 'cur_touched' => $dbw->timestamp( time()
+ 120 ) ), 
        array( 
                'cur_namespace' => $wgTitle->getNamespace(), 
                'cur_title' => $wgTitle->getDBkey() 
                ), 'BrowserOsDetect' 
        );
    $aParams = array();
    $bCountSet = false;
    $sStartList = '<ul>';
    $sEndList = '</ul>';
    $sStartItem = '<li>';
    $sEndItem = '</li>';

    $sOrderMethod = 'categoryadd';
    $sOrder = 'descending';

    $bNamespace = false;
    $iNamespace = 0;

    $bSuppressErrors = false;
    $bShowNamespace = true;

    $aParams = explode("\n", $input);

    foreach($aParams as $sParam)
    {
      $aParam = explode("=", $sParam);
      if( count( $aParam ) < 2 )
         continue;
      $sType = strtolower(trim($aParam[0]));
      $sArg = trim($aParam[1]);
      if ($sType == 'category')
      {
        $title = Title::newFromText( $sArg );
        if( is_null( $title ) )
          continue;
        $aCategories[] = $title; 
      }
      else if ($sType == 'notcategory')
      {
        $title = Title::newFromText( $sArg );
        if( is_null( $title ) )
          continue;
        $aExcludeCategories[] = $title; 
      }
      else if ('namespace' == $sType)
      {
        //$ns = $wgContLang->getNsIndex($sArg);
        //if (NULL != $ns)
        //{
          //$iNamespace = $ns;
          //$bNamespace = true;
        //}
        //else
        //{
          $iNamespace = intval($sArg);
          if ($iNamespace >= 0)
          {
            $bNamespace = true;
          }
          else
          {
            $bNamespace = false;
          }
        //}
      }
      else if ('count' == $sType)
      {
        //ensure that $iCount is a number;
        $iCount = IntVal( $sArg );
        $bCountSet = true;
      }
      else if ('mode' == $sType)
      {
        switch ($sArg)
        {
        case 'none':
          $sStartList = '';
          $sEndList = '';
          $sStartItem = '';
          $sEndItem = '<br/>';
          break;
        case 'ordered':
          $sStartList = '<ol>';
          $sEndList = '</ol>';
          $sStartItem = '<li>';
          $sEndItem = '</li>';
          break;
        case 'unordered':
        default:
          $sStartList = '<ul>';
          $sEndList = '</ul>';
          $sStartItem = '<li>';
          $sEndItem = '</li>';
          break;
        }
      }
      else if ('order' == $sType)
      {
        switch ($sArg)
        {
        case 'ascending':
          $sOrder = 'ascending';
          break;
        case 'descending':
        default:
          $sOrder = 'descending';
          break;
        }
      }
      else if ('ordermethod' == $sType)
      {
        switch ($sArg)
        {
        case 'lastedit':
          $sOrderMethod = 'lastedit';
          break;
        case 'alphabetic':
          $sOrderMethod = 'alphabetic';
          break;
        case 'categoryadd':
        default:
          $sOrderMethod = 'categoryadd';
          break;
        }
      }
      else if ('suppresserrors' == $sType)
      {
        if ('true' == $sArg)
          $bSuppressErrors = true;
      }
      else if ('shownamespace' == $sType)
      {
        if ('false' == $sArg)
          $bShowNamespace = false;
      }
    }

    $iCatCount = count($aCategories);
    $iExcludeCatCount = count($aExcludeCategories);
    $iTotalCatCount = $iCatCount + $iExcludeCatCount;


    if ($iCatCount < 1 && false == $bNamespace)
    {
      if (false == $bSuppressErrors)
        return htmlspecialchars( wfMsg( 'dynamicpagelist_noincludecats'
) ); // "!!no included categories!!";
      else
        return '';
    }

    if ($iTotalCatCount < $wgDLPminCategories)
    {
      if (false == $bSuppressErrors)
        return htmlspecialchars( wfMsg( 'dynamicpagelist_toofewcats' )
); // "!!too few categories!!";
      else
        return '';
    }

    if ( $iTotalCatCount > $wgDLPmaxCategories &&
!$wgDLPAllowUnlimitedCategories )
    {
      if (false == $bSuppressErrors)
        return htmlspecialchars( wfMsg( 'dynamicpagelist_toomanycats' )
); // "!!too many categories!!";
      else
        return '';
    }

    if ($bCountSet)
    {
      if ($iCount < $wgDLPMinResultCount)
        $iCount = $wgDLPMinResultCount;
      if ($iCount > $wgDLPMaxResultCount)
        $iCount = $wgDLPMaxResultCount;
    }
    else
    {
      if (!$wgDLPAllowUnlimitedResults)
      {
        $iCount = $wgDLPMaxResultCount;
        $bCountSet = true;
      }
    }


    //build the SQL query
    $dbr =& wfGetDB( DB_SLAVE );
    $sPageTable = $dbr->tableName( 'cur' );
    $categorylinks = $dbr->tableName( 'categorylinks' );
    $sSqlSelectFrom = "SELECT cur_namespace, cur_title FROM
$sPageTable";

    if (true == $bNamespace)
      $sSqlWhere = ' WHERE cur_namespace='.$iNamespace.'  AND
cur_is_redirect=0 AND cur_id = c1.cl_from ';
    else
      $sSqlWhere = ' WHERE cur_id = c1.cl_from ';


    for ($i = 0; $i < $iCatCount; $i++) {
      $sSqlSelectFrom .= " INNER JOIN $categorylinks AS c" .
($iCurrentTableNumber+1);
      $sSqlSelectFrom .= ' ON cur_id =
c'.($iCurrentTableNumber+1).'.cl_from';
      $sSqlSelectFrom .= ' AND c'.($iCurrentTableNumber+1).'.cl_to='.
        $dbr->addQuotes( $aCategories[$i]->getDbKey() );

      $iCurrentTableNumber++;
    }

    for ($i = 0; $i < $iExcludeCatCount; $i++) {
      $sSqlSelectFrom .= " LEFT OUTER JOIN $categorylinks AS c" .
($iCurrentTableNumber+1);
      $sSqlSelectFrom .= ' ON cur_id =
c'.($iCurrentTableNumber+1).'.cl_from';
      $sSqlSelectFrom .= ' AND c'.($iCurrentTableNumber+1).'.cl_to='.
        $dbr->addQuotes( $aExcludeCategories[$i]->getDbKey() );

      $sSqlWhere .= ' AND c'.($iCurrentTableNumber+1).'.cl_to IS NULL';

      $iCurrentTableNumber++;
    }

    if ('descending' == $sOrder)
      $sSqlOrder = 'DESC';
    else
      $sSqlOrder = 'ASC';

    if ('lastedit' == $sOrderMethod)
      $sSqlWhere .= ' ORDER BY cur_touched ';
    elseif ('alphabetic' == $sOrderMethod)
      $sSqlWhere .= ' ORDER BY cur_title ';
    else
      $sSqlWhere .= ' ORDER BY c1.cl_timestamp ';

    $sSqlWhere .= $sSqlOrder;
    
    if ($bCountSet)
    {
      $sSqlWhere .= ' LIMIT ' . $iCount;
    }


    //DEBUG: output SQL query 
    //$output = $sSqlSelectFrom . $sSqlWhere . "<br />\n";    

    // process the query
    $res = $dbr->query($sSqlSelectFrom . $sSqlWhere);
        
    $sk =& $wgUser->getSkin();

    if ($dbr->numRows( $res ) == 0) 
    {
      if (false == $bSuppressErrors)
        return htmlspecialchars( wfMsg( 'dynamicpagelist_noresults' ) );
      else
        return '';
    }

    //start unordered list
    $output .= $sStartList . "\n";
        
    //process results of query, outputing equivalent of
<li>[[Article]]</li> for each result,
    //or something similar if the list uses other startlist/endlist
    while ($row = $dbr->fetchObject( $res ) ) {
      $title = Title::makeTitle( $row->cur_namespace, $row->cur_title);
      $output .= $sStartItem;
      if (true == $bShowNamespace)
        $output .= $sk->makeKnownLinkObj($title);
      else
        $output .= $sk->makeKnownLinkObj($title,
htmlspecialchars($title->getText()));
      $output .= $sEndItem . "\n";
    }

    //end unordered list
    $output .= $sEndList . "\n";

    return $output;
}
?>

-----Original Message-----
From: mediawiki-l-bounces at Wikimedia.org
[mailto:mediawiki-l-bounces at Wikimedia.org] On Behalf Of Andrea Forte
Sent: Monday, September 26, 2005 6:11 PM
To: MediaWiki announcements and site admin list
Subject: Re: [Mediawiki-l] Taxonomy, navigation and hacking

I don't know if this is quite what you were looking for, but I bet you
could modify this to do nested links...

http://www.dynamicdrive.com/dynamicindex17/switchcontent.htm

andrea






More information about the MediaWiki-l mailing list