r114185 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114184‎ | r114185 | r114186 >
Date:21:09, 19 March 2012
Author:daniel
Status:deferred
Tags:
Comment:
isCountable()
Modified paths:
  • /branches/Wikidata/phase3/includes/Content.php (modified) (history)
  • /branches/Wikidata/phase3/includes/Revision.php (modified) (history)
  • /branches/Wikidata/phase3/includes/WikiPage.php (modified) (history)

Diff [purge]

Index: branches/Wikidata/phase3/includes/Content.php
@@ -27,14 +27,30 @@
2828 */
2929 public abstract function getNativeData( );
3030
31 - public abstract function getSize( );
 31+ /**
 32+ * returns the content's nominal size in bogo-bytes.
 33+ */
 34+ public abstract function getSize( ); #XXX: do we really need/want this here? we could just use the byte syse of the serialized form...
3235
 36+ /**
 37+ * Returns true if this content is countable as a "real" wiki page, provided
 38+ * that it's also in a countable location (e.g. a current revision in the main namespace).
 39+ *
 40+ * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
 41+ * to avoid redundant parsing to find out.
 42+ */
 43+ public abstract function isCountable( $hasLinks = null ) ;
 44+
3345 public abstract function getParserOutput( Title $title = null, $revId = null, ParserOptions $options = NULL );
3446
3547 public function getRedirectChain() {
3648 return null;
3749 }
3850
 51+ public function isRedirect() {
 52+ return false;
 53+ }
 54+
3955 /**
4056 * Returns the section with the given id.
4157 *
@@ -62,11 +78,8 @@
6379 #TODO: implement specialized ParserOutput for Wikidata model
6480 #TODO: provide "combined" ParserOutput for Multipart... somehow.
6581
66 - # TODO: Wikipage::isCountable(Content $a)
 82+ # XXX: isCacheable( ) # can/should we do this here?
6783
68 - # TODO: isCacheable( )
69 - # TODO: getSize( )
70 -
7184 # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
7285 # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags )
7386
@@ -94,6 +107,34 @@
95108 }
96109
97110 /**
 111+ * returns the content's nominal size in bogo-bytes.
 112+ */
 113+ public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
 114+ $text = $this->getNativeData( );
 115+ return strlen( $text );
 116+ }
 117+
 118+ /**
 119+ * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
 120+ *
 121+ * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
 122+ * to avoid redundant parsing to find out.
 123+ */
 124+ public function isCountable( $hasLinks = null ) {
 125+ global $wgArticleCountMethod;
 126+
 127+ if ( $this->isRedirect( ) ) {
 128+ return false;
 129+ }
 130+
 131+ if ( $wgArticleCountMethod === 'any' ) {
 132+ return true;
 133+ }
 134+
 135+ return false;
 136+ }
 137+
 138+ /**
98139 * Returns the text represented by this Content object, as a string.
99140 *
100141 * @return String the raw text
@@ -245,6 +286,46 @@
246287 return Title::newFromRedirectArray( $text );
247288 }
248289
 290+ public function isRedirect() {
 291+ $text = $this->getNativeData();
 292+ return Title::newFromRedirect( $text ) !== null;
 293+ }
 294+
 295+ /**
 296+ * Returns true if this content is not a redirect, and this content's text is countable according to
 297+ * the criteria defiend by $wgArticleCountMethod.
 298+ *
 299+ * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
 300+ * to avoid redundant parsing to find out.
 301+ */
 302+ public function isCountable( $hasLinks = null ) {
 303+ global $wgArticleCountMethod;
 304+
 305+ if ( $this->isRedirect( ) ) {
 306+ return false;
 307+ }
 308+
 309+ $text = $this->getNativeData();
 310+
 311+ switch ( $wgArticleCountMethod ) {
 312+ case 'any':
 313+ return true;
 314+ case 'comma':
 315+ if ( $text === false ) {
 316+ $text = $this->getRawText();
 317+ }
 318+ return strpos( $text, ',' ) !== false;
 319+ case 'link':
 320+ if ( $hasLinks === null ) { # not know, find out
 321+ $po = $this->getParserOutput();
 322+ $links = $po->getLinks();
 323+ $hasLinks = !empty( $links );
 324+ }
 325+
 326+ return $hasLinks;
 327+ }
 328+ }
 329+
249330 }
250331
251332 class MessageContent extends TextContent {
Index: branches/Wikidata/phase3/includes/Revision.php
@@ -496,7 +496,8 @@
497497 $this->mCurrent = false;
498498 # If we still have no length, see it we have the text to figure it out
499499 if ( !$this->mSize ) {
500 - $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText ); #FIXME: do strlen in Content object
 500+ #XXX: my be inconsistent with the notion of "size" use for the present content model
 501+ $this->mSize = is_null( $this->mText ) ? null : strlen( $this->mText );
501502 }
502503 # Same for sha1
503504 if ( $this->mSha1 === null ) {
Index: branches/Wikidata/phase3/includes/WikiPage.php
@@ -300,15 +300,10 @@
301301 * @return bool
302302 */
303303 public function isRedirect( $text = false ) {
304 - if ( $text === false ) {
305 - if ( !$this->mDataLoaded ) {
306 - $this->loadPageData();
307 - }
 304+ if ( $text === false ) $content = $this->getContent();
 305+ else $content = ContentHandler::makeContent( $text, $this->mTitle ); # TODO: allow model and format to be provided; or better, expect a Content object
308306
309 - return (bool)$this->mIsRedirect;
310 - } else {
311 - return Title::newFromRedirect( $text ) !== null;
312 - }
 307+ return $content->isRedirect();
313308 }
314309
315310 /**
@@ -391,7 +386,7 @@
392387 * Revision::FOR_PUBLIC to be displayed to all users
393388 * Revision::FOR_THIS_USER to be displayed to $wgUser
394389 * Revision::RAW get the text regardless of permissions
395 - * @return String|null The content of the current revision
 390+ * @return Content|null The content of the current revision
396391 */
397392 public function getContent( $audience = Revision::FOR_PUBLIC ) {
398393 $this->loadLastEdit();
@@ -549,39 +544,42 @@
550545 * if false, the current database state will be used
551546 * @return Boolean
552547 */
553 - public function isCountable( $editInfo = false ) { #FIXME: move this to Content object
 548+ public function isCountable( $editInfo = false ) {
554549 global $wgArticleCountMethod;
555550
556551 if ( !$this->mTitle->isContentPage() ) {
557552 return false;
558553 }
559554
560 - $text = $editInfo ? $editInfo->pst : false;
 555+ if ( $editInfo ) {
 556+ $content = ContentHandler::makeContent( $editInfo->pst, $this->mTitle );
 557+ # TODO: take model and format from edit info!
 558+ } else {
 559+ $content = $this->getContent();
 560+ }
561561
562 - if ( $this->isRedirect( $text ) ) {
 562+ if ( $content->isRedirect( ) ) {
563563 return false;
564564 }
565565
566 - switch ( $wgArticleCountMethod ) {
567 - case 'any':
568 - return true;
569 - case 'comma':
570 - if ( $text === false ) {
571 - $text = $this->getRawText();
572 - }
573 - return strpos( $text, ',' ) !== false;
574 - case 'link':
575 - if ( $editInfo ) {
576 - // ParserOutput::getLinks() is a 2D array of page links, so
577 - // to be really correct we would need to recurse in the array
578 - // but the main array should only have items in it if there are
579 - // links.
580 - return (bool)count( $editInfo->output->getLinks() );
581 - } else {
582 - return (bool)wfGetDB( DB_SLAVE )->selectField( 'pagelinks', 1,
583 - array( 'pl_from' => $this->getId() ), __METHOD__ );
584 - }
585 - }
 566+ $hasLinks = null;
 567+
 568+ if ( $wgArticleCountMethod === 'link' ) {
 569+ # nasty special case to avoid re-parsing to detect links
 570+
 571+ if ( $editInfo ) {
 572+ // ParserOutput::getLinks() is a 2D array of page links, so
 573+ // to be really correct we would need to recurse in the array
 574+ // but the main array should only have items in it if there are
 575+ // links.
 576+ $hasLinks = (bool)count( $editInfo->output->getLinks() );
 577+ } else {
 578+ $hasLinks = (bool)wfGetDB( DB_SLAVE )->selectField( 'pagelinks', 1,
 579+ array( 'pl_from' => $this->getId() ), __METHOD__ );
 580+ }
 581+ }
 582+
 583+ return $content->isCountable( $hasLinks );
586584 }
587585
588586 /**

Status & tagging log