3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Storage\RevisionRecord
;
29 class ApiParse
extends ApiBase
{
31 /** @var string $section */
32 private $section = null;
34 /** @var Content $content */
35 private $content = null;
37 /** @var Content $pstContent */
38 private $pstContent = null;
41 private $contentIsDeleted = false, $contentIsSuppressed = false;
43 public function execute() {
44 // The data is hot but user-dependent, like page views, so we set vary cookies
45 $this->getMain()->setCacheMode( 'anon-public-user-private' );
48 $params = $this->extractRequestParams();
50 // No easy way to say that text and title or revid are allowed together
51 // while the rest aren't, so just do it in three calls.
52 $this->requireMaxOneParameter( $params, 'page', 'pageid', 'oldid', 'text' );
53 $this->requireMaxOneParameter( $params, 'page', 'pageid', 'oldid', 'title' );
54 $this->requireMaxOneParameter( $params, 'page', 'pageid', 'oldid', 'revid' );
56 $text = $params['text'];
57 $title = $params['title'];
58 if ( $title === null ) {
59 $titleProvided = false;
60 // A title is needed for parsing, so arbitrarily choose one
63 $titleProvided = true;
66 $page = $params['page'];
67 $pageid = $params['pageid'];
68 $oldid = $params['oldid'];
70 $model = $params['contentmodel'];
71 $format = $params['contentformat'];
73 $prop = array_flip( $params['prop'] );
75 if ( isset( $params['section'] ) ) {
76 $this->section
= $params['section'];
77 if ( !preg_match( '/^((T-)?\d+|new)$/', $this->section
) ) {
78 $this->dieWithError( 'apierror-invalidsection' );
81 $this->section
= false;
84 // The parser needs $wgTitle to be set, apparently the
85 // $title parameter in Parser::parse isn't enough *sigh*
86 // TODO: Does this still need $wgTitle?
91 $needContent = isset( $prop['wikitext'] ) ||
92 isset( $prop['parsetree'] ) ||
$params['generatexml'];
95 $result = $this->getResult();
97 if ( !is_null( $oldid ) ||
!is_null( $pageid ) ||
!is_null( $page ) ) {
98 if ( $this->section
=== 'new' ) {
99 $this->dieWithError( 'apierror-invalidparammix-parse-new-section', 'invalidparammix' );
101 if ( !is_null( $oldid ) ) {
102 // Don't use the parser cache
103 $rev = Revision
::newFromId( $oldid );
105 $this->dieWithError( [ 'apierror-nosuchrevid', $oldid ] );
108 $this->checkTitleUserPermissions( $rev->getTitle(), 'read' );
109 if ( !$rev->userCan( RevisionRecord
::DELETED_TEXT
, $this->getUser() ) ) {
111 [ 'apierror-permissiondenied', $this->msg( 'action-deletedtext' ) ]
115 $titleObj = $rev->getTitle();
116 $wgTitle = $titleObj;
117 $pageObj = WikiPage
::factory( $titleObj );
118 list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
119 $p_result = $this->getParsedContent(
120 $pageObj, $popts, $suppressCache, $pageid, $rev, $needContent
122 } else { // Not $oldid, but $pageid or $page
123 if ( $params['redirects'] ) {
127 if ( !is_null( $pageid ) ) {
128 $reqParams['pageids'] = $pageid;
130 $reqParams['titles'] = $page;
132 $req = new FauxRequest( $reqParams );
133 $main = new ApiMain( $req );
134 $pageSet = new ApiPageSet( $main );
136 $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
139 foreach ( $pageSet->getRedirectTitles() as $title ) {
140 $to = $title->getFullText();
142 $pageParams = [ 'title' => $to ];
143 } elseif ( !is_null( $pageid ) ) {
144 $pageParams = [ 'pageid' => $pageid ];
146 $pageParams = [ 'title' => $page ];
149 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
150 $titleObj = $pageObj->getTitle();
151 if ( !$titleObj ||
!$titleObj->exists() ) {
152 $this->dieWithError( 'apierror-missingtitle' );
155 $this->checkTitleUserPermissions( $titleObj, 'read' );
156 $wgTitle = $titleObj;
158 if ( isset( $prop['revid'] ) ) {
159 $oldid = $pageObj->getLatest();
162 list( $popts, $reset, $suppressCache ) = $this->makeParserOptions( $pageObj, $params );
163 $p_result = $this->getParsedContent(
164 $pageObj, $popts, $suppressCache, $pageid, null, $needContent
167 } else { // Not $oldid, $pageid, $page. Hence based on $text
168 $titleObj = Title
::newFromText( $title );
169 if ( !$titleObj ||
$titleObj->isExternal() ) {
170 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
172 $revid = $params['revid'];
173 if ( $revid !== null ) {
174 $rev = Revision
::newFromId( $revid );
176 $this->dieWithError( [ 'apierror-nosuchrevid', $revid ] );
178 $pTitleObj = $titleObj;
179 $titleObj = $rev->getTitle();
180 if ( $titleProvided ) {
181 if ( !$titleObj->equals( $pTitleObj ) ) {
182 $this->addWarning( [ 'apierror-revwrongpage', $rev->getId(),
183 wfEscapeWikiText( $pTitleObj->getPrefixedText() ) ] );
186 // Consider the title derived from the revid as having
188 $titleProvided = true;
191 $wgTitle = $titleObj;
192 if ( $titleObj->canExist() ) {
193 $pageObj = WikiPage
::factory( $titleObj );
195 // Do like MediaWiki::initializeArticle()
196 $article = Article
::newFromTitle( $titleObj, $this->getContext() );
197 $pageObj = $article->getPage();
200 list( $popts, $reset ) = $this->makeParserOptions( $pageObj, $params );
201 $textProvided = !is_null( $text );
203 if ( !$textProvided ) {
204 if ( $titleProvided && ( $prop ||
$params['generatexml'] ) ) {
205 if ( $revid !== null ) {
206 $this->addWarning( 'apiwarn-parse-revidwithouttext' );
208 $this->addWarning( 'apiwarn-parse-titlewithouttext' );
211 // Prevent warning from ContentHandler::makeContent()
215 // If we are parsing text, do not use the content model of the default
216 // API title, but default to wikitext to keep BC.
217 if ( $textProvided && !$titleProvided && is_null( $model ) ) {
218 $model = CONTENT_MODEL_WIKITEXT
;
219 $this->addWarning( [ 'apiwarn-parse-nocontentmodel', $model ] );
223 $this->content
= ContentHandler
::makeContent( $text, $titleObj, $model, $format );
224 } catch ( MWContentSerializationException
$ex ) {
225 $this->dieWithException( $ex, [
226 'wrap' => ApiMessage
::create( 'apierror-contentserializationexception', 'parseerror' )
230 if ( $this->section
!== false ) {
231 if ( $this->section
=== 'new' ) {
232 // Insert the section title above the content.
233 if ( !is_null( $params['sectiontitle'] ) && $params['sectiontitle'] !== '' ) {
234 $this->content
= $this->content
->addSectionHeader( $params['sectiontitle'] );
237 $this->content
= $this->getSectionContent( $this->content
, $titleObj->getPrefixedText() );
241 if ( $params['pst'] ||
$params['onlypst'] ) {
242 $this->pstContent
= $this->content
->preSaveTransform( $titleObj, $this->getUser(), $popts );
244 if ( $params['onlypst'] ) {
245 // Build a result and bail out
247 $result_array['text'] = $this->pstContent
->serialize( $format );
248 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'text';
249 if ( isset( $prop['wikitext'] ) ) {
250 $result_array['wikitext'] = $this->content
->serialize( $format );
251 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'wikitext';
253 if ( !is_null( $params['summary'] ) ||
254 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
256 $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
257 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'parsedsummary';
260 $result->addValue( null, $this->getModuleName(), $result_array );
265 // Not cached (save or load)
266 if ( $params['pst'] ) {
267 $p_result = $this->pstContent
->getParserOutput( $titleObj, $revid, $popts );
269 $p_result = $this->content
->getParserOutput( $titleObj, $revid, $popts );
275 $result_array['title'] = $titleObj->getPrefixedText();
276 $result_array['pageid'] = $pageid ?
: $pageObj->getId();
277 if ( $this->contentIsDeleted
) {
278 $result_array['textdeleted'] = true;
280 if ( $this->contentIsSuppressed
) {
281 $result_array['textsuppressed'] = true;
284 if ( isset( $params['useskin'] ) ) {
285 $factory = MediaWikiServices
::getInstance()->getSkinFactory();
286 $skin = $factory->makeSkin( Skin
::normalizeKey( $params['useskin'] ) );
292 if ( $skin ||
isset( $prop['headhtml'] ) ||
isset( $prop['categorieshtml'] ) ) {
293 // Enabling the skin via 'useskin', 'headhtml', or 'categorieshtml'
294 // gets OutputPage and Skin involved, which (among others) applies
296 // - ParserOutputHooks
297 // - Hook: LanguageLinks
298 // - Hook: OutputPageParserOutput
299 // - Hook: OutputPageMakeCategoryLinks
300 $context = new DerivativeContext( $this->getContext() );
301 $context->setTitle( $titleObj );
302 $context->setWikiPage( $pageObj );
305 // Use the skin specified by 'useskin'
306 $context->setSkin( $skin );
307 // Context clones the skin, refetch to stay in sync. (T166022)
308 $skin = $context->getSkin();
310 // Make sure the context's skin refers to the context. Without this,
311 // $outputPage->getSkin()->getOutput() !== $outputPage which
312 // confuses some of the output.
313 $context->setSkin( $context->getSkin() );
316 $outputPage = new OutputPage( $context );
317 $outputPage->addParserOutputMetadata( $p_result );
318 if ( $this->content
) {
319 $outputPage->addContentOverride( $titleObj, $this->content
);
321 $context->setOutput( $outputPage );
324 // Based on OutputPage::headElement()
325 $skin->setupSkinUserCss( $outputPage );
326 // Based on OutputPage::output()
327 $outputPage->loadSkinModules( $skin );
330 Hooks
::run( 'ApiParseMakeOutputPage', [ $this, $outputPage ] );
333 if ( !is_null( $oldid ) ) {
334 $result_array['revid'] = (int)$oldid;
337 if ( $params['redirects'] && !is_null( $redirValues ) ) {
338 $result_array['redirects'] = $redirValues;
341 if ( isset( $prop['text'] ) ) {
342 $result_array['text'] = $p_result->getText( [
343 'allowTOC' => !$params['disabletoc'],
344 'enableSectionEditLinks' => !$params['disableeditsection'],
345 'wrapperDivClass' => $params['wrapoutputclass'],
346 'deduplicateStyles' => !$params['disablestylededuplication'],
348 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'text';
351 if ( !is_null( $params['summary'] ) ||
352 ( !is_null( $params['sectiontitle'] ) && $this->section
=== 'new' )
354 $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
355 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'parsedsummary';
358 if ( isset( $prop['langlinks'] ) ) {
360 $langlinks = $outputPage->getLanguageLinks();
362 $langlinks = $p_result->getLanguageLinks();
363 // The deprecated 'effectivelanglinks' option depredates OutputPage
364 // support via 'useskin'. If not already applied, then run just this
365 // one hook of OutputPage::addParserOutputMetadata here.
366 if ( $params['effectivelanglinks'] ) {
368 Hooks
::run( 'LanguageLinks', [ $titleObj, &$langlinks, &$linkFlags ] );
372 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
374 if ( isset( $prop['categories'] ) ) {
375 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
377 if ( isset( $prop['categorieshtml'] ) ) {
378 $result_array['categorieshtml'] = $outputPage->getSkin()->getCategories();
379 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'categorieshtml';
381 if ( isset( $prop['links'] ) ) {
382 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
384 if ( isset( $prop['templates'] ) ) {
385 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
387 if ( isset( $prop['images'] ) ) {
388 $result_array['images'] = array_keys( $p_result->getImages() );
390 if ( isset( $prop['externallinks'] ) ) {
391 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
393 if ( isset( $prop['sections'] ) ) {
394 $result_array['sections'] = $p_result->getSections();
396 if ( isset( $prop['parsewarnings'] ) ) {
397 $result_array['parsewarnings'] = $p_result->getWarnings();
400 if ( isset( $prop['displaytitle'] ) ) {
401 $result_array['displaytitle'] = $p_result->getDisplayTitle() !== false
402 ?
$p_result->getDisplayTitle() : $titleObj->getPrefixedText();
405 if ( isset( $prop['headitems'] ) ) {
407 $result_array['headitems'] = $this->formatHeadItems( $outputPage->getHeadItemsArray() );
409 $result_array['headitems'] = $this->formatHeadItems( $p_result->getHeadItems() );
413 if ( isset( $prop['headhtml'] ) ) {
414 $result_array['headhtml'] = $outputPage->headElement( $context->getSkin() );
415 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'headhtml';
418 if ( isset( $prop['modules'] ) ) {
420 $result_array['modules'] = $outputPage->getModules();
421 // Deprecated since 1.32 (T188689)
422 $result_array['modulescripts'] = [];
423 $result_array['modulestyles'] = $outputPage->getModuleStyles();
425 $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
426 // Deprecated since 1.32 (T188689)
427 $result_array['modulescripts'] = [];
428 $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
432 if ( isset( $prop['jsconfigvars'] ) ) {
433 $jsconfigvars = $skin ?
$outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
434 $result_array['jsconfigvars'] = ApiResult
::addMetadataToResultVars( $jsconfigvars );
437 if ( isset( $prop['encodedjsconfigvars'] ) ) {
438 $jsconfigvars = $skin ?
$outputPage->getJsConfigVars() : $p_result->getJsConfigVars();
439 $result_array['encodedjsconfigvars'] = FormatJson
::encode(
444 $result_array[ApiResult
::META_SUBELEMENTS
][] = 'encodedjsconfigvars';
447 if ( isset( $prop['modules'] ) &&
448 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
449 $this->addWarning( 'apiwarn-moduleswithoutvars' );
452 if ( isset( $prop['indicators'] ) ) {
454 $result_array['indicators'] = (array)$outputPage->getIndicators();
456 $result_array['indicators'] = (array)$p_result->getIndicators();
458 ApiResult
::setArrayType( $result_array['indicators'], 'BCkvp', 'name' );
461 if ( isset( $prop['iwlinks'] ) ) {
462 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
465 if ( isset( $prop['wikitext'] ) ) {
466 $result_array['wikitext'] = $this->content
->serialize( $format );
467 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'wikitext';
468 if ( !is_null( $this->pstContent
) ) {
469 $result_array['psttext'] = $this->pstContent
->serialize( $format );
470 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'psttext';
473 if ( isset( $prop['properties'] ) ) {
474 $result_array['properties'] = (array)$p_result->getProperties();
475 ApiResult
::setArrayType( $result_array['properties'], 'BCkvp', 'name' );
478 if ( isset( $prop['limitreportdata'] ) ) {
479 $result_array['limitreportdata'] =
480 $this->formatLimitReportData( $p_result->getLimitReportData() );
482 if ( isset( $prop['limitreporthtml'] ) ) {
483 $result_array['limitreporthtml'] = EditPage
::getPreviewLimitReport( $p_result );
484 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'limitreporthtml';
487 if ( isset( $prop['parsetree'] ) ||
$params['generatexml'] ) {
488 if ( $this->content
->getModel() != CONTENT_MODEL_WIKITEXT
) {
489 $this->dieWithError( 'apierror-parsetree-notwikitext', 'notwikitext' );
492 $parser = MediaWikiServices
::getInstance()->getParser();
493 $parser->startExternalParse( $titleObj, $popts, Parser
::OT_PREPROCESS
);
494 // @phan-suppress-next-line PhanUndeclaredMethod
495 $xml = $parser->preprocessToDom( $this->content
->getText() )->__toString();
496 $result_array['parsetree'] = $xml;
497 $result_array[ApiResult
::META_BC_SUBELEMENTS
][] = 'parsetree';
503 'categories' => 'cl',
507 'externallinks' => 'el',
512 'indicators' => 'ind',
513 'modulescripts' => 'm',
514 'modulestyles' => 'm',
515 'properties' => 'pp',
516 'limitreportdata' => 'lr',
517 'parsewarnings' => 'pw'
519 $this->setIndexedTagNames( $result_array, $result_mapping );
520 $result->addValue( null, $this->getModuleName(), $result_array );
524 * Constructs a ParserOptions object
526 * @param WikiPage $pageObj
527 * @param array $params
529 * @return array [ ParserOptions, ScopedCallback, bool $suppressCache ]
531 protected function makeParserOptions( WikiPage
$pageObj, array $params ) {
532 $popts = $pageObj->makeParserOptions( $this->getContext() );
533 $popts->enableLimitReport( !$params['disablepp'] && !$params['disablelimitreport'] );
534 $popts->setIsPreview( $params['preview'] ||
$params['sectionpreview'] );
535 $popts->setIsSectionPreview( $params['sectionpreview'] );
536 if ( $params['disabletidy'] ) {
537 $popts->setTidy( false );
539 if ( $params['wrapoutputclass'] !== '' ) {
540 $popts->setWrapOutputClass( $params['wrapoutputclass'] );
544 $suppressCache = false;
545 Hooks
::run( 'ApiMakeParserOptions',
546 [ $popts, $pageObj->getTitle(), $params, $this, &$reset, &$suppressCache ] );
548 // Force cache suppression when $popts aren't cacheable.
549 $suppressCache = $suppressCache ||
!$popts->isSafeToCache();
551 return [ $popts, $reset, $suppressCache ];
555 * @param WikiPage $page
556 * @param ParserOptions $popts
557 * @param bool $suppressCache
559 * @param Revision|null $rev
560 * @param bool $getContent
561 * @return ParserOutput
563 private function getParsedContent(
564 WikiPage
$page, $popts, $suppressCache, $pageId, $rev, $getContent
566 $revId = $rev ?
$rev->getId() : null;
567 $isDeleted = $rev && $rev->isDeleted( RevisionRecord
::DELETED_TEXT
);
569 if ( $getContent ||
$this->section
!== false ||
$isDeleted ) {
571 $this->content
= $rev->getContent( RevisionRecord
::FOR_THIS_USER
, $this->getUser() );
572 if ( !$this->content
) {
573 $this->dieWithError( [ 'apierror-missingcontent-revid', $revId ] );
576 $this->content
= $page->getContent( RevisionRecord
::FOR_THIS_USER
, $this->getUser() );
577 if ( !$this->content
) {
578 $this->dieWithError( [ 'apierror-missingcontent-pageid', $page->getId() ] );
581 $this->contentIsDeleted
= $isDeleted;
582 $this->contentIsSuppressed
= $rev &&
583 $rev->isDeleted( RevisionRecord
::DELETED_TEXT | RevisionRecord
::DELETED_RESTRICTED
);
586 if ( $this->section
!== false ) {
587 $this->content
= $this->getSectionContent(
589 $pageId === null ?
$page->getTitle()->getPrefixedText() : $this->msg( 'pageid', $pageId )
591 return $this->content
->getParserOutput( $page->getTitle(), $revId, $popts );
595 // getParserOutput can't do revdeled revisions
596 $pout = $this->content
->getParserOutput( $page->getTitle(), $revId, $popts );
598 // getParserOutput will save to Parser cache if able
599 $pout = $page->getParserOutput( $popts, $revId, $suppressCache );
602 $this->dieWithError( [ 'apierror-nosuchrevid', $revId ?
: $page->getLatest() ] ); // @codeCoverageIgnore
609 * Extract the requested section from the given Content
611 * @param Content $content
612 * @param string|Message $what Identifies the content in error messages, e.g. page title.
615 private function getSectionContent( Content
$content, $what ) {
616 // Not cached (save or load)
617 $section = $content->getSection( $this->section
);
618 if ( $section === false ) {
619 $this->dieWithError( [ 'apierror-nosuchsection-what', $this->section
, $what ], 'nosuchsection' );
621 if ( $section === null ) {
622 $this->dieWithError( [ 'apierror-sectionsnotsupported-what', $what ], 'nosuchsection' );
630 * This mimicks the behavior of EditPage in formatting a summary
632 * @param Title $title of the page being parsed
633 * @param array $params The API parameters of the request
634 * @return Content|bool
636 private function formatSummary( $title, $params ) {
637 $summary = $params['summary'] ??
'';
638 $sectionTitle = $params['sectiontitle'] ??
'';
640 if ( $this->section
=== 'new' && ( $sectionTitle === '' ||
$summary === '' ) ) {
641 if ( $sectionTitle !== '' ) {
642 $summary = $params['sectiontitle'];
644 if ( $summary !== '' ) {
645 $summary = wfMessage( 'newsectionsummary' )
646 ->rawParams( MediaWikiServices
::getInstance()->getParser()
647 ->stripSectionName( $summary ) )
648 ->inContentLanguage()->text();
651 return Linker
::formatComment( $summary, $title, $this->section
=== 'new' );
654 private function formatLangLinks( $links ) {
656 foreach ( $links as $link ) {
658 $bits = explode( ':', $link, 2 );
659 $title = Title
::newFromText( $link );
661 $entry['lang'] = $bits[0];
663 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
664 // localised language name in 'uselang' language
665 $entry['langname'] = Language
::fetchLanguageName(
666 $title->getInterwiki(),
667 $this->getLanguage()->getCode()
670 // native language name
671 $entry['autonym'] = Language
::fetchLanguageName( $title->getInterwiki() );
673 ApiResult
::setContentValue( $entry, 'title', $bits[1] );
680 private function formatCategoryLinks( $links ) {
687 // Fetch hiddencat property
689 $lb->setArray( [ NS_CATEGORY
=> $links ] );
690 $db = $this->getDB();
691 $res = $db->select( [ 'page', 'page_props' ],
692 [ 'page_title', 'pp_propname' ],
693 $lb->constructSet( 'page', $db ),
697 'LEFT JOIN', [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ]
701 foreach ( $res as $row ) {
702 $hiddencats[$row->page_title
] = isset( $row->pp_propname
);
705 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
707 foreach ( $links as $link => $sortkey ) {
709 $entry['sortkey'] = $sortkey;
710 // array keys will cast numeric category names to ints, so cast back to string
711 ApiResult
::setContentValue( $entry, 'category', (string)$link );
712 if ( !isset( $hiddencats[$link] ) ) {
713 $entry['missing'] = true;
715 // We already know the link doesn't exist in the database, so
716 // tell LinkCache that before calling $title->isKnown().
717 $title = Title
::makeTitle( NS_CATEGORY
, $link );
718 $linkCache->addBadLinkObj( $title );
719 if ( $title->isKnown() ) {
720 $entry['known'] = true;
722 } elseif ( $hiddencats[$link] ) {
723 $entry['hidden'] = true;
731 private function formatLinks( $links ) {
733 foreach ( $links as $ns => $nslinks ) {
734 foreach ( $nslinks as $title => $id ) {
737 ApiResult
::setContentValue( $entry, 'title', Title
::makeTitle( $ns, $title )->getFullText() );
738 $entry['exists'] = $id != 0;
746 private function formatIWLinks( $iw ) {
748 foreach ( $iw as $prefix => $titles ) {
749 foreach ( array_keys( $titles ) as $title ) {
751 $entry['prefix'] = $prefix;
753 $title = Title
::newFromText( "{$prefix}:{$title}" );
755 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
758 ApiResult
::setContentValue( $entry, 'title', $title->getFullText() );
766 private function formatHeadItems( $headItems ) {
768 foreach ( $headItems as $tag => $content ) {
770 $entry['tag'] = $tag;
771 ApiResult
::setContentValue( $entry, 'content', $content );
778 private function formatLimitReportData( $limitReportData ) {
781 foreach ( $limitReportData as $name => $value ) {
783 $entry['name'] = $name;
784 if ( !is_array( $value ) ) {
787 ApiResult
::setIndexedTagNameRecursive( $value, 'param' );
788 $entry = array_merge( $entry, $value );
795 private function setIndexedTagNames( &$array, $mapping ) {
796 foreach ( $mapping as $key => $name ) {
797 if ( isset( $array[$key] ) ) {
798 ApiResult
::setIndexedTagName( $array[$key], $name );
803 public function getAllowedParams() {
807 ApiBase
::PARAM_TYPE
=> 'text',
810 ApiBase
::PARAM_TYPE
=> 'integer',
815 ApiBase
::PARAM_TYPE
=> 'integer',
817 'redirects' => false,
819 ApiBase
::PARAM_TYPE
=> 'integer',
822 ApiBase
::PARAM_DFLT
=> 'text|langlinks|categories|links|templates|' .
823 'images|externallinks|sections|revid|displaytitle|iwlinks|' .
824 'properties|parsewarnings',
825 ApiBase
::PARAM_ISMULTI
=> true,
826 ApiBase
::PARAM_TYPE
=> [
841 'encodedjsconfigvars',
852 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [
853 'parsetree' => [ 'apihelp-parse-paramvalue-prop-parsetree', CONTENT_MODEL_WIKITEXT
],
855 ApiBase
::PARAM_DEPRECATED_VALUES
=> [
856 'headitems' => 'apiwarn-deprecation-parse-headitems',
859 'wrapoutputclass' => 'mw-parser-output',
862 'effectivelanglinks' => [
863 ApiBase
::PARAM_DFLT
=> false,
864 ApiBase
::PARAM_DEPRECATED
=> true,
868 ApiBase
::PARAM_TYPE
=> 'string',
871 ApiBase
::PARAM_DFLT
=> false,
872 ApiBase
::PARAM_DEPRECATED
=> true,
874 'disablelimitreport' => false,
875 'disableeditsection' => false,
877 ApiBase
::PARAM_DFLT
=> false,
878 ApiBase
::PARAM_DEPRECATED
=> true, // Since 1.32
880 'disablestylededuplication' => false,
882 ApiBase
::PARAM_DFLT
=> false,
883 ApiBase
::PARAM_HELP_MSG
=> [
884 'apihelp-parse-param-generatexml', CONTENT_MODEL_WIKITEXT
886 ApiBase
::PARAM_DEPRECATED
=> true,
889 'sectionpreview' => false,
890 'disabletoc' => false,
892 ApiBase
::PARAM_TYPE
=> array_keys( Skin
::getAllowedSkins() ),
895 ApiBase
::PARAM_TYPE
=> ContentHandler
::getAllContentFormats(),
898 ApiBase
::PARAM_TYPE
=> ContentHandler
::getContentModels(),
903 protected function getExamplesMessages() {
905 'action=parse&page=Project:Sandbox'
906 => 'apihelp-parse-example-page',
907 'action=parse&text={{Project:Sandbox}}&contentmodel=wikitext'
908 => 'apihelp-parse-example-text',
909 'action=parse&text={{PAGENAME}}&title=Test'
910 => 'apihelp-parse-example-texttitle',
911 'action=parse&summary=Some+[[link]]&prop='
912 => 'apihelp-parse-example-summary',
916 public function getHelpUrls() {
917 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Parsing_wikitext#parse';