From a6e563da20d454a2343e53ddff0b3ba5c8f3dfee Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Mon, 8 Oct 2007 14:24:54 +0000 Subject: [PATCH] * (bug 11173) Allow limited wikicode rendering via api.php * (bug 11572) API should provide interface for expanding templates Patches by VasilevVV --- RELEASE-NOTES | 2 + includes/AutoLoader.php | 2 + includes/api/ApiExpandTemplates.php | 93 ++++++++++++++++++++++++++++ includes/api/ApiMain.php | 3 + includes/api/ApiRender.php | 94 +++++++++++++++++++++++++++++ 5 files changed, 194 insertions(+) create mode 100644 includes/api/ApiExpandTemplates.php create mode 100644 includes/api/ApiRender.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 0d335711d5..87b6d83116 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -108,6 +108,8 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API * Fixed rvlimit of the revisions query to only enforce the lower query limit if revision content is requested. * Include svn revision number (if install is checked-out from svn) in siteinfo query. +* (bug 11173) Allow limited wikicode rendering via api.php +* (bug 11572) API should provide interface for expanding templates === Languages updated in 1.12 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 486bdaa6c5..e1534fe19b 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -308,6 +308,7 @@ function __autoload($className) { 'ApiLogin' => 'includes/api/ApiLogin.php', 'ApiMain' => 'includes/api/ApiMain.php', 'ApiOpenSearch' => 'includes/api/ApiOpenSearch.php', + 'ApiExpandTemplates' => 'includes/api/ApiExpandTemplates.php', 'ApiPageSet' => 'includes/api/ApiPageSet.php', 'ApiQuery' => 'includes/api/ApiQuery.php', 'ApiQueryAllpages' => 'includes/api/ApiQueryAllpages.php', @@ -333,6 +334,7 @@ function __autoload($className) { 'ApiQuerySiteinfo' => 'includes/api/ApiQuerySiteinfo.php', 'ApiQueryUserInfo' => 'includes/api/ApiQueryUserInfo.php', 'ApiQueryWatchlist' => 'includes/api/ApiQueryWatchlist.php', + 'ApiRender' => 'includes/api/ApiRender.php', 'ApiResult' => 'includes/api/ApiResult.php', ); diff --git a/includes/api/ApiExpandTemplates.php b/includes/api/ApiExpandTemplates.php new file mode 100644 index 0000000000..5cfb8024de --- /dev/null +++ b/includes/api/ApiExpandTemplates.php @@ -0,0 +1,93 @@ +@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * http://www.gnu.org/copyleft/gpl.html + */ + +if (!defined('MEDIAWIKI')) { + // Eclipse helper - will be ignored in production + require_once ("ApiBase.php"); +} + +/** + * @addtogroup API + */ +class ApiExpandTemplates extends ApiBase { + + public function __construct($main, $action) { + parent :: __construct($main, $action); + } + + public function execute() { + // Get parameters + $params = $this->extractRequestParams(); + $text = $params['text']; + $title = $params['title']; + $retval = ''; + + //Create title for parser + $title_obj = Title :: newFromText($params['title']); + if(!$title_obj) + $title_obj = Title :: newFromText("API"); // Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it + + // Parse text + global $wgParser; + $retval = $wgParser->preprocess( $text, $title_obj, new ParserOptions() ); + + // Return result + $result = $this->getResult(); + $retval_array = array(); + $result->setContent( $retval_array, $retval ); + $result->addValue( null, 'expandtemplates', $retval_array ); + } + + protected function getAllowedParams() { + return array ( + 'title' => array( + ApiBase :: PARAM_DFLT => 'API', + ), + 'text' => null + ); + } + + protected function getParamDescription() { + return array ( + 'text' => 'Wikitext to convert', + 'title' => 'Title of page', + ); + } + + protected function getDescription() { + return 'This module expand all templates in wikitext'; + } + + protected function getExamples() { + return array ( + 'api.php?action=expandtemplates&text={{Project:Sandbox}}' + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +} + diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 49542e6844..52ea73d517 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -54,6 +54,8 @@ class ApiMain extends ApiBase { private static $Modules = array ( 'login' => 'ApiLogin', 'query' => 'ApiQuery', + 'expandtemplates' => 'ApiExpandTemplates', + 'render' => 'ApiRender', 'opensearch' => 'ApiOpenSearch', 'feedwatchlist' => 'ApiFeedWatchlist', 'help' => 'ApiHelp', @@ -539,3 +541,4 @@ class UsageException extends Exception { } } + diff --git a/includes/api/ApiRender.php b/includes/api/ApiRender.php new file mode 100644 index 0000000000..ecb9967e85 --- /dev/null +++ b/includes/api/ApiRender.php @@ -0,0 +1,94 @@ +@gmail.com + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * http://www.gnu.org/copyleft/gpl.html + */ + +if (!defined('MEDIAWIKI')) { + // Eclipse helper - will be ignored in production + require_once ("ApiBase.php"); +} + +/** + * @addtogroup API + */ +class ApiRender extends ApiBase { + + public function __construct($main, $action) { + parent :: __construct($main, $action); + } + + public function execute() { + // Get parameters + $params = $this->extractRequestParams(); + $text = $params['text']; + $title = $params['title']; + $retval = ''; + + //Create title for parser + $title_obj = Title :: newFromText($params['title']); + if(!$title_obj) + $title_obj = Title :: newFromText("API"); // Default title is "API". For example, ExpandTemplates uses "ExpendTemplates" for it + + // Parse text + global $wgParser; + $p_result = $wgParser->parse( $text, $title_obj, new ParserOptions() ); + $retval = $p_result->getText(); + + // Return result + $result = $this->getResult(); + $retval_array = array(); + $result->setContent( $retval_array, $retval ); + $result->addValue( null, 'render', $retval_array ); + } + + protected function getAllowedParams() { + return array ( + 'title' => array( + ApiBase :: PARAM_DFLT => 'API', + ), + 'text' => null + ); + } + + protected function getParamDescription() { + return array ( + 'text' => 'Wikitext to render', + 'title' => 'Title of page', + ); + } + + protected function getDescription() { + return 'This module allows to get rendered wikitext'; + } + + protected function getExamples() { + return array ( + 'api.php?action=render&text={{Project:Sandbox}}' + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +} + -- 2.20.1