Merge "Revert "Limit searches at 500 per page""
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 88904c0..47ad80f 100644 (file)
@@ -176,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 " .
@@ -197,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." );
                        }
@@ -249,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() );
@@ -354,6 +362,15 @@ 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" );
@@ -382,6 +399,7 @@ class ApiParse extends ApiBase {
                        'sections' => 's',
                        'headitems' => 'hi',
                        'properties' => 'pp',
+                       'limitreportdata' => 'lr',
                );
                $this->setIndexedTagNames( $result_array, $result_mapping );
                $result->addValue( null, $this->getModuleName(), $result_array );
@@ -469,6 +487,10 @@ 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;
@@ -637,6 +659,25 @@ class ApiParse extends ApiBase {
                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;
+       }
+
        private function setIndexedTagNames( &$array, $mapping ) {
                foreach ( $mapping as $key => $name ) {
                        if ( isset( $array[$key] ) ) {
@@ -680,6 +721,8 @@ class ApiParse extends ApiBase {
                                        'iwlinks',
                                        'wikitext',
                                        'properties',
+                                       'limitreportdata',
+                                       'limitreporthtml',
                                )
                        ),
                        'pst' => false,
@@ -691,6 +734,7 @@ class ApiParse extends ApiBase {
                        'generatexml' => false,
                        'preview' => false,
                        'sectionpreview' => false,
+                       'disabletoc' => false,
                        'contentformat' => array(
                                ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
                        ),
@@ -709,7 +753,7 @@ class ApiParse extends ApiBase {
                        '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",
@@ -733,6 +777,10 @@ class ApiParse extends ApiBase {
                                ' 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',
@@ -753,13 +801,14 @@ class ApiParse extends ApiBase {
                        '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",
                        ),
                );
@@ -799,14 +848,13 @@ class ApiParse extends ApiBase {
                                '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={{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',