summaryrefslogtreecommitdiff
path: root/includes/api/ApiParse.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/api/ApiParse.php')
-rw-r--r--includes/api/ApiParse.php278
1 files changed, 176 insertions, 102 deletions
diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php
index a369994b..06fdf85b 100644
--- a/includes/api/ApiParse.php
+++ b/includes/api/ApiParse.php
@@ -27,7 +27,7 @@
*/
class ApiParse extends ApiBase {
- /** @var String $section */
+ /** @var string $section */
private $section = null;
/** @var Content $content */
@@ -60,7 +60,10 @@ class ApiParse extends ApiBase {
$format = $params['contentformat'];
if ( !is_null( $page ) && ( !is_null( $text ) || $titleProvided ) ) {
- $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
+ $this->dieUsage(
+ 'The page parameter cannot be used together with the text and title parameters',
+ 'params'
+ );
}
$prop = array_flip( $params['prop'] );
@@ -76,9 +79,12 @@ class ApiParse extends ApiBase {
// TODO: Does this still need $wgTitle?
global $wgParser, $wgTitle;
- // Currently unnecessary, code to act as a safeguard against any change in current behavior of uselang
+ // Currently unnecessary, code to act as a safeguard against any change
+ // in current behavior of uselang
$oldLang = null;
- if ( isset( $params['uselang'] ) && $params['uselang'] != $this->getContext()->getLanguage()->getCode() ) {
+ if ( isset( $params['uselang'] )
+ && $params['uselang'] != $this->getContext()->getLanguage()->getCode()
+ ) {
$oldLang = $this->getContext()->getLanguage(); // Backup language
$this->getContext()->setLanguage( Language::factory( $params['uselang'] ) );
}
@@ -125,7 +131,7 @@ class ApiParse extends ApiBase {
'action' => 'query',
'redirects' => '',
);
- if ( !is_null ( $pageid ) ) {
+ if ( !is_null( $pageid ) ) {
$reqParams['pageids'] = $pageid;
} else { // $page
$reqParams['titles'] = $page;
@@ -170,15 +176,19 @@ class ApiParse extends ApiBase {
if ( !$titleObj || $titleObj->isExternal() ) {
$this->dieUsageMsg( array( 'invalidtitle', $title ) );
}
- if ( !$titleObj->canExist() ) {
- $this->dieUsage( "Namespace doesn't allow actual pages", 'pagecannotexist' );
- }
$wgTitle = $titleObj;
- $pageObj = WikiPage::factory( $titleObj );
+ if ( $titleObj->canExist() ) {
+ $pageObj = WikiPage::factory( $titleObj );
+ } else {
+ // Do like MediaWiki::initializeArticle()
+ $article = Article::newFromTitle( $titleObj, $this->getContext() );
+ $pageObj = $article->getPage();
+ }
$popts = $this->makeParserOptions( $pageObj, $params );
+ $textProvided = !is_null( $text );
- if ( is_null( $text ) ) {
+ if ( !$textProvided ) {
if ( $titleProvided && ( $prop || $params['generatexml'] ) ) {
$this->setWarning(
"'title' used without 'text', and parsed page properties were requested " .
@@ -191,7 +201,7 @@ class ApiParse extends ApiBase {
// If we are parsing text, do not use the content model of the default
// API title, but default to wikitext to keep BC.
- if ( !$titleProvided && is_null( $model ) ) {
+ if ( $textProvided && !$titleProvided && is_null( $model ) ) {
$model = CONTENT_MODEL_WIKITEXT;
$this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
}
@@ -203,7 +213,7 @@ class ApiParse extends ApiBase {
}
if ( $this->section !== false ) {
- $this->content = $this->getSectionContent( $this->content, $titleObj->getText() );
+ $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
}
if ( $params['pst'] || $params['onlypst'] ) {
@@ -219,6 +229,7 @@ class ApiParse extends ApiBase {
ApiResult::setContent( $result_array['wikitext'], $this->content->serialize( $format ) );
}
$result->addValue( null, $this->getModuleName(), $result_array );
+
return;
}
@@ -242,6 +253,10 @@ class ApiParse extends ApiBase {
$result_array['redirects'] = $redirValues;
}
+ if ( $params['disabletoc'] ) {
+ $p_result->setTOCEnabled( false );
+ }
+
if ( isset( $prop['text'] ) ) {
$result_array['text'] = array();
ApiResult::setContent( $result_array['text'], $p_result->getText() );
@@ -249,10 +264,13 @@ class ApiParse extends ApiBase {
if ( !is_null( $params['summary'] ) ) {
$result_array['parsedsummary'] = array();
- ApiResult::setContent( $result_array['parsedsummary'], Linker::formatComment( $params['summary'], $titleObj ) );
+ ApiResult::setContent(
+ $result_array['parsedsummary'],
+ Linker::formatComment( $params['summary'], $titleObj )
+ );
}
- if ( isset( $prop['langlinks'] ) || isset( $prop['languageshtml'] ) ) {
+ if ( isset( $prop['langlinks'] ) ) {
$langlinks = $p_result->getLanguageLinks();
if ( $params['effectivelanglinks'] ) {
@@ -268,12 +286,6 @@ class ApiParse extends ApiBase {
if ( isset( $prop['langlinks'] ) ) {
$result_array['langlinks'] = $this->formatLangLinks( $langlinks );
}
- if ( isset( $prop['languageshtml'] ) ) {
- $languagesHtml = $this->languagesHtml( $langlinks );
-
- $result_array['languageshtml'] = array();
- ApiResult::setContent( $result_array['languageshtml'], $languagesHtml );
- }
if ( isset( $prop['categories'] ) ) {
$result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
}
@@ -300,14 +312,14 @@ class ApiParse extends ApiBase {
if ( isset( $prop['displaytitle'] ) ) {
$result_array['displaytitle'] = $p_result->getDisplayTitle() ?
- $p_result->getDisplayTitle() :
- $titleObj->getPrefixedText();
+ $p_result->getDisplayTitle() :
+ $titleObj->getPrefixedText();
}
if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
$context = $this->getContext();
$context->setTitle( $titleObj );
- $context->getOutput()->addParserOutputNoText( $p_result );
+ $context->getOutput()->addParserOutputMetadata( $p_result );
if ( isset( $prop['headitems'] ) ) {
$headItems = $this->formatHeadItems( $p_result->getHeadItems() );
@@ -321,10 +333,20 @@ class ApiParse extends ApiBase {
if ( isset( $prop['headhtml'] ) ) {
$result_array['headhtml'] = array();
- ApiResult::setContent( $result_array['headhtml'], $context->getOutput()->headElement( $context->getSkin() ) );
+ ApiResult::setContent(
+ $result_array['headhtml'],
+ $context->getOutput()->headElement( $context->getSkin() )
+ );
}
}
+ if ( isset( $prop['modules'] ) ) {
+ $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
+ $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
+ $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
+ $result_array['modulemessages'] = array_values( array_unique( $p_result->getModuleMessages() ) );
+ }
+
if ( isset( $prop['iwlinks'] ) ) {
$result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
}
@@ -341,6 +363,16 @@ class ApiParse extends ApiBase {
$result_array['properties'] = $this->formatProperties( $p_result->getProperties() );
}
+ if ( isset( $prop['limitreportdata'] ) ) {
+ $result_array['limitreportdata'] =
+ $this->formatLimitReportData( $p_result->getLimitReportData() );
+ }
+ if ( isset( $prop['limitreporthtml'] ) ) {
+ $limitreportHtml = EditPage::getPreviewLimitReport( $p_result );
+ $result_array['limitreporthtml'] = array();
+ ApiResult::setContent( $result_array['limitreporthtml'], $limitreportHtml );
+ }
+
if ( $params['generatexml'] ) {
if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
$this->dieUsage( "generatexml is only supported for wikitext content", "notwikitext" );
@@ -368,7 +400,12 @@ class ApiParse extends ApiBase {
'iwlinks' => 'iw',
'sections' => 's',
'headitems' => 'hi',
+ 'modules' => 'm',
+ 'modulescripts' => 'm',
+ 'modulestyles' => 'm',
+ 'modulemessages' => 'm',
'properties' => 'pp',
+ 'limitreportdata' => 'lr',
);
$this->setIndexedTagNames( $result_array, $result_mapping );
$result->addValue( null, $this->getModuleName(), $result_array );
@@ -393,16 +430,18 @@ class ApiParse extends ApiBase {
$popts->enableLimitReport( !$params['disablepp'] );
$popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
$popts->setIsSectionPreview( $params['sectionpreview'] );
+ $popts->setEditSection( !$params['disableeditsection'] );
wfProfileOut( __METHOD__ );
+
return $popts;
}
/**
- * @param $page WikiPage
- * @param $popts ParserOptions
- * @param $pageId Int
- * @param $getWikitext Bool
+ * @param WikiPage $page
+ * @param ParserOptions $popts
+ * @param int $pageId
+ * @param bool $getWikitext
* @return ParserOutput
*/
private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
@@ -411,24 +450,31 @@ class ApiParse extends ApiBase {
if ( $this->section !== false && $this->content !== null ) {
$this->content = $this->getSectionContent(
$this->content,
- !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getText() );
+ !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getPrefixedText()
+ );
// Not cached (save or load)
return $this->content->getParserOutput( $page->getTitle(), null, $popts );
- } else {
- // Try the parser cache first
- // getParserOutput will save to Parser cache if able
- $pout = $page->getParserOutput( $popts );
- if ( !$pout ) {
- $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
- }
- if ( $getWikitext ) {
- $this->content = $page->getContent( Revision::RAW );
- }
- return $pout;
}
+
+ // Try the parser cache first
+ // getParserOutput will save to Parser cache if able
+ $pout = $page->getParserOutput( $popts );
+ if ( !$pout ) {
+ $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
+ }
+ if ( $getWikitext ) {
+ $this->content = $page->getContent( Revision::RAW );
+ }
+
+ return $pout;
}
+ /**
+ * @param Content $content
+ * @param string $what Identifies the content in error messages, e.g. page title.
+ * @return Content|bool
+ */
private function getSectionContent( Content $content, $what ) {
// Not cached (save or load)
$section = $content->getSection( $this->section );
@@ -439,6 +485,7 @@ class ApiParse extends ApiBase {
$this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
$section = false;
}
+
return $section;
}
@@ -452,64 +499,67 @@ class ApiParse extends ApiBase {
$entry['lang'] = $bits[0];
if ( $title ) {
$entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
+ // localised language name in user language (maybe set by uselang=)
+ $entry['langname'] = Language::fetchLanguageName(
+ $title->getInterwiki(),
+ $this->getLanguage()->getCode()
+ );
+
+ // native language name
+ $entry['autonym'] = Language::fetchLanguageName( $title->getInterwiki() );
}
ApiResult::setContent( $entry, $bits[1] );
$result[] = $entry;
}
+
return $result;
}
private function formatCategoryLinks( $links ) {
$result = array();
+
+ if ( !$links ) {
+ return $result;
+ }
+
+ // Fetch hiddencat property
+ $lb = new LinkBatch;
+ $lb->setArray( array( NS_CATEGORY => $links ) );
+ $db = $this->getDB();
+ $res = $db->select( array( 'page', 'page_props' ),
+ array( 'page_title', 'pp_propname' ),
+ $lb->constructSet( 'page', $db ),
+ __METHOD__,
+ array(),
+ array( 'page_props' => array(
+ 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
+ ) )
+ );
+ $hiddencats = array();
+ foreach ( $res as $row ) {
+ $hiddencats[$row->page_title] = isset( $row->pp_propname );
+ }
+
foreach ( $links as $link => $sortkey ) {
$entry = array();
$entry['sortkey'] = $sortkey;
ApiResult::setContent( $entry, $link );
+ if ( !isset( $hiddencats[$link] ) ) {
+ $entry['missing'] = '';
+ } elseif ( $hiddencats[$link] ) {
+ $entry['hidden'] = '';
+ }
$result[] = $entry;
}
+
return $result;
}
private function categoriesHtml( $categories ) {
$context = $this->getContext();
$context->getOutput()->addCategoryLinks( $categories );
- return $context->getSkin()->getCategories();
- }
- /**
- * @deprecated since 1.18 No modern skin generates language links this way, please use language links
- * data to generate your own HTML.
- * @param $languages array
- * @return string
- */
- private function languagesHtml( $languages ) {
- wfDeprecated( __METHOD__, '1.18' );
-
- global $wgContLang, $wgHideInterlanguageLinks;
-
- if ( $wgHideInterlanguageLinks || count( $languages ) == 0 ) {
- return '';
- }
-
- $s = htmlspecialchars( wfMessage( 'otherlanguages' )->text() . wfMessage( 'colon-separator' )->text() );
-
- $langs = array();
- foreach ( $languages as $l ) {
- $nt = Title::newFromText( $l );
- $text = Language::fetchLanguageName( $nt->getInterwiki() );
-
- $langs[] = Html::element( 'a',
- array( 'href' => $nt->getFullURL(), 'title' => $nt->getText(), 'class' => 'external' ),
- $text == '' ? $l : $text );
- }
-
- $s .= implode( wfMessage( 'pipe-separator' )->escaped(), $langs );
-
- if ( $wgContLang->isRTL() ) {
- $s = Html::rawElement( 'span', array( 'dir' => 'LTR' ), $s );
- }
-
- return $s;
+ return $context->getSkin()->getCategories();
}
private function formatLinks( $links ) {
@@ -525,6 +575,7 @@ class ApiParse extends ApiBase {
$result[] = $entry;
}
}
+
return $result;
}
@@ -544,6 +595,7 @@ class ApiParse extends ApiBase {
$result[] = $entry;
}
}
+
return $result;
}
@@ -555,6 +607,7 @@ class ApiParse extends ApiBase {
ApiResult::setContent( $entry, $content );
$result[] = $entry;
}
+
return $result;
}
@@ -566,6 +619,7 @@ class ApiParse extends ApiBase {
ApiResult::setContent( $entry, $value );
$result[] = $entry;
}
+
return $result;
}
@@ -577,6 +631,26 @@ class ApiParse extends ApiBase {
ApiResult::setContent( $entry, $link );
$result[] = $entry;
}
+
+ return $result;
+ }
+
+ private function formatLimitReportData( $limitReportData ) {
+ $result = array();
+ $apiResult = $this->getResult();
+
+ foreach ( $limitReportData as $name => $value ) {
+ $entry = array();
+ $entry['name'] = $name;
+ if ( !is_array( $value ) ) {
+ $value = array( $value );
+ }
+ $apiResult->setIndexedTagName( $value, 'param' );
+ $apiResult->setIndexedTagName_recursive( $value, 'param' );
+ $entry = array_merge( $entry, $value );
+ $result[] = $entry;
+ }
+
return $result;
}
@@ -602,12 +676,12 @@ class ApiParse extends ApiBase {
ApiBase::PARAM_TYPE => 'integer',
),
'prop' => array(
- ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|images|externallinks|sections|revid|displaytitle|iwlinks|properties',
+ ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
+ 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_TYPE => array(
'text',
'langlinks',
- 'languageshtml',
'categories',
'categorieshtml',
'links',
@@ -619,9 +693,12 @@ class ApiParse extends ApiBase {
'displaytitle',
'headitems',
'headhtml',
+ 'modules',
'iwlinks',
'wikitext',
'properties',
+ 'limitreportdata',
+ 'limitreporthtml',
)
),
'pst' => false,
@@ -630,9 +707,11 @@ class ApiParse extends ApiBase {
'uselang' => null,
'section' => null,
'disablepp' => false,
+ 'disableeditsection' => false,
'generatexml' => false,
'preview' => false,
'sectionpreview' => false,
+ 'disabletoc' => false,
'contentformat' => array(
ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
),
@@ -645,12 +724,13 @@ class ApiParse extends ApiBase {
public function getParamDescription() {
$p = $this->getModulePrefix();
$wikitext = CONTENT_MODEL_WIKITEXT;
+
return array(
'text' => "Text to parse. Use {$p}title or {$p}contentmodel to control the content model",
'summary' => 'Summary to parse',
'redirects' => "If the {$p}page or the {$p}pageid parameter is set to a redirect, resolve it",
'title' => "Title of page the text belongs to. " .
- "If omitted, \"API\" is used as the title with content model $wikitext",
+ "If omitted, {$p}contentmodel must be specified, and \"API\" will be used as the title",
'page' => "Parse the content of this page. Cannot be used together with {$p}text and {$p}title",
'pageid' => "Parse the content of this page. Overrides {$p}page",
'oldid' => "Parse the content of this revision. Overrides {$p}page and {$p}pageid",
@@ -660,7 +740,6 @@ class ApiParse extends ApiBase {
' langlinks - Gives the language links in the parsed wikitext',
' categories - Gives the categories in the parsed wikitext',
' categorieshtml - Gives the HTML version of the categories',
- ' languageshtml - Gives the HTML version of the language links',
' links - Gives the internal links in the parsed wikitext',
' templates - Gives the templates in the parsed wikitext',
' images - Gives the images in the parsed wikitext',
@@ -670,13 +749,18 @@ class ApiParse extends ApiBase {
' displaytitle - Adds the title of the parsed wikitext',
' headitems - Gives items to put in the <head> of the page',
' headhtml - Gives parsed <head> of the page',
+ ' modules - Gives the ResourceLoader modules used on the page',
' iwlinks - Gives interwiki links in the parsed wikitext',
' wikitext - Gives the original wikitext that was parsed',
' properties - Gives various properties defined in the parsed wikitext',
+ ' limitreportdata - Gives the limit report in a structured way.',
+ " Gives no data, when {$p}disablepp is set.",
+ ' limitreporthtml - Gives the HTML version of the limit report.',
+ " Gives no data, when {$p}disablepp is set.",
),
'effectivelanglinks' => array(
'Includes language links supplied by extensions',
- '(for use with prop=langlinks|languageshtml)',
+ '(for use with prop=langlinks)',
),
'pst' => array(
'Do a pre-save transform on the input before parsing it',
@@ -690,16 +774,18 @@ class ApiParse extends ApiBase {
'uselang' => 'Which language to parse the request in',
'section' => 'Only retrieve the content of this section number',
'disablepp' => 'Disable the PP Report from the parser output',
+ 'disableeditsection' => 'Disable edit section links from the parser output',
'generatexml' => "Generate XML parse tree (requires contentmodel=$wikitext)",
'preview' => 'Parse in preview mode',
'sectionpreview' => 'Parse in section preview mode (enables preview mode too)',
+ 'disabletoc' => 'Disable table of contents in output',
'contentformat' => array(
'Content serialization format used for the input text',
"Only valid when used with {$p}text",
),
'contentmodel' => array(
- "Content model of the input text. Default is the model of the " .
- "specified ${p}title, or $wikitext if ${p}title is not specified",
+ "Content model of the input text. If omitted, ${p}title must be specified, " .
+ "and default will be the model of the specified ${p}title",
"Only valid when used with {$p}text",
),
);
@@ -707,9 +793,11 @@ class ApiParse extends ApiBase {
public function getDescription() {
$p = $this->getModulePrefix();
+
return array(
- 'Parses content and returns parser output',
- 'See the various prop-Modules of action=query to get information from the current version of a page',
+ 'Parses content and returns parser output.',
+ 'See the various prop-Modules of action=query to get information from the current' .
+ 'version of a page.',
'There are several ways to specify the text to parse:',
"1) Specify a page or revision, using {$p}page, {$p}pageid, or {$p}oldid.",
"2) Specify content explicitly, using {$p}text, {$p}title, and {$p}contentmodel.",
@@ -717,26 +805,12 @@ class ApiParse extends ApiBase {
);
}
- public function getPossibleErrors() {
- return array_merge( parent::getPossibleErrors(), array(
- array( 'code' => 'params', 'info' => 'The page parameter cannot be used together with the text and title parameters' ),
- array( 'code' => 'missingrev', 'info' => 'There is no revision ID oldid' ),
- array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revisions' ),
- array( 'code' => 'missingtitle', 'info' => 'The page you specified doesn\'t exist' ),
- array( 'code' => 'nosuchsection', 'info' => 'There is no section sectionnumber in page' ),
- array( 'nosuchpageid' ),
- array( 'invalidtitle', 'title' ),
- array( 'code' => 'parseerror', 'info' => 'Failed to parse the given text.' ),
- array( 'code' => 'notwikitext', 'info' => 'The requested operation is only supported on wikitext content.' ),
- array( 'code' => 'pagecannotexist', 'info' => "Namespace doesn't allow actual pages" ),
- ) );
- }
-
public function getExamples() {
return array(
'api.php?action=parse&page=Project:Sandbox' => 'Parse a page',
- 'api.php?action=parse&text={{Project:Sandbox}}' => 'Parse wikitext',
- 'api.php?action=parse&text={{PAGENAME}}&title=Test' => 'Parse wikitext, specifying the page title',
+ 'api.php?action=parse&text={{Project:Sandbox}}&contentmodel=wikitext' => 'Parse wikitext',
+ 'api.php?action=parse&text={{PAGENAME}}&title=Test'
+ => 'Parse wikitext, specifying the page title',
'api.php?action=parse&summary=Some+[[link]]&prop=' => 'Parse a summary',
);
}