/** * Returns all the html files (chapters) from a file package * * @param stored_file $package file to be processed * @param string $type type of the package ('typezipdirs' or 'typezipfiles') * * @return array the html files found in the package */ function toolbook_importhtml_get_chapter_files($package, $type) { $packer = get_file_packer('application/zip'); $files = $package->list_files($packer); $tophtmlfiles = array(); $subhtmlfiles = array(); $topdirs = array(); foreach ($files as $file) { if (empty($file->pathname)) { continue; } if (substr($file->pathname, -1) === '/') { if (substr_count($file->pathname, '/') !== 1) { // skip subdirs continue; } if (!isset($topdirs[$file->pathname])) { $topdirs[$file->pathname] = array(); } } else { $mime = mimeinfo('icon', $file->pathname); if ($mime !== 'html') { continue; } $level = substr_count($file->pathname, '/'); if ($level === 0) { $tophtmlfiles[$file->pathname] = $file; } else if ($level === 1) { $subhtmlfiles[$file->pathname] = $file; $dir = preg_replace('|/.*$|', '', $file->pathname); $topdirs[$dir][$file->pathname] = $file; } else { // lower levels are not interesting continue; } } } // TODO: natural dir sorting would be nice here... textlib::asort($tophtmlfiles); textlib::asort($subhtmlfiles); textlib::asort($topdirs); $chapterfiles = array(); if ($type == 2) { $chapterfiles = $tophtmlfiles; } else if ($type == 1) { foreach ($topdirs as $dir => $htmlfiles) { if (empty($htmlfiles)) { continue; } textlib::asort($htmlfiles); if (isset($htmlfiles[$dir.'/index.html'])) { $htmlfile = $htmlfiles[$dir.'/index.html']; } else if (isset($htmlfiles[$dir.'/index.htm'])) { $htmlfile = $htmlfiles[$dir.'/index.htm']; } else if (isset($htmlfiles[$dir.'/Default.htm'])) { $htmlfile = $htmlfiles[$dir.'/Default.htm']; } else { $htmlfile = reset($htmlfiles); } $chapterfiles[$htmlfile->pathname] = $htmlfile; } } else if ($type == 0) { if ($tophtmlfiles) { if (isset($tophtmlfiles['index.html'])) { $htmlfile = $tophtmlfiles['index.html']; } else if (isset($tophtmlfiles['index.htm'])) { $htmlfile = $tophtmlfiles['index.htm']; } else if (isset($tophtmlfiles['Default.htm'])) { $htmlfile = $tophtmlfiles['Default.htm']; } else { $htmlfile = reset($tophtmlfiles); } } else { $htmlfile = reset($subhtmlfiles); } $chapterfiles[$htmlfile->pathname] = $htmlfile; } return $chapterfiles; }