[MKDoc-commit] Internal links redirect fix from Sam.

chris at mkdoc.demon.co.uk chris at mkdoc.demon.co.uk
Mon Jul 25 10:33:41 BST 2005


Log Message:
-----------
Internal links redirect fix from Sam.

Tags:
----
mkdoc-1-6

Modified Files:
--------------
    mkd/MKDoc/Util:
        LinkParser.pm

-------------- next part --------------
Index: LinkParser.pm
===================================================================
RCS file: /var/spool/cvs/mkd/MKDoc/Util/Attic/LinkParser.pm,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -LMKDoc/Util/LinkParser.pm -LMKDoc/Util/LinkParser.pm -u -r1.1.2.2 -r1.1.2.3
--- MKDoc/Util/LinkParser.pm
+++ MKDoc/Util/LinkParser.pm
@@ -258,19 +258,51 @@
     my $path = $uri->path;
     $path .= '/' unless $path =~ m!/$!;
 
+    # do a look for this path, returning immediately if it was found
+    my ($id, undef) = $self->lookup_path($path, $dbh);
+    return $self->{document_id} = $id if $id;
+
+    # walk down path components replacing them with redirects as
+    # needed.  This is necessary to deal with interstitial redirects.
+    my @old = grep { defined and length } split('/', $path);
+    my @new;
+    while (@old) {
+        # step down one level
+        push @new, shift @old;
+        
+        # lookup this path for redirects
+        my $this_path = '/' . join('/', @new) . '/';
+        ($id, $path) = $self->lookup_path($this_path, $dbh);
+
+        # if we found something and it's not a direct link then base
+        # the new path off that
+        if ($path and $path ne $this_path) {
+            @new = grep { defined and length } split('/', $path);
+        }
+    }
+
+    # return what we found, undef if no match was found in the search
+    # or the ID if one was
+    return $self->{document_id} = $id;
+}
+
+# lookup a path in Document and Redirect.  Returns the ID and
+# corrected path, if one was found.
+sub lookup_path {
+    my ($self, $path, $dbh) = @_;
+
     my ($id) = $dbh->selectrow_array('SELECT ID
                                       FROM Document 
                                       WHERE Full_Path = ?', undef, $path);
     return $self->{document_id} = $id if $id;
 
     # it wasn't in Document, but it might be in Redirect
-    ($id) = $dbh->selectrow_array('SELECT d.ID
-                                   FROM Document d, Redirect r
-                                   WHERE d.Full_Path = r.New_Path AND
-                                         r.Old_Path = ?',
-                                  undef, $path);
+    ($id, $path) = $dbh->selectrow_array('SELECT d.ID, d.Full_Path
+                                          FROM Document d, Redirect r
+                                          WHERE d.Full_Path = r.New_Path AND
+                                                r.Old_Path = ?', undef, $path);
 
-    return $self->{document_id} = $id ? $id : 0;
+    return ($id, $path);
 }
 
 sub uri { shift->{uri} }


More information about the MKDoc-commit mailing list