Benutzer:Wibogel/Source

Aus Piratenwiki
Wechseln zu: Navigation, Suche

Lizenz

CC-BY-SA icon.svg

Der Quelltext wurde wie alle Artikel dieses Wikis unter der CC-BY-SA 3.0-Lizenz veröffentlicht.

Quelltext

#!/usr/bin/perl -w

use MediaWiki::API;
use Tree::Simple;
use Digest::MD5 qw(md5_hex);
use utf8;

#
#
# READ THE SITE STRUCTURE
#
#
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = 'http://wiki.piratenpartei.de/wiki/api.php';

# log in
$mw->login( { lgname => 'Wibogel', lgpassword => '*****' } ) || die "Wikibot wibogel: ".$mw->{error}->{code} . ': ' . $mw->{error}->{details};

# get all pages with Prefix "Landesverband" in namespace 246 (LSA)
my $articles = $mw->list ( {
	action => 'query',
	list => 'allpages',
	apprefix => 'Landesverband',
	apnamespace => '246',
	apfilterredir => 'nonredirects',
	aplimit => '2000'

} ) || die "Wikibot wibogel: ".$mw->{error}->{code} . ': ' . $mw->{error}->{details};

# to collect all data, we use a simple graph and assume tree structure caused by wiki name conventions
my $tree = Tree::Simple->new("/", Tree::Simple->ROOT);

# hang in subtrees
foreach (@{$articles}) {
	addTree( $tree, $_->{title} );
}

# uncomment to get a html encoded preview
#use Tree::Simple::View::HTML;
#my $viewer = Tree::Simple::View::HTML->new( $tree );
#print $viewer->expandAll();

# inform users about the bots acitvity
$wikiheader = qq~<noinclude>
{{Achtung|Diese Wikiseite wird durch den Wiki-Bot [[Benutzer:Wibogel|Wibogel]] automatisch gepflegt. Wende Dich zuerst an die Betreiber des Bots, bevor Du &Auml;nderungen vornimmst, der Bot wird sonst Deine &Auml;nderungen ungepr&uuml;ft &uuml;berschreiben.}}
</noinclude>~;

# header of the whole frame
$wikiheader = $wikiheader.qq~{{LSA-Menu}}{{#ifeq: {{#titleparts:{{FULLPAGENAME}}|1|0}} | LSA:Landesverband | {{#ifeq: {{FULLPAGENAME}} | LSA:Landesverband | <!-- LSA-Menu wird auf der Startseite durch ein individuelles Men&uuml; ersetzt --> | <div style="padding:0px;margin:0px;float:right;width:320px;overflow:auto;">{{Vorlage:Textbox-Orange|Navigation|2=\n~;#<span><!-- span as workaround --></span>~;

# get the menu itself
my $menu = printTree( $tree->getChild(0), -1 , "LSA:Landesverband", 1, 0 , "");

# finish with footer, usage and licence information
my $wikifooter ="<p style=\"text-align:center;font-size:10pt;\">[[Benutzer:Wibogel|Menü bearbeiten]]</p>}}";
$wikifooter = $wikifooter.qq~
</div>
{{#if: {{{1|}}}
|<div style="margin-right:270px;">{{{1}}}</div>
}} | <!-- Menü wird nur im Bereich des Landesverbandes angezeigt --> }}
}}
<noinclude>
= Verwendung =
<pre>
{{PST:Header|1=
Seiteninhalt
}}
</pre>

Der Seiteninhalt muss nicht zwingend in die Header-Vorlage &uuml;bernommen wird. Bei sich &uuml;berlappenden Div-Boxen (beispielsweise mit dem Men&uuml;) oder &Uuml;berlappungen mit anderen Vorlagen aus dem Wiki, ist das allerdigns empfehlenswert.

= Info =
* Filter f&uuml;r den Internet Explorer (IE) werden zur Zeit nicht vom Wiki unterst&uuml;tzt.
* R&uuml;ckfragen bitte per http://www.twitter.com/0l1h7

= Grundlage f&uuml;r diese Arbeit =
* Theme Name: PiratenSachsenTheme
* Theme URI: http://www.piraten-sachsen.de/theme
* Descripxion: This theme is made for the pirate party saxony. The whole * Template, Design and it containing files are licencend under CC-BY-SA
* Author: Thomas
* Author URI: http://twitter.com/dasllama
* Version: 0.9
* Tags: orange, multiple sidebars, cms, widgets
</noinclude>
~;

# connect all information for output
$content = $wikiheader.$menu.$wikifooter;

#
#
# CHECK IF CHANGES EXIST
#
#

my $lastmd5 = "";
open( FILE, "+>/PATH/TO/lsamd5" ) || die "WikiBot wibogel: Couldn't read former md5sum.";
read(FILE,  $lastmd5, 32);
my $newmd5 = md5_hex( $content );

# uncomment for debugging
# print $content."\n";

if( $lastmd5 ne  $newmd5 ) # && 0 == 1 ) # include for debugging
{

	#
	#
	# APPLY CHANGES
	#
	#

	# write information to the wiki
	my $pagename = "Vorlage:LSA-Header";
	my $ref = $mw->get_page( { title => $pagename } );

	unless ( $ref->{missing} ) {
		my $timestamp = $ref->{timestamp};
		$mw->edit( {
			action => 'edit',
			title => $pagename,
			basetimestamp => $timestamp, # to avoid edit conflicts
			text => $content
		} )
		|| die "Wikibot wibogel: ".$mw->{error}->{code} . ': ' . $mw->{error}->{details};
	}
	print FILE $newmd5;
}
close( FILE );

#
# addTree( Tree::Simple $tree, String $wikipagename)
#
sub addTree {
	my $tree  = shift;		# tree/subtree node
	my $wikipagename = shift;	# pagename of the node/subtree, which should be added

	my $result = -1;

	# take out the next node's value
	if($wikipagename =~ /\//g)
	{
		$result = pos($wikipagename);
	}

	my $nextNodeValue;

	# check if its a leaf
	if( $result > 1 )
	{
		$nextNodeValue = substr( $wikipagename, 0, $result - 1 );
		$wikipagename = substr( $wikipagename, $result );
	}
	else
	{
		$nextNodeValue = $wikipagename;
		$wikipagename = "";
	}

	my $nextNode;

	# for each child node add new subtree recursively
	# first check, if new node allready exists
	my @children = $tree->getAllChildren();
	foreach (@children)
	{
		if( $_->getNodeValue() eq $nextNodeValue )
		{
			$nextNode = $_;
			last;
		}
	}

	# create new subtree, if the new node does not exist in the tree
	if( !defined( $nextNode ) )
	{
		$nextNode = Tree::Simple->new( $nextNodeValue, $tree );
	}

	# add the new node to the tree
	if( $wikipagename ne "" )
	{
		addTree( $nextNode, $wikipagename );
	}
}

#
# printTree( Tree::Simple $tree, int $depth, String $link, int $siblingsc, int $siblingscc, String $depthline )
#
sub printTree {
	my $tree       = shift; # tree structure for recursive traverse
	my $depth      = shift; # depth of traverse
	my $link       = shift; # wikilink according to the node
	my $siblingsc  = shift; # number of siblings
	my $siblingscc = shift; # sibling id of the node
	my $depthline  = shift;	# visualisation of the tree structure

	my $nodeValue         = $tree->getNodeValue();
	my $menu              = "";
	my $fork              = "&#9500;";

	my @children          = $tree->getAllChildren();
	my $childrenc         = scalar @children;
	my $depthlineAddition = "&#9474;";

	if( $siblingscc + 1 == $siblingsc )
	{
		$fork = "&#9492;";
		$depthlineAddition = "&nbsp;";
	}
	else
	{
		if( $siblingscc == 0 && $depth < 0 )
		{
			$fork = "&#9484;";
		}
	}

	my $newtree = "&#9516;";
	# check if we are root
	if( $nodeValue eq "LSA:Landesverband" )
	{
		$nodeValue = "Startseite";
		$depthline = "";
	}


	# if there are no children, make a simple menu entry
	# if there are children, print submenu frame and call prinTree recursively
	if( $childrenc == 0 )
	{
		$menu = "<div style=\"white-space:nowrap;\">{{PST:MenuTree|$depthline$fork|&#9642;}} [[$link|$nodeValue]]</div>\n";
	}
	else
	{

		$menu = "{{#ifeq: {{#titleparts:{{FULLPAGENAME}}|".($depth+2)."|0}} | $link | <div style=\"white-space:nowrap;\">{{PST:MenuTree|$depthline$fork$newtree|&#9662;}} [[$link|$nodeValue]]</div>\n";

		my $childrencc = 0;
		foreach (@children)
		{
			$menu = $menu.printTree($_, $depth + 1, $link."/".$_->getNodeValue(), $childrenc, $childrencc, $depthline.$depthlineAddition );
			$childrencc++;
		}

		$menu = $menu."| <div style=\"white-space:nowrap;\">{{PST:MenuTree|$depthline$fork|&#9656;}}  [[$link|$nodeValue]]</div>}}\n";

	}
	return($menu);
}

$mw->logout();
$tree->DESTROY;

exit;