Skip to content
Snippets Groups Projects
Commit c9b78897 authored by Simon Urli's avatar Simon Urli
Browse files

XWIKI-12987: Relative links are made absolute or even broken after moving a page (#3634)

The idea of this work is to:

    * Change the way AbstractCopyOrMoveJob works to perform computation of couple source/target documents before processing them
    * Provide a way to access that map source/target documents
    * Use that information when performing a call to ReferenceRenamer to define if a relative untyped link should be handled or not

The PR provides mainly:

    - new APIs in ReferenceRenamer and MacroRefactoring to integrate the map of references that have been moved as part of same job
   - refactorings of AbstractCopyOrMoveJob:
      *  specific computation of getEntities to actually visit the hierarchy and populate the entities with the couple of source/target documents
      * new abstract methods to avoid duplications (not strictly needed for this work)
      * new method to retrieve the map of source/target documents
    - new conditions in ResourceReferenceRenamer to decide if a link should be renamed or not: most of the logic of the fix is encoded there (see also clarifications)
    - new calls in XWiki#updateLinksForRename and BackLinkUpdaterListener#updateBackLinks to give the map of source/target when calling the rename of references
    - new integration test simulating the scenario indicated in the ticket and also performing a supplementary check related to a regression found afterwards
    - same integration test also performed on a subwiki in SubWikiIT

The refactoring of references is currently called at two places:

    by the BackLinkUpdaterListener for all backlinks after a document has been renamed (triggered by a document event)
    by XWiki#updateLinksForRename to rename the internal links of the current document (which is always a call to updateResourceReferenceRelative, see below)

The problem of XWIKI-12987 is that XWiki#updateLinksForRename is called first and does perform an absolute rename of the relative links.
Now ResourceReferenceRenamer APIs names might be misleading: updateResourceReferenceRelative and updateResourceReferenceAbsolute are not about the references being absolute or relative: it's about the renamed references being absolute or relative respectively to the current document. It took me a while to integrate this, and I'm still struggling a bit with it.

So the problem was to find a proper condition to decide when to not refactor links, for this I'm performing a check for assessing if a link is absolute or not, by trying to resolve the ResourceReference without any parameter: if the result equals the reference with parameter then it was absolute.

Then for the updateResourceReferenceAbsolute the idea is to only perform update of the links, if the provided link is absolute, or if it's relative but the current document hasn't been moved as part of same job: in such case we do need to update the relative link, because there won't be a call to XWiki#updateLinksForRename on that document to update the link, we only get the call from BackLinkUpdaterListener.

For the updateResourceReferenceRelative the check is a bit more complex.
We only update links that are relative here, we don't want to update absolute references (is that correct? Can't find a counter example right now).
Then since we only perform refactoring of links relative to current document, we also check that the link about to be refactored is not related to pages that are part of the moved document in the same job: if those are also moved in the same job, then they're moved using same "direction", they're part of same hierarchy and we don't want to change the relative links wrt to them. This check is the main part of avoiding to update the relative links.

And finally we perform the update of the link only if the doc actually exists: we would create absolute links for those not existing doc, which doesn't make sense, we should keep the relative link we don't really know what the user wanted to do with those. Note that we could do the same check in updateResourceReferenceAbsolute but we don't really have the need since this is only called from the BackLinkUpdaterListener and if I'm correct we'll never have registered backlinks for a not existing doc.

Note that initially we discussed about using untyped link as a condition to perform or not the refactoring: I dropped the idea because we currently always create image resource references as untyped references from the WYSIWYG editor.

Note that this work currently includes a known regression when renaming links on the form `page:../SubSpace/Page`, this is going to be handled in further work.

(cherry picked from commit fc01c140)
parent 8c736ac4
No related branches found
No related tags found
No related merge requests found
Showing
with 517 additions and 153 deletions
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment