Topic on Project:Support desk

How to access a Pages Source Text ?

4
95.91.210.42 (talkcontribs)

I tried Article->getContent() but this is depraced since 1.21 and refers to WikiPage::getContent(). This function cannot be parsed with Parser::parse beacuse this Function needs a Text. When I check the Object (WikiPage::getContent()) I get an Property mText but this is private and not accesible. How can I get the Source Text of a Page or Article to use it in PHP Code ?

Thank's a lot

Cheers

Dirk

95.91.210.42 (talkcontribs)

I fixed it with a Hack but I'm pretty sure that there must be a official way.

function accessProtected($obj, $prop) {

 // THe Hack to be able to access a Pages Text because getText and getTextRaw are deprecated
 $reflection = new ReflectionClass($obj);
 $property = $reflection->getProperty($prop);
 $property->setAccessible(true);
 return $property->getValue($obj);

} function wfAddSidebarTree( $out, $skin ) {

 $title = Title::newFromText( 'SidebarTree', NS_MEDIAWIKI );
 $wikipage = WikiPage::newFromId( $title->getArticleID() );
 $content = accessProtected($wikipage->getContent(), 'mText');
 print_r($content);
 $html = $out->parse( $content );

$out->addHTML( "

$html

" );

 return true;

}

TheDJ (talkcontribs)

A page/article doesn't have content, it has revisions. The revisions have content.

WikiPage::getRevision () -> getContent()

also note that a revision is using a content model. It can be wiki text, javascript, json or many other things. In articles it's usually wiki text, but you might have to account for when it is not.

This post was hidden by AhmadF.Cheema (history)
Reply to "How to access a Pages Source Text ?"