<?php if (!defined('PmWiki')) exit();
/*  Copyright 2017-2018 wizzwizz4 (partially derived from Patrick R. Michaud's author.php)
    This file is FriendlyAuthorLinks.php; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.  

    This recipe rewrites author links to use their friendly names via
	[[~AuthorName|+]]. It requires the $CurrentTime variable which isn't
	defined by PmWiki until after the config files are included, so
	this recipe will use the default hard-coded into PmWiki unless
	$CurrentTime is already set. This means that author links created
	via the ~~~~ syntax might have a time format different to that of
	the rest of the wiki if:
	  * A new version of PmWiki changes the default value of $TimeFmt; or
	  * Either $CurrentTime or $TimeFmt are modified after the inclusion of
	    this recipe.
	
	This recipe also sets $AuthorGroup to 'Profiles' iff it hasn't already
	been set iff ?action=diff. This may cause side-effects.
	However, you should be fine to change the value of $AuthorGroup after this recipe
	is included since the values are only used AFTER all of config.php has run.
	Side-effects may include:
	  * SDV failing to set the $AuthorGroup in other recipes, resulting in
	    mis-linked contributions.
    
    To use this recipe, simply copy it into the cookbook/ directory, and
    add the following line to a local customization:

        include_once("$FarmD/cookbook/FriendlyAuthorLinks.php");

*/

$RecipeInfo['FriendlyAuthorLinks']['Version'] = '2018-01-01';

// I need $CurrentTime.
// If you want it customised, set it before this runs.
if (isset($CurrentTime)) {
	$FriendlyAuthorLinksCurrentTime = $CurrentTime;  // Paranoia due to no anonymous namespaces.
} else {
	$FriendlyAuthorLinksCurrentTime = strftime($TimeFmt, $Now);
}
// I could have used SDV but that might have broken other recipes. (Unlikely but possible.)

// Main code below.

SDV($AuthorLink, "[[~$Author|+]]");  // $RecentChangesFmt uses this.

if (IsEnabled($EnableAuthorSignature,1)) {  // Don't want to set if it's disabled!
	SDVA($ROSPatterns, array(  // Copypasta from author.php
		'/(?<!~)~~~~(?!~)/' => "[[~$Author|+]] $FriendlyAuthorLinksCurrentTime",
		'/(?<!~)~~~(?!~)/' => "[[~$Author|+]]",
	));
}

if ($action == "diff") {
	SDV($AuthorGroup,'Profiles');  // Woah. Side-effect alert!
	$FmtPV['$DiffAuthorName'] = 'PageVar(ResolvePageName($GLOBALS[\'AuthorGroup\'] . \'.\' . $GLOBALS[\'FmtV\'][\'$DiffAuthor\']), \'$Title\') ?: \'$DiffAuthor\'';
	SDV($DiffStartFmt,"
      <div class='diffbox'><div class='difftime'><a name='diff\$DiffGMT' href='#diff\$DiffGMT'>\$DiffTime</a>
        \$[by] <span class='diffauthor' title='\$DiffHost'>\$DiffAuthorName</span> - \$DiffChangeSum</div>");
}

unset($FriendlyAuthorLinksCurrentTime);