|
| 1 | +<?php |
| 2 | + |
| 3 | +use dokuwiki\Extension\Plugin; |
| 4 | + |
| 5 | +/** |
| 6 | + * DokuWiki Plugin imgpaste (Helper Component) |
| 7 | + * |
| 8 | + * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html |
| 9 | + * @author Andreas Gohr, Anna Dabrowska <[email protected]> |
| 10 | + */ |
| 11 | +class helper_plugin_imgpaste extends Plugin |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Create a relative ID from a given reference ID and a full ID to link to |
| 15 | + * |
| 16 | + * PHP rewrite of the original Javascript written by Andreas Gohr |
| 17 | + * in /lib/scripts/linkwiz.js |
| 18 | + * |
| 19 | + * Both IDs are expected to be clean, e.g., the result of a clean ID function. |
| 20 | + * No relative paths, leading colons, or similar things are allowed. As long as |
| 21 | + * pages have a common prefix, a relative link is constructed. |
| 22 | + * |
| 23 | + * @param string $ref The ID of a page the link is used on |
| 24 | + * @param string $id The ID to link to |
| 25 | + * @return string The relative ID |
| 26 | + */ |
| 27 | + public static function createRelativeID($ref, $id) { |
| 28 | + // Split the reference and target IDs into namespaces |
| 29 | + $sourceNs = explode(':', $ref); |
| 30 | + array_pop($sourceNs); // Remove the page part, keep only the namespace |
| 31 | + $targetNs = explode(':', $id); |
| 32 | + $targetPage = array_pop($targetNs); // Remove the page part of the target ID |
| 33 | + $relativeID = []; |
| 34 | + |
| 35 | + // Find the common prefix length |
| 36 | + $commonPrefixLength = 0; |
| 37 | + while ($commonPrefixLength < count($sourceNs) && |
| 38 | + $commonPrefixLength < count($targetNs) && |
| 39 | + $sourceNs[$commonPrefixLength] === $targetNs[$commonPrefixLength]) { |
| 40 | + $commonPrefixLength++; |
| 41 | + } |
| 42 | + |
| 43 | + if (count($sourceNs) > 0) { |
| 44 | + if ($commonPrefixLength > 0) { |
| 45 | + if ($commonPrefixLength === count($sourceNs) && $commonPrefixLength === count($targetNs)) { |
| 46 | + // Both pages are in the same namespace |
| 47 | + } elseif ($commonPrefixLength < count($sourceNs)) { |
| 48 | + // Add '..' for each missing namespace from common to the target |
| 49 | + $relativeID = array_merge( |
| 50 | + $relativeID, |
| 51 | + array_fill(0, count($sourceNs) - $commonPrefixLength, '..') |
| 52 | + ); |
| 53 | + } else { |
| 54 | + // Target is below common prefix, add '.' |
| 55 | + $relativeID[] = '.'; |
| 56 | + } |
| 57 | + } elseif (count($targetNs) === 0) { |
| 58 | + // Target is in the root namespace, but source is not, make it absolute |
| 59 | + $relativeID[] = ''; |
| 60 | + } |
| 61 | + |
| 62 | + // Add any remaining parts of the target namespace |
| 63 | + $relativeID = array_merge($relativeID, array_slice($targetNs, $commonPrefixLength)); |
| 64 | + } else { |
| 65 | + // Source is in the root namespace, just use target as is |
| 66 | + $relativeID = $targetNs; |
| 67 | + } |
| 68 | + |
| 69 | + // Add the target page to the relative ID |
| 70 | + $relativeID[] = $targetPage; |
| 71 | + |
| 72 | + return implode(':', $relativeID); |
| 73 | + } |
| 74 | +} |
0 commit comments