https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114185

Revision: 114185
Author:   daniel
Date:     2012-03-19 21:09:37 +0000 (Mon, 19 Mar 2012)
Log Message:
-----------
isCountable()

Modified Paths:
--------------
    branches/Wikidata/phase3/includes/Content.php
    branches/Wikidata/phase3/includes/Revision.php
    branches/Wikidata/phase3/includes/WikiPage.php

Modified: branches/Wikidata/phase3/includes/Content.php
===================================================================
--- branches/Wikidata/phase3/includes/Content.php       2012-03-19 21:04:09 UTC 
(rev 114184)
+++ branches/Wikidata/phase3/includes/Content.php       2012-03-19 21:09:37 UTC 
(rev 114185)
@@ -27,14 +27,30 @@
      */
     public abstract function getNativeData( );
 
-    public abstract function getSize( );
+    /**
+     * returns the content's nominal size in bogo-bytes.
+     */
+    public abstract function getSize( ); #XXX: do we really need/want this 
here? we could just use the byte syse of the serialized form...
 
+    /**
+     * Returns true if this content is countable as a "real" wiki page, 
provided
+     * that it's also in a countable location (e.g. a current revision in the 
main namespace).
+     *
+     * @param $hasLinks Bool: if it is known whether this content contains 
links, provide this information here,
+     *                        to avoid redundant parsing to find out.
+     */
+    public abstract function isCountable( $hasLinks = null ) ;
+
     public abstract function getParserOutput( Title $title = null, $revId = 
null, ParserOptions $options = NULL );
 
     public function getRedirectChain() {
         return null;
     }
 
+    public function isRedirect() {
+        return false;
+    }
+
     /**
      * Returns the section with the given id.
      *
@@ -62,11 +78,8 @@
     #TODO: implement specialized ParserOutput for Wikidata model
     #TODO: provide "combined" ParserOutput for Multipart... somehow.
 
-    # TODO: Wikipage::isCountable(Content $a)
+    # XXX: isCacheable( ) # can/should we do this here?
 
-    # TODO: isCacheable( )
-    # TODO: getSize( )
-
     # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
     # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags )
 
@@ -94,6 +107,34 @@
     }
 
     /**
+     * returns the content's nominal size in bogo-bytes.
+     */
+    public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
+        $text = $this->getNativeData( );
+        return strlen( $text );
+    }
+
+    /**
+     * Returns true if this content is not a redirect, and 
$wgArticleCountMethod is "any".
+     *
+     * @param $hasLinks Bool: if it is known whether this content contains 
links, provide this information here,
+     *                        to avoid redundant parsing to find out.
+     */
+    public function isCountable( $hasLinks = null ) {
+        global $wgArticleCountMethod;
+
+        if ( $this->isRedirect( ) ) {
+            return false;
+        }
+
+        if (  $wgArticleCountMethod === 'any' ) {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
      * Returns the text represented by this Content object, as a string.
      *
      * @return String the raw text
@@ -245,6 +286,46 @@
         return Title::newFromRedirectArray( $text );
     }
 
+    public function isRedirect() {
+        $text = $this->getNativeData();
+        return Title::newFromRedirect( $text ) !== null;
+    }
+
+    /**
+     * Returns true if this content is not a redirect, and this content's text 
is countable according to
+     * the criteria defiend by $wgArticleCountMethod.
+     *
+     * @param $hasLinks Bool: if it is known whether this content contains 
links, provide this information here,
+     *                        to avoid redundant parsing to find out.
+     */
+    public function isCountable( $hasLinks = null ) {
+        global $wgArticleCountMethod;
+
+        if ( $this->isRedirect( ) ) {
+            return false;
+        }
+
+        $text = $this->getNativeData();
+
+        switch ( $wgArticleCountMethod ) {
+            case 'any':
+                return true;
+            case 'comma':
+                if ( $text === false ) {
+                    $text = $this->getRawText();
+                }
+                return strpos( $text,  ',' ) !== false;
+            case 'link':
+                if ( $hasLinks === null ) { # not know, find out
+                    $po = $this->getParserOutput();
+                    $links = $po->getLinks();
+                    $hasLinks = !empty( $links );
+                }
+
+                return $hasLinks;
+        }
+    }
+
 }
 
 class MessageContent extends TextContent {

Modified: branches/Wikidata/phase3/includes/Revision.php
===================================================================
--- branches/Wikidata/phase3/includes/Revision.php      2012-03-19 21:04:09 UTC 
(rev 114184)
+++ branches/Wikidata/phase3/includes/Revision.php      2012-03-19 21:09:37 UTC 
(rev 114185)
@@ -496,7 +496,8 @@
                        $this->mCurrent   = false;
                        # If we still have no length, see it we have the text 
to figure it out
                        if ( !$this->mSize ) {
-                               $this->mSize = is_null( $this->mText ) ? null : 
strlen( $this->mText ); #FIXME: do strlen in Content object
+                #XXX: my be inconsistent with the notion of "size" use for the 
present content model
+                               $this->mSize = is_null( $this->mText ) ? null : 
strlen( $this->mText );
                        }
                        # Same for sha1
                        if ( $this->mSha1 === null ) {

Modified: branches/Wikidata/phase3/includes/WikiPage.php
===================================================================
--- branches/Wikidata/phase3/includes/WikiPage.php      2012-03-19 21:04:09 UTC 
(rev 114184)
+++ branches/Wikidata/phase3/includes/WikiPage.php      2012-03-19 21:09:37 UTC 
(rev 114185)
@@ -300,15 +300,10 @@
         * @return bool
         */
        public function isRedirect( $text = false ) {
-               if ( $text === false ) {
-                       if ( !$this->mDataLoaded ) {
-                               $this->loadPageData();
-                       }
+        if ( $text === false ) $content = $this->getContent();
+        else $content = ContentHandler::makeContent( $text, $this->mTitle ); # 
TODO: allow model and format to be provided; or better, expect a Content object
 
-                       return (bool)$this->mIsRedirect;
-               } else {
-                       return Title::newFromRedirect( $text ) !== null;
-               }
+        return $content->isRedirect();
        }
 
        /**
@@ -391,7 +386,7 @@
      *      Revision::FOR_PUBLIC       to be displayed to all users
      *      Revision::FOR_THIS_USER    to be displayed to $wgUser
      *      Revision::RAW              get the text regardless of permissions
-     * @return String|null The content of the current revision
+     * @return Content|null The content of the current revision
      */
     public function getContent( $audience = Revision::FOR_PUBLIC ) {
         $this->loadLastEdit();
@@ -549,39 +544,42 @@
         *        if false, the current database state will be used
         * @return Boolean
         */
-       public function isCountable( $editInfo = false ) { #FIXME: move this to 
Content object
+       public function isCountable( $editInfo = false ) {
                global $wgArticleCountMethod;
 
                if ( !$this->mTitle->isContentPage() ) {
                        return false;
                }
 
-               $text = $editInfo ? $editInfo->pst : false;
+        if ( $editInfo ) {
+            $content = ContentHandler::makeContent( $editInfo->pst, 
$this->mTitle );
+            # TODO: take model and format from edit info!
+        } else {
+            $content = $this->getContent();
+        }
 
-               if ( $this->isRedirect( $text ) ) {
+               if ( $content->isRedirect( ) ) {
                        return false;
                }
 
-               switch ( $wgArticleCountMethod ) {
-               case 'any':
-                       return true;
-               case 'comma':
-                       if ( $text === false ) {
-                               $text = $this->getRawText();
-                       }
-                       return strpos( $text,  ',' ) !== false;
-               case 'link':
-                       if ( $editInfo ) {
-                               // ParserOutput::getLinks() is a 2D array of 
page links, so
-                               // to be really correct we would need to 
recurse in the array
-                               // but the main array should only have items in 
it if there are
-                               // links.
-                               return (bool)count( 
$editInfo->output->getLinks() );
-                       } else {
-                               return (bool)wfGetDB( DB_SLAVE )->selectField( 
'pagelinks', 1,
-                                       array( 'pl_from' => $this->getId() ), 
__METHOD__ );
-                       }
-               }
+        $hasLinks = null;
+
+        if ( $wgArticleCountMethod === 'link' ) {
+            # nasty special case to avoid re-parsing to detect links
+
+            if ( $editInfo ) {
+                // ParserOutput::getLinks() is a 2D array of page links, so
+                // to be really correct we would need to recurse in the array
+                // but the main array should only have items in it if there are
+                // links.
+                $hasLinks = (bool)count( $editInfo->output->getLinks() );
+            } else {
+                $hasLinks = (bool)wfGetDB( DB_SLAVE )->selectField( 
'pagelinks', 1,
+                    array( 'pl_from' => $this->getId() ), __METHOD__ );
+            }
+        }
+
+               return $content->isCountable( $hasLinks );
        }
 
        /**


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to