[Home]WikiPatches/LikeLinks

UseModWiki | WikiPatches | RecentChanges | Preferences

This patch implements LikeLink?s as seen on http://c2.com/cgi/like?LispHumor, but without site preview.
  -- jl


--- wiki.pl     Sun Apr 22 02:44:10 2001
+++ wiki_patched_like.pl        Fri Jan 10 19:13:07 2003
@@ -52,7 +52,8 @@
   %KeptRevisions %UserCookie %SetCookie %UserData %IndexHash %Translate
   %LinkIndex $InterSiteInit $SaveUrlIndex $SaveNumUrlIndex $MainPage
   $OpenPageName @KeptList @IndexList $IndexInit
-  $q $Now $UserID $TimeZoneOffset $ScriptName $BrowseCode $OtherCode);
+  $q $Now $UserID $TimeZoneOffset $ScriptName $BrowseCode $OtherCode  
+  $EnableShowLike);
 
 # == Configuration =====================================================
 $DataDir     = "/tmp/mywikidb"; # Main wiki directory
@@ -84,6 +85,7 @@
 $NewText     = "";              # New page text ("" for default message)
 $HttpCharset = "";              # Charset for pages, like "iso-8859-2"
 $UserGotoBar = "";              # HTML added to end of goto bar
+$EnableShowLike = 1;            # Enable like link
 
 # Major options:
 $UseSubpage  = 1;       # 1 = use subpages,       0 = do not use subpages
@@ -877,6 +879,11 @@
   return &ScriptLink("action=random", T('Random Page'));
 }
 
+sub GetLikeLink {
+  my $id = shift;
+  return &ScriptLink("action=showlike;id=$id", T("Like $id"), 'showlike');
+}
+
 sub ScriptLinkDiff {
   my ($diff, $id, $text, $rev) = @_;
 
@@ -1095,6 +1102,9 @@
   }
   $bartext .= " | " . &GetPageLink($RCName);
   $bartext .= " | " . &GetPrefsLink();
+  if ($EnableShowLike){
+    $bartext .= " | " . &GetLikeLink($id||$HomePage);
+  }
   if (&GetParam("linkrandom", 0)) {
     $bartext .= " | " . &GetRandomLink();
   }
@@ -2475,6 +2485,8 @@
       &DoEditPrefs();  # Also creates new ID
     } elsif ($action eq "version") {
       &DoShowVersion();
+    } elsif ($action eq "showlike" && $EnableShowLike){
+      &DoShowLike($id);
     } else {
       # Later improve error reporting
       &ReportError(Ts('Invalid action parameter %s', $action));
@@ -2906,6 +2918,15 @@
   # Later consider returning status?
 }
 
+sub DoShowLike {
+  my $id = shift;
+
+  print &GetHeader('', T("Like $id"));
+  &PrintLikeLinks($id);
+  print &GetCommonFooter();
+}
+
+
 sub DoIndex {
   print &GetHeader('', T('Index of all pages'), '');
   print '<br>';
@@ -3046,6 +3067,37 @@
     print &GetPageLink($pagename), "<br>\n";
   }
 }
+
+
+sub PrintLikeLinks {
+  my $id = shift; 
+  return "" unless $id =~ m/^([A-Z]+[^A-Z]+)(?:[A-Z]+[^A-Z]+)*([A-Z]+.*)$/;
+  my ($prefix, $suffix) = ($1,$2); 
+  my (%prefixList, %suffixList);
+  my %allHits;
+ 
+  for(map { m:([^/]+)/(.+)$: ? ($1,$2) : $_ } &AllPagesList()){
+    next if $_ eq $id;
+    if (m/^([A-Z]+[^A-Z]+)(?:[A-Z]+[^A-Z]+)*([A-Z]+.*)$/) {
+      if($prefix eq $1){
+        $prefixList{$_} = 1;
+      } elsif($suffix eq $2){
+        $suffixList{$_} = 1;
+      }
+    }
+  }
+ 
+  print "<h2>Words starting with $prefix</h2>\n";
+  print "<ul>\n";
+  print "  <li>" . &GetPageLink($_) . "</li>\n" for(keys %prefixList);
+  print "</ul>\n";
+  print "<h2>Words ending with $suffix</h2>\n";
+  print "<ul>\n";
+  print "  <li>" . &GetPageLink($_) . "</li>\n" for(keys %suffixList);
+  print "</ul>\n";
+}  
+  
+
 
 sub DoLinks {
   print &GetHeader('', &QuoteHtml(T('Full Link List')), '');


I modified the patch to handle better the links with spaces inside - they are coverted to underscores for usage in query parts of URL. The basic patch shows them as part of a start or end word. For example, link Development_FAQ will offer the start part Development_ and the end part FAQ. The underscore is mostly not wanted, that is why this patch removes the underscores to get Development and FAQ.

The modification is done only in two regular expressions in the subroutine PrintLikeLinks?; you must search the two occurences of this regular expression:

 m/^([A-Z]+[^A-Z]+)(?:[A-Z]+[^A-Z]+)*([A-Z]+.*)$/

and replace them with the following regular expression:

 m/^([A-Z]+[^A-Z_]+)(?:[A-Z_]+[^A-Z]+)*_*([A-Z]+.*)$/

  -- FerdinandPrantl


I modified the patch to search for not only the starting and ending word of the wiki name, but for all words the wiki name consits of. For example, link ThreeJoinedWords? will offer the words starting with Three, the words ending with Words, and the words containing any of the three: Three, Joined and Words. Modifications touch only the soubroutine DoShowLike?.

Find this line in the patch:

  my ($prefix, $suffix) = ($1,$2); 
and insert the following lines after it:
  $_ = $id;
  s/_//g;
  my @words = split /(?=[A-Z])/;
  for (my $i = $#words; $i > 0; $i--) {
    if ($words[$i] =~ /^[A-Z]+$/ && $words[$i - 1] =~ /^[A-Z]+$/) {
      $words[$i - 1] .= $words[$i];
      pop @words;
    }
  }

Replace this loop in the patch:

  for(map { m:([^/]+)/(.+)$: ? ($1,$2) : $_ } &AllPagesList()){
    next if $_ eq $id;
    if (m/^([A-Z]+[^A-Z]+)(?:[A-Z]+[^A-Z]+)*([A-Z]+.*)$/) {
      if($prefix eq $1){
        $prefixList{$_} = 1;
      } elsif($suffix eq $2){
        $suffixList{$_} = 1;
      }
    }
  }
with the following one (the underscore patch for regex from above is applied too):
  for (map { /([^\/]+)\/(.+)$/ ? ($1,$2) : $_ } &AllPagesList()) {
    next if $_ eq $id;
    if (/^([A-Z]+[^A-Z_]+)([A-Z_]+[^A-Z]+)*_*([A-Z]+.*)$/) {
      if ($prefix eq $1) {
        $prefixList{$_} = 1;
      } elsif ($suffix eq $3) {
        $suffixList{$_} = 1;
      }
    }
    if ($#words >= 0) {
      my $original = $_;
      s/_//g;
      my $modified = $_;
      $allHits{$original} = 1 if (grep { $modified =~ /$_/ } @words);
    }
  }

Replace the rest of the subroutine:

  print "<ul>\n";
  print "  <li>" . &GetPageLink($_) . "</li>\n" for(keys %prefixList);
  print "</ul>\n";
  print "<h2>Words ending with $suffix</h2>\n";
  print "<ul>\n";
  print "  <li>" . &GetPageLink($_) . "</li>\n" for(keys %suffixList);
  print "</ul>\n";
with the following code:
  print "<h2>Words starting with $prefix</h2>\n";
  print &GetPageLink($_), "<br>\n" for(keys %prefixList);
  print "<h2>Words ending with $suffix</h2>\n";
  print &GetPageLink($_), "<br>\n" for(keys %suffixList);
  print "<h2>Words containing part of $id</h2>\n";
  print &GetPageLink($_), "<br>\n" for(keys %allHits);

The latest change also prints the links found on a separated lines without bullets, just like the results of a search.

  -- FerdinandPrantl

  Cool! If you have a UnifiedDiff? to usemod-0.92 at hand, please substitute my patch with yours :) -- jl

Singularity "all words the wiki name consits of" .

Example, it found:

  1. Page: TestLoreIpsum
  2. Page: TesterLoreIpsum

(Tester isn`t an item of CamelCase.)


This mod goes, in order to find nodes that meet both conditions;
      if ($prefix eq $1) {
        $prefixList{$_} = 1;
-      } elsif ($suffix eq $3) {
+     } 
+     if ($suffix eq $3) {
        $suffixList{$_} = 1;
      }
--JuanmaMP

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited December 19, 2009 6:35 am by JuanmaMP (diff)
Search: