Revision [14263]

This is an old revision of LinkUsingPageTitle made by MasinAlDujaili on 2006-05-18 06:54:05.

 


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.

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

    function Link($tag, $method='', $linktext='', $track=TRUE, $escapeText=TRUE, $title='') {
        if (!$linktext)
        {
          $text = $tag;
        }
        else
        {
          $text = $linktext;
      }
        // escape text?
        if ($escapeText) $text = $this->htmlspecialchars_ent($text);
        $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);

      // Edit by Stefan Koopmanship, re-edit by Masin Al-Dujaili, get link text
      if($linktext=='') {
        $text = $this->PageTitle($linkedPage['body'], 'yes');
                if($text=='') $text = $tag;
                else $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 $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

CategoryUserContributions
There are 10 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki