mwdocgen.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Generate class and file reference documentation for MediaWiki using doxygen.
  4. *
  5. * If the dot DOT language processor is available, attempt call graph
  6. * generation.
  7. *
  8. * Usage:
  9. * php mwdocgen.php
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. * http://www.gnu.org/copyleft/gpl.html
  25. *
  26. * @file
  27. * @todo document
  28. * @ingroup Maintenance
  29. *
  30. * @author Antoine Musso <hashar at free dot fr>
  31. * @author Brion Vibber
  32. * @author Alexandre Emsenhuber
  33. * @version first release
  34. */
  35. require_once __DIR__ . '/Maintenance.php';
  36. /**
  37. * Maintenance script that builds doxygen documentation.
  38. * @ingroup Maintenance
  39. */
  40. class MWDocGen extends Maintenance {
  41. /**
  42. * Prepare Maintenance class
  43. */
  44. public function __construct() {
  45. parent::__construct();
  46. $this->addDescription( 'Build doxygen documentation' );
  47. $this->addOption( 'doxygen',
  48. 'Path to doxygen',
  49. false, true );
  50. $this->addOption( 'version',
  51. 'Pass a MediaWiki version',
  52. false, true );
  53. $this->addOption( 'generate-man',
  54. 'Whether to generate man files' );
  55. $this->addOption( 'file',
  56. "Only process given file or directory. Multiple values " .
  57. "accepted with comma separation. Path relative to \$IP.",
  58. false, true );
  59. $this->addOption( 'output',
  60. 'Path to write doc to',
  61. false, true );
  62. $this->addOption( 'no-extensions',
  63. 'Ignore extensions' );
  64. }
  65. public function getDbType() {
  66. return Maintenance::DB_NONE;
  67. }
  68. protected function init() {
  69. global $IP;
  70. $this->doxygen = $this->getOption( 'doxygen', 'doxygen' );
  71. $this->mwVersion = $this->getOption( 'version', 'master' );
  72. $this->input = '';
  73. $inputs = explode( ',', $this->getOption( 'file', '' ) );
  74. foreach ( $inputs as $input ) {
  75. # Doxygen inputs are space separted and double quoted
  76. $this->input .= " \"$IP/$input\"";
  77. }
  78. $this->output = $this->getOption( 'output', "$IP/docs" );
  79. $this->inputFilter = wfShellWikiCmd( $IP . '/maintenance/mwdoc-filter.php' );
  80. $this->template = $IP . '/maintenance/Doxyfile';
  81. $this->excludes = [
  82. 'vendor',
  83. 'node_modules',
  84. 'images',
  85. 'static',
  86. ];
  87. $this->excludePatterns = [];
  88. if ( $this->hasOption( 'no-extensions' ) ) {
  89. $this->excludePatterns[] = 'extensions';
  90. }
  91. $this->doDot = `which dot`;
  92. $this->doMan = $this->hasOption( 'generate-man' );
  93. }
  94. public function execute() {
  95. global $IP;
  96. $this->init();
  97. # Build out directories we want to exclude
  98. $exclude = '';
  99. foreach ( $this->excludes as $item ) {
  100. $exclude .= " $IP/$item";
  101. }
  102. $excludePatterns = implode( ' ', $this->excludePatterns );
  103. $conf = strtr( file_get_contents( $this->template ),
  104. [
  105. '{{OUTPUT_DIRECTORY}}' => $this->output,
  106. '{{STRIP_FROM_PATH}}' => $IP,
  107. '{{CURRENT_VERSION}}' => $this->mwVersion,
  108. '{{INPUT}}' => $this->input,
  109. '{{EXCLUDE}}' => $exclude,
  110. '{{EXCLUDE_PATTERNS}}' => $excludePatterns,
  111. '{{HAVE_DOT}}' => $this->doDot ? 'YES' : 'NO',
  112. '{{GENERATE_MAN}}' => $this->doMan ? 'YES' : 'NO',
  113. '{{INPUT_FILTER}}' => $this->inputFilter,
  114. ]
  115. );
  116. $tmpFile = tempnam( wfTempDir(), 'MWDocGen-' );
  117. if ( file_put_contents( $tmpFile, $conf ) === false ) {
  118. $this->error( "Could not write doxygen configuration to file $tmpFile\n",
  119. /** exit code: */ 1 );
  120. }
  121. $command = $this->doxygen . ' ' . $tmpFile;
  122. $this->output( "Executing command:\n$command\n" );
  123. $exitcode = 1;
  124. system( $command, $exitcode );
  125. $this->output( <<<TEXT
  126. ---------------------------------------------------
  127. Doxygen execution finished.
  128. Check above for possible errors.
  129. You might want to delete the temporary file:
  130. $tmpFile
  131. ---------------------------------------------------
  132. TEXT
  133. );
  134. if ( $exitcode !== 0 ) {
  135. $this->error( "Something went wrong (exit: $exitcode)\n",
  136. $exitcode );
  137. }
  138. }
  139. }
  140. $maintClass = 'MWDocGen';
  141. require_once RUN_MAINTENANCE_IF_MAIN;