1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
|
<?php
/*
SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams@gmail.com>
SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
if (!isset($_SERVER['argv'][1])) {
$msg = "Usage:\n".$_SERVER['argv'][0]." [path to phpdoc]\n";
$msg .= "you may checkout from the php svn server using this command:\n";
$msg .= "svn checkout http://svn.php.net/repository/phpdoc/en/trunk ./phpdoc-en\n";
$msg .= "\nTo debug files/directories use this: ".$_SERVER['argv'][0]." --debug PATH ...\n";
file_put_contents('php://stderr', $msg);
exit(-1);
}
$skipClasses = array();
$skipClasses[] = 'self';
$skipClasses[] = 'parent';
$skipClasses[] = '__php_incomplete_class';
$skipClasses[] = 'php_user_filter';
$skipClasses[] = 'static'; // O_o where does that come from?
$skipClasses[] = 'componere\abstract\definition'; // invalid namespace identifier, discussed in https://github.com/krakjoe/componere/issues/5
// interfaces wrongly declared as classes
$interfaceClasses = array();
$interfaceClasses[] = 'sessionhandlerinterface';
$interfaceClasses[] = 'yaf_route_interface';
$interfaceClasses[] = 'yaf_view_interface';
$abstractClasses[] = array();
$abstractClasses[] = 'reflectionfunctionabstract';
$abstractClasses[] = 'xmldiff\base';
$abstractClasses[] = 'yaf_action_abstract';
$skipComments = array();
$skipComments[] = ':';
$skipComments[] = '(method):';
$skipComments[] = 'Description here.';
$skipComments[] = 'Description here...';
$skipComments[] = 'Description';
$skipComments[] = 'The function description goes here.';
$classes = array();
$constants = array();
$constants_comments = array();
$variables = array();
$existingFunctions = array();
$versions = array();
if ($_SERVER['argv'][1] == '--debug') {
// only debug given file
define('DEBUG', true);
$dirs = array();
foreach ( $_SERVER['argv'] as $i => $v ) {
if ( $i <= 1 ) {
continue;
} else if ( is_dir($v) ) {
$dirs[] = $v;
} else if ( file_exists($v) ) {
parseFile(new SplFileInfo($v));
} else {
trigger_error("bad argument: ".$v, E_USER_ERROR);
}
}
} else {
define('DEBUG', false);
if (!file_exists($_SERVER['argv'][1])) {
file_put_contents('php://stderr', "phpdoc path not found");
exit(-1);
}
$dirs = array(
$_SERVER['argv'][1]."/reference",
$_SERVER['argv'][1]."/features",
$_SERVER['argv'][1]."/appendices",
$_SERVER['argv'][1]."/language/predefined/"
);
}
foreach ($dirs as $dir) {
$dirIt = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir));
foreach ($dirIt as $file) {
parseFile($file);
}
}
unset($existingFunctions);
/*
Here be dirty hacks! PHP's documentation isn't as good as could be wished for...
*/
// Add constant only classes
foreach (array_keys($constants) as $c) {
if ($pos = strpos($c, '::')) {
$class = substr($c, 0, $pos);
$isInterface = false;
$isAbstractClass = false;
newClassEntry($class);
}
}
$skipFunctions = array();
// remove delete() function which only acts as a pointer to unlink
// in the documentation but does not actually exist as a alias in PHP
$skipFunctions[] = 'delete';
// awesome uncallable functions - noone knows wth that should be...
$skipMethods = array();
$skipMethods[] = 'list';
$skipMethods[] = 'declare';
$skipMethods[] = 'do';
$skipMethods[] = 'echo';
$skipMethods[] = 'function';
/*
Here ends the hackings...
*/
function constTypeValue($ctype) {
if ($ctype == 'integer' || $ctype == 'int') {
return "0";
} else if ($ctype == 'string') {
return "''";
} else if ($ctype == 'bool') {
return "false";
} else if ($ctype == 'float') {
return "0.0";
} else {
// default to integer const type
return "0";
}
}
function removeTag($xml, $tag) {
$tag = preg_quote($tag, '#');
return trim(preg_replace('#(^<'.$tag.'[^>]*>|</'.$tag.'>$)#s', '', trim($xml)));
}
function cleanupComment($comment) {
// <function|parameter>...</> to {@link ...}
$comment = preg_replace('#<(function|parameter)>(.+)</\1>#U', '{@link $2}', $comment);
// remove <para> and other stuff
///TODO: support web-links, lists and tables
$comment = strip_tags($comment);
$comment = html_entity_decode($comment);
// make sure no */ occurs in a comment...
$comment = preg_replace('#\*/#', '* /', $comment);
$comment = preg_replace('#(?<=[^\n])\n(?=[^\n])#s', ' ', $comment);
$comment = preg_replace('# +#', ' ', $comment);
$comment = preg_replace('#^ | $#m', '', $comment);
$comment = preg_replace("#\n{3,}#", "\n\n", $comment);
$comment = trim($comment);
return $comment;
}
function prepareComment($comment, array $more, $indent = '') {
$comment = cleanupComment($comment);
if (empty($comment) && empty($more)) {
return '';
}
$comment = wordwrap($comment, 70, "\n", false);
if ( !empty($more) ) {
if ( !empty($comment) ) {
$comment .= "\n\n";
}
foreach($more as $s) {
$comment .= str_replace("\n", "\n ", // indent
wordwrap(cleanupComment($s), 68, "\n", false)
)."\n";
}
}
$comment = rtrim($comment);
// add indentation and asterisk
$comment = preg_replace("#^#m", $indent." * ", $comment);
$comment = str_replace(" * \n", " *\n", $comment);
return $indent."/**\n".
$comment."\n".
$indent." **/\n";
}
function sortByName($a, $b) {
return strnatcasecmp($a['name'], $b['name']);
}
$fileHeader = "<?php\n";
$fileHeader .= "// THIS FILE IS GENERATED\n";
$fileHeader .= "// WARNING! All changes made in this file will be lost!\n\n";
$declarationCount = 0;
$out = $fileHeader;
// make sure the output it somehow ordered to prevent huge svn diffs
uksort($variables, 'strnatcasecmp');
uksort($classes, 'strnatcasecmp');
uksort($constants, 'strnatcasecmp');
// put some base classes up front
$baseClasses = [ 'exception', 'error', 'throwable', 'iterator', 'traversable' ];
foreach ($baseClasses as $baseClass) {
if (array_key_exists($baseClass, $classes)) {
$base = $classes[$baseClass];
unset($classes[$baseClass]);
$classes = array_merge(array($baseClass => $base), $classes);
reset($classes);
}
}
foreach ($variables as $name=>$var) {
$declarationCount++;
$moreDesc = array();
if ($var['deprecated']) {
$moreDesc[] = "@deprecated";
}
if (isset($var['superglobal']) && $var['superglobal']) {
$moreDesc[] = "@superglobal";
}
$out .= prepareComment($var['desc'], $moreDesc);
$out .= "$name = array();\n\n";
}
// make skipclasses lowercase
foreach ($skipClasses as &$name) {
$name = strtolower($name);
}
foreach ($classes as $class => $i) {
if (in_array($class, $skipClasses)) continue; //skip those as they are documented in spl.php
if ($class != 'global') {
if (isset($i['desc'])) {
$out .= prepareComment($i['desc'], array());
}
if (!empty($i['namespace'])) {
$out .= 'namespace ' . $i['namespace'] . " {\n";
}
$class = $i['prettyName'];
$out .= ($i['isAbstract']) ? 'abstract ' : '';
$out .= ($i['isFinal'] && !$i['isInterface']) ? 'final ' : '';
$out .= ($i['isInterface'] ? 'interface' : 'class') . " " . $class;
if (isset($i['extends'])) {
if (!is_array($i['extends']) && !in_array(strtolower($i['extends']), $skipClasses)) {
$out .= " extends {$i['extends']}";
} elseif (is_array($i['extends'])) {
$out .= " extends ";
foreach ($i['extends'] as $entry) {
if (!in_array(strtolower($entry), $skipClasses)) {
$out .= "$entry, ";
}
}
$out = rtrim($out, ', ');
}
}
if (isset($i['implements'])) {
$out .= " implements ".implode(", ", $i['implements']);
}
$out .= " {\n";
$declarationCount++;
}
$indent = '';
if ($class != 'global') $indent = ' ';
if (array_key_exists('extends', $i)) {
if (!is_array($i['extends'])) {
$extends = [ $i['extends'] ];
} else {
$extends = $i['extends'];
}
foreach ($extends as $entry) {
if (in_array(strtolower($entry), $skipClasses)) {
$base = $classes[strtolower($entry)];
$i['properties'] = array_merge($i['properties'], $base['properties']);
$i['functions'] = array_merge($i['functions'], $base['functions']);
}
}
}
foreach ($constants as $c=>$ctype) {
if ($pos = strpos($c, '::')) {
if (substr($c, 0, $pos) == $class) {
$moreDesc = array();
$moreDesc[] = "@var {$ctype}";
if ( isset($constants_comments[$c]) ) {
$out .= prepareComment($constants_comments[$c], $moreDesc, $indent);
}
unset($constants[$c]);
$c = substr($c, $pos+2);
$out .= " const $c = ".constTypeValue($ctype).";\n\n";
$declarationCount++;
}
}
}
usort($i['properties'], 'sortByName');
$handled = [];
foreach ($i['properties'] as $f) {
if (in_array($f['name'], $handled)) {
continue;
}
$handled[] = $f['name'];
$moreDesc = array();
if ($f['type']) {
$moreDesc[] = "@var {$f['type']}";
}
///HACK the directory stuff has really bad documentation
if ($class != 'directory') {
$out .= prepareComment($f['desc'], $moreDesc, $indent);
}
if ($class != 'global' && array_key_exists('modifiers', $f) && is_array($f['modifiers'])) {
$f['modifiers'] = array_filter($f['modifiers'], function ($value){ return $value != 'readonly'; });
$modifiers = implode(' ', $f['modifiers']);
$modifiers .= empty($modifiers) ? 'public ' : ' ';
}
$out .= "{$indent}{$modifiers}$".$f['name'].";\n\n";
$declarationCount++;
}
usort($i['functions'], 'sortByName');
$handled = [];
foreach ($i['functions'] as $f) {
if (in_array($f['name'], $handled)) {
continue;
}
$handled[] = $f['name'];
if ( $class == 'global' && in_array($f['name'], $skipFunctions) ) {
continue;
} else if ( $class != 'global' && in_array($f['name'], $skipMethods) ) {
continue;
}
if ($endpos = strrpos($f['name'], '\\')) {
$out .= 'namespace ' . substr($f['name'], 0, $endpos) . " {\n\n";
$f['name'] = substr($f['name'], $endpos + 1);
}
$moreDesc = array();
foreach ($f['params'] as $pi=>$param) {
$desc = '';
if ( isset($param['desc']) ) {
$desc = trim($param['desc']);
}
$param_name = str_replace('$...', '...$', '$'.$param['name']);
$moreDesc[] = "@param {$param['type']} $param_name $desc";
}
if ($f['type']) {
$moreDesc[] = rtrim("@return {$f['type']} {$f['return_desc']}");
}
$version_key = strtolower(($class == 'global' ? '' : $class.'::') . $f['name']);
if (isset($versions[$version_key])) {
$moreDesc[] = "@since {$versions[$version_key]}";
}
///HACK the directory stuff has really bad documentation
if ($class != 'directory') {
$out .= prepareComment($f['desc'], $moreDesc, $indent);
}
if ($f['name'] == '__construct' && strtolower($class) == 'eventutil') {
//HACK: PHP doesn't allow abstract final classes, so make the constructor private instead.
$f['modifiers'] = [ 'private' ];
}
if ($class != 'global' && array_key_exists('modifiers', $f) && is_array($f['modifiers'])) {
if ($i['isInterface'] === true) {
$f['modifiers'] = array_filter($f['modifiers'], function ($value){ return $value != 'abstract' && $value != 'final'; });
}
$modifiers = implode(' ', $f['modifiers']);
$modifiers .= empty($modifiers) ? '' : ' ';
} else {
$modifiers = '';
}
$out .= "{$indent}{$modifiers}function ".$f['name'];
$out .= "(";
$first = true;
$params_total = count($f['params'])-1;
foreach ($f['params'] as $pi=>$param) {
$param_name = str_replace('$...', '...$', '$'.$param['name']);
$param_name = str_replace('"', '', $param_name);
if (substr($param_name, 0, 3) == '...' && $pi < $params_total) {
// varargs in the middle of arguments are invalid syntax.
// print the documentation, but ignore them in the function signature
continue;
}
if (!$first) $out .= ", ";
$first = false;
if ($param['isRef']) $out .= "&";
$out .= $param_name;
}
$out .= ")";
if ( $i['isInterface'] || (array_key_exists('modifiers', $f) && in_array('abstract', $f['modifiers'])) ) {
$out .= ";";
} else {
$out .= "{}";
}
$out .= "\n\n";
if ($endpos) $out .= "}\n\n";
$declarationCount++;
}
if ($class != 'global') $out .= "}\n";
if (!empty($i['namespace'])) $out .= "}\n";
}
foreach ($constants as $c=>$ctype) {
if (strpos($c, '::')===false) {
if ( isset($constants_comments[$c]) ) {
$out .= prepareComment($constants_comments[$c], array());
}
$out .= "define('$c', ".constTypeValue($ctype).");\n";
$declarationCount++;
}
}
chdir(dirname(__FILE__));
if ( !DEBUG ) {
echo "saving phpfunctions.php file\n";
file_put_contents("phpfunctions.php", $out);
if ( shell_exec("which php-parser") ) {
echo "making sure phpfunctions file is valid...\n";
system("php-parser phpfunctions.php", $ret);
if ( $ret != 0 ) {
die("could not parse file, aborting\n");
}
} else {
echo "note: put php-parser in your path and I can check the generated file directly...\n";
}
echo "done\n";
} else {
echo "phpfunctions.php\n~~~~\n$out\n~~~~\n";
}
echo "wrote ".$declarationCount." declarations\n";
/**
* Parse file
*
* @param SplFileInfo $file File handler
* @return bool
*/
function parseFile($file, $funcOverload="", $classOverload="") {
global $existingFunctions, $constants, $constants_comments, $variables, $classes, $isInterface, $isAbstractClass, $isFinalClass, $versions;
if (substr($file->getFilename(), -4) != '.xml') return false;
if (substr($file->getFilename(), 0, 9) == 'entities.') return false;
$string = file_get_contents($file->getPathname());
$isInterface = (strpos($string, '<phpdoc:classref') !== false &&
strpos($string, '&reftitle.interfacesynopsis;') !== false) ||
strpos($string, ' interface</title>') !== false;
$isAbstractClass = false;
$isFinalClass = false;
$string = str_replace('&null;', 'NULL', $string);
$string = str_replace('&true;', 'TRUE', $string);
$string = str_replace('&false;', 'FALSE', $string);
$string = preg_replace('#(?:(&|>|<)|&[A-Za-z\\.0-9-_]+;)#', '$1', $string);
$removeSections = array();
$removeSections[] = 'apd.installwin32';
$removeSections[] = 'intl.intldateformatter-constants.calendartypes';
foreach ($removeSections as $i) {
$string = preg_replace('#'.preg_quote('<section xml:id="'.$i.'">').'.*?</section>#s', '', $string);
}
echo "reading documentation from {$file->getPathname()}\n";
libxml_use_internal_errors(TRUE);
$xml = simplexml_load_string($string, "SimpleXMLElement", LIBXML_NOCDATA);
if ($xml === false) {
echo " parsing XMl failed!\n";
return false;
}
if ( $file->getFilename() == 'versions.xml' ) {
foreach ( $xml->xpath('/versions/function') as $f ) {
$attrs = $f->attributes();
$versions[strtolower($attrs['name'])] = (string) $attrs['from'];
}
return;
}
$xml->registerXPathNamespace('db', 'http://docbook.org/ns/docbook');
$xml->registerXPathNamespace('phpdoc', 'http://php.net/ns/phpdoc');
if ($vars = $xml->xpath('//phpdoc:varentry//db:refnamediv')) {
foreach ($vars as $var) {
foreach ($var->refname as $i) {
$i = (string)$i;
if ( isset($variables[$i]) ) {
$v = $variables[$i];
} else {
$v = array();
}
if (substr($i, 0, 1) != '$') continue;
if (substr($i, -10) == ' [removed]') continue;
if (substr($i, -13) == ' [deprecated]') {
$i = substr($i, 0, -13);
$v['deprecated'] = true;
} else {
$v['deprecated'] = false;
}
$v['desc'] = (string)$var->refpurpose;
$variables[$i] = $v;
}
}
}
if ($vars = $xml->xpath("//phpdoc:varentry[@xml:id='language.variables.superglobals']//db:member/db:varname")) {
foreach ($vars as $var) {
$variables[(string)$var]['superglobal'] = true;
}
}
if (isset($xml->variablelist)) {
foreach ($xml->variablelist as $list) {
foreach ($list->varlistentry as $i=>$varlistentry) {
if ($c = (string)$varlistentry->term->constant) {
if (!isset($constants[$c])) {
if (strpos($c, '=')) {
$c = substr($c, 0, strpos($c, '='));
}
$ctype = $varlistentry->term->type;
if (!$ctype) {
$ctype = $varlistentry->term->link;
}
$constants[$c] = (string)$ctype;
}
}
}
}
}
// handle class constants (and their global aliases) not defined as fieldsynopsis
$consts = $xml->xpath("db:partintro//db:varlistentry");
foreach ($consts as $c) {
foreach ($c->term as $t) {
if ($name = (string)$t->constant) {
if (!isset($constants[$name])) {
if (strpos($name, '=')) {
$c = substr($name, 0, strpos($name, '='));
}
$constants[$name] = 'mixed';
$constants_comments[$name] = cleanupComment($c->listitem->para->asXML());
}
}
}
}
// handle constants under section
if ($file->getFilename() == 'constants.xml') {
$consts = $xml->xpath("db:section//db:varlistentry");
foreach ($consts as $c) {
if ($name = (string)$c->term->constant) {
if (!isset($constants[$name])) {
if (strpos($name, '=')) {
$c = substr($name, 0, strpos($name, '='));
}
$ctype = $c->term->type;
if (!$ctype) {
$ctype = $c->term->link;
}
$constants[$name] = (string)$ctype;
}
}
}
}
// handle constants within tables
if ($file->getFilename() == 'constants.xml') {
$consts = $xml->xpath("db:section//db:row");
foreach ($consts as $c) {
if (!$c->entry && !$c->entry[0] && !$c->entry[0]->constant) {
continue;
}
$name = (string)$c->entry[0]->constant;
switch ($name) {
case '':
continue 2;
default:
if ($c->entry[2]) {
$constants[$name] = $c->term->type;
$constants_comments[$name] = cleanupComment($c->entry[2]->asXML());
} else if ($c->entry[1]) {
$constants[$name] = 'mixed';
$constants_comments[$name] = cleanupComment($c->entry[1]->asXML());
}
}
}
}
// handle constants within para
if ($file->getFilename() == 'constants.xml') {
$consts = $xml->xpath("db:para//db:varlistentry");
foreach ($consts as $c) {
if ($name = (string)$c->term->constant) {
if ($name == '__default') {
continue;
}
if (!isset($constants[$name])) {
if (strpos($name, '=')) {
$c = substr($name, 0, strpos($name, '='));
}
$ctype = $c->term->type;
if (!$ctype) {
$ctype = $c->term->link;
}
$constants[$name] = (string)$ctype;
}
}
}
}
if ($file->getFilename() == 'constants.xml') {
$consts = $xml->xpath("db:para//db:simpara");
foreach ($consts as $c) {
if ($name = (string)$c->constant) {
if ($name[0] == '"') {
continue;
}
if (!isset($constants[$name])) {
$constants[$name] = 'mixed';
}
}
}
}
if ($file->getFilename() == 'ciphers.xml') {
$consts = $xml->xpath("db:para//db:simpara");
foreach ($consts as $c) {
if ($name = (string)$c) {
$name = explode(' ', $name)[0];
$name = explode('(', $name)[0];
if (!isset($constants[$name])) {
$constants[$name] = 'mixed';
}
}
}
}
// handle constants in tables within refsect1
if ($file->getFilename() != 'attr.xml') {
$consts = $xml->xpath("db:refsect1//db:row");
foreach ($consts as $c) {
if (!$c->entry && !$c->entry[0] && !$c->entry[0]->constant) {
continue;
}
$name = (string)$c->entry[0]->constant;
switch ($name) {
case '':
case '0':
continue 2;
case 'ABDAY_(1-7)':
case 'DAY_(1-7)':
for ($i=1; $i<8;$i++) {
$cname = substr($name, 0, strpos($name, '_') + 1) . $i;
$constants[$cname] = 'mixed';
if ($c->entry[1]) {
$constants_comments[$cname] = cleanupComment($c->entry[1]->asXML());
}
}
break;
case 'ABMON_(1-12)':
case 'MON_(1-12)':
for ($i=1; $i<13;$i++) {
$cname = substr($name, 0, strpos($name, '_') + 1) . $i;
$constants[$cname] = 'mixed';
if ($c->entry[1]) {
$constants_comments[$cname] = cleanupComment($c->entry[1]->asXML());
}
}
break;
default:
$constants[$name] = 'mixed';
if ($c->entry[1]) {
$constants_comments[$name] = cleanupComment($c->entry[1]->asXML());
}
}
}
}
// handle constants within file-upload.xml
if ($file->getFilename() == 'file-upload.xml') {
$consts = $xml->xpath("db:sect1//db:varlistentry");
foreach ($consts as $c) {
if ($name = (string)$c->term->constant) {
if (!isset($constants[$name])) {
if (strpos($name, '=')) {
$c = substr($name, 0, strpos($name, '='));
}
$ctype = $c->term->type;
if (!$ctype) {
$ctype = $c->term->link;
}
$constants[$name] = (string)$ctype;
$constants_comments[$name] = cleanupComment($c->listitem->para->asXML());
}
}
}
}
// handle constants within tokens.xml
if ($file->getFilename() == 'tokens.xml') {
$consts = $xml->xpath("db:table//db:row");
foreach ($consts as $c) {
if (!$c->entry && !$c->entry[0] && !$c->entry[0]->constant) {
continue;
}
$name = (string)$c->entry[0]->constant;
switch ($name) {
case '':
continue 2;
default:
$constants[$name] = 'mixed';
}
}
}
// handle url-stat constants
if ($file->getFilename() == 'url-stat.xml') {
$consts = $xml->xpath("db:refsect1//db:informaltable//db:row");
foreach ($consts as $c) {
if (!$c->entry && !$c->entry[0]) {
continue;
}
$name = (string)$c->entry[0];
switch ($name) {
case '':
case 'Flag':
continue 2;
default:
$constants[$name] = 'mixed';
$constants_comments[$name] = cleanupComment($c->entry[1]->asXML());
}
}
}
// handle constants.xml with different layout as those above
if ( !isset($xml->variablelist) && $file->getFilename() == 'constants.xml' && $xml->xpath("//db:constant") ) {
$consts = $xml->xpath("//db:entry");
foreach ( $consts as $i=>$p ) {
if ( isset($p->constant) ) {
if ( !isset($p->type) ) {
// default to integer constants
$p->type = 'integer';
} else {
// check for comment
// next entry is the value of the constant which is followed by the comment
if ( isset($consts[$i+2]) && !$consts[$i+2]->children() ) {
$comment = $consts[$i+2]->asXml();
if ( !empty($comment) ) {
$constants_comments[(string)$p->constant] = $comment;
}
}
}
$name = (string)$p->constant;
if ($name == 'LOG_LOCAL0 ... LOG_LOCAL7') {
for ($i=0; $i<8; $i++) {
$constants['LOG_LOCAL' . $i] = (string)$p->type;
}
} else {
$constants[$name] = (string)$p->type;
}
}
}
} else if (!isset($xml->variablelist) && $file->getFilename() == 'commandline.xml') {
// yay for non-unified xml structures :-X
$consts = $xml->xpath("//db:row");
foreach ( $consts as $i=>$p ) {
$constant = "";
// default to integer constants
$type = "integer";
if ( isset($p->entry[0]) && isset($p->entry[0]->constant) ) {
$constant = trim((string) $p->entry[0]->constant);
if ( isset($p->entry[0]->constant->type) ) {
$type = (string)$p->entry[0]->constant->type;
}
}
if (empty($constant)) {
continue;
}
// check for comment
// next entry is the comment
if ( isset($p->entry[1]) ) {
$comment = $p->entry[1]->para->asXml();
if ( !empty($comment) ) {
$constants_comments[$constant] = $comment;
}
}
$constants[$constant] = $type;
}
}
if ($list = $xml->xpath('//db:sect2[starts-with(@xml:id, "reserved.classes")]/db:variablelist/db:varlistentry')) {
foreach ($list as $l) {
$classname = newClassEntry((string)$l->term->classname);
$classes[$classname]['desc'] = cleanupComment(removeTag($l->listitem->asXML(), 'listitem'));
}
}
$cEls = $xml->xpath('//db:classsynopsis/db:classsynopsisinfo');
if ($cEls) {
foreach ($cEls as $class) {
$class->registerXPathNamespace('db', 'http://docbook.org/ns/docbook');
$className = (string)$class->ooclass->classname;
if ((string)$class->ooclass->modifier === 'abstract') {
$isAbstractClass = true;
}
if ((string)$class->ooclass->modifier === 'final') {
$isFinalClass = true;
}
if (!$className) continue;
$className = newClassEntry($className);
if ($interfaces = $class->xpath('//db:oointerface/db:interfacename')) {
$key = $isInterface ? 'extends' : 'implements';
foreach ($interfaces as $if) {
$classes[$className][$key][] = (string)$if;
}
}
if ($extends = $class->xpath('//db:ooclass')) {
foreach ($extends as $c) {
if ($c->modifier == 'extends') {
if (array_key_exists('extends', $classes[$className]) && is_array($classes[$className]['extends'])) {
array_unshift($classes[$className]['extends'], (string)$c->classname);
} else {
$classes[$className]['extends'] = (string)$c->classname;
}
} elseif ($c->modifier == 'implements') {
if (array_key_exists('implements', $classes[$className]) && is_array($classes[$className]['implements'])) {
array_unshift($classes[$className]['implements'], (string)$c->classname);
} else {
$classes[$className]['implements'][] = (string)$c->classname;
}
}
}
}
if ($paras = $xml->xpath('//db:section[starts-with(@xml:id, "'.$className.'")]/db:para')) {
foreach ($paras as $p) {
$classes[$className]['desc'] .= "\n". cleanupComment(removeTag($p->asXML(), 'para'));
}
} elseif ($paras = $xml->xpath('//db:section[starts-with(@xml:id, "'. str_replace('_', '-', $className) .'")]/db:para')) {
foreach ($paras as $p) {
$classes[$className]['desc'] .= "\n". cleanupComment(removeTag($p->asXML(), 'para'));
}
}
}
}
$addedSomething = false;
if (isset($xml->partintro) && !isset($xml->refsect1)) {
if (isset($xml->partintro->section[1]->classsynopsis->fieldsynopsis)) {
$synopsis = $xml->partintro->section[1]->classsynopsis->fieldsynopsis;
$class = $xml->partintro->section[1]->classsynopsis->ooclass->classname;
$classname = newClassEntry($class);
foreach ($synopsis as $property){
$name = $property->varname;
$modifiers = (array) $property->modifier;
$type = isset($property->type) ? $property->type : 'mixed';
$desc = '';
if (in_array('const', $modifiers)) {
$constantname = strtolower(substr((string) $name, strrpos((string) $name, '::') + 2));
if ($constant_descs = $xml->xpath('//db:section[@xml:id="' . $classname .'.constants"]//db:varlistentry[@xml:id="'. $classname .'.constants.'. $constantname .'"]') ) {
foreach ($constant_descs as $constant_desc) {
$desc .= getDocumentationFromPartIntro($constant_desc);
}
}
$constants_comments[(string) $name] = $desc;
$constants[(string) $name] = $type;
} else {
if ($property_descs = $xml->xpath('//db:section[@xml:id="' . $classname .'.props"]//db:varlistentry[@xml:id="'. $classname .'.props.'. $name .'"]') ) {
foreach ($property_descs as $property_desc) {
$desc .= getDocumentationFromPartIntro($property_desc);
}
}
newPropertyEntry($class, $name, $desc, $type, $modifiers);
}
}
$addedSomething = true;
}
}
if (!isset($xml->refsect1)) return $addedSomething;
$desc = getDocumentation($xml);
// file could contain function + property
if (isset($xml->refsect1->classsynopsis) && isset($xml->refsect1->classsynopsis->fieldsynopsis)) {
$class = (string)$xml->refsect1->classsynopsis->ooclass->classname;
foreach ( $xml->refsect1->classsynopsis->fieldsynopsis as $synopsis ) {
newPropertyEntry($class, $synopsis->varname, $desc, $synopsis->type );
$addedSomething = true;
}
}
if (isset($xml->refsect1->fieldsynopsis)) {
$synopsis = $xml->refsect1->fieldsynopsis;
$class = substr($synopsis->varname, 0, strpos($synopsis->varname, '->'));
$name = substr($synopsis->varname, strpos($synopsis->varname, '->') + 2);
newPropertyEntry($class, $name, $desc, $synopsis->type);
$addedSomething = true;
}
if (isset($xml->refsect1->methodsynopsis)) {
$skip = false;
if ($funcOverload != "") {
foreach( $xml->refsect1->methodsynopsis as $synopsis ) {
if (isset($synopsis->methodname->replaceable)) {
if ($funcOverload == (string) $synopsis->methodname->replaceable) {
$skip = true;
}
} else {
if ($funcOverload == (string) $synopsis->methodname) {
$skip = true;
}
}
}
}
if (!$skip) {
foreach( $xml->refsect1->methodsynopsis as $synopsis ) {
if (isset($synopsis->methodname->replaceable)) {
newMethodEntry('global', $synopsis->methodname->replaceable, $funcOverload, $synopsis, $desc, $xml, $classOverload);
} else {
newMethodEntry('global', $synopsis->methodname, $funcOverload, $synopsis, $desc, $xml, $classOverload);
}
$addedSomething = true;
}
}
}
if (isset($xml->refsect1->classsynopsis) && isset($xml->refsect1->classsynopsis->methodsynopsis)) {
$methodsynopsis = $xml->refsect1->classsynopsis->methodsynopsis;
$classOverloadName = $classOverload ?? $xml->refsect1->classsynopsis->ooclass->classname;
if (isset($synopsis->methodname->replaceable)) {
newMethodEntry($classOverloadName, $methodsynopsis->methodname->replaceable, $funcOverload, $methodsynopsis, $desc, $xml, $classOverload);
} else {
newMethodEntry($classOverloadName, $methodsynopsis->methodname, $funcOverload, $methodsynopsis, $desc, $xml, $classOverload);
}
$addedSomething = true;
}
if ( !$addedSomething && (isset($xml->refnamediv->refpurpose->function) || isset($xml->refnamediv->refpurpose->methodname)) ) {
// This is function alias
$functionNames[] = (string)$xml->refnamediv->refname;
if ($xml->refnamediv->refpurpose->function) {
$aliasName = (string)$xml->refnamediv->refpurpose->function;
switch ($aliasName) {
case 'mysqli_stmt_execute':
$baseFileName = dirname($file->getPathname()).'/../mysqli_stmt/execute.xml';
$overload = "global";
break;
case 'mysqli_options':
$functionNames[] = 'mysqli_set_opt';
$baseFileName = dirname($file->getPathname()).'/../mysqli/options.xml';
$overload = "global";
break;
case 'mysqli_real_escape_string':
$baseFileName = dirname($file->getPathname()).'/../mysqli/real-escape-string.xml';
$overload = "global";
break;
case 'sql_regcase':
$baseFileName = dirname($file->getPathname()).'/../../regex/functions/sql-regcase.xml';
$overload = "global";
break;
default:
$baseFileName = dirname($file->getPathname()).'/'.str_replace('_', '-', $aliasName).'.xml';
$overload = "";
}
} else {
$aliasName = (string)$xml->refnamediv->refpurpose->methodname;
$baseFileName = dirname($file->getPathname()).'/'.strtolower(str_replace('::', '/', $aliasName)).'.xml';
$baseFileName = str_replace('/functions/', '/', $baseFileName);
$overload = "global";
}
if ( $baseFileName == $file->getPathname() || !file_exists($baseFileName) ) {
return false;
}
foreach ($functionNames as $functionName) {
parseFile(new SplFileInfo($baseFileName), $functionName, $overload);
}
$addedSomething = true;
}
return $addedSomething;
} // end of function parseFile()
/**
* Create a new class entry if it not exists.
*
* Key in $classes will be the lower-case @p $name.
* The prettyName member will be @p $name, if it contains non-lowercase chars.
*
* Returns the lower-cased @p $name
*/
function newClassEntry($name) {
global $classes, $isInterface, $isAbstractClass, $isFinalClass, $interfaceClasses, $abstractClasses;
if (strpos($name, '\\') !== false) {
$endpos = strrpos($name, '\\');
$class = substr($name, $endpos + 1);
$namespace = substr($name, 0, $endpos);
} else {
$class = $name;
$namespace = null;
}
// This affects OCI-Collection and OCI-Log
// Technically, this creates wrong class names, but they are otherwise illegal syntax...
$class = str_replace('-','',$class);
$lower = strtolower($name);
if (!$isInterface && in_array($lower, $interfaceClasses)) {
$isInterface = true;
}
if (!$isAbstractClass && in_array($lower, $abstractClasses)) {
$isAbstractClass = true;
}
if (!isset($classes[$lower])) {
$classes[$lower] = array(
'functions' => array(),
'properties' => array(),
'namespace' => $namespace,
'prettyName' => $class,
'desc' => '',
'isInterface' => $isInterface,
'isAbstract' => $isAbstractClass,
'isFinal' => $isFinalClass,
);
} else {
if ( $lower != $class ) {
$classes[$lower]['prettyName'] = $class;
}
if ( $isInterface ) {
$classes[$lower]['isInterface'] = true;
}
if ( $isAbstractClass ) {
$classes[$lower]['isAbstract'] = true;
}
if ( $isFinalClass ) {
$classes[$lower]['isFinal'] = true;
}
}
return $lower;
}
/**
* get the documentation for an entry
* @return string
*/
function getDocumentation(SimpleXMLElement $xml) {
global $skipComments;
$descs = array();
$purpose = $xml->refnamediv->refpurpose;
if (!in_array($purpose, $skipComments)) {
$descs[] = $purpose;
}
foreach ($xml->refsect1->para as $p ) {
$p = removeTag($p->asXML(), 'para');
if ( stripos($p, 'procedural style') !== false || stripos($p, 'procedure style') !== false
|| stripos($p, 'object oriented style') !== false ) {
// uninteresting
continue;
}
if (in_array($p, $skipComments)) {
continue;
}
if ($p == $purpose || $p == "$purpose.") {
// avoid duplicate comments
continue;
}
$descs[] = $p;
}
return implode("\n\n", $descs);
}
/**
* get the documentation for an entry
* @return string
*/
function getDocumentationFromPartIntro(SimpleXMLElement $xml) {
global $skipComments;
$descs = array();
foreach ($xml->listitem->para as $p ) {
$p = removeTag($p->asXML(), 'para');
if (in_array($p, $skipComments)) {
continue;
}
$descs[] = $p;
}
return implode("\n\n", $descs);
}
/**
* create a new property entry for @p $class
*/
function newPropertyEntry($class, $name, $desc, $type, $modifiers = []) {
global $classes;
$class = newClassEntry($class);
$classes[$class]['properties'][] = array(
'name' => (string) $name,
'desc' => (string) $desc,
'type' => (string) $type,
'modifiers' => $modifiers
);
}
/**
* create a new method entry for @p $class
*/
function newMethodEntry($class, $function, $funcOverload, $methodsynopsis, $desc, SimpleXMLElement $xml, $classOverload = "") {
global $existingFunctions, $classes;
$class = (string) $class;
$function = (string) $function;
$funcOverload = (string) $funcOverload;
if (strpos($function, '::')) {
if ($classOverload == '') {
$class = substr($function, 0, strpos($function, '::'));
}
$function = substr($function, strpos($function, '::')+2);
if (strpos($funcOverload, '::')) {
$class = substr($funcOverload, 0, strpos($funcOverload, '::'));
$funcOverload = substr($funcOverload, strpos($funcOverload, '::')+2);
}
} else if (strpos($funcOverload, '::')) {
$class = substr($funcOverload, 0, strpos($funcOverload, '::'));
$funcOverload = substr($funcOverload, strpos($funcOverload, '::')+2);
} else if (strpos($function, '->')) {
if ($classOverload == '') {
$class = substr($function, 0, strpos($function, '->'));
}
$function = substr($function, strpos($function, '->')+2);
} else {
if ($function == '__halt_compiler') return false;
if ($function == 'exit') return false;
if ($function == 'die') return false;
if ($function == 'eval') return false;
if ($function == 'echo') return false;
if ($function == 'print') return false;
if ($function == 'array') return false;
if ($function == 'list') return false;
if ($function == 'isset') return false;
if ($function == 'unset') return false;
if ($function == 'empty') return false;
}
if (strpos($function, '-')) return false;
if (strpos($class, '-')) return false;
if ($function == 'isSet') return false; //todo: bug in lexer
if ($function == 'clone') return false; //todo: bug in lexer
if (substr($class, 0, 3) == 'DOM') $class = 'Dom'.substr($class, 3);
$class = trim($class);
if ($class == 'imagick') $class = 'Imagick';
if (in_array($class.'::'.($funcOverload ? $funcOverload : $function), $existingFunctions)) return false;
$existingFunctions[] = $class.'::'.($funcOverload ? $funcOverload : $function);
$params = array();
foreach ($methodsynopsis->methodparam as $param) {
$paramName = $param->parameter;
if (trim($paramName) == '...') {
// Add a variable name for functions taking variable arguments
$paramName = '...$vararg';
}
if (!trim($paramName)) continue;
$paramName = str_replace('/', '', $paramName);
$paramName = str_replace('-', '', $paramName);
$paramName = str_replace('$', '', $paramName);
$paramName = trim(trim(trim($paramName), '*'), '&');
if ($pipe_pos = strpos($paramName, '|')) $paramName = substr($paramName, 0, $pipe_pos);
if (is_numeric(substr($paramName, 0, 1))) $paramName = '_'.$paramName;
$params[] = array(
'name' => $paramName,
'type' => (string)$param->type,
'isRef' => isset($param->parameter->attributes()->role) ? ($param->parameter->attributes()->role == "reference") : false,
);
}
// get description of params
if ( $param_descs = $xml->xpath('db:refsect1[@role="parameters"]//db:varlistentry') ) {
$i = 0;
foreach ( $param_descs as $d ) {
if ( !isset($params[$i]) ) {
continue;
}
$paramName = (string) $d->term->parameter;
$params[$i]['desc'] = '';
foreach ( $d->listitem->para as $p ) {
$p = removeTag($p->asXML(), 'para');
$params[$i]['desc'] .= $p . "\n";
}
++$i;
}
}
if ($return_desc = $xml->xpath('db:refsect1[@role="returnvalues"]//db:para')) {
$return_desc = removeTag($return_desc[0]->asXML(), 'para');
} else {
$return_desc = '';
}
$class = newClassEntry($class);
$classes[$class]['functions'][] = array(
'name' => $funcOverload ? $funcOverload : $function,
'modifiers' => (array) $methodsynopsis->modifier,
'params' => $params,
'type' => (string)$methodsynopsis->type,
'desc' => $funcOverload ? str_replace($function, $funcOverload, $desc) : $desc,
'return_desc' => $return_desc,
);
}
/* don't add a closing ?> here, we use this file in a benchmark as well */
|