Menü aufrufen
Toggle preferences menu
Persönliches Menü aufrufen
Nicht angemeldet
Ihre IP-Adresse wird öffentlich sichtbar sein, wenn Sie Änderungen vornehmen.

Benutzer:Rene/BlockSpammer

aus Stargate Wiki, dem deutschsprachigen Stargate-Lexikon
Version vom 2. November 2008, 00:49 Uhr von Rene (Diskussion | Beiträge) (Die Seite wurde neu angelegt: BlockSpammer ist eine Mediawiki Erweiterung die verhindert das unangemeldete Benutzer (IPs) externe Links in Artikel eintragen. ==BlockSpammer.php== <pre> <?php if ( !...)
(Unterschied) ← Nächstältere Version | Aktuelle Version zeigen (Unterschied) | Nächstjüngere Version → (Unterschied)

BlockSpammer ist eine Mediawiki Erweiterung die verhindert das unangemeldete Benutzer (IPs) externe Links in Artikel eintragen.

BlockSpammer.php

<?php
if ( ! defined( 'MEDIAWIKI' ) )
	die();
    
/**
 * Prüfe Artikel von IP Benutzern auf Spam
 *
 * 1. IPs dürfen keine externen links anlegen
 */

$wgExtensionFunctions[] = 'wfBlockSpammer';
$wgExtensionCredits['other'][] = array(
	'name' => 'BlockSpammer',
	'author' => 'René Raule',
	'description' => 'Nicht angemeldete Benutzer dürfen keine externen Links anlegen.',
	'descriptionmsg' => 'block_spammer_error-desc',
	'url' => 'http://www.stargate-wiki.de/index.ph/Benutzer:Rene/BlockSpammer',
);

$dir = dirname(__FILE__) . '/';
$wgExtensionMessagesFiles['BlockSpammer'] = $dir . 'BlockSpammer.i18n.php';
// Welche RegEx zeigen einen Spammer
$wgBlockSpammerPatterns = array (
		"/^http/i",  // if you want to block titles of articles that are URLs
	);

$wgHooks['ArticleSave'][] = 'wfCheckBlockSpammer';

function wfBlockSpammer() {
	wfLoadExtensionMessages( 'BlockSpammer' );
}

function wfCheckBlockSpammer (&$article,&$user,&$text ) {
	global $wgBlockSpammerPatterns;
	global $wgOut;
  if($user->mId == 0) {
    $t = PHPDiff($article->mContent."\n",$text."\n",2);
  	foreach ($wgBlockSpammerPatterns as $re) {
	  	if (preg_match($re, $t)) {
		  	// too bad you can't pass parameter to errorpage
		  	$wgOut->errorpage('block_spammer_error_page_title', 'block_spammer_error' );
		  	return false;
  		}	
  	}
  }

	return true;
}

## PHPDiff returns the differences between $old and $new, formatted
## in the standard diff(1) output format. when mode=0
## mode=1 shows deleted part
## mode=2 shows added part
function PHPDiff($old,$new,$mode=0) 
{
   # split the source text into arrays of lines
   $t1 = explode("\n",$old);
   $x=array_pop($t1); 
   if ($x>'') $t1[]="$x\n\\ No newline at end of file";
   $t2 = explode("\n",$new);
   $x=array_pop($t2); 
   if ($x>'') $t2[]="$x\n\\ No newline at end of file";

   # build a reverse-index array using the line as key and line number as value
   # don't store blank lines, so they won't be targets of the shortest distance
   # search
   foreach($t1 as $i=>$x) if ($x>'') $r1[$x][]=$i;
   foreach($t2 as $i=>$x) if ($x>'') $r2[$x][]=$i;

   $a1=0; $a2=0;   # start at beginning of each list
   $actions=array();

   # walk this loop until we reach the end of one of the lists
   while ($a1<count($t1) && $a2<count($t2)) {
     # if we have a common element, save it and go to the next
     if ($t1[$a1]==$t2[$a2]) { $actions[]=4; $a1++; $a2++; continue; } 

     # otherwise, find the shortest move (Manhattan-distance) from the
     # current location
     $best1=count($t1); $best2=count($t2);
     $s1=$a1; $s2=$a2;
     while(($s1+$s2-$a1-$a2) < ($best1+$best2-$a1-$a2)) {
       $d=-1;
       foreach((array)@$r1[$t2[$s2]] as $n) 
         if ($n>=$s1) { $d=$n; break; }
       if ($d>=$s1 && ($d+$s2-$a1-$a2)<($best1+$best2-$a1-$a2))
         { $best1=$d; $best2=$s2; }
       $d=-1;
       foreach((array)@$r2[$t1[$s1]] as $n) 
         if ($n>=$s2) { $d=$n; break; }
       if ($d>=$s2 && ($s1+$d-$a1-$a2)<($best1+$best2-$a1-$a2))
         { $best1=$s1; $best2=$d; }
       $s1++; $s2++;
     }
     while ($a1<$best1) { $actions[]=1; $a1++; }  # deleted elements
     while ($a2<$best2) { $actions[]=2; $a2++; }  # added elements
  }

  # we've reached the end of one list, now walk to the end of the other
  while($a1<count($t1)) { $actions[]=1; $a1++; }  # deleted elements
  while($a2<count($t2)) { $actions[]=2; $a2++; }  # added elements

  # and this marks our ending point
  $actions[]=8;

  # now, let's follow the path we just took and report the added/deleted
  # elements into $out.
  $op = 0;
  $x0=$x1=0; $y0=$y1=0;
  $out = array(); $del = array(); $add = array();
  foreach($actions as $act) {
    if ($act==1) { $op|=$act; $x1++; continue; }
    if ($act==2) { $op|=$act; $y1++; continue; }
    if ($op>0) {
      $xstr = ($x1==($x0+1)) ? $x1 : ($x0+1).",$x1";
      $ystr = ($y1==($y0+1)) ? $y1 : ($y0+1).",$y1";
      if ($op==1) $out[] = "{$xstr}d{$y1}";
      elseif ($op==3) $out[] = "{$xstr}c{$ystr}";
      while ($x0<$x1) { $out[] = '< '.$t1[$x0]; $del[] = $t1[$x0]; $x0++; }   # deleted elems
      if ($op==2) $out[] = "{$x1}a{$ystr}";
      elseif ($op==3) $out[] = '---';
      while ($y0<$y1) { $out[] = '> '.$t2[$y0]; $add[] = $t2[$y0]; $y0++; }   # added elems
    }
    $x1++; $x0=$x1;
    $y1++; $y0=$y1;
    $op=0;
  }
  $out[] = ''; $del[] = ''; $add[] = '';
  if ($mode == 0) { return join("\n",$out); }
  if ($mode == 1) { return join("\n",$del); }
  if ($mode == 2) { return join("\n",$add); }
}

BlockSpammer.i18n.php

<?php
/**
 * Internationalisation file for extension BlockSpammer.
 *
 */

$messages = array();

/**
 * English
 */
$messages['en'] = array(
	'block_spammer_error_page_title' => 'Spam suspicion',
	'block_spammer_error-desc'       => 'IP User are not allowed to make external links',
	'block_spammer_error'            => 'Sorry, your edit seems to be spam. You must register to make this edit.',
);

/** Message documentation (Message documentation)
 * @author Purodha
 */
$messages['qqq'] = array(
	'block_spammer_error-desc' => 'Shown in [[Special:Version]]',
);


/** German (Deutsch)
 */
$messages['de'] = array(
	'block_spammer_error_page_title' => 'Spam Verdacht',
	'block_spammer_error-desc' => 'Nicht angemeldete Benutzer dürfen keine externen Links anlegen.',
	'block_spammer_error' => 'Die letzte Änderungen kann nicht gespeichert werden da der Verdacht auf Spam besteht. Änderungen dieser Art sind nur angemeldeten Benutzern erlaubt.',
);