Jump to content

Displaying content from a wiki


Darim Design
 Share

Recommended Posts

Hey, I want to display a certain part of a wiki on my site, a certain div id. To me it sounds simple enough to make, but searching google and such doesn't always help. I've been into sourcing, web fetching, screen scraping and what else.. without luck. I just want something really simple, something that puts the text on my site and updates it, instead of me copy+pasting each time someone edits the wiki. So if someone has a simple script I can use I would be really grateful! :)

Link to comment
Share on other sites

You can use PHP

<?php$page = file_get_contents("wiki_page.php");preg_match("/<div(?:.*?)id=\"theid\"(?:.*?)>(.*?)<\/div>/is", $page, $content);$wanted_bit = $content[1];echo $wanted_bit;?>

You'll need to name your page with the .php extension and your host has to support PHP for it to work.

Link to comment
Share on other sites

A less error prone alternative (which requires PHP5!!!) is to use DOM, like so:

<?php$wikiDom = new DOMDocument;$wikiDom->resolveExternals = false; /* Prevents a DTD check. Vital to performance. */$wikiDom->load('wiki_page.php'); /* Your Wiki page doesn't need to be ".php", and doesn't need to be on the same server even, but your host does need to support PHP5. If the page is external, the host must also set the allow_url_fopen to "1" if they haven't already. */$wiki = new DOMXPath($wikiDom);$theDiv = $wiki->query('//div[@id="theid"]')->item(0);/* "theid" is the ID of the div element. */$wantBit = new DOMDocument;$wantBit->importNode($theDiv);unset($theDiv, $wiki, $wikiDom);/* Optional, but it's a good thing to do if you want to do more than just output the result. */echo $wantBit->saveXML(); /* If you want to further change the $wantBit, use DOM further instead of using saveXML(). */?>

Link to comment
Share on other sites

Thank you aswell boen_robot. Tried both. The DOM version didn't work, so I guess the host doesn't support PHP5 or something. First one does work nicely though. But I've come over another problem.. the wiki doesn't seem to put the content into a div id :) I can source different boxes containing links, the header etc etc, but not the wiki text itself. This is how the source code looks:

					<div id="jump-to-nav">Jump to: <a href="#column-one">navigation</a>, <a href="#searchInput">search</a></div>			<!-- start content -->			<a name="The_City-state_of_Tronac."></a><h2><span class="editsection">[<a href="/index.php?title=City-State_of_Tronac&action=edit&section=1" title="Edit section: The City-state of Tronac.">edit</a>]</span> <span class="mw-headline"> <b>The City-state of Tronac.</b> </span></h2>

And after that bit of code the content starts in paragraphs (<p>?) and brackets. Any ideas? :)

Link to comment
Share on other sites

If the Wiki software is a bit old or isn't so good and doesn't output well-formed XHTML Boen Robot's approach won't work.For my approach, there needs to be some sort of unique identifier for the division you want, even if its a class or a certain title string. What page are you looking at?

Link to comment
Share on other sites

Basically ever page on the wiki. Let me give you abit more info about my little project. I'm working on http://www.hyboria.darimq-design.com, it's abit slow to load but I'm working on that. When you click on a country a box pops up. Right now, I am the one who has to edit/copy+paste the info there. I want to source that info from http://www.aquilonia.eu. I've gotten permission to do so. If you go to http://eleytheria.darimq-design.com/tests/...ple-iframe.html and click on 'content in iframe' you can see how I plan to have my boxes later. That iframe now sources the whole aquilonia.eu main page.So far I see that sourcing specific data directly into the iframe won't work or will be hard, so I plan to make a html or php page for every country there is and then have iframe source the data from those whole pages. Hope this makes it easier to understand.

Link to comment
Share on other sites

Well, lets say the page titles are the country names and that you only want the content. You can do this:country.php (excluding structural HTML)

<?php$page = file_get_contents("http://wiki.aquilonia.eu/index.php/{$_GET['country']}");preg_match("/<!-- start content -->(.*?)<!--(.*?)NewPP limit report/is", $page, $content);$wanted_bit = $content[1];echo $wanted_bit;?>

And then, say you wanted "America", you can load country.php?country=America.

Link to comment
Share on other sites

Wow, thanks mate! That works perfectly :)http://eleytheria.darimq-design.com/tests/TEST.phpThe {$_GET['country']} doesn't work though, had to do ...uilonia.eu/index.php/country"); The GET thing doesn't change anything.Another, very tiny, problem though, it doesn't display special characters correctly: That's becomes that’s and so on. If you know how to fix that I would be ever grateful. Thank you a thousand times for the help! :)

Link to comment
Share on other sites

The {$_GET['country']} doesn't work though,
Did you use the GET in conjunction with the querystring, e.g.http://eleytheria.darimq-design.com/tests/TEST.php?country=Tronac?
Another, very tiny, problem though, it doesn't display special characters correctly: That's becomes that’s and so on. If you know how to fix that I would be ever grateful.
Hmm that's weird - for some reason its ignoring the charset header. Try saving the PHP page itself as UTF-8 (e.g. in Notepad in the Save As dialog change the Encoding option right down the bottom to "UTF-8").
Link to comment
Share on other sites

If the Wiki software is a bit old or isn't so good and doesn't output well-formed XHTML Boen Robot's approach won't work.
I thought even the first Wikis (that appeared like what... two years ago?) output well formed XHTML too...Anyway, if that was the problem, a simple solution is to use "loadHTMLFile()" in place of "load()". For this however, not having PHP5 does seem to be the reason.@Darim Design, if you want to check out your PHP version, create a new PHP file with
<?php phpinfo() ?>

and look at the top. If it's at least 5.0.0, then try to use loadHTMLFile(). If it's 4.*.*, there's no point in trying. Synook's way is the only way.By the way, I don't see a BOM (the ""). Not in IE at least. I see it in Firefox, but if you inspect your page's headers (with a tool like Firebug for example), you'll see it's still served as

text/html; charset=ISO-8859-1

At the very top of your PHP, add

header('Content-type: text/html;charset=utf-8');

and this shall be cured for all browsers.

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><?php$page = file_get_contents("http://wiki.aquilonia.eu/index.php/City-State_of_Tronac");preg_match("/<!-- start content -->(.*?)<!--(.*?)NewPP limit report/is", $page, $content);$wanted_bit = $content[1];echo $wanted_bit;?></body></html>

That's the full code (without your code added).

<?phpheader('Content-type: text/html;charset=utf-8');$page = file_get_contents("http://wiki.aquilonia.eu/index.php/City-State_of_Tronac");preg_match("/<!-- start content -->(.*?)<!--(.*?)NewPP limit report/is", $page, $content);$wanted_bit = $content[1];echo $wanted_bit;?>

That gives me a warning and it doesn't fix the problem:Warning: Cannot modify header information - headers already sent by (output started at /home/darimq-design/web/eleytheria.darimq-design.com/tests/TEST.php:8) in /home/darimq-design/web/eleytheria.darimq-design.com/tests/TEST.php on line 9I think I've put it in the wrong place, where should it be?

Link to comment
Share on other sites

Headers have to be sent before anything else is written:

<?php header('Content-type: text/html;charset=utf-8'); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Link to comment
Share on other sites

Aha, thank you Ingolme. Now everything works :) And then just a quick last question if any of you drop by again, what's the best image filetype with transparent background? I need good quality and quick enough load times.http://hyboria.darimq-design.com/# the quality on the highlights (only on Vanaheim and Asgard so far) is really bad. Used gif.

Link to comment
Share on other sites

PNG.It's the only format supporting alpha transparancy (i.e. partial transparancy), and it also supports 16bit colors and is often smaller than GIF too.GIF is sometimes smaller, but supports only up to 256 colors and only supports binary transparancy (i.e. a pixel can be either completely transparent or not transparent at all).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...