Index: branches/Wikidata/phase3/includes/Content.php |
— | — | @@ -27,14 +27,30 @@ |
28 | 28 | */ |
29 | 29 | public abstract function getNativeData( ); |
30 | 30 | |
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... |
32 | 35 | |
| 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 | + |
33 | 45 | public abstract function getParserOutput( Title $title = null, $revId = null, ParserOptions $options = NULL ); |
34 | 46 | |
35 | 47 | public function getRedirectChain() { |
36 | 48 | return null; |
37 | 49 | } |
38 | 50 | |
| 51 | + public function isRedirect() { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
39 | 55 | /** |
40 | 56 | * Returns the section with the given id. |
41 | 57 | * |
— | — | @@ -62,11 +78,8 @@ |
63 | 79 | #TODO: implement specialized ParserOutput for Wikidata model |
64 | 80 | #TODO: provide "combined" ParserOutput for Multipart... somehow. |
65 | 81 | |
66 | | - # TODO: Wikipage::isCountable(Content $a) |
| 82 | + # XXX: isCacheable( ) # can/should we do this here? |
67 | 83 | |
68 | | - # TODO: isCacheable( ) |
69 | | - # TODO: getSize( ) |
70 | | - |
71 | 84 | # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null ) |
72 | 85 | # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags ) |
73 | 86 | |
— | — | @@ -94,6 +107,34 @@ |
95 | 108 | } |
96 | 109 | |
97 | 110 | /** |
| 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 | + /** |
98 | 139 | * Returns the text represented by this Content object, as a string. |
99 | 140 | * |
100 | 141 | * @return String the raw text |
— | — | @@ -245,6 +286,46 @@ |
246 | 287 | return Title::newFromRedirectArray( $text ); |
247 | 288 | } |
248 | 289 | |
| 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 | + |
249 | 330 | } |
250 | 331 | |
251 | 332 | class MessageContent extends TextContent { |
Index: branches/Wikidata/phase3/includes/Revision.php |
— | — | @@ -496,7 +496,8 @@ |
497 | 497 | $this->mCurrent = false; |
498 | 498 | # If we still have no length, see it we have the text to figure it out |
499 | 499 | 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 ); |
501 | 502 | } |
502 | 503 | # Same for sha1 |
503 | 504 | if ( $this->mSha1 === null ) { |
Index: branches/Wikidata/phase3/includes/WikiPage.php |
— | — | @@ -300,15 +300,10 @@ |
301 | 301 | * @return bool |
302 | 302 | */ |
303 | 303 | 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 |
308 | 306 | |
309 | | - return (bool)$this->mIsRedirect; |
310 | | - } else { |
311 | | - return Title::newFromRedirect( $text ) !== null; |
312 | | - } |
| 307 | + return $content->isRedirect(); |
313 | 308 | } |
314 | 309 | |
315 | 310 | /** |
— | — | @@ -391,7 +386,7 @@ |
392 | 387 | * Revision::FOR_PUBLIC to be displayed to all users |
393 | 388 | * Revision::FOR_THIS_USER to be displayed to $wgUser |
394 | 389 | * 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 |
396 | 391 | */ |
397 | 392 | public function getContent( $audience = Revision::FOR_PUBLIC ) { |
398 | 393 | $this->loadLastEdit(); |
— | — | @@ -549,39 +544,42 @@ |
550 | 545 | * if false, the current database state will be used |
551 | 546 | * @return Boolean |
552 | 547 | */ |
553 | | - public function isCountable( $editInfo = false ) { #FIXME: move this to Content object |
| 548 | + public function isCountable( $editInfo = false ) { |
554 | 549 | global $wgArticleCountMethod; |
555 | 550 | |
556 | 551 | if ( !$this->mTitle->isContentPage() ) { |
557 | 552 | return false; |
558 | 553 | } |
559 | 554 | |
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 | + } |
561 | 561 | |
562 | | - if ( $this->isRedirect( $text ) ) { |
| 562 | + if ( $content->isRedirect( ) ) { |
563 | 563 | return false; |
564 | 564 | } |
565 | 565 | |
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 ); |
586 | 584 | } |
587 | 585 | |
588 | 586 | /** |