From 99081812d7b4d8a8a26badef7d2b0655d3dacc53 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 29 Jan 2008 14:47:27 +0000 Subject: [PATCH] API: Adding page parameter to action=parse to facilitate parsing existing pages. Note that this isn't the same as action=raw: action=parse also returns the section tree and other useful stuff. The parameter is called "page" rather than "title" because there is a "title" parameter already and I decided to go the backwards compatible road for a change :D --- RELEASE-NOTES | 1 + includes/api/ApiParse.php | 43 +++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 86ad332fca..308fe087c5 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -474,6 +474,7 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API * Added uiprop=editcount to meta=userinfo * Added list=users which fetches user information * Added list=random which fetches a list of random pages +* Added page parameter to action=parse to facilitate parsing of existing pages === Languages updated in 1.12 === diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index 205fa0b930..fd24ca372d 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -42,16 +42,33 @@ class ApiParse extends ApiBase { $params = $this->extractRequestParams(); $text = $params['text']; $title = $params['title']; + $page = $params['page']; + if(!is_null($page) && (!is_null($text) || $title != "API")) + $this->dieUsage("The page parameter cannot be used together with the text and title parameters", 'params'); $prop = array_flip($params['prop']); + + global $wgParser, $wgUser; + if(!is_null($page)) { + $titleObj = Title::newFromText($page); + if(!$titleObj) + $this->dieUsageMsg(array('missingtitle', $page)); - //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() ); + // Try the parser cache first + $articleObj = new Article($titleObj); + $pcache =& ParserCache::singleton(); + $p_result = $pcache->get($articleObj, $wgUser); + if(!$p_result) { + $p_result = $wgParser->parse($articleObj->getContent(), $titleObj, new ParserOptions()); + global $wgUseParserCache; + if($wgUseParserCache) + $pcache->save($p_result, $articleObj, $wgUser); + } + } else { + $titleObj = Title::newFromText($title); + if(!$titleObj) + $titleObj = Title::newFromText("API"); + $p_result = $wgParser->parse($text, $titleObj, new ParserOptions()); + } // Return result $result = $this->getResult(); @@ -74,7 +91,7 @@ class ApiParse extends ApiBase { $result_array['externallinks'] = array_keys($p_result->getExternalLinks()); if(isset($prop['sections'])) $result_array['sections'] = $p_result->getSections(); - + $result_mapping = array( 'langlinks' => 'll', 'categories' => 'cl', @@ -139,6 +156,7 @@ class ApiParse extends ApiBase { ApiBase :: PARAM_DFLT => 'API', ), 'text' => null, + 'page' => null, 'prop' => array( ApiBase :: PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections', ApiBase :: PARAM_ISMULTI => true, @@ -159,8 +177,11 @@ class ApiParse extends ApiBase { public function getParamDescription() { return array ( 'text' => 'Wikitext to parse', - 'title' => 'Title of page', - 'prop' => 'Which pieces of information to get' + 'title' => 'Title of page the text belongs to', + 'page' => 'Parse the content of this page. Cannot be used together with text and title', + 'prop' => array('Which pieces of information to get.', + 'NOTE: Section tree is only generated if there are more than 4 sections, or if the __TOC__ keyword is present' + ), ); } -- 2.20.1