Merge "Made JobRunner bail more smoothly on near OOM"
[lhc/web/wiklou.git] / includes / api / ApiParse.php
1 <?php
2 /**
3 * Created on Dec 01, 2007
4 *
5 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 /**
26 * @ingroup API
27 */
28 class ApiParse extends ApiBase {
29
30 /** @var string $section */
31 private $section = null;
32
33 /** @var Content $content */
34 private $content = null;
35
36 /** @var Content $pstContent */
37 private $pstContent = null;
38
39 public function execute() {
40 // The data is hot but user-dependent, like page views, so we set vary cookies
41 $this->getMain()->setCacheMode( 'anon-public-user-private' );
42
43 // Get parameters
44 $params = $this->extractRequestParams();
45 $text = $params['text'];
46 $title = $params['title'];
47 if ( $title === null ) {
48 $titleProvided = false;
49 // A title is needed for parsing, so arbitrarily choose one
50 $title = 'API';
51 } else {
52 $titleProvided = true;
53 }
54
55 $page = $params['page'];
56 $pageid = $params['pageid'];
57 $oldid = $params['oldid'];
58
59 $model = $params['contentmodel'];
60 $format = $params['contentformat'];
61
62 if ( !is_null( $page ) && ( !is_null( $text ) || $titleProvided ) ) {
63 $this->dieUsage(
64 'The page parameter cannot be used together with the text and title parameters',
65 'params'
66 );
67 }
68
69 $prop = array_flip( $params['prop'] );
70
71 if ( isset( $params['section'] ) ) {
72 $this->section = $params['section'];
73 if ( !preg_match( '/^((T-)?\d+|new)$/', $this->section ) ) {
74 $this->dieUsage(
75 "The section parameter must be a valid section id or 'new'", "invalidsection"
76 );
77 }
78 } else {
79 $this->section = false;
80 }
81
82 // The parser needs $wgTitle to be set, apparently the
83 // $title parameter in Parser::parse isn't enough *sigh*
84 // TODO: Does this still need $wgTitle?
85 global $wgParser, $wgTitle;
86
87 $redirValues = null;
88
89 // Return result
90 $result = $this->getResult();
91
92 if ( !is_null( $oldid ) || !is_null( $pageid ) || !is_null( $page ) ) {
93 if ( $this->section === 'new' ) {
94 $this->dieUsage(
95 'section=new cannot be combined with oldid, pageid or page parameters. ' .
96 'Please use text', 'params'
97 );
98 }
99 if ( !is_null( $oldid ) ) {
100 // Don't use the parser cache
101 $rev = Revision::newFromId( $oldid );
102 if ( !$rev ) {
103 $this->dieUsage( "There is no revision ID $oldid", 'missingrev' );
104 }
105 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
106 $this->dieUsage( "You don't have permission to view deleted revisions", 'permissiondenied' );
107 }
108
109 $titleObj = $rev->getTitle();
110 $wgTitle = $titleObj;
111 $pageObj = WikiPage::factory( $titleObj );
112 $popts = $this->makeParserOptions( $pageObj, $params );
113
114 // If for some reason the "oldid" is actually the current revision, it may be cached
115 // Deliberately comparing $pageObj->getLatest() with $rev->getId(), rather than
116 // checking $rev->isCurrent(), because $pageObj is what actually ends up being used,
117 // and if its ->getLatest() is outdated, $rev->isCurrent() won't tell us that.
118 if ( $rev->getId() == $pageObj->getLatest() ) {
119 // May get from/save to parser cache
120 $p_result = $this->getParsedContent( $pageObj, $popts,
121 $pageid, isset( $prop['wikitext'] ) );
122 } else { // This is an old revision, so get the text differently
123 $this->content = $rev->getContent( Revision::FOR_THIS_USER, $this->getUser() );
124
125 if ( $this->section !== false ) {
126 $this->content = $this->getSectionContent( $this->content, 'r' . $rev->getId() );
127 }
128
129 // Should we save old revision parses to the parser cache?
130 $p_result = $this->content->getParserOutput( $titleObj, $rev->getId(), $popts );
131 }
132 } else { // Not $oldid, but $pageid or $page
133 if ( $params['redirects'] ) {
134 $reqParams = array(
135 'redirects' => '',
136 );
137 if ( !is_null( $pageid ) ) {
138 $reqParams['pageids'] = $pageid;
139 } else { // $page
140 $reqParams['titles'] = $page;
141 }
142 $req = new FauxRequest( $reqParams );
143 $main = new ApiMain( $req );
144 $pageSet = new ApiPageSet( $main );
145 $pageSet->execute();
146 $redirValues = $pageSet->getRedirectTitlesAsResult( $this->getResult() );
147
148 $to = $page;
149 foreach ( $pageSet->getRedirectTitles() as $title ) {
150 $to = $title->getFullText();
151 }
152 $pageParams = array( 'title' => $to );
153 } elseif ( !is_null( $pageid ) ) {
154 $pageParams = array( 'pageid' => $pageid );
155 } else { // $page
156 $pageParams = array( 'title' => $page );
157 }
158
159 $pageObj = $this->getTitleOrPageId( $pageParams, 'fromdb' );
160 $titleObj = $pageObj->getTitle();
161 if ( !$titleObj || !$titleObj->exists() ) {
162 $this->dieUsage( "The page you specified doesn't exist", 'missingtitle' );
163 }
164 $wgTitle = $titleObj;
165
166 if ( isset( $prop['revid'] ) ) {
167 $oldid = $pageObj->getLatest();
168 }
169
170 $popts = $this->makeParserOptions( $pageObj, $params );
171
172 // Potentially cached
173 $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
174 isset( $prop['wikitext'] ) );
175 }
176 } else { // Not $oldid, $pageid, $page. Hence based on $text
177 $titleObj = Title::newFromText( $title );
178 if ( !$titleObj || $titleObj->isExternal() ) {
179 $this->dieUsageMsg( array( 'invalidtitle', $title ) );
180 }
181 $wgTitle = $titleObj;
182 if ( $titleObj->canExist() ) {
183 $pageObj = WikiPage::factory( $titleObj );
184 } else {
185 // Do like MediaWiki::initializeArticle()
186 $article = Article::newFromTitle( $titleObj, $this->getContext() );
187 $pageObj = $article->getPage();
188 }
189
190 $popts = $this->makeParserOptions( $pageObj, $params );
191 $textProvided = !is_null( $text );
192
193 if ( !$textProvided ) {
194 if ( $titleProvided && ( $prop || $params['generatexml'] ) ) {
195 $this->setWarning(
196 "'title' used without 'text', and parsed page properties were requested " .
197 "(did you mean to use 'page' instead of 'title'?)"
198 );
199 }
200 // Prevent warning from ContentHandler::makeContent()
201 $text = '';
202 }
203
204 // If we are parsing text, do not use the content model of the default
205 // API title, but default to wikitext to keep BC.
206 if ( $textProvided && !$titleProvided && is_null( $model ) ) {
207 $model = CONTENT_MODEL_WIKITEXT;
208 $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
209 }
210
211 try {
212 $this->content = ContentHandler::makeContent( $text, $titleObj, $model, $format );
213 } catch ( MWContentSerializationException $ex ) {
214 $this->dieUsage( $ex->getMessage(), 'parseerror' );
215 }
216
217 if ( $this->section !== false ) {
218 if ( $this->section === 'new' ) {
219 // Insert the section title above the content.
220 if ( !is_null( $params['sectiontitle'] ) && $params['sectiontitle'] !== '' ) {
221 $this->content = $this->content->addSectionHeader( $params['sectiontitle'] );
222 }
223 } else {
224 $this->content = $this->getSectionContent( $this->content, $titleObj->getPrefixedText() );
225 }
226 }
227
228 if ( $params['pst'] || $params['onlypst'] ) {
229 $this->pstContent = $this->content->preSaveTransform( $titleObj, $this->getUser(), $popts );
230 }
231 if ( $params['onlypst'] ) {
232 // Build a result and bail out
233 $result_array = array();
234 $result_array['text'] = $this->pstContent->serialize( $format );
235 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
236 if ( isset( $prop['wikitext'] ) ) {
237 $result_array['wikitext'] = $this->content->serialize( $format );
238 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'wikitext';
239 }
240 if ( !is_null( $params['summary'] ) ||
241 ( !is_null( $params['sectiontitle'] ) && $this->section === 'new' )
242 ) {
243 $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
244 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsedsummary';
245 }
246
247 $result->addValue( null, $this->getModuleName(), $result_array );
248
249 return;
250 }
251
252 // Not cached (save or load)
253 if ( $params['pst'] ) {
254 $p_result = $this->pstContent->getParserOutput( $titleObj, null, $popts );
255 } else {
256 $p_result = $this->content->getParserOutput( $titleObj, null, $popts );
257 }
258 }
259
260 $result_array = array();
261
262 $result_array['title'] = $titleObj->getPrefixedText();
263 $result_array['pageid'] = $pageid ? $pageid : $pageObj->getId();
264
265 if ( !is_null( $oldid ) ) {
266 $result_array['revid'] = intval( $oldid );
267 }
268
269 if ( $params['redirects'] && !is_null( $redirValues ) ) {
270 $result_array['redirects'] = $redirValues;
271 }
272
273 if ( $params['disabletoc'] ) {
274 $p_result->setTOCEnabled( false );
275 }
276
277 if ( isset( $prop['text'] ) ) {
278 $result_array['text'] = $p_result->getText();
279 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'text';
280 }
281
282 if ( !is_null( $params['summary'] ) ||
283 ( !is_null( $params['sectiontitle'] ) && $this->section === 'new' )
284 ) {
285 $result_array['parsedsummary'] = $this->formatSummary( $titleObj, $params );
286 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsedsummary';
287 }
288
289 if ( isset( $prop['langlinks'] ) ) {
290 $langlinks = $p_result->getLanguageLinks();
291
292 if ( $params['effectivelanglinks'] ) {
293 // Link flags are ignored for now, but may in the future be
294 // included in the result.
295 $linkFlags = array();
296 Hooks::run( 'LanguageLinks', array( $titleObj, &$langlinks, &$linkFlags ) );
297 }
298 } else {
299 $langlinks = false;
300 }
301
302 if ( isset( $prop['langlinks'] ) ) {
303 $result_array['langlinks'] = $this->formatLangLinks( $langlinks );
304 }
305 if ( isset( $prop['categories'] ) ) {
306 $result_array['categories'] = $this->formatCategoryLinks( $p_result->getCategories() );
307 }
308 if ( isset( $prop['categorieshtml'] ) ) {
309 $result_array['categorieshtml'] = $this->categoriesHtml( $p_result->getCategories() );
310 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'categorieshtml';
311 }
312 if ( isset( $prop['links'] ) ) {
313 $result_array['links'] = $this->formatLinks( $p_result->getLinks() );
314 }
315 if ( isset( $prop['templates'] ) ) {
316 $result_array['templates'] = $this->formatLinks( $p_result->getTemplates() );
317 }
318 if ( isset( $prop['images'] ) ) {
319 $result_array['images'] = array_keys( $p_result->getImages() );
320 }
321 if ( isset( $prop['externallinks'] ) ) {
322 $result_array['externallinks'] = array_keys( $p_result->getExternalLinks() );
323 }
324 if ( isset( $prop['sections'] ) ) {
325 $result_array['sections'] = $p_result->getSections();
326 }
327
328 if ( isset( $prop['displaytitle'] ) ) {
329 $result_array['displaytitle'] = $p_result->getDisplayTitle() ?
330 $p_result->getDisplayTitle() :
331 $titleObj->getPrefixedText();
332 }
333
334 if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
335 $context = $this->getContext();
336 $context->setTitle( $titleObj );
337 $context->getOutput()->addParserOutputMetadata( $p_result );
338
339 if ( isset( $prop['headitems'] ) ) {
340 $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
341
342 $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
343
344 $scripts = array( $context->getOutput()->getHeadScripts() );
345
346 $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
347 }
348
349 if ( isset( $prop['headhtml'] ) ) {
350 $result_array['headhtml'] = $context->getOutput()->headElement( $context->getSkin() );
351 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'headhtml';
352 }
353 }
354
355 if ( isset( $prop['modules'] ) ) {
356 $result_array['modules'] = array_values( array_unique( $p_result->getModules() ) );
357 $result_array['modulescripts'] = array_values( array_unique( $p_result->getModuleScripts() ) );
358 $result_array['modulestyles'] = array_values( array_unique( $p_result->getModuleStyles() ) );
359 // To be removed in 1.27
360 $result_array['modulemessages'] = array();
361 $this->setWarning( 'modulemessages is deprecated since MediaWiki 1.26' );
362 }
363
364 if ( isset( $prop['jsconfigvars'] ) ) {
365 $result_array['jsconfigvars'] =
366 ApiResult::addMetadataToResultVars( $p_result->getJsConfigVars() );
367 }
368
369 if ( isset( $prop['encodedjsconfigvars'] ) ) {
370 $result_array['encodedjsconfigvars'] = FormatJson::encode(
371 $p_result->getJsConfigVars(), false, FormatJson::ALL_OK
372 );
373 $result_array[ApiResult::META_SUBELEMENTS][] = 'encodedjsconfigvars';
374 }
375
376 if ( isset( $prop['modules'] ) &&
377 !isset( $prop['jsconfigvars'] ) && !isset( $prop['encodedjsconfigvars'] ) ) {
378 $this->setWarning( "Property 'modules' was set but not 'jsconfigvars' " .
379 "or 'encodedjsconfigvars'. Configuration variables are necessary " .
380 "for proper module usage." );
381 }
382
383 if ( isset( $prop['indicators'] ) ) {
384 $result_array['indicators'] = (array)$p_result->getIndicators();
385 ApiResult::setArrayType( $result_array['indicators'], 'BCkvp', 'name' );
386 }
387
388 if ( isset( $prop['iwlinks'] ) ) {
389 $result_array['iwlinks'] = $this->formatIWLinks( $p_result->getInterwikiLinks() );
390 }
391
392 if ( isset( $prop['wikitext'] ) ) {
393 $result_array['wikitext'] = $this->content->serialize( $format );
394 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'wikitext';
395 if ( !is_null( $this->pstContent ) ) {
396 $result_array['psttext'] = $this->pstContent->serialize( $format );
397 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'psttext';
398 }
399 }
400 if ( isset( $prop['properties'] ) ) {
401 $result_array['properties'] = (array)$p_result->getProperties();
402 ApiResult::setArrayType( $result_array['properties'], 'BCkvp', 'name' );
403 }
404
405 if ( isset( $prop['limitreportdata'] ) ) {
406 $result_array['limitreportdata'] =
407 $this->formatLimitReportData( $p_result->getLimitReportData() );
408 }
409 if ( isset( $prop['limitreporthtml'] ) ) {
410 $result_array['limitreporthtml'] = EditPage::getPreviewLimitReport( $p_result );
411 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'limitreporthtml';
412 }
413
414 if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) {
415 if ( !isset( $prop['parsetree'] ) ) {
416 $this->logFeatureUsage( 'action=parse&generatexml' );
417 }
418 if ( $this->content->getModel() != CONTENT_MODEL_WIKITEXT ) {
419 $this->dieUsage( "parsetree is only supported for wikitext content", "notwikitext" );
420 }
421
422 $wgParser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS );
423 $dom = $wgParser->preprocessToDom( $this->content->getNativeData() );
424 if ( is_callable( array( $dom, 'saveXML' ) ) ) {
425 $xml = $dom->saveXML();
426 } else {
427 $xml = $dom->__toString();
428 }
429 $result_array['parsetree'] = $xml;
430 $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsetree';
431 }
432
433 $result_mapping = array(
434 'redirects' => 'r',
435 'langlinks' => 'll',
436 'categories' => 'cl',
437 'links' => 'pl',
438 'templates' => 'tl',
439 'images' => 'img',
440 'externallinks' => 'el',
441 'iwlinks' => 'iw',
442 'sections' => 's',
443 'headitems' => 'hi',
444 'modules' => 'm',
445 'indicators' => 'ind',
446 'modulescripts' => 'm',
447 'modulestyles' => 'm',
448 'modulemessages' => 'm',
449 'properties' => 'pp',
450 'limitreportdata' => 'lr',
451 );
452 $this->setIndexedTagNames( $result_array, $result_mapping );
453 $result->addValue( null, $this->getModuleName(), $result_array );
454 }
455
456 /**
457 * Constructs a ParserOptions object
458 *
459 * @param WikiPage $pageObj
460 * @param array $params
461 *
462 * @return ParserOptions
463 */
464 protected function makeParserOptions( WikiPage $pageObj, array $params ) {
465
466 $popts = $pageObj->makeParserOptions( $this->getContext() );
467 $popts->enableLimitReport( !$params['disablepp'] );
468 $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
469 $popts->setIsSectionPreview( $params['sectionpreview'] );
470 $popts->setEditSection( !$params['disableeditsection'] );
471
472 return $popts;
473 }
474
475 /**
476 * @param WikiPage $page
477 * @param ParserOptions $popts
478 * @param int $pageId
479 * @param bool $getWikitext
480 * @return ParserOutput
481 */
482 private function getParsedContent( WikiPage $page, $popts, $pageId = null, $getWikitext = false ) {
483 $this->content = $page->getContent( Revision::RAW ); //XXX: really raw?
484
485 if ( $this->section !== false && $this->content !== null ) {
486 $this->content = $this->getSectionContent(
487 $this->content,
488 !is_null( $pageId ) ? 'page id ' . $pageId : $page->getTitle()->getPrefixedText()
489 );
490
491 // Not cached (save or load)
492 return $this->content->getParserOutput( $page->getTitle(), null, $popts );
493 }
494
495 // Try the parser cache first
496 // getParserOutput will save to Parser cache if able
497 $pout = $page->getParserOutput( $popts );
498 if ( !$pout ) {
499 $this->dieUsage( "There is no revision ID {$page->getLatest()}", 'missingrev' );
500 }
501 if ( $getWikitext ) {
502 $this->content = $page->getContent( Revision::RAW );
503 }
504
505 return $pout;
506 }
507
508 /**
509 * @param Content $content
510 * @param string $what Identifies the content in error messages, e.g. page title.
511 * @return Content|bool
512 */
513 private function getSectionContent( Content $content, $what ) {
514 // Not cached (save or load)
515 $section = $content->getSection( $this->section );
516 if ( $section === false ) {
517 $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
518 }
519 if ( $section === null ) {
520 $this->dieUsage( "Sections are not supported by " . $what, 'nosuchsection' );
521 $section = false;
522 }
523
524 return $section;
525 }
526
527 /**
528 * This mimicks the behavior of EditPage in formatting a summary
529 *
530 * @param Title $title of the page being parsed
531 * @param Array $params the API parameters of the request
532 * @return Content|bool
533 */
534 private function formatSummary( $title, $params ) {
535 global $wgParser;
536 $summary = !is_null( $params['summary'] ) ? $params['summary'] : '';
537 $sectionTitle = !is_null( $params['sectiontitle'] ) ? $params['sectiontitle'] : '';
538
539 if ( $this->section === 'new' && ( $sectionTitle === '' || $summary === '' ) ) {
540 if ( $sectionTitle !== '' ) {
541 $summary = $params['sectiontitle'];
542 }
543 if ( $summary !== '' ) {
544 $summary = wfMessage( 'newsectionsummary' )
545 ->rawParams( $wgParser->stripSectionName( $summary ) )
546 ->inContentLanguage()->text();
547 }
548 }
549 return Linker::formatComment( $summary, $title, $this->section === 'new' );
550 }
551
552 private function formatLangLinks( $links ) {
553 $result = array();
554 foreach ( $links as $link ) {
555 $entry = array();
556 $bits = explode( ':', $link, 2 );
557 $title = Title::newFromText( $link );
558
559 $entry['lang'] = $bits[0];
560 if ( $title ) {
561 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
562 // localised language name in 'uselang' language
563 $entry['langname'] = Language::fetchLanguageName(
564 $title->getInterwiki(),
565 $this->getLanguage()->getCode()
566 );
567
568 // native language name
569 $entry['autonym'] = Language::fetchLanguageName( $title->getInterwiki() );
570 }
571 ApiResult::setContentValue( $entry, 'title', $bits[1] );
572 $result[] = $entry;
573 }
574
575 return $result;
576 }
577
578 private function formatCategoryLinks( $links ) {
579 $result = array();
580
581 if ( !$links ) {
582 return $result;
583 }
584
585 // Fetch hiddencat property
586 $lb = new LinkBatch;
587 $lb->setArray( array( NS_CATEGORY => $links ) );
588 $db = $this->getDB();
589 $res = $db->select( array( 'page', 'page_props' ),
590 array( 'page_title', 'pp_propname' ),
591 $lb->constructSet( 'page', $db ),
592 __METHOD__,
593 array(),
594 array( 'page_props' => array(
595 'LEFT JOIN', array( 'pp_propname' => 'hiddencat', 'pp_page = page_id' )
596 ) )
597 );
598 $hiddencats = array();
599 foreach ( $res as $row ) {
600 $hiddencats[$row->page_title] = isset( $row->pp_propname );
601 }
602
603 foreach ( $links as $link => $sortkey ) {
604 $entry = array();
605 $entry['sortkey'] = $sortkey;
606 ApiResult::setContentValue( $entry, 'category', $link );
607 if ( !isset( $hiddencats[$link] ) ) {
608 $entry['missing'] = true;
609 } elseif ( $hiddencats[$link] ) {
610 $entry['hidden'] = true;
611 }
612 $result[] = $entry;
613 }
614
615 return $result;
616 }
617
618 private function categoriesHtml( $categories ) {
619 $context = $this->getContext();
620 $context->getOutput()->addCategoryLinks( $categories );
621
622 return $context->getSkin()->getCategories();
623 }
624
625 private function formatLinks( $links ) {
626 $result = array();
627 foreach ( $links as $ns => $nslinks ) {
628 foreach ( $nslinks as $title => $id ) {
629 $entry = array();
630 $entry['ns'] = $ns;
631 ApiResult::setContentValue( $entry, 'title', Title::makeTitle( $ns, $title )->getFullText() );
632 $entry['exists'] = $id != 0;
633 $result[] = $entry;
634 }
635 }
636
637 return $result;
638 }
639
640 private function formatIWLinks( $iw ) {
641 $result = array();
642 foreach ( $iw as $prefix => $titles ) {
643 foreach ( array_keys( $titles ) as $title ) {
644 $entry = array();
645 $entry['prefix'] = $prefix;
646
647 $title = Title::newFromText( "{$prefix}:{$title}" );
648 if ( $title ) {
649 $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
650 }
651
652 ApiResult::setContentValue( $entry, 'title', $title->getFullText() );
653 $result[] = $entry;
654 }
655 }
656
657 return $result;
658 }
659
660 private function formatHeadItems( $headItems ) {
661 $result = array();
662 foreach ( $headItems as $tag => $content ) {
663 $entry = array();
664 $entry['tag'] = $tag;
665 ApiResult::setContentValue( $entry, 'content', $content );
666 $result[] = $entry;
667 }
668
669 return $result;
670 }
671
672 private function formatCss( $css ) {
673 $result = array();
674 foreach ( $css as $file => $link ) {
675 $entry = array();
676 $entry['file'] = $file;
677 ApiResult::setContentValue( $entry, 'link', $link );
678 $result[] = $entry;
679 }
680
681 return $result;
682 }
683
684 private function formatLimitReportData( $limitReportData ) {
685 $result = array();
686 $apiResult = $this->getResult();
687
688 foreach ( $limitReportData as $name => $value ) {
689 $entry = array();
690 $entry['name'] = $name;
691 if ( !is_array( $value ) ) {
692 $value = array( $value );
693 }
694 ApiResult::setIndexedTagNameRecursive( $value, 'param' );
695 $entry = array_merge( $entry, $value );
696 $result[] = $entry;
697 }
698
699 return $result;
700 }
701
702 private function setIndexedTagNames( &$array, $mapping ) {
703 foreach ( $mapping as $key => $name ) {
704 if ( isset( $array[$key] ) ) {
705 ApiResult::setIndexedTagName( $array[$key], $name );
706 }
707 }
708 }
709
710 public function getAllowedParams() {
711 return array(
712 'title' => null,
713 'text' => array(
714 ApiBase::PARAM_TYPE => 'text',
715 ),
716 'summary' => null,
717 'page' => null,
718 'pageid' => array(
719 ApiBase::PARAM_TYPE => 'integer',
720 ),
721 'redirects' => false,
722 'oldid' => array(
723 ApiBase::PARAM_TYPE => 'integer',
724 ),
725 'prop' => array(
726 ApiBase::PARAM_DFLT => 'text|langlinks|categories|links|templates|' .
727 'images|externallinks|sections|revid|displaytitle|iwlinks|properties',
728 ApiBase::PARAM_ISMULTI => true,
729 ApiBase::PARAM_TYPE => array(
730 'text',
731 'langlinks',
732 'categories',
733 'categorieshtml',
734 'links',
735 'templates',
736 'images',
737 'externallinks',
738 'sections',
739 'revid',
740 'displaytitle',
741 'headitems',
742 'headhtml',
743 'modules',
744 'jsconfigvars',
745 'encodedjsconfigvars',
746 'indicators',
747 'iwlinks',
748 'wikitext',
749 'properties',
750 'limitreportdata',
751 'limitreporthtml',
752 'parsetree',
753 ),
754 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(
755 'parsetree' => array( 'apihelp-parse-paramvalue-prop-parsetree', CONTENT_MODEL_WIKITEXT ),
756 ),
757 ),
758 'pst' => false,
759 'onlypst' => false,
760 'effectivelanglinks' => false,
761 'section' => null,
762 'sectiontitle' => array(
763 ApiBase::PARAM_TYPE => 'string',
764 ),
765 'disablepp' => false,
766 'disableeditsection' => false,
767 'generatexml' => array(
768 ApiBase::PARAM_DFLT => false,
769 ApiBase::PARAM_HELP_MSG => array(
770 'apihelp-parse-param-generatexml', CONTENT_MODEL_WIKITEXT
771 ),
772 ApiBase::PARAM_DEPRECATED => true,
773 ),
774 'preview' => false,
775 'sectionpreview' => false,
776 'disabletoc' => false,
777 'contentformat' => array(
778 ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
779 ),
780 'contentmodel' => array(
781 ApiBase::PARAM_TYPE => ContentHandler::getContentModels(),
782 )
783 );
784 }
785
786 protected function getExamplesMessages() {
787 return array(
788 'action=parse&page=Project:Sandbox'
789 => 'apihelp-parse-example-page',
790 'action=parse&text={{Project:Sandbox}}&contentmodel=wikitext'
791 => 'apihelp-parse-example-text',
792 'action=parse&text={{PAGENAME}}&title=Test'
793 => 'apihelp-parse-example-texttitle',
794 'action=parse&summary=Some+[[link]]&prop='
795 => 'apihelp-parse-example-summary',
796 );
797 }
798
799 public function getHelpUrls() {
800 return 'https://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
801 }
802 }