API: Have action=parse indicate hidden categories
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index bde1d99..301affb 100644 (file)
@@ -44,6 +44,14 @@ class ApiParse extends ApiBase {
                $params = $this->extractRequestParams();
                $text = $params['text'];
                $title = $params['title'];
+               if ( $title === null ) {
+                       $titleProvided = false;
+                       // A title is needed for parsing, so arbitrarily choose one
+                       $title = 'API';
+               } else {
+                       $titleProvided = true;
+               }
+
                $page = $params['page'];
                $pageid = $params['pageid'];
                $oldid = $params['oldid'];
@@ -51,7 +59,7 @@ class ApiParse extends ApiBase {
                $model = $params['contentmodel'];
                $format = $params['contentformat'];
 
-               if ( !is_null( $page ) && ( !is_null( $text ) || $title != 'API' ) ) {
+               if ( !is_null( $page ) && ( !is_null( $text ) || $titleProvided ) ) {
                        $this->dieUsage( 'The page parameter cannot be used together with the text and title parameters', 'params' );
                }
 
@@ -94,8 +102,7 @@ class ApiParse extends ApiBase {
                                $titleObj = $rev->getTitle();
                                $wgTitle = $titleObj;
                                $pageObj = WikiPage::factory( $titleObj );
-                               $popts = $pageObj->makeParserOptions( $this->getContext() );
-                               $popts->enableLimitReport( !$params['disablepp'] );
+                               $popts = $this->makeParserOptions( $pageObj, $params );
 
                                // If for some reason the "oldid" is actually the current revision, it may be cached
                                if ( $rev->isCurrent() ) {
@@ -152,8 +159,7 @@ class ApiParse extends ApiBase {
                                        $oldid = $pageObj->getLatest();
                                }
 
-                               $popts = $pageObj->makeParserOptions( $this->getContext() );
-                               $popts->enableLimitReport( !$params['disablepp'] );
+                               $popts = $this->makeParserOptions( $pageObj, $params );
 
                                // Potentially cached
                                $p_result = $this->getParsedContent( $pageObj, $popts, $pageid,
@@ -170,11 +176,24 @@ class ApiParse extends ApiBase {
                        $wgTitle = $titleObj;
                        $pageObj = WikiPage::factory( $titleObj );
 
-                       $popts = $pageObj->makeParserOptions( $this->getContext() );
-                       $popts->enableLimitReport( !$params['disablepp'] );
+                       $popts = $this->makeParserOptions( $pageObj, $params );
 
                        if ( is_null( $text ) ) {
-                               $this->dieUsage( 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?', 'params' );
+                               if ( $titleProvided && ( $prop || $params['generatexml'] ) ) {
+                                       $this->setWarning(
+                                               "'title' used without 'text', and parsed page properties were requested " .
+                                               "(did you mean to use 'page' instead of 'title'?)"
+                                       );
+                               }
+                               // Prevent warning from ContentHandler::makeContent()
+                               $text = '';
+                       }
+
+                       // 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 ) ) {
+                               $model = CONTENT_MODEL_WIKITEXT;
+                               $this->setWarning( "No 'title' or 'contentmodel' was given, assuming $model." );
                        }
 
                        try {
@@ -359,6 +378,26 @@ class ApiParse extends ApiBase {
                }
        }
 
+       /**
+        * Constructs a ParserOptions object
+        *
+        * @param WikiPage $pageObj
+        * @param array $params
+        *
+        * @return ParserOptions
+        */
+       protected function makeParserOptions( WikiPage $pageObj, array $params ) {
+               wfProfileIn( __METHOD__ );
+
+               $popts = $pageObj->makeParserOptions( $this->getContext() );
+               $popts->enableLimitReport( !$params['disablepp'] );
+               $popts->setIsPreview( $params['preview'] || $params['sectionpreview'] );
+               $popts->setIsSectionPreview( $params['sectionpreview'] );
+
+               wfProfileOut( __METHOD__ );
+               return $popts;
+       }
+
        /**
         * @param $page WikiPage
         * @param $popts ParserOptions
@@ -422,12 +461,41 @@ class ApiParse extends ApiBase {
 
        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;
        }
 
@@ -551,9 +619,7 @@ class ApiParse extends ApiBase {
 
        public function getAllowedParams() {
                return array(
-                       'title' => array(
-                               ApiBase::PARAM_DFLT => 'API',
-                       ),
+                       'title' => null,
                        'text' => null,
                        'summary' => null,
                        'page' => null,
@@ -594,6 +660,8 @@ class ApiParse extends ApiBase {
                        'section' => null,
                        'disablepp' => false,
                        'generatexml' => false,
+                       'preview' => false,
+                       'sectionpreview' => false,
                        'contentformat' => array(
                                ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
                        ),
@@ -605,11 +673,13 @@ class ApiParse extends ApiBase {
 
        public function getParamDescription() {
                $p = $this->getModulePrefix();
+               $wikitext = CONTENT_MODEL_WIKITEXT;
                return array(
-                       'text' => 'Wikitext to parse',
+                       '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',
+                       'title' => "Title of page the text belongs to. " .
+                               "If omitted, \"API\" is used as the title with content model $wikitext",
                        '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",
@@ -639,32 +709,46 @@ class ApiParse extends ApiBase {
                        ),
                        'pst' => array(
                                'Do a pre-save transform on the input before parsing it',
-                               'Ignored if page, pageid or oldid is used'
+                               "Only valid when used with {$p}text",
                        ),
                        'onlypst' => array(
                                'Do a pre-save transform (PST) on the input, but don\'t parse it',
-                               'Returns the same wikitext, after a PST has been applied. Ignored if page, pageid or oldid is used'
+                               'Returns the same wikitext, after a PST has been applied.',
+                               "Only valid when used with {$p}text",
                        ),
                        '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',
-                       'generatexml' => 'Generate XML parse tree (requires prop=wikitext)',
-                       'contentformat' => 'Content serialization format used for the input text',
-                       'contentmodel' => 'Content model of the new content',
+                       'generatexml' => "Generate XML parse tree (requires contentmodel=$wikitext)",
+                       'preview' => 'Parse in preview mode',
+                       'sectionpreview' => 'Parse in section preview mode (enables preview mode too)',
+                       '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",
+                               "Only valid when used with {$p}text",
+                       ),
                );
        }
 
        public function getDescription() {
+               $p = $this->getModulePrefix();
                return array(
-                       'Parses wikitext and returns parser output',
+                       '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.",
+                       "3) Specify only a summary to parse. {$p}prop should be given an empty value.",
                );
        }
 
        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' => 'params', 'info' => 'The text parameter should be passed with the title parameter. Should you be using the "page" parameter instead?' ),
                        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' ),
@@ -679,7 +763,10 @@ class ApiParse extends ApiBase {
 
        public function getExamples() {
                return array(
-                       'api.php?action=parse&text={{Project:Sandbox}}'
+                       '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&summary=Some+[[link]]&prop=' => 'Parse a summary',
                );
        }