src/wcmf/lib/presentation/link/LinkProcessor.php
public static function processLinks($node, $base, LinkProcessorStrategy $strategy,
          $recursive=true) {
    if (!$node) {
      return;
    }
    $invalidURLs = [];
    $logger = LogManager::getLogger(__CLASS__);

    // iterate over all node values
    $iter = new NodeValueIterator($node, $recursive);
    for($iter->rewind(); $iter->valid(); $iter->next()) {

      $currentNode = $iter->currentNode();
      $valueName = $iter->key();
      $value = $currentNode->getValue($valueName);
      $oldValue = $value;

      // find links in texts
      $urls = array_fill_keys(StringUtil::getUrls($value), 'embedded');
      // find direct attribute urls
      if (preg_match('/^[a-zA-Z]+:\/\//', $value) || InternalLink::isLink($value)) {
        $urls[$value] = 'direct';
      }

      // process urls
      foreach ($urls as $url => $type) {
        // translate relative urls
        if (!InternalLink::isLink($url) && !preg_match('/^#|^{|^$|^[a-zA-Z]+:\/\/|^javascript:|^mailto:/', $url) &&
          @file_exists($url) === false) {
          // translate relative links
          $urlConv = URIUtil::translate($url, $base);
          $value = self::replaceUrl($value, $url, $urlConv['absolute'], $type);
          $url = $urlConv['absolute'];
        }

        // check url
        $urlOK = self::checkUrl($url, $strategy);
        if ($urlOK) {
          $urlConv = null;
          if (InternalLink::isLink($url)) {
            // convert internal urls
            $urlConv = self::convertInternalLink($url, $strategy);
          }
          elseif (preg_match('/^#/', $url)) {
            // convert hash links
            $urlConv = $strategy->getObjectUrl($node).$url;
          }
          if ($urlConv !== null) {
            $value = self::replaceUrl($value, $url, $urlConv, $type);
          }
        }
        else {
          // invalid url
          $logger->error("Invalid URL found: ".$url);
          $oidStr = $currentNode->getOID()->__toString();
          if (!isset($invalidURLs[$oidStr])) {
            $invalidURLs[] = [];
          }
          $invalidURLs[$oidStr][] = $url;
          $value = self::replaceUrl($value, $url, '#', $type);
        }
      }
      if ($oldValue != $value) {
        $currentNode->setValue($valueName, $value, true);
      }
    }
    return $invalidURLs;
  }
public static function processLinks($node, $base, LinkProcessorStrategy $strategy,
          $recursive=true) {
    if (!$node) {
      return;
    }
    $invalidURLs = [];
    $logger = LogManager::getLogger(__CLASS__);

    // iterate over all node values
    $iter = new NodeValueIterator($node, $recursive);
    for($iter->rewind(); $iter->valid(); $iter->next()) {

      $currentNode = $iter->currentNode();
      $valueName = $iter->key();
      $value = $currentNode->getValue($valueName);
      $oldValue = $value;

      // find links in texts
      $urls = array_fill_keys(StringUtil::getUrls($value), 'embedded');
      // find direct attribute urls
      if (preg_match('/^[a-zA-Z]+:\/\//', $value) || InternalLink::isLink($value)) {
        $urls[$value] = 'direct';
      }

      // process urls
      foreach ($urls as $url => $type) {
        // translate relative urls
        if (!InternalLink::isLink($url) && !preg_match('/^#|^{|^$|^[a-zA-Z]+:\/\/|^javascript:|^mailto:/', $url) &&
          @file_exists($url) === false) {
          // translate relative links
          $urlConv = URIUtil::translate($url, $base);
          $value = self::replaceUrl($value, $url, $urlConv['absolute'], $type);
          $url = $urlConv['absolute'];
        }

        // check url
        $urlOK = self::checkUrl($url, $strategy);
        if ($urlOK) {
          $urlConv = null;
          if (InternalLink::isLink($url)) {
            // convert internal urls
            $urlConv = self::convertInternalLink($url, $strategy);
          }
          elseif (preg_match('/^#/', $url)) {
            // convert hash links
            $urlConv = $strategy->getObjectUrl($node).$url;
          }
          if ($urlConv !== null) {
            $value = self::replaceUrl($value, $url, $urlConv, $type);
          }
        }
        else {
          // invalid url
          $logger->error("Invalid URL found: ".$url);
          $oidStr = $currentNode->getOID()->__toString();
          if (!isset($invalidURLs[$oidStr])) {
            $invalidURLs[] = [];
          }
          $invalidURLs[$oidStr][] = $url;
          $value = self::replaceUrl($value, $url, '#', $type);
        }
      }
      if ($oldValue != $value) {
        $currentNode->setValue($valueName, $value, true);
      }
    }
    return $invalidURLs;
  }
public static function processLinks ( $ node , $ base , LinkProcessorStrategy $ strategy , $ recursive = true ) { if ( ! $ node ) { return ; } $ invalidURLs = [ ] ; $ logger = LogManager :: getLogger ( __CLASS__ ) ; // iterate over all node values $ iter = new NodeValueIterator ( $ node , $ recursive ) ; for ( $ iter -> rewind ( ) ; $ iter -> valid ( ) ; $ iter -> next ( ) ) { $ currentNode = $ iter -> currentNode ( ) ; $ valueName = $ iter -> key ( ) ; $ value = $ currentNode -> getValue ( $ valueName ) ; $ oldValue = $ value ; // find links in texts $ urls = array_fill_keys ( StringUtil :: getUrls ( $ value ) , 'embedded' ) ; // find direct attribute urls if ( preg_match ( '/^[a-zA-Z]+:\/\//' , $ value ) || InternalLink :: isLink ( $ value ) ) { $ urls [ $ value ] = 'direct' ; } // process urls foreach ( $ urls as $ url => $ type ) { // translate relative urls if ( ! InternalLink :: isLink ( $ url ) && ! preg_match ( '/^#|^{|^$|^[a-zA-Z]+:\/\/|^javascript:|^mailto:/' , $ url ) && @ file_exists ( $ url ) === false ) { // translate relative links $ urlConv = URIUtil :: translate ( $ url , $ base ) ; $ value = self :: replaceUrl ( $ value , $ url , $ urlConv [ 'absolute' ] , $ type ) ; $ url = $ urlConv [ 'absolute' ] ; } // check url $ urlOK = self :: checkUrl ( $ url , $ strategy ) ; if ( $ urlOK ) { $ urlConv = null ; if ( InternalLink :: isLink ( $ url ) ) { // convert internal urls $ urlConv = self :: convertInternalLink ( $ url , $ strategy ) ; } elseif ( preg_match ( '/^#/' , $ url ) ) { // convert hash links $ urlConv = $ strategy -> getObjectUrl ( $ node ) . $ url ; } if ( $ urlConv !== null ) { $ value = self :: replaceUrl ( $ value , $ url , $ urlConv , $ type ) ; } } else { // invalid url $ logger -> error ( "Invalid URL found: " . $ url ) ; $ oidStr = $ currentNode -> getOID ( ) -> __toString ( ) ; if ( ! isset ( $ invalidURLs [ $ oidStr ] ) ) { $ invalidURLs [ ] = [ ] ; } $ invalidURLs [ $ oidStr ] [ ] = $ url ; $ value = self :: replaceUrl ( $ value , $ url , '#' , $ type ) ; } } if ( $ oldValue != $ value ) { $ currentNode -> setValue ( $ valueName , $ value , true ) ; } } return $ invalidURLs ; }
Check and convert links in the given node.
@param $node Node instance
@param $base The base url of relative links as seen from the executing script
@param $strategy The strategy used to check and create urls
@param recursive Boolean whether to process child nodes to (default: true)
@return Array of invalid urls
Check and convert links in the given node .