prep getSection, replaceSection
authorDaniel Kinzler <daniel.kinzler@wikimedia.de>
Fri, 9 Mar 2012 17:51:10 +0000 (17:51 +0000)
committerDaniel Kinzler <daniel.kinzler@wikimedia.de>
Wed, 4 Apr 2012 17:55:30 +0000 (19:55 +0200)
includes/Content.php
includes/ContentHandler.php

index 6a3ecdb..9872868 100644 (file)
@@ -47,10 +47,30 @@ abstract class Content {
         return null;
     }
 
-    public function getSection( $section ) { #FIXME: should this return text? or a Content object? or what??
+    /**
+     * Returns the section with the given id.
+     *
+     * The default implementation returns null.
+     *
+     * @param String $sectionId the section's id
+     * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
+     */
+    public function getSection( $sectionId ) {
         return null;
     }
 
+    /**
+     * Replaces the section with the given id.
+     *
+     * The default implementation returns $this.
+     *
+     * @param String $sectionId the section's id
+     * @param Content $with the section's new content
+     * @return Content a new content object with the section replaced, or this content object if the section couldn't be replaced.
+     */
+    public function replaceSection( $sectionId ) {
+    }
+
     #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
 
 
@@ -67,6 +87,11 @@ abstract class Content {
 
     # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
 
+
+    # TODO: tie into API to provide contentModel for Revisions
+    # TODO: tie into API to provide serialized version and contentFormat for Revisions
+    # TODO: tie into API edit interface
+
 }
 
 class TextContent extends Content {
@@ -114,7 +139,7 @@ class TextContent extends Content {
     }
 
     public function getRedirectChain() {
-        #XXX: really do this for all text, or just in WikitextContent
+        #XXX: really do this for all text, or just in WikitextContent?
         $text = $this->getRawData();
         return Title::newFromRedirectArray( $text );
     }
@@ -154,13 +179,86 @@ class WikitextContent extends TextContent {
         return $po;
     }
 
+    /**
+     * Returns the section with the given id.
+     *
+     * @param String $sectionId the section's id
+     * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
+     */
     public function getSection( $section ) {
         global $wgParser;
 
         $text = $this->getRawData();
-        return $wgParser->getSection( $text, $section, false );
+        $sect = $wgParser->getSection( $text, $section, false );
+        $title = Title::newFromDBkey( $this->mTitle->getText() . '#' . $section, $this->mTitle->getNamespace() ); #FIXME: get rid of titles here
+
+        return  new WikitextContent( $sect, $title );
     }
 
+    /**
+     * Replaces the section with the given id.
+     *
+     * @param String $sectionId the section's id
+     * @param Content $with the section's new content
+     * @return Boolean true if te section was replaced sucessfully, false otherwise
+     */
+    #FIXME: implement replaceSection(), use in WikiPage
+
+    /**
+     * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...)
+     * @param $text String: new text of the section
+     * @param $sectionTitle String: new section's subject, only if $section is 'new'
+     * @param $edittime String: revision timestamp or null to use the current revision
+     * @return string Complete article text, or null if error
+     */
+    /*public function replaceSection( $section, $text, $sectionTitle = '', $edittime = null ) { #FIXME: adopt this!
+        wfProfileIn( __METHOD__ );
+
+        if ( strval( $section ) == '' ) {
+            // Whole-page edit; let the whole text through
+        } else {
+            // Bug 30711: always use current version when adding a new section
+            if ( is_null( $edittime ) || $section == 'new' ) {
+                $oldtext = $this->getRawText();
+                if ( $oldtext === false ) {
+                    wfDebug( __METHOD__ . ": no page text\n" );
+                    wfProfileOut( __METHOD__ );
+                    return null;
+                }
+            } else {
+                $dbw = wfGetDB( DB_MASTER );
+                $rev = Revision::loadFromTimestamp( $dbw, $this->mTitle, $edittime );
+
+                if ( !$rev ) {
+                    wfDebug( "WikiPage::replaceSection asked for bogus section (page: " .
+                        $this->getId() . "; section: $section; edittime: $edittime)\n" );
+                    wfProfileOut( __METHOD__ );
+                    return null;
+                }
+
+                $oldtext = $rev->getText();
+            }
+
+            if ( $section == 'new' ) {
+                # Inserting a new section
+                $subject = $sectionTitle ? wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
+                if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
+                    $text = strlen( trim( $oldtext ) ) > 0
+                        ? "{$oldtext}\n\n{$subject}{$text}"
+                        : "{$subject}{$text}";
+                }
+            } else {
+                # Replacing an existing section; roll out the big guns
+                global $wgParser;
+
+                $text = $wgParser->replaceSection( $oldtext, $section, $text );
+            }
+        }
+
+        wfProfileOut( __METHOD__ );
+        return $text;
+    } */
+
 }
 
 class MessageContent extends TextContent {
index 138c641..a80eaeb 100644 (file)
@@ -145,6 +145,8 @@ abstract class ContentHandler {
 
     public abstract function unserialize( $blob, Title $title, $format = null ); #FIXME: ...and revId?
 
+    public abstract function newContent( Title $title );
+
     # public abstract function doPreSaveTransform( $title, $obj ); #TODO...
 
     /**
@@ -208,6 +210,8 @@ abstract class ContentHandler {
 
     #TODO: how to handle extra message for JS/CSS previews??
     #TODO: Article::showCssOrJsPage ---> specialized classes!
+
+    #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
 }
 
 
@@ -232,6 +236,10 @@ class WikitextContentHandler extends TextContentHandler {
         return new WikitextContent($text, $title);
     }
 
+    public function newContent( Title $title) {
+        return new WikitextContent("", $title);
+    }
+
 }
 
 class JavaScriptContentHandler extends TextContentHandler {
@@ -244,6 +252,9 @@ class JavaScriptContentHandler extends TextContentHandler {
         return new JavaScriptContent($text, $title);
     }
 
+    public function newContent( Title $title) {
+        return new JavaScriptContent("", $title);
+    }
 }
 
 class CssContentHandler extends TextContentHandler {
@@ -256,4 +267,8 @@ class CssContentHandler extends TextContentHandler {
         return new CssContent($text, $title);
     }
 
+    public function newContent( Title $title) {
+        return new CssContent("", $title);
+    }
+
 }