| 1 |
<?php
|
| 2 |
// $Id: docbook2helphook.php,v 1.4 2005/10/27 22:34:17 webchick Exp $
|
| 3 |
|
| 4 |
// Path to local virgin Drupal HEAD modules directory
|
| 5 |
$path = 'D:\\Development\\Drupal\\drupal\\modules\\';
|
| 6 |
|
| 7 |
// Path to docbook export of modules section.
|
| 8 |
$docbook = 'modules.xml';
|
| 9 |
|
| 10 |
// Erase contents of adminhelp.txt
|
| 11 |
$fh = fopen('adminhelp.txt', 'w') or die("Could not open adminhelp.txt");
|
| 12 |
fwrite($fh, "\n") or die ("Could not write to adminhelp.txt");
|
| 13 |
fclose($fh);
|
| 14 |
|
| 15 |
// Retrieve list of core modules
|
| 16 |
$modules = get_module_list($path);
|
| 17 |
|
| 18 |
// Or, specifc non-core module(s)
|
| 19 |
//$modules = array('audio', 'video', 'playlist');
|
| 20 |
|
| 21 |
// Variable to hold array of ALL replacements, to check for duplicates.
|
| 22 |
$all_replacements = array();
|
| 23 |
|
| 24 |
/** BEGIN XML PARSING **/
|
| 25 |
|
| 26 |
// Array to hold help documentation
|
| 27 |
$help = array();
|
| 28 |
// Current XML tag
|
| 29 |
$tag = '';
|
| 30 |
// Indicates whether or not we're in a current module's handbook page
|
| 31 |
$inpage = false;
|
| 32 |
// Stores name of current module
|
| 33 |
$module = '';
|
| 34 |
|
| 35 |
// Setup an XML parser
|
| 36 |
$xml_parser = xml_parser_create();
|
| 37 |
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
|
| 38 |
xml_set_character_data_handler($xml_parser, 'characterData');
|
| 39 |
|
| 40 |
$data = file_get_contents($docbook);
|
| 41 |
xml_parse($xml_parser, $data);
|
| 42 |
|
| 43 |
// Perform clean-up
|
| 44 |
xml_parser_free($xml_parser);
|
| 45 |
|
| 46 |
/** END XML PARSING **/
|
| 47 |
|
| 48 |
/*
|
| 49 |
* $help array now looks like the following:
|
| 50 |
* Array
|
| 51 |
* (
|
| 52 |
* [modulename] => Array
|
| 53 |
* (
|
| 54 |
* [name] => ProperModuleName
|
| 55 |
* [data] => Array
|
| 56 |
* (
|
| 57 |
* [0] => Line 1 of handbook page
|
| 58 |
* [1] => Line 2 of handbook page
|
| 59 |
* ...
|
| 60 |
* )
|
| 61 |
* )
|
| 62 |
* [modulename2] ...
|
| 63 |
*/
|
| 64 |
|
| 65 |
// Uncomment the following line for debugging
|
| 66 |
//print_r($help);
|
| 67 |
|
| 68 |
// Now, generate hook_help function output for each module:
|
| 69 |
$output = '';
|
| 70 |
$help_hook = array();
|
| 71 |
$replace_start = ', array(';
|
| 72 |
$replace_end = ")";
|
| 73 |
foreach ($help as $module => $page) {
|
| 74 |
$output = "case 'admin/help#$module':\n";
|
| 75 |
$firstline = true;
|
| 76 |
$module_name = $page['name'];
|
| 77 |
|
| 78 |
$inlist = false;
|
| 79 |
// Go through each data line
|
| 80 |
foreach($page['data'] as $line => $data) {
|
| 81 |
// Escape quotes
|
| 82 |
$data = str_replace("'", "\'", $data);
|
| 83 |
|
| 84 |
// Remove leading/trailing whitespace
|
| 85 |
$data = trim($data);
|
| 86 |
|
| 87 |
// Get link replacements
|
| 88 |
$replacements = parse_links($data);
|
| 89 |
|
| 90 |
if (count($replacements) > 0) {
|
| 91 |
foreach ($replacements as $newpath => $link) {
|
| 92 |
// Replace links with paths
|
| 93 |
$data = preg_replace('!href\s*=\s*[\'|"]('. $link['link'] .')[\'|"]!i', "href=\"%$newpath\"", $data);
|
| 94 |
}
|
| 95 |
// Format replacement array
|
| 96 |
$replace = build_replacement_string($replacements);
|
| 97 |
} else {
|
| 98 |
$replace = '';
|
| 99 |
}
|
| 100 |
|
| 101 |
// Check for first line
|
| 102 |
if (!$inlist) {
|
| 103 |
if ($firstline == true) {
|
| 104 |
$output .= " \$output = ";
|
| 105 |
$firstline = false;
|
| 106 |
}
|
| 107 |
else {
|
| 108 |
$output .= " \$output .= ";
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
// Check for <ul> next line
|
| 113 |
if (strstr($page['data'][$line +1], '<ul>')) {
|
| 114 |
$inlist = true;
|
| 115 |
$listtext = "$data\n";
|
| 116 |
$list_replacements = array();
|
| 117 |
}
|
| 118 |
else if ($inlist == true) {
|
| 119 |
$listtext .= "$data\n";
|
| 120 |
if ($replace != '') {
|
| 121 |
$list_replacements[] = $replace;
|
| 122 |
}
|
| 123 |
if (strstr($data, '</ul>')) {
|
| 124 |
$inlist = false;
|
| 125 |
$output .= "t('$listtext'";
|
| 126 |
if (!empty($list_replacements)) {
|
| 127 |
$new_replace = array();
|
| 128 |
foreach($list_replacements as $replace) {
|
| 129 |
if (!in_array($replace, $new_replace)) {
|
| 130 |
$new_replace[] = $replace;
|
| 131 |
}
|
| 132 |
}
|
| 133 |
$output .= $replace_start . implode(', ', $new_replace) . $replace_end;
|
| 134 |
}
|
| 135 |
$output .= ");\n";
|
| 136 |
$listtext = '';
|
| 137 |
$list_replacements = array();
|
| 138 |
}
|
| 139 |
}
|
| 140 |
else {
|
| 141 |
// Get start and end tag
|
| 142 |
$start_tag = preg_match('!^<(.*?)>!', $data, $start_tag_match);
|
| 143 |
$end_tag = preg_match('!.*(</(.*?)>)$!', $data, $end_tag_match);
|
| 144 |
|
| 145 |
// Prepend start tag if it exists
|
| 146 |
if ($start_tag) {
|
| 147 |
$start = "'{$start_tag_match[0]}'. ";
|
| 148 |
$data = str_replace($start_tag_match[0], '', $data);
|
| 149 |
} else {
|
| 150 |
$start = '';
|
| 151 |
}
|
| 152 |
|
| 153 |
// Append end tag if it exists
|
| 154 |
if ($end_tag) {
|
| 155 |
$end = " .'{$end_tag_match[1]}'";
|
| 156 |
$data = str_replace($end_tag_match[1], '', $data);
|
| 157 |
} else {
|
| 158 |
$end = '';
|
| 159 |
}
|
| 160 |
|
| 161 |
// Format the output string
|
| 162 |
$output .= $start ."t('$data'";
|
| 163 |
if ($replace != '') {
|
| 164 |
$output .= $replace_start . $replace . $replace_end;
|
| 165 |
}
|
| 166 |
$output .= ")$end;\n";
|
| 167 |
}
|
| 168 |
}
|
| 169 |
$output .= " \$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href=\"%$module\">$module_name page</a>.', array('%$module' => 'http://www.drupal.org/handbook/modules/$module/')) .'</p>';\n";
|
| 170 |
$output .= " return \$output;";
|
| 171 |
|
| 172 |
$help_hook[$module] = $output;
|
| 173 |
|
| 174 |
}
|
| 175 |
|
| 176 |
foreach ($modules as $module) {
|
| 177 |
$contents = file_get_contents($path .'/'. $module .'.module');
|
| 178 |
if (strstr($contents, "case 'admin/help#$module")) {
|
| 179 |
// Replace current admin/help#module text
|
| 180 |
$pattern = "!(case 'admin/help#$module':(.*?))\s+case '!ms";
|
| 181 |
if (preg_match($pattern, $contents, $matches)) {
|
| 182 |
$contents = str_replace($matches[1], $help_hook[$module], $contents);
|
| 183 |
} else {
|
| 184 |
print "Couldn't find admin/help#module to replace: $module<br>\n";
|
| 185 |
}
|
| 186 |
|
| 187 |
} else {
|
| 188 |
// Insert admin/help#module text into existing help hook
|
| 189 |
if (strstr($contents, "_help")) {
|
| 190 |
$search = "function ". $module ."_help(\$section) {
|
| 191 |
switch (\$section) {
|
| 192 |
";
|
| 193 |
$contents = str_replace($search, $search . $help_hook[$module] ."\n ", $contents);
|
| 194 |
} else {
|
| 195 |
print "No help hook found in: $module<br>\n";
|
| 196 |
}
|
| 197 |
}
|
| 198 |
|
| 199 |
$fh = fopen($path. '/' .$module . '.module', 'w') or die("Could not open $module file for writing.");
|
| 200 |
fwrite($fh, $contents) or die("Could not write contents to $module file.");
|
| 201 |
fclose($fh);
|
| 202 |
|
| 203 |
$fh = fopen('adminhelp.txt', 'a') or die("Could not open adminhelp.txt");
|
| 204 |
fwrite($fh, " {$help_hook[$module]}\n\n\n\n") or die("Could not write to adminhelp.txt");
|
| 205 |
fclose($fh);
|
| 206 |
|
| 207 |
}
|
| 208 |
|
| 209 |
echo "Done. Modules rewritten with new help text. If errors, help text has also been written to adminhelp.txt, where you can copy/paste specific sections that didn't work out.";
|
| 210 |
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Get list of modules in directory
|
| 214 |
*
|
| 215 |
* @param $path
|
| 216 |
* Absolute path to modules directory
|
| 217 |
* @return array
|
| 218 |
* Array containing module names
|
| 219 |
*/
|
| 220 |
function get_module_list($path) {
|
| 221 |
$modules = array();
|
| 222 |
$handle = opendir($path);
|
| 223 |
while (false !== ($file = readdir($handle))) {
|
| 224 |
if (substr($file, -7) == '.module') {
|
| 225 |
// Extract module name
|
| 226 |
$module = basename($file, '.module');
|
| 227 |
$modules[] = $module;
|
| 228 |
}
|
| 229 |
}
|
| 230 |
closedir($handle);
|
| 231 |
return $modules;
|
| 232 |
} // Find each module's name by searching Drupal's modules directory
|
| 233 |
|
| 234 |
|
| 235 |
/**
|
| 236 |
* For a given piece of data, parse out the link name, path, and 'friendly path'
|
| 237 |
*
|
| 238 |
* @param $data
|
| 239 |
* String of text
|
| 240 |
* @return array
|
| 241 |
* Returns array of link replacements, in the form of:
|
| 242 |
* Array
|
| 243 |
* (
|
| 244 |
* [friendly-path] => Array
|
| 245 |
* (
|
| 246 |
* [text] => Link text
|
| 247 |
* [link] => Link URL
|
| 248 |
* )
|
| 249 |
* [friendly-path2] ...
|
| 250 |
*/
|
| 251 |
function parse_links($data) {
|
| 252 |
$replace = array();
|
| 253 |
|
| 254 |
// Find links
|
| 255 |
$pattern = '!<\s*a\s+.*?href\s*=\s*[\'|"](.*?)[\'|"].*?>(.*?)<\s*/a\s*>!i';
|
| 256 |
if (preg_match_all($pattern, $data, $matches)) {
|
| 257 |
$replacements = array();
|
| 258 |
foreach ($matches[1] as $key => $link) {
|
| 259 |
if (strpos($link, ':')) {
|
| 260 |
// External link, so change paths like external-www-drupal-org
|
| 261 |
$parts = parse_url($link);
|
| 262 |
$newpath = 'external-'. $parts['scheme']. '-' .str_replace('.', '-', $parts['host']);
|
| 263 |
if (isset($parts['path']) && $parts['path'] != '/') {
|
| 264 |
$newpath .= str_replace('/', '-', $parts['path']);
|
| 265 |
$newpath = str_replace('.', '-', $newpath);
|
| 266 |
if (substr($newpath, -1, 1) == '-') {
|
| 267 |
$newpath = substr($newpath, -1);
|
| 268 |
}
|
| 269 |
}
|
| 270 |
}
|
| 271 |
else if (strrchr($link, '.') == '.php') {
|
| 272 |
// Links to individual file, so change paths like file-cron
|
| 273 |
$newpath = 'file-'. basename($link, '.php');
|
| 274 |
}
|
| 275 |
else {
|
| 276 |
// Internal link, so change paths like admin/help/system to admin-help-system
|
| 277 |
$newpath = implode('-', explode('/', $link));
|
| 278 |
}
|
| 279 |
// Place link details into array
|
| 280 |
$replacements[$newpath]['text'] = $matches[2][$key];
|
| 281 |
$replacements[$newpath]['link'] = $link;
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
return $replacements;
|
| 286 |
|
| 287 |
}
|
| 288 |
|
| 289 |
/**
|
| 290 |
* Format an array of link replacements into replacement string
|
| 291 |
*
|
| 292 |
* @param $array
|
| 293 |
* array of link replacements (see parse_links)
|
| 294 |
* @return string
|
| 295 |
* string containing replacement text
|
| 296 |
*/
|
| 297 |
function build_replacement_string($array) {
|
| 298 |
$output = array();
|
| 299 |
|
| 300 |
foreach ($array as $path => $link_info) {
|
| 301 |
$path_start = substr($path, 0, strpos($path, '-'));
|
| 302 |
if ($path_start == 'file' || $path_start == 'external') {
|
| 303 |
// This is an external link
|
| 304 |
$output[] = "'%$path' => '{$link_info['link']}'";
|
| 305 |
}
|
| 306 |
else {
|
| 307 |
// This is an internal link
|
| 308 |
$output[] = "'%$path' => url('{$link_info['link']}')";
|
| 309 |
}
|
| 310 |
}
|
| 311 |
return implode(', ', $output);
|
| 312 |
}
|
| 313 |
|
| 314 |
/** XML PARSING FUNCTIONS **/
|
| 315 |
// Set current tag name
|
| 316 |
function startElement($parser, $name, $attribs) {
|
| 317 |
global $tag;
|
| 318 |
$tag = $name;
|
| 319 |
}
|
| 320 |
|
| 321 |
// Reset current tag name
|
| 322 |
function endElement($parser, $name) {
|
| 323 |
global $tag;
|
| 324 |
$tag = '';
|
| 325 |
if ($name == 'LITERALLAYOUT') {
|
| 326 |
$inpage = false;
|
| 327 |
}
|
| 328 |
}
|
| 329 |
|
| 330 |
function characterData($parser, $data) {
|
| 331 |
global $tag, $inpage, $modules, $module, $help;
|
| 332 |
|
| 333 |
if ($tag == 'TITLE') {
|
| 334 |
// Determine if title is in the "Module name: " format, and
|
| 335 |
// if it matches one of the existing module names
|
| 336 |
if (preg_match('!^(.*?): !', $data, $matches) && in_array($module = strtolower($matches[1]), $modules)) {
|
| 337 |
$inpage = true;
|
| 338 |
$help[$module]['name'] = $matches[1];
|
| 339 |
} else {
|
| 340 |
$module = false;
|
| 341 |
$inpage = false;
|
| 342 |
}
|
| 343 |
}
|
| 344 |
if ($inpage == true && $tag != 'RELEASEINFO' && $tag != 'TITLE' && !preg_match('!^\s+$!', $data)) {
|
| 345 |
$help[$module]['data'][] = $data;
|
| 346 |
}
|
| 347 |
}
|
| 348 |
/** END XML PARSING FUNCTIONS **/
|
| 349 |
|
| 350 |
?>
|