Link Using Page Title
Working for 1.3.6 (and probably prior versions)
With the advent of the new pagetitle column in table pages it's now a lot easier to patch this functionality.Put into wikka.config.php the following line:
"link_using_page_title" => "1",
In libs/Wakka.class.php change the beginning of function Link() from
function Link($tag, $method='', $text='', $track=TRUE, $escapeText=TRUE, $title='') {
$get_title = 0;
if (!$text) {
$text = $tag;
}
$get_title = 0;
if (!$text) {
$text = $tag;
}
to
function Link($tag, $method='', $text='', $track=TRUE, $escapeText=TRUE, $title='') {
$get_title = 0;
if (!$text) {
$text = $tag;
if ($this->GetConfigValue('link_using_page_title') == 1) $text = $this->PageTitle($tag);
}
$get_title = 0;
if (!$text) {
$text = $tag;
if ($this->GetConfigValue('link_using_page_title') == 1) $text = $this->PageTitle($tag);
}
Older versions
Working for 1.1.6.0 to 1.2
Positions of changes in code have changed. CTRL+F is your friend.
One of the things I was immediately annoyed with was the fact that Wiki links used the tag, and not the page title, when a link is created, unless you specified the link text yourself. It is much better, imho, to automatically fetch the page title, and use that as the link text. And so I went to work on this. It's not too hard to implement this, and only required you to edit parts of wikka.php.Positions of changes in code have changed. CTRL+F is your friend.
Put into wikka.config.php the following line:
"link_using_page_title" => "1",
The base of the change occurs, obviously, in the Link() method. My new Link() method looks like this:
function Link($tag, $method='', $text='', $track=TRUE, $escapeText=TRUE, $title='') {
$get_title = 0;
if (!$text) {
$text = $tag;
if ($this->GetConfigValue('link_using_page_title') == 1) $get_title = 1;
}
// escape text?
if ($escapeText) $text = $this->htmlspecialchars_ent($text);
$tag = $this->htmlspecialchars_ent($tag); #142 & #148
$method = $this->htmlspecialchars_ent($method);
$title = $this->htmlspecialchars_ent($title);
$url = '';
// is this an interwiki link?
if (preg_match("/^([A-ZÄÖÜ][A-Za-zÄÖÜßäöü]+)[:](\S*)$/", $tag, $matches)) # before the : should be a WikiName; anything after can be (nearly) anything that's allowed in a URL
{
$url = $this->GetInterWikiUrl($matches[1], $matches[2]);
}
elseif (preg_match("/^(http|https|ftp):\/\/([^\\s\"<>]+)$/", $tag))
{
$url = $tag; // this is a valid external URL
}
// is this a full link? ie, does it contain alpha-numeric characters?
elseif (preg_match("/[^[:alnum:],ÄÖÜ,ßäöü]/", $tag))
{
// check for email addresses
if (preg_match("/^.+\@.+$/", $tag))
{
$url = "mailto:".$tag;
}
// check for protocol-less URLs
else if (!preg_match("/:/", $tag))
{
$url = "http://".$tag;
}
}
else
{
// it's a wiki link
if ($_SESSION["linktracking"] && $track) $this->TrackLinkTo($tag);
$linkedPage = $this->LoadPage($tag);
if($get_title && $linkedPage) {
$text = $this->PageTitle($linkedPage['body'], 'yes');
if($text=='') $text = $tag;
$text = $this->htmlspecialchars_ent($text);
}
// return ($linkedPage ? "<a href=\"".$this->Href($method, $linkedPage['tag'])."\">".$text."</a>" : "<span class=\"missingpage\">".$text."</span><a href=\"".$this->Href("edit", $tag)."\" title=\"Create this page\">?</a>");
return ($linkedPage ? "<a href=\"".$this->Href($method, $linkedPage['tag'])."\" title=\"$title\">".$text."</a>" : "<a class=\"missingpage\" href=\"".$this->Href("edit", $tag)."\" title=\"Create this page\">".$text."</a>");
}
$external_link_tail = $this->GetConfigValue("external_link_tail");
return $url ? "<a class=\"ext\" href=\"$url\">$text</a>$external_link_tail" : $text;
}
$get_title = 0;
if (!$text) {
$text = $tag;
if ($this->GetConfigValue('link_using_page_title') == 1) $get_title = 1;
}
// escape text?
if ($escapeText) $text = $this->htmlspecialchars_ent($text);
$tag = $this->htmlspecialchars_ent($tag); #142 & #148
$method = $this->htmlspecialchars_ent($method);
$title = $this->htmlspecialchars_ent($title);
$url = '';
// is this an interwiki link?
if (preg_match("/^([A-ZÄÖÜ][A-Za-zÄÖÜßäöü]+)[:](\S*)$/", $tag, $matches)) # before the : should be a WikiName; anything after can be (nearly) anything that's allowed in a URL
{
$url = $this->GetInterWikiUrl($matches[1], $matches[2]);
}
elseif (preg_match("/^(http|https|ftp):\/\/([^\\s\"<>]+)$/", $tag))
{
$url = $tag; // this is a valid external URL
}
// is this a full link? ie, does it contain alpha-numeric characters?
elseif (preg_match("/[^[:alnum:],ÄÖÜ,ßäöü]/", $tag))
{
// check for email addresses
if (preg_match("/^.+\@.+$/", $tag))
{
$url = "mailto:".$tag;
}
// check for protocol-less URLs
else if (!preg_match("/:/", $tag))
{
$url = "http://".$tag;
}
}
else
{
// it's a wiki link
if ($_SESSION["linktracking"] && $track) $this->TrackLinkTo($tag);
$linkedPage = $this->LoadPage($tag);
if($get_title && $linkedPage) {
$text = $this->PageTitle($linkedPage['body'], 'yes');
if($text=='') $text = $tag;
$text = $this->htmlspecialchars_ent($text);
}
// return ($linkedPage ? "<a href=\"".$this->Href($method, $linkedPage['tag'])."\">".$text."</a>" : "<span class=\"missingpage\">".$text."</span><a href=\"".$this->Href("edit", $tag)."\" title=\"Create this page\">?</a>");
return ($linkedPage ? "<a href=\"".$this->Href($method, $linkedPage['tag'])."\" title=\"$title\">".$text."</a>" : "<a class=\"missingpage\" href=\"".$this->Href("edit", $tag)."\" title=\"Create this page\">".$text."</a>");
}
$external_link_tail = $this->GetConfigValue("external_link_tail");
return $url ? "<a class=\"ext\" href=\"$url\">$text</a>$external_link_tail" : $text;
}
I load the page content into $page, and pass on the text to the PageTitle() method for further processing. To be sure that there's always a title, I then check if $text is empty (which could be, if the linked page does not have a formatted page title) and then I end up using the tag anyway.
To accomplish this, I needed to slightly alter the PageTitle() method.
function PageTitle($text='', $link='no') {
$title = "";
// Edit by Stefan Koopmanschap: argument should be possible
if($text=='') {
$pagecontent = $this->page["body"];
} else {
$pagecontent = $text;
}
if (ereg( "(=){3,5}([^=\n]+)(=){3,5}", $pagecontent, $title)) {
$formatting_tags = array("**", "//", "__", "##", "''", "++", "#%", "@@", "\"\"");
$title = str_replace($formatting_tags, "", $title[2]);
}
// if ($title) return strip_tags($this->Format($title)); # fix for forced links in heading
if ($title) return $title;
else {
if($link=='no') {
return $this->GetPageTag();
} else {
return '';
}
}
}
$title = "";
// Edit by Stefan Koopmanschap: argument should be possible
if($text=='') {
$pagecontent = $this->page["body"];
} else {
$pagecontent = $text;
}
if (ereg( "(=){3,5}([^=\n]+)(=){3,5}", $pagecontent, $title)) {
$formatting_tags = array("**", "//", "__", "##", "''", "++", "#%", "@@", "\"\"");
$title = str_replace($formatting_tags, "", $title[2]);
}
// if ($title) return strip_tags($this->Format($title)); # fix for forced links in heading
if ($title) return $title;
else {
if($link=='no') {
return $this->GetPageTag();
} else {
return '';
}
}
}
The PageTitle() method now does not automatically take the current page content, but instead also allows for arguments to be passed. The first argument is, obviously, the full body text of the linked page, from which the title should be extracted. The second argument is there to ensure backward compatibility: If there is no page title in the passed text, I'd want it to pass back an empty value instead of the PageTag of the current page. Thus, when called in the Link() method, the second argument is passed a 'yes' value (but this could be anything other than 'no'). By default this is a 'no' value, which will result in the PageTag of the current page being passed back.
These small changes result in a functioning version of links that contain the PageTitle as linktext, instead of the PageTag. Now all you need to do is create useful PageTitles ;)
A working version of this system can be found on the Electronic Music World Wiki
To do:
- rewrite PageTitle() to strip page titles of every formatting string like //, ** and so on -- is there any function doing just that?.
- The fix for forced links doesn't work with this implementation. Further investigation showed, that pagetitles containig links to the page itself result in endless loops. This might also happen if two or more pages refer to another in circular fashion. Circular references need to be detected. See http://gio.larp-bb.de/SandBox
- check if wakka.php can be modified to call Link() the same way for wiki links and forced links (see comment)
Additions
Find the following lines (check in the vicinity of line 270) in formatters/wakka.php: list (, $url, , $text) = $matches;
if ($url)
{
//if ($url!=($url=(preg_replace("/@@|££||\[\[/","",$url))))$result="</span>";
if (!$text) $text = $url;
//$text=preg_replace("/@@|££|\[\[/","",$text);
return $result.$wakka->Link($url, "", $text);
}
if ($url)
{
//if ($url!=($url=(preg_replace("/@@|££||\[\[/","",$url))))$result="</span>";
if (!$text) $text = $url;
//$text=preg_replace("/@@|££|\[\[/","",$text);
return $result.$wakka->Link($url, "", $text);
}
Comment out if(!text) $text = $url; to let the function Link() in wikka.php decide which text to use as link text:
list (, $url, , $text) = $matches;
if ($url)
{
//if ($url!=($url=(preg_replace("/@@|££||\[\[/","",$url))))$result="</span>";
//if (!$text) $text = $url;
//$text=preg_replace("/@@|££|\[\[/","",$text);
return $result.$wakka->Link($url, "", $text);
}
if ($url)
{
//if ($url!=($url=(preg_replace("/@@|££||\[\[/","",$url))))$result="</span>";
//if (!$text) $text = $url;
//$text=preg_replace("/@@|££|\[\[/","",$text);
return $result.$wakka->Link($url, "", $text);
}
CategoryUserContributions
Glad to see BTW that the Dutch Wikka community is growing :) You can create your userpage and tell us more about you and your Wikka hacks if you wish.
I'll definately create my userpage later on, when I have a bit more time to burn.
Here's another approach -- http://wikka.jsnx.com/UncamelAction.
Best wishes,
Denny
Two points you might want to look at.
1. Links to pages that have not been created yet all show the name of the page they are on, rather than the tag of the unmade page;
2. It would be nice if forced links still worked so that I could use the word "Dylan" in a sentence and have it link to BobDylan, for example.
Great work
Owen
3. The links in the footer are replaced by the current page title.
I shall try to find time to poke around and see if a workaround suggests itself :)
Owen
Currently, with this extension forced links keep their link text while wiki links don't, because it's only checked for an empty $linktext. I //could// check for equality but that's not what everyone wants in case someone consciently put the name of the forced link as link text -- in which case the forced link's link text would be replaced by the linked page's titel.
Is it safe to remove the $text = $tag assignment in file wakka.php[249]?
Because this is *the* way forced links work: forced links have an optional link text. CamelCase links - by definition - don't.
currently the following extensions are installed:
*SmartyPants
*LinkUsingPageTitle
*SideNoteAction
*CommentFormatting
BTW: The problem I had on 2006-05-19 is solved meanwhile. I was to stupid to recognise, that not every forced link is just [[Link]] -- there's still [[Link text]] :-).
What bothers me right now is that forced links in headings don't get their markup stripped. In 1.1.6.2 there's a fix for this, but it seems to send the script in an endless loop when I use PageTitles for links on pages that reference themselves in the heading (e.g.SandBox). So I commented out the fix and use a more primitive version. I think, I have to implement a circular reference detection. I guess there is something like this in include.php, so I'll take a look at this.