/** * Answer a new MediaFile from a MediaFile id string * * @param string $idString * @return object MediaFile * @access public * @since 4/30/07 * @static */ public static function withIdString($idString) { if (preg_match('/^(?:(?:repositoryId=(.+)&(?:amp;)?)|(?:&(?:amp;)?))?assetId=(.+)&(?:amp;)?recordId=([^&]+)/', $idString, $matches)) { return MediaFile::withIdStrings($matches[1], $matches[2], $matches[3]); } else { if (preg_match('/^(?:(?:repository_id=(.+)&(?:amp;)?)|(?:&(?:amp;)?))?asset_id=(.+)&(?:amp;)?record_id=([^&]+)/', $idString, $matches)) { return MediaFile::withIdStrings($matches[1], $matches[2], $matches[3]); } else { throw new InvalidArgumentException("Invalid Id format, '" . $idString . "'"); } } }
/** * 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&asset_id=1234&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; }
/** * Rewrite file urls that weren't properly localized. * * @param object BlockSiteComponent $siteComponent * @return void */ protected function rewriteNonlocalFileUrls(BlockSiteComponent $siteComponent) { static $baseUrls; if (empty($baseUrls)) { $baseUrls = array('http://segue.middlebury.edu', 'https://segue.middlebury.edu', 'http://seguecommunity.middlebury.edu', 'https://seguecommunity.middlebury.edu'); foreach (SlotAbstract::getLocationCategories() as $locationCategory) { $baseUrls[] = rtrim(SiteDispatcher::getBaseUrlForLocationCategory($locationCategory), '/'); } $baseUrls = array_unique($baseUrls); } $content = $siteComponent->getAsset()->getContent(); foreach ($baseUrls as $baseUrl) { if (preg_match_all('#[\'"]' . $baseUrl . '/repository/(viewfile|viewfile_flash|viewthumbnail|viewthumbnail_flash)/polyphony-repository___repository_id/edu.middlebury.segue.sites_repository/polyphony-repository___asset_id/([0-9]+)/polyphony-repository___record_id/([0-9]+)(?:polyphony-repository___file_name/(.+))?[\'"]#', $content->asString(), $matches, PREG_SET_ORDER)) { foreach ($matches as $m) { $urlParts = array('module' => 'repository', 'action' => $m[1], 'polyphony-repository___repository_id' => 'edu.middlebury.segue.sites_repository', 'polyphony-repository___asset_id' => $m[2], 'polyphony-repository___record_id' => $m[3]); if (!empty($m[4])) { $urlParts['polyphony-repository___file_name'] = $m[4]; } else { if ($m[1] == 'viewfile') { try { $file = MediaFile::withIdStrings('edu.middlebury.segue.sites_repository', $m[2], $m[3]); $urlParts['polyphony-repository___file_name'] = $file->getFilename(); } catch (UnknownIdException $e) { } } } $url = http_build_query($urlParts, '', '&'); $content->_setValue(str_replace($m[0], '"' . '[[localurl:' . $url . ']]' . '"', $content->value())); } } } }