Benutzer:Bully1966/CategoriesCount

Aus Perrypedia
Zur Navigation springen Zur Suche springen

Mit der Extension CategoriesCount für MediaWiki kann man die Anzahl der Artikel in einer Kategorie gezählt werden und die Anzahl in den laufenden Text einbinden.

Beispiel

Verbesserungsvorschläge

  • Zur Zeit keine

Versionshistory

  • 19.09.2007 - 1.0.0 - Freigabe der ersten Version

Installation

Die Installation geht mit folgenden Schritten ab:

  • Erzeuge aus dem Quellcode, der im nächsten Kapitel steht eine Datei mit Namen CategoriesCount.php.
  • Kopiere die Datei in das Unterverzeichnis extensions der MediaWiki Installation.
  • Füge folgende Zeile
    include("extensions/CategoriesCount.php");
    in die LocalSettings.php unterhalb des Includes der DefaultSettings.php Datei ein.

Hinweis: Probiere dies erst an einem Testsystem aus, bevor du das in einem laufenden System machst. Denn Fehler können geschehen.

Quellcode

<?php
# Copyright (C) 2007 Thomas Klein <tkl-online@gmx.de>
# http://www.mediawiki.org/
# 
# 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


/**
* Extension to counting of article in a category
*
* Use with:
*
* <categorycount>CategoryName[;CategoryName]</useredit>
*
* @author Thomas Klein <tkl-online@gmx.de>
* @package MediaWiki
* @subpackage Extensions
*/

/**
* Version History
*
* 19.09.2007 1.0.0
*  - Release of the first version
*/

if( !defined( 'MEDIAWIKI' ) ) {
  die();
}

require_once( 'Sanitizer.php' );
require_once( 'HttpFunctions.php' );

$wgExtensionFunctions[] = "wfCategoriesCount";
$wgExtensionCredits['parserhook'][] = array(
                                      'name' => 'CategoriesCount',
                                      'author' => 'Thomas Klein',
                                      'url' => 'http://www.perrypedia.proc.org/Benutzer:Bully1966/CategoriesCount',
                                      'description' => 'Extension to counting of article on a categories',
                                      'version'=>'1.0.0');

function wfCategoriesCount() {
  global $wgParser;
  
  $wgParser->setHook( "categoriescount" , 'counting_categories' ) ;

  return true;
}

function counting_categories( $text ) {
  global $wgVersion, $wgOut;
  global $wgParser;

  $ret = "" ;
  
  if ( version_compare( $wgVersion, '1.8', '<' ) ) {
    $ret = "1.8.x  of MediaWiki required";
    return $ret ;
  }

  $wgParser->disableCache();

  $totalall = 0;

  // Parse each parameter
  $params = explode(';', $text);
  foreach ($params as $CategoryName) 
  {
    $total = countArticleOnCategory( $CategoryName );

    if ($uid != $total) {
      $totalall = $totalall + $total;
    } else {
      $totalall = -1;  
      break;
    }
  }
  
  if ($totalall != -1) {
    global $wgLang;

    $ret = $wgLang->formatNum( $totalall );
  } else {
    $ret = "Kategorie nicht bekannt";  
  }

  return $ret ;
}

/**
 * Count the number of edits of a userid
 *
 * @param int $uid The user ID to check
 * @return array
 */
function countArticleOnCategory( $CategoryName ) {
  $fname = 'CategoriesCount::countArticleOnCategory';

  $dbr    =& wfGetDB( DB_SLAVE );
  $table  = $dbr->tableName( 'categorylinks' );
  $sql    = "SELECT count(*) AS count FROM $table WHERE cl_to = \"$CategoryName\"";
  $res    = $dbr->query( $sql, $fname );

  $row = $dbr->fetchObject( $res );
  return $row->count;
}

?>