Made DifferenceEngine use a context instead of global variables and updaters calls...
[lhc/web/wiklou.git] / includes / api / ApiParse.php
index 1b0e940..b1e344c 100644 (file)
@@ -62,7 +62,8 @@ class ApiParse extends ApiBase {
 
                // The parser needs $wgTitle to be set, apparently the
                // $title parameter in Parser::parse isn't enough *sigh*
-               global $wgParser, $wgUser, $wgTitle, $wgLang;
+               // TODO: Does this still need $wgTitle?
+               global $wgParser, $wgTitle, $wgLang;
 
                // Currently unnecessary, code to act as a safeguard against any change in current behaviour of uselang breaks
                $oldLang = null;
@@ -99,6 +100,7 @@ class ApiParse extends ApiBase {
                                if ( $titleObj->getLatestRevID() === intval( $oldid ) )  {
                                        $articleObj = new Article( $titleObj, 0 );
 
+                                       // May get from/save to parser cache
                                        $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
                                                 isset( $prop['wikitext'] ) ) ;
                                } else { // This is an old revision, so get the text differently
@@ -110,9 +112,10 @@ class ApiParse extends ApiBase {
                                                $this->text = $this->getSectionText( $this->text, 'r' . $rev->getId() );
                                        }
 
+                                       // Should we save old revision parses to the parser cache?
                                        $p_result = $wgParser->parse( $this->text, $titleObj, $popts );
                                }
-                       } else { // Not $oldid
+                       } else { // Not $oldid, but $pageid or $page
                                if ( $params['redirects'] ) {
                                        $reqParams = array(
                                                'action' => 'query',
@@ -159,6 +162,7 @@ class ApiParse extends ApiBase {
                                        $oldid = $articleObj->getRevIdFetched();
                                }
 
+                               // Potentially cached
                                $p_result = $this->getParsedSectionOrText( $articleObj, $titleObj, $popts, $pageid,
                                         isset( $prop['wikitext'] ) ) ;
                        }
@@ -176,10 +180,11 @@ class ApiParse extends ApiBase {
                        }
 
                        if ( $params['pst'] || $params['onlypst'] ) {
-                               $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $wgUser, $popts );
+                               $this->pstText = $wgParser->preSaveTransform( $this->text, $titleObj, $this->getUser(), $popts );
                        }
                        if ( $params['onlypst'] ) {
                                // Build a result and bail out
+                               $result_array = array();
                                $result_array['text'] = array();
                                $result->setContent( $result_array['text'], $this->pstText );
                                if ( isset( $prop['wikitext'] ) ) {
@@ -189,6 +194,7 @@ class ApiParse extends ApiBase {
                                $result->addValue( null, $this->getModuleName(), $result_array );
                                return;
                        }
+                       // Not cached (save or load)
                        $p_result = $wgParser->parse( $params['pst'] ? $this->pstText : $this->text, $titleObj, $popts );
                }
 
@@ -211,7 +217,7 @@ class ApiParse extends ApiBase {
 
                if ( !is_null( $params['summary'] ) ) {
                        $result_array['parsedsummary'] = array();
-                       $result->setContent( $result_array['parsedsummary'], $wgUser->getSkin()->formatComment( $params['summary'], $titleObj ) );
+                       $result->setContent( $result_array['parsedsummary'], Linker::formatComment( $params['summary'], $titleObj ) );
                }
 
                if ( isset( $prop['langlinks'] ) ) {
@@ -253,16 +259,16 @@ class ApiParse extends ApiBase {
                }
 
                if ( isset( $prop['headitems'] ) || isset( $prop['headhtml'] ) ) {
-                       $context = new RequestContext;
+                       $context = $this->getContext();
+                       $context->setTitle( $titleObj );
                        $context->getOutput()->addParserOutputNoText( $p_result );
 
                        if ( isset( $prop['headitems'] ) ) {
                                $headItems = $this->formatHeadItems( $p_result->getHeadItems() );
 
-                               $context->getSkin()->setupUserCss( $context->getOutput() );
                                $css = $this->formatCss( $context->getOutput()->buildCssLinksArray() );
 
-                               $scripts = array( $context->getOutput()->getHeadScripts( $context->getSkin() ) );
+                               $scripts = array( $context->getOutput()->getHeadScripts() );
 
                                $result_array['headitems'] = array_merge( $headItems, $css, $scripts );
                        }
@@ -318,12 +324,14 @@ class ApiParse extends ApiBase {
                if ( $this->section !== false ) {
                        global $wgParser;
 
-                       $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null ( $pageId )
+                       $this->text = $this->getSectionText( $articleObj->getRawText(), !is_null( $pageId )
                                        ? 'page id ' . $pageId : $titleObj->getText() );
 
+                       // Not cached (save or load)
                        return $wgParser->parse( $this->text, $titleObj, $popts );
                } else {
                        // Try the parser cache first
+                       // getParserOutput will save to Parser cache if able
                        $pout = $articleObj->getParserOutput();
                        if ( $getWikitext ) {
                                $rev = Revision::newFromTitle( $titleObj );
@@ -337,6 +345,7 @@ class ApiParse extends ApiBase {
 
        private function getSectionText( $text, $what ) {
                global $wgParser;
+               // Not cached (save or load)
                $text = $wgParser->getSection( $text, $this->section, false );
                if ( $text === false ) {
                        $this->dieUsage( "There is no section {$this->section} in " . $what, 'nosuchsection' );
@@ -353,7 +362,7 @@ class ApiParse extends ApiBase {
 
                        $entry['lang'] = $bits[0];
                        if ( $title ) {
-                               $entry['url'] = $title->getFullURL();
+                               $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
                        }
                        $this->getResult()->setContent( $entry, $bits[1] );
                        $result[] = $entry;
@@ -373,7 +382,7 @@ class ApiParse extends ApiBase {
        }
 
        private function categoriesHtml( $categories ) {
-               $context = $this->createContext();
+               $context = $this->getContext();
                $context->getOutput()->addCategoryLinks( $categories );
                return $context->getSkin()->getCategories();
        }
@@ -381,6 +390,8 @@ class ApiParse extends ApiBase {
        /**
         * @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 ) {
                global $wgContLang, $wgHideInterlanguageLinks;
@@ -435,7 +446,7 @@ class ApiParse extends ApiBase {
 
                                $title = Title::newFromText( "{$prefix}:{$title}" );
                                if ( $title ) {
-                                       $entry['url'] = $title->getFullURL();
+                                       $entry['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT );
                                }
 
                                $this->getResult()->setContent( $entry, $title->getFullText() );
@@ -579,12 +590,16 @@ class ApiParse extends ApiBase {
                ) );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
                        'api.php?action=parse&text={{Project:Sandbox}}'
                );
        }
 
+       public function getHelpUrls() {
+               return 'http://www.mediawiki.org/wiki/API:Parsing_wikitext#parse';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }