MediaWiki/archive/embedding

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search

Languages other than PHP

This is easiest to do if the other application is also written in PHP, but it is possible to do this using other languages as well. Perl has a library, PHP::Interpreter, that allows it to execute PHP code; other languages may have similar capabilities. If they do not, then one can still accomplish a certain amount by loading MediaWiki pages using http and the "?action=render" directive, which displays the page's bare contents without any of the navigation aids.

Embedding within PHP

Research has uncovered two ways to do this. One is less elegant (MediaWiki tells PHP to generate unnecessary http headers, and the resulting warning messages are suppressed), but is included anyway in case parts of it turn out to be useful elsewhere. Both of these methods have only been tested with MediaWiki 1.7.1.

The Slightly More Elegant Method

  • Create the file extract.php using these contents
  • Add the following code wherever you want to include an extract:
define('kPathToMediaWiki','/path/to/your/MediaWiki/installation/');
define('kEmbeddedPagePrefix','prefix for all your embedded page titles');
$page = 'name_of_page_to_display';
include 'extract.php';

"kEmbeddedPagePrefix" can be empty, but the idea is that you might want to avoid conflicts between the names of your embedded pages and the names of regular pages in your wiki (if you're using the wiki as a regular wiki too). It all depends on how you are using the embedded pages.

name_of_page_to_display must use the canonical URL pagename format with underlines for spaces and all URL-safe characters.

I need to work out the code for detecting if a given page exists in the wiki, so the main application can display default values other than the wiki's default ("This page doesn't exist yet..."). Detecting this with the method below may or may not be possible; if MediaWiki returns a 404 code for non-existent articles, then that could be used, but I don't yet know if it does. --Woozle 18:52, 15 October 2006 (EDT)

The Slightly Less Elegant Method

  • Create the file extract.php from the following contents, and place it in your MediaWiki root folder (i.e. the folder where the main wiki's index.php is):
<?php

// Adapted from extract2.php.html by Aero for use with SluggySquad site
// Cleaned up slightly for htyp.org by Woozle

$pathtowiki = '/path/to/main/MediaWiki/folder';

header("Content-Type: text/html; charset=utf-8");
define( "MEDIAWIKI", true );
require_once($pathtowiki."/includes/Defines.php" );
require_once($pathtowiki."/LocalSettings.php");
require_once($pathtowiki."/includes/Setup.php");

function getWikiPage($pageName)
{
  global $wgRequest,$wgOut;
  $useportal = $wgRequest->getText( 'title', $pageName );
  $wgTitle = Title::newFromText( $useportal );
  $wgArticle = new Article( $wgTitle );
  $mainText = $wgOut->parse( $wgArticle->getContent( false ) );
  return $mainText;
}

?>

  • The following code displays text from the wiki page whose name (in URL format, with underlines for spaces) is passed to getWikiPage:
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
require_once('../wiki.sluggysquad.com/extract.php');
echo str_replace("/index.php/","http://your.wiki's.base.url/",getWikiPage('WWW_Wiki_Intro'));

The str_replace will need some tweaking depending on the URL form your wiki uses. This example assumes http://your.wiki.domain/index.php/Page_title

Remaining Issues

  • We don't yet know how to get MediaWiki to emit fully-qualified URLs; it is currently necessary to do a search-and-replace to fix links targeting the wiki.