Example #1
0
 /**
  * Convert a single wiki link [[SomeTitle]] to an html link.
  *
  * Forms:
  *		[[Some Title]]
  *		[[Some Title|alternate text to display]]
  *		[[site:my_other_slot_name Some Title]]
  *		[[site:my_other_slot_name Some Title|alternate text to display]]
  *		[[node:12345 Some Title]]
  *		[[node:12345|alternate text to display]]
  *
  * Local URL form:
  *		[[localurl:module=modName&action=actName&param1=value1]]
  * File URL form:
  *		[[fileurl:repository_id=123&asset_id=1234&record_id=12345]]
  * Unlike other forms, the local URL form and the file URL form do not write link tags. 
  * They gets replaced with only the URL string itself.
  * 
  * @param string $wikiText
  * @param object SiteComponent $startingSiteComponent
  * @return string An HTML version of the link
  * @access private
  * @since 11/30/07
  */
 private function makeHtmlLink($wikiText, SiteComponent $startingSiteComponent)
 {
     $regexp = "/\n\n^\t\t# Anchor for the beginning of the line\n\\[\\[\t# The opening link tags\n\n\t\\s*\t\t# optional whitespace\n\n\t(?: site:([a-z0-9_\\-]+) \\s+ )?\t# An optional designator for linking to another site\n\t\n\t([^\\]#\\|]+)\t# The Title of the linked section, page, story\n\t\t\n\t(?: \\s*\\|\\s* ([^\\]]+) )?\t# The optional link-text to display instead of the title\n\n\t\\s*\t\t# optional whitespace\n\n\\]\\]\t# The closing link tags\n\$\t\t# Anchor for the end of the line\n\n/xi";
     $siteOnlyRegexp = "/\n\n^\t\t# Anchor for the beginning of the line\n\\[\\[\t# The opening link tags\n\n\t\\s*\t\t# optional whitespace\n\n\t(?: site:([a-z0-9_\\-]+) )?\t# A designator for linking to another site\n\t\n\t(?: \\s*\\|\\s* ([^\\]]+) )?\t# The optional link-text to display instead of the title\n\n\t\\s*\t\t# optional whitespace\n\n\\]\\]\t# The closing link tags\n\$\t\t# Anchor for the end of the line\n\n/xi";
     $nodeRegexp = "/\n\n^\t\t# Anchor for the beginning of the line\n\\[\\[\t# The opening link tags\n\n\t\\s*\t\t# optional whitespace\n\n\t(?: node:([a-z0-9_\\-]+) )?\t# A designator for linking to a particular node\n\t\n\t(?: \\s*\\|\\s* ([^\\]]+) )?\t# The optional link-text to display instead of the title\n\n\t\\s*\t\t# optional whitespace\n\n\\]\\]\t# The closing link tags\n\$\t\t# Anchor for the end of the line\n\n/xi";
     $localUrlRegexp = "/\n\n^\t\t# Anchor for the beginning of the line\n\\[\\[\t# The opening link tags\n\n\t\\s*\t\t# optional whitespace\n\n\t(?: localurl:([^\\]]+) )?\t# A designator for linking to a local url\n\n\\]\\]\t# The closing link tags\n\$\t\t# Anchor for the end of the line\n\n/xi";
     $fileUrlRegexp = "/\n\n^\t\t# Anchor for the beginning of the line\n\\[\\[\t# The opening link tags\n\n\t\\s*\t\t# optional whitespace\n\n\t(?: fileurl:([^\\]]+) )?\t# A designator for linking to a local file\n\n\\]\\]\t# The closing link tags\n\$\t\t# Anchor for the end of the line\n\n/xi";
     // Check for a link only to a site [[site:my_other_site]]
     if (preg_match($siteOnlyRegexp, $wikiText, $matches)) {
         $slotName = $matches[1];
         if (isset($matches[2]) && $matches[2]) {
             $display = $matches[2];
             $slotMgr = SlotManager::instance();
             $slot = $slotMgr->getSlotByShortName($slotName);
             if (!$slot->siteExists()) {
                 return $display . " ?";
             }
         } else {
             $slotMgr = SlotManager::instance();
             $slot = $slotMgr->getSlotByShortName($slotName);
             if ($slot->siteExists()) {
                 $director = $startingSiteComponent->getDirector();
                 $site = $director->getSiteComponentById($slot->getSiteId()->getIdString());
                 if (strlen($site->getDisplayName())) {
                     $display = $site->getDisplayName();
                 } else {
                     $display = $slotName;
                 }
             } else {
                 return $slotName . " ?";
             }
         }
         return $this->getSlotLink($slotName, $display);
     }
     // Check for a link to a node [[node:12345]]
     if (preg_match($nodeRegexp, $wikiText, $matches)) {
         $nodeIdString = $matches[1];
         try {
             if (isset($matches[2]) && $matches[2]) {
                 $display = $matches[2];
                 // Try getting the title to check if the node exists
                 $title = $this->getNodeTitle($nodeIdString, $startingSiteComponent);
             } else {
                 $display = $this->getNodeTitle($nodeIdString, $startingSiteComponent);
             }
         } catch (UnknownIdException $e) {
             if (isset($display)) {
                 return $display . " ?";
             } else {
                 return $nodeIdString . " ?";
             }
         }
         return $this->getNodeLink($nodeIdString, $display);
     }
     // Check for a link to a local url:
     // [[localurl:module=modName&action=actName&param1=value1]]
     if (preg_match($localUrlRegexp, $wikiText, $matches)) {
         preg_match_all('/(&(amp;)?)?([^&=]+)=([^&=]+)/', $matches[1], $paramMatches);
         $args = array();
         for ($i = 0; $i < count($paramMatches[1]); $i++) {
             $key = $paramMatches[3][$i];
             $value = $paramMatches[4][$i];
             if ($key == 'module') {
                 $module = $value;
             } else {
                 if ($key == 'action') {
                     $action = $value;
                 } else {
                     $args[$key] = $value;
                 }
             }
         }
         // If no params are specified, return our base url
         if (!count($args) && !isset($module) && !isset($action)) {
             return MYURL;
         }
         if (!isset($module)) {
             $module = 'ui1';
         }
         if (!isset($action)) {
             $action = 'view';
         }
         // Convert thumbnail urls
         if ($module == 'repository' && $action == 'viewthumbnail' && !empty($args['polyphony-repository___repository_id']) && !empty($args['polyphony-repository___asset_id']) && !empty($args['polyphony-repository___record_id'])) {
             $repositoryId = $args['polyphony-repository___repository_id'];
             $assetId = $args['polyphony-repository___asset_id'];
             $recordId = $args['polyphony-repository___record_id'];
             $file = MediaFile::withIdStrings($repositoryId, $assetId, $recordId);
             return $file->getThumbnailUrl();
         }
         $harmoni = Harmoni::instance();
         $harmoni->request->startNamespace(null);
         $newUrl = $harmoni->request->mkURLWithoutContext($module, $action, $args);
         $harmoni->request->endNamespace();
         return $newUrl->write();
     }
     // Check for a link to a file url:
     // [[fileurl:repository_id=123&amp;asset_id=1234&amp;record_id=12345]]
     if (preg_match($fileUrlRegexp, $wikiText, $matches)) {
         try {
             return MediaFile::withIdString($matches[1])->getUrl();
         } catch (InvalidArgumentException $e) {
         } catch (UnknownIdException $e) {
         }
     }
     // Links of the form [[Assignments]]
     if (preg_match($regexp, $wikiText, $matches)) {
         if (isset($matches[1]) && $matches[1]) {
             $slotMgr = SlotManager::instance();
             $slot = $slotMgr->getSlotByShortName($matches[1]);
             if ($slot->siteExists()) {
                 $director = $startingSiteComponent->getDirector();
                 $startingSiteComponent = $director->getSiteComponentById($slot->getSiteId()->getIdString());
             } else {
                 $title = $matches[2];
                 if (isset($matches[3]) && $matches[3]) {
                     $display = $matches[3];
                 } else {
                     $display = $title;
                 }
                 return $display . " ?";
             }
         }
         $title = $matches[2];
         if (isset($matches[3]) && $matches[3]) {
             $display = $matches[3];
         } else {
             $display = $title;
         }
         try {
             $nodeIdString = $this->titleSearcher->getNodeId($title, $startingSiteComponent);
             return $this->getNodeLink($nodeIdString, $display);
         } catch (UnknownTitleException $e) {
             return $this->getAddLink($title, $display, $startingSiteComponent);
         }
     }
     // If invalid, just return the wiki text.
     return $wikiText;
 }
 /**
  * Answer the media file
  * 
  * @return object MediaFile
  * @access public
  * @since 4/25/07
  */
 function getMediaFile()
 {
     if (!isset($this->_mediaFile)) {
         try {
             if ($this->getContent()) {
                 $this->_mediaFile = MediaFile::withIdString($this->getContent());
             } else {
                 return null;
             }
         } catch (InvalidArgumentException $e) {
             HarmoniErrorHandler::logException($e, 'Segue');
             return null;
         } catch (UnknownIdException $e) {
             HarmoniErrorHandler::logException($e, 'Segue');
             return null;
         }
     }
     return $this->_mediaFile;
 }
 /**
  * Translate any local-system url-tokens back into valid URLs.
  * 
  * @param string $htmlString
  * @return string The HTML text with tokens translated into valid URLs.
  * @access public
  * @since 1/24/08
  */
 public function untokenizeLocalUrls($htmlString)
 {
     $harmoni = Harmoni::instance();
     // File URLs
     preg_match_all('/\\[\\[fileurl:([^\\]]*)\\]\\]/', $htmlString, $matches);
     for ($i = 0; $i < count($matches[0]); $i++) {
         try {
             $mediaFile = MediaFile::withIdString($matches[1][$i]);
             $htmlString = $this->str_replace_once($matches[0][$i], $mediaFile->getUrl(), $htmlString);
         } catch (InvalidArgumentException $e) {
         } catch (UnknownIdException $e) {
         }
     }
     // other local urls
     $harmoni->request->startNamespace(null);
     while (preg_match('/\\[\\[localurl:([^\\]]*)\\]\\]/', $htmlString, $matches)) {
         preg_match_all('/(&(amp;)?)?([^&=]+)=([^&=]+)/', $matches[1], $paramMatches);
         $args = array();
         for ($i = 0; $i < count($paramMatches[1]); $i++) {
             $key = $paramMatches[3][$i];
             $value = $paramMatches[4][$i];
             if ($key == 'module') {
                 $module = $value;
             } else {
                 if ($key == 'action') {
                     $action = $value;
                 } else {
                     $args[$key] = $value;
                 }
             }
         }
         // /sites/sitename urls
         if ($module == 'sites' && !count($args)) {
             $args['site'] = $action;
             unset($module, $action);
         }
         if (!count($args)) {
             $newUrl = MYURL;
         } else {
             if (!isset($module)) {
                 $module = 'view';
             }
             if (!isset($action)) {
                 $action = 'html';
             }
             $newUrl = $harmoni->request->mkURLWithoutContext($module, $action, $args);
             $newUrl = $newUrl->write();
         }
         $htmlString = $this->str_replace_once($matches[0], $newUrl, $htmlString);
     }
     $harmoni->request->endNamespace();
     return $htmlString;
 }