<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$
 */

  uses(
    'scriptlet.xml.workflow.AbstractState', 
    'util.PropertyManager',
    'io.FileUtil',
    'io.File',
    'text.doclet.markup.MarkupBuilder',
    'io.collections.FileCollection',
    'io.collections.iterate.FilteredIOCollectionIterator',
    'io.collections.iterate.NegationOfFilter',
    'io.collections.iterate.AnyOfFilter',
    'io.collections.iterate.NameMatchesFilter',
    'util.MimeType'
  );

  /**
   * Handles /xml/browse
   *
   * @purpose  State
   */
  class BrowseState extends AbstractState {

    /**
     * Process this state.
     *
     * @param   scriptlet.xml.workflow.WorkflowScriptletRequest request
     * @param   scriptlet.xml.XMLScriptletResponse response
     */
    public function process($request, $response) {
      $path= $request->getQueryString();

      $prop= PropertyManager::getInstance()->getProperties('storage');
      $base= new FileCollection($prop->readString('storage', 'base').DIRECTORY_SEPARATOR.strtr($path, array(
        ','   => DIRECTORY_SEPARATOR, 
        '..'  => ''
      )));
      $excludes= array();
      foreach ($prop->readArray('storage', 'excludes') as $pattern) {
        $excludes[]= new NameMatchesFilter('/'.$pattern.'/i');
      }
      
      $i= new FilteredIOCollectionIterator($base, new NegationOfFilter(new AnyOfFilter($excludes)));
      $n= $response->addFormResult(new Node('list', NULL, array(
        'path' => $path
      )));

      // Add list of elements
      foreach ($i as $element) {
        $name= basename($element->getUri());

        if ($element instanceof IOCollection) {
          $e= $n->addChild(new Node('collection'));
        } else {
          $e= $n->addChild(new Node('element'));
          $e->addChild(new Node('mime', MimeType::getByFilename($element->getUri())));

          if ('README' === $name) {
            $builder= new MarkupBuilder();
            $response->addFormresult(new Node('readme', new PCData(
              '<p>'.$builder->markupFor(FileUtil::getContents(new File($element->getUri()))).'</p>'
            )));
          }
        }

        $e->addChild(new Node('name', basename($element->getUri())));
        $e->addChild(Node::fromObject($element->lastModified(), 'modified'));<commit_msg>- Yield a nice 404 if paths do not exist<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$
 */

  uses(
    'scriptlet.xml.workflow.AbstractState', 
    'util.PropertyManager',
    'io.FileUtil',
    'io.File',
    'io.Folder',
    'text.doclet.markup.MarkupBuilder',
    'io.collections.FileCollection',
    'io.collections.iterate.FilteredIOCollectionIterator',
    'io.collections.iterate.NegationOfFilter',
    'io.collections.iterate.AnyOfFilter',
    'io.collections.iterate.NameMatchesFilter',
    'util.MimeType'
  );

  /**
   * Handles /xml/browse
   *
   * @purpose  State
   */
  class BrowseState extends AbstractState {

    /**
     * Process this state.
     *
     * @param   scriptlet.xml.workflow.WorkflowScriptletRequest request
     * @param   scriptlet.xml.XMLScriptletResponse response
     */
    public function process($request, $response) {
      $path= $request->getQueryString();

      $prop= PropertyManager::getInstance()->getProperties('storage');
      $base= new Folder($prop->readString('storage', 'base').DIRECTORY_SEPARATOR.strtr($path, array(
        ','   => DIRECTORY_SEPARATOR, 
        '..'  => ''
      )));
      if (!$base->exists()) {
        throw new HttpScriptletException('Path "'.$path.'" does not exist', HTTP_NOT_FOUND);
      }

      $excludes= array();
      foreach ($prop->readArray('storage', 'excludes') as $pattern) {
        $excludes[]= new NameMatchesFilter('/'.$pattern.'/i');
      }
      
      $i= new FilteredIOCollectionIterator(new FileCollection($base->getUri()), new NegationOfFilter(new AnyOfFilter($excludes)));
      $n= $response->addFormResult(new Node('list', NULL, array(
        'path' => $path
      )));

      // Add list of elements
      foreach ($i as $element) {
        $name= basename($element->getUri());

        if ($element instanceof IOCollection) {
          $e= $n->addChild(new Node('collection'));
        } else {
          $e= $n->addChild(new Node('element'));
          $e->addChild(new Node('mime', MimeType::getByFilename($element->getUri())));

          if ('README' === $name) {
            $builder= new MarkupBuilder();
            $response->addFormresult(new Node('readme', new PCData(
              '<p>'.$builder->markupFor(FileUtil::getContents(new File($element->getUri()))).'</p>'
            )));
          }
        }

        $e->addChild(new Node('name', basename($element->getUri())));
        $e->addChild(Node::fromObject($element->lastModified(), 'modified'));
{'fraction_non_alphanumeric': 0.17012448132780084, 'fraction_numerical': 0.0006224066390041493, 'mean_word_length': 2.84756584197925, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3755499', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1450, 'n_words': 254}