On Nov 7, 2010, at 11:50 AM, sdumitriu (SVN) wrote:

> Author: sdumitriu
> Date: 2010-11-07 11:50:04 +0100 (Sun, 07 Nov 2010)
> New Revision: 32582
> 
> Modified:
>   
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
> Log:
> XWIKI-4726: Forward links are not refactored on rename causing broken links 
> on rename to different space.
> Fixed.
> Patch from Stefan Abageru applied with some cleanup.
> 
> Modified: 
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
> ===================================================================
> --- 
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
>  2010-11-06 21:41:04 UTC (rev 32581)
> +++ 
> platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java
>  2010-11-07 10:50:04 UTC (rev 32582)
> @@ -5670,12 +5670,48 @@
>             xwiki.saveDocument(backlinkDocument, saveMessage, true, context);
>         }
> 
> -        // Step 4: Delete the old document
> +        // Step 4: Refactor the links contained in the document
> +        XWikiDocument newDocument = xwiki.getDocument(newDocumentReference, 
> context);
> +        XDOM newDocumentXDOM = newDocument.getXDOM();
> +        List<LinkBlock> linkBlockList = 
> newDocumentXDOM.getChildrenByType(LinkBlock.class, true);
> +        XWikiDocument currentContextDoc = context.getDoc();
> +
> +        try {
> +            boolean modified = false;
> +            for (LinkBlock linkBlock : linkBlockList) {
> +                ResourceReference linkReference = linkBlock.getReference();
> +                if (linkReference.getType().equals(ResourceType.DOCUMENT)) {
> +                    context.setDoc(this);
> +                    DocumentReference currentLinkReference =
> +                        
> this.currentDocumentReferenceResolver.resolve(linkReference.getReference());
> +
> +                    context.setDoc(newDocument);
> +                    DocumentReference newLinkReference =
> +                        
> this.currentDocumentReferenceResolver.resolve(linkReference.getReference());
> +
> +                    if (!newLinkReference.equals(currentLinkReference)) {
> +                        modified = true;
> +                        
> linkReference.setReference(this.compactWikiEntityReferenceSerializer
> +                            .serialize(currentLinkReference));
> +                    }
> +                }
> +            }
> +            // Set new content and save document if needed
> +            if (modified) {
> +                newDocument.setContent(newDocumentXDOM);
> +                xwiki.saveDocument(newDocument, context);
> +            }
> +        } finally {
> +            // Restore original context
> +            context.setDoc(currentContextDoc);
> +        }

cool thanks stefan and sergiu. IMO we would benefit to introduce some private 
methods (or not so private if it has a general purpose) to perform this step 
(or other steps, I don't remember the existing code). checktyle wouldn't 
probably pass on this method as it is now.

BTW we have a refactoring module and I think it would be a good place to move 
the rename implementation to this new module... when someone has the time and 
energy to do so...

Thanks
-Vincent

> +
> +        // Step 5: Delete the old document
>         xwiki.deleteDocument(this, context);
> 
> -        // Step 5: The current document needs to point to the renamed 
> document as otherwise it's pointing to an
> +        // Step 6: The current document needs to point to the renamed 
> document as otherwise it's pointing to an
>         // invalid XWikiDocument object as it's been deleted...
> -        clone(xwiki.getDocument(newDocumentReference, context));
> +        clone(newDocument);
>     }
> 
>     /**
_______________________________________________
devs mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/devs

Reply via email to