How to display page content with an extension?
It looks like your main issue is that you don't have a return statement in your efSampleRender function. Without a return statement, the function will generate data, but not put any of it in the page. The other thing I notice is $wgAPIModules['sample'] = 'ApiSample'; . That's only needed if you want to make a new api module. If you just plan to use an existing API moudle, you don't need a line like that.
Additionally, it was probably really bad advice on my part to suggest the parse api module. It didn't click in my head at the time, but that would call the parser recursively, which is a bad thing. (Basically, mediawiki can only parse one thing at a time, when your extension is running, its already parsing the current page, so it can't parse anything else, and calling api parse module would cause it to parse something else, which is bad (GoogleNewsSitemap extension didn't have that problem since it runs not while parsing another page). Generally what happens when the parser is called recursively is that mysterious markers like UNIQ3858f81c6798e00f-sample-00000009-QINU appear in the page. You can still use other api modules in your extension safely, just not the parse one (or any other module that involves parsing things)
Sine you just want to include a page, and are not interested in the actual html of the page, you might consider the $parser->recursiveTagParse method (Which does not count as calling the parser recursively). It takes wikitext and returns html (ish anyways). So you could just use normal template inclusion syntax. (Alternatively if you want to process the wikitext first, you could get the text from either the API, or something like Article::fetchContent(), process the text, and stick it in recursiveTagParse)
For example, if you wanted to include a random page from some category (for example, if you wanted <sample>Foo</sample> return a random entry from category:Foo, your efSampleRender would look something like:
//Assume the boilerplate you had in your post is here. function efSampleRender( $input, $args, $parser, $frame ) { // $input is the input to the tag, $args is its "attributes" // So if you had <sample baz="bar">foo</sample> // $input == "foo"; $args == array( 'baz' => 'bar' ); // $parser is an instance of the MediaWiki parser, $frame is related to what template // args are the page was called with, and generally isn't too useful // This turns the input in to a title object, which does some normalization (like first letter capitalized, etc) // Second argument says that if no other namespace specified, assume category. $category = Title::newFromText( $input, NS_CATEGORY ); if ( !$category ) return '<span class="error">Invalid category given</span>'; // This gets a reference to the database. DB_SLAVE means get a read-only connection $dbr = wfGetDB( DB_SLAVE ); $page = $dbr->selectField( // selectField means get just a single item (instead of a whole bunch of stuff) 'categorylinks', // This means get it from the categorylinks table, which stores what page is in what category 'cl_from', // this means get the value of cl_from, which stores the page id of a page in some category array( 'cl_to' => $category->getDBKey() ), // get the name of the category for purposes of the db __METHOD__, // This isn't important, just for profiling purposes array( 'ORDER BY' => 'rand()') // This means to pick a random entry in the category instead of the "first" one. ); // $page is now a number, which is the article id of the article we want. Now to convert it into a name $title = Title::newFromID( $page ); // If $page not valid id. For example, if no pages in given category (or if there is stray entry in db, which would be kind of odd) if (!$title) return '<span class="error">Could not retrieve any pages</span>'; // getFullText means get entire page name, including namespace. $page = $title->getFullText(); // Include this page standard template way. Leading colon is needed to denote not template namespace by defualt. // The return part actually puts this new page into the page. return $parser->recursiveTagParse( '{{:' . $page . '}}', $frame ); }
The database stuff can be a little complicated, especially when you are new to it, and in some ways it might be easier to use the API for that step. If your curious about the db stuff though, there's docs at Manual:Database access [1] [2] and Manual:Categorylinks table ). If you're familiar with sql, the example query I gave above is equivalent to SELECT /* */ cl_from FROM `categorylinks` WHERE cl_to = 'Foo' ORDER BY rand() LIMIT 1
OH MY GOD I LOVE YOU!!!!!!!!!!! :D :D :D :D
I've been trying to figure this out on and off since SUMMER!! And you... AAAAAAAAAAAA :D :D You're freaking amazing!!
I had tried calling the database before but I only got the category titles, so I figured maybe I should try the API instead. But this works beautifully! Now the only thing I'll need to do is make it so that the random page will change daily or monthly (depending on how it's set) instead of every time the page is refreshed :)
Thankyouthankyouthankyouthankyouthankyou!!!!!!!!!!