Wiki source for LinkUsingPageTitle


Show raw source

=====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:
%%(php)
"link_using_page_title" => "1",
%%

In ##libs/Wakka.class.php## change the beginning of function Link() from
%%(php)
function Link($tag, $method='', $text='', $track=TRUE, $escapeText=TRUE, $title='') {
$get_title = 0;
if (!$text) {
$text = $tag;
}
%%
to
%%(php)
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);
}
%%


====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.

Put into ##wikka.config.php## the following line:
%%(php)
"link_using_page_title" => "1",
%%

The base of the change occurs, obviously, in the Link() method. My new Link() method looks like this:

%%(php)
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;
}

%%
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.

%%(php)
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 '';
}
}
}


%%

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 [[http://wiki.electronicmusicworld.com/ 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##:

%%(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);
}
%%

Comment out ##if(!text) $text = $url;## to let the function Link() in ##wikka.php## decide which text to use as link text:

%%(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);
}
%%

CategoryUserContributions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki