cvs: peardoc /scripts pearwebpackagetocs.php
| From: | Christian Weiske | Date: | Tue, 11 Nov 2008 08:49:00 +0000 |
| Subject: | cvs: peardoc /scripts pearwebpackagetocs.php | ||
| Groups: | php.pear.doc | ||
| Request: | Send a blank email to pear-doc+get-11259@lists.php.net to get a copy of this message | ||
cweiske Tue Nov 11 08:49:00 2008 UTC
Added files:
/peardoc/scripts pearwebpackagetocs.php
Log:
Add pearweb package TOC generation script
http://cvs.php.net/viewvc.cgi/peardoc/scripts/pearwebpackagetocs.php?view=markup&rev=1.1
Index: peardoc/scripts/pearwebpackagetocs.php
+++ peardoc/scripts/pearwebpackagetocs.php
#!/usr/bin/env php
<?php
$opts = parseArgs();
try {
$indexFile = getIndexFile($opts);
$outputDir = ensureOutputDirExists($opts['out_dir']);
$nCount = createPackageTocs($indexFile, $outputDir);
echo 'Generated TOCs for ' . $nCount . ' packages' . "\n";
} catch (Exception $e) {
echo "Exception:\n";
echo $e->getMessage() . "\n";
exit($e->getCode());
}
/**
* Parses commandline arguments, displays help if necessary and returns
* an array of parsed parameters.
*
* @return array Key-value pair array, parameter name is key, value is value.
*/
function parseArgs()
{
require_once 'Console/CommandLine.php';
$parser = new Console_CommandLine(array(
'description' => 'Create package TOC files for pearweb',
'version' => '$Id: pearwebpackagetocs.php,v 1.1 2008/11/11 08:49:00
cweiske Exp $'
));
$parser->addOption('verbose', array(
'short_name' => '-v',
'long_name' => '--verbose',
'action' => 'StoreTrue',
'description' => 'Display detailled messages',
'default' => false,
));
$parser->addOption('giant_file', array(
'short_name' => '-h',
'long_name' => '--giant',
'action' => 'StoreString',
'description' => 'File name of merged (giant) manual',
'default' => 'giant.xml',
));
$parser->addOption('index_file', array(
'short_name' => '-i',
'long_name' => '--index',
'action' => 'StoreString',
'description' => 'File name of index file. Overrides giant file
setting',
'default' => null,
));
$parser->addOption('out_dir', array(
'short_name' => '-o',
'long_name' => '--output-directory',
'action' => 'StoreString',
'description' => 'In which directory the written TOC files shall be
written',
'default' => dirname(__FILE__) . '/../build/en/packagetocs/',
));
try {
$result = $parser->parse();
return $result->options;
} catch (Exception $exc) {
$parser->displayError($exc->getMessage());
exit(-2);
}
}
/**
* Checks if the given output directory exists and is writable.
* Tries to create it if it does not exist.
* Adds a trailing slash if it hasn't one
*
* @param string $outputDir Output directory
*
* @return string Output directory with trailing slash
*
* @throws Exception When the directory could not be created or is not writable.
*/
function ensureOutputDirExists($outputDir)
{
if (!is_dir($outputDir)) {
if (!mkdir($outputDir, 0755, true)) {
throw new Exception(
'Output directory could not be created'
. "\n" . $outputDir, -1
);
}
}
if (!is_writable($outputDir)) {
throw new Exception('Output directory is not writable', -2);
}
if (substr($outputDir, -1) !== '/') {
$outputDir .= '/';
}
return $outputDir;
}
/**
* Reads the index file from the options.
* Takes the giant_file as well as the index_file setting into account.
*
* @param array $opts Array of options parsed from cmdline args
*
* @return string Path of serialized index file
*
* @throws Exception When no index file is given or none could be found.
*/
function getIndexFile($opts)
{
if ($opts['index_file'] !== null) {
$indexFile = $opts['index_file'];
} else {
$indexFile = '.index_' . basename($opts['giant_file'], '.xml')
. '.ser';
}
if (!file_exists($indexFile)) {
throw new Exception(
'Index file does not exist: ' . $indexFile . "\n"
. 'Run phd first.', -11
);
}
if (!is_readable($indexFile)) {
throw new Exception(
'Index file is not readable: ' . $indexFile, -12
);
}
return $indexFile;
}
function createPearwebPackageToc($arPackage, $depth = 2)
{
$chunks = $arPackage['children'];
if ($depth == 0 || !count($chunks)) {
return '';
}
$content = '';
$content .= "<ul class=\"packagetoc\">\n";
foreach ($chunks as $chunkid => $junk) {
$long = $junk['ldesc'];
$short = $junk['sdesc'];
$desc = ($long ? $long : $short) . '</a>';
$content .= " <li><a
href=\"/manual/en/{$arPackage['filename']}\">" . $desc;
if ($depth > 1) {
$content .= createPearwebPackageToc($junk, $depth - 1);
}
$content .= "</li>\n";;
}
$content .= "</ul>\n";
return $content;
}
/**
* Creates TOC files for all packages.
*
* @param string $indexFile Path of index file
* @param string $outputDir Output directory with trailing slash
*
* @return integer Number of created package TOC files
*/
function createPackageTocs($indexFile, $outputDir)
{
$arIndex = unserialize(file_get_contents($indexFile));
$nCount = 0;
foreach ($arIndex['packages']['children'] as $arCategories) {
foreach ($arCategories['children'] as $arPackage) {
$strToc = createPearwebPackageToc($arPackage);
if ($strToc) {
$strName = strtolower($arPackage['ldesc']);
file_put_contents(
$outputDir . $strName . '.htm',
$strToc
);
++$nCount;
}
}
}
return $nCount;
}
?>