ApiExpandTemplates.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * Created on Oct 05, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ("ApiBase.php");
  27. }
  28. /**
  29. * API module that functions as a shortcut to the wikitext preprocessor. Expands
  30. * any templates in a provided string, and returns the result of this expansion
  31. * to the caller.
  32. *
  33. * @ingroup API
  34. */
  35. class ApiExpandTemplates extends ApiBase {
  36. public function __construct($main, $action) {
  37. parent :: __construct($main, $action);
  38. }
  39. public function execute() {
  40. // Get parameters
  41. $params = $this->extractRequestParams();
  42. //Create title for parser
  43. $title_obj = Title :: newFromText( $params['title'] );
  44. if(!$title_obj)
  45. $title_obj = Title :: newFromText( "API" ); // default
  46. $result = $this->getResult();
  47. // Parse text
  48. global $wgParser;
  49. $options = new ParserOptions();
  50. if ( $params['generatexml'] )
  51. {
  52. $wgParser->startExternalParse( $title_obj, $options, OT_PREPROCESS );
  53. $dom = $wgParser->preprocessToDom( $params['text'] );
  54. if ( is_callable( array( $dom, 'saveXML' ) ) ) {
  55. $xml = $dom->saveXML();
  56. } else {
  57. $xml = $dom->__toString();
  58. }
  59. $xml_result = array();
  60. $result->setContent( $xml_result, $xml );
  61. $result->addValue( null, 'parsetree', $xml_result);
  62. }
  63. $retval = $wgParser->preprocess( $params['text'], $title_obj, $options );
  64. // Return result
  65. $retval_array = array();
  66. $result->setContent( $retval_array, $retval );
  67. $result->addValue( null, $this->getModuleName(), $retval_array );
  68. }
  69. public function getAllowedParams() {
  70. return array (
  71. 'title' => array(
  72. ApiBase :: PARAM_DFLT => 'API',
  73. ),
  74. 'text' => null,
  75. 'generatexml' => false,
  76. );
  77. }
  78. public function getParamDescription() {
  79. return array (
  80. 'text' => 'Wikitext to convert',
  81. 'title' => 'Title of page',
  82. 'generatexml' => 'Generate XML parse tree',
  83. );
  84. }
  85. public function getDescription() {
  86. return 'This module expand all templates in wikitext';
  87. }
  88. protected function getExamples() {
  89. return array (
  90. 'api.php?action=expandtemplates&text={{Project:Sandbox}}'
  91. );
  92. }
  93. public function getVersion() {
  94. return __CLASS__ . ': $Id: ApiExpandTemplates.php 44719 2008-12-17 16:34:01Z catrope $';
  95. }
  96. }