Revision [2905]
This is an old revision of IncludeRemote made by DarTar on 2004-12-07 10:18:55.
Fetching Remote Wikka Content
Last edited by DarTar:
FetchRemote action v.0.6 - download link + better pattern for forced links
Tue, 07 Dec 2004 10:18 UTC [diff]
FetchRemote action v.0.6 - download link + better pattern for forced links
Tue, 07 Dec 2004 10:18 UTC [diff]
FetchRemote v.0.6 available for testing
Download the source and save it as:
actions/fetchremote.php
Feedback is welcome!
Download the source and save it as:
actions/fetchremote.php
Feedback is welcome!
FetchRemote Action
Version 0.6
What it does
- Connects to the main Wikka server and fetches Wikka Documentation Pages.
A "raw" handler must be available on the main Wikka server, in order to produce raw wikka-formatted content with header and footer stripped.
- Displays an error message if remote pages do not exist on the server or if a connection is not available.
- Parses the fetched page and rewrites internal links as links to fetchable pages.
- Prints the fetched page locally, together with a header.
- Allows fetched pages to be safely stored on the Wikka client.
- If a page with the same name already exists on the Wikka client, a "see local version" button instead of the "download" button is displayed.
How to use it
Simply add {{fetchremote}} in one of your pages.You can specify a starting page by adding: {{fetchremote page="HomePage"}}
Notes
- Basically, the idea is to make the main Wikka site work as a server providing wikka-formatted content to Wikka-clients. There are several advantages in this approach, compared to merely fetching HTML:
- the fetched content integrates seamlessly with the layout and structure of the Wikka-client;
- the user can choose to download locally a fetched page, so as to make it available in its Wikka site.
- No MySQL connection to the central database is needed, provided that a method exists for retrieving pure page content with the header and footer stripped;
- Remote fetching of pages through fopen() must be allowed by php (by default it is).
Todo:
- Forced link rewriting: done - check regex for consistency with wikka formatters.
- CamelCase link rewriting: done - check regex for consistency with wikka formatters.
- Interwiki link rewriting
The code (actions/fetchremote.php)
<?php
/**
* Connects to a specified Wikka server, fetches a remote page and formats it for local use.
*
* This action allows the user to locally browse in a Wikka client content fetched from a
* remote Wikka server. It displays an error message if the remote page does not exist
* on the server or if a connection is not available.
* Once a connection is established, the fetched page is parsed for internal links, which
* are rewritten as links to fetchable pages, and printed on the screen.
* Fetched pages can then be safely stored on the Wikka client. If a local version
* of a fetched page is available, a "see local version" button replaces the default
* "download" button.
*
* A "raw" method must be available on the main Wikka server, in order to
* produce raw wikka-formatted content with header and footer stripped.
*
* @package Actions
* @name FetchRemote
*
* @author {@link http://wikka.jsnx.com/DarTar DarTar}
* @version 0.6
* @since Wikka 1.1.X
*
* @input stringĀ $pageĀ optional: Starting page on the main Wikka server;
* default: HelpInfo
* can be overridden by a $_REQUEST["page"] parameter.
* @output prints fetched documentation pages
*
* @todo -Forced link rewriting: check regex for consistency with wikka formatters.
* -CamelCase link rewriting: check regex for consistency with wikka formatters.
* -Interwiki link rewriting.
*/
// SET DEFAULTS
$remote_server_root = "http://wikka.jsnx.com/"; # set remote server root
$defaultpage = 'HelpInfo'; # define default page to be fetched
if (isset($page)) $defaultpage = $page; # pick up action parameter
if (isset($_REQUEST["page"])) $defaultpage = $_REQUEST["page"]; # pick up URL parameter
$page = $defaultpage; # ready to roll
// PERFORM REDIRECTIONS
// redirect to main documentation page
if ($_POST["action"] == "Return to Wikka Documentation") $this->Redirect($this->GetPageTag());
// redirect to Wikka homepage on disconnection
if ($_POST["action"] == "Disconnect") $this->Redirect($this->GetConfigValue("root_page"));
// switch to local version of the page
if ($_POST["action"] == "See local version") $this->Redirect($page);
// automatically redirect to local page if it exists
// NOTE: the use of this feature is discouraged since it traps users 'locally'
// and prevents them from accessing recently updated versions of the Wikka documentation
//if ($this->LoadPage($page)) $this->Redirect($page);
// SET HEADER & FORM ELEMENTS
// header style
// to be replaced by a CSS selector in the definitive version
$style = "text-align: center; margin: 30px 25%; border: 1px dotted #333; background-color: #EEE; padding: 5px;";
// build form chunks
$form_local = "<input type='submit' name='action' value='See local version' />";
$form_main = "<input type='submit' name='action' value='Return to Wikka Documentation' />";
$form_disconnect = "<input type='submit' name='action' value='Disconnect' />";
$form_page = "<input type='hidden' name='page' value='".$page."' />";
$form_download = "<input type='submit' name='action' value='Download this page' />";
// TRY TO CONNECT
$remote_page = fopen($remote_server_root.$page."/raw", "r");
if (!$remote_page) {
// NO CONNECTION AVAILABLE
echo $this->Format("=====Wikka Documentation===== --- Visit the **[[http://wikka.jsnx.com/HelpInfo Wikka Documentation Project]]** --- --- ");
// if a local version of the starting page is available:
if ($this->LoadPage($page)) print $this->FormOpen().$form_local.$this->FormClose();
} else {
// CONNECTION ESTABLISHED
// fetch raw content of remote page
while (!feof($remote_page)) {
$content .= fgets($remote_page, 1024);
}
if (!$content) {
// missing or empty page: show error message
$header = "Sorry, **";
$header .= "\"\"<a href='".$this->Href("","","page=".$page)."'>".$page."</a>\"\"";
$header .= "** cannot be found on the [[".$remote_server_root.$page." Wikka server]]! --- --- ";
$form = $this->FormOpen().$form_page;
$form .= ($this->LoadPage($page))? $form_local : "";
$form .= $form_main.$this->FormClose();
} else {
// START LINK-REWRITING ENGINE
// regex pattern for forced links to *internal pages* on remote server
//$forced = "/\[\[([^ \/]+) ([^\]]+)\]\]/"; #original
//$forced = "/\[\[(\S*)(\s+(.+))?\]\]/"; #JW's suggestion
//$forced = "/\[\[([^\s\/]+)(\s+([^\]]+))?\]\]/"; # patched
$forced = "/\[\[([^\s\/]+)(\s+(.+))??\]\]/"; # this is clean and working
// regex pattern for CamelCase links to *internal pages* on remote server
$camel = "/[^a-z=>\|\"\[\/\{]([A-Z]+[a-z]+[A-Z][A-Za-z0-9]+)+/";
// rewrite forced links
//$content = preg_replace($forced, '""<a href="'.$this->Href('','',"page=\\1").'">'."\\2".'</a>""', $content);
// build callback function for marking CamelCase words within forced links
function MarkCamel($fmatches) {
global $wakka;
$camel = "/([A-Z]+[a-z]+[A-Z][A-Za-z0-9]+)+/";
$markedcamel = preg_replace($camel, "|||\\1|||", $fmatches[2]);
// 1) This is working
$output = '""<a href="'.$fmatches[1].'">'.$markedcamel.'</a>""';
// 2) This is NOT working
// How do I call a global function from within a callback function?
$output = '""<a href="'.$wakka->Href('','',"page=".$fmatches[1]).'">'.$markedcamel.'</a>""';
return $output;
}
// rewrite forced links
$content = preg_replace_callback($forced, 'MarkCamel', $content);
// rewrite camelcase links
$content = preg_replace($camel, ' ""<a href="'.$this->Href('','',"page=\\1").'">'."\\1".'</a>""', $content);
//strip "|||" markers from content
$content = str_replace("|||", "", $content);
if ($_POST["action"] == "Download this page") {
// SAVING FETCHED PAGE
if ($this->LoadPage($page)) {
// local page with this name already exists => display error message
// in the future we might show a form to ask if the local version should be overwritten
$header = "Sorry, a page named **[[".$page."]]** already exists on this site! --- ";
$form = $this->FormOpen().$form_main.$form_disconnect.$this->FormClose();
} else {
// local page does not exist => proceed
// write page to database and display message
$note = "fetched from the Wikka server";
$this->SavePage($page, $content, $note);
$header = "This page is now available on your site! --- --- ";
$form = $this->FormOpen().$form_page.$form_local.$form_main.$this->FormClose();
}
} else {
// display default header & form
$header = "You are currently browsing: **";
$header .= "\"\"<a href='".$this->Href("","","page=".$page)."'>".$page."</a>\"\"";
$header .= "** --- from the **[[".$this->GetPageTag()." Wikka Documentation Project]]** --- ";
$header .= "(fetched from the [[".$remote_server_root.$page." Wikka server]])";
$form = $this->FormOpen().$form_page;
$form .= ($this->LoadPage($page))? $form_local : $form_download;
$form .= $form_disconnect.$this->FormClose();
}
}
// PRINT HEADER AND CONTENT
print "<div style='".$style."'>".$this->Format($header).$form."</div>".$this->Format($content);
}
// CLOSE CONNECTION
fclose($remote_page);
?>
/**
* Connects to a specified Wikka server, fetches a remote page and formats it for local use.
*
* This action allows the user to locally browse in a Wikka client content fetched from a
* remote Wikka server. It displays an error message if the remote page does not exist
* on the server or if a connection is not available.
* Once a connection is established, the fetched page is parsed for internal links, which
* are rewritten as links to fetchable pages, and printed on the screen.
* Fetched pages can then be safely stored on the Wikka client. If a local version
* of a fetched page is available, a "see local version" button replaces the default
* "download" button.
*
* A "raw" method must be available on the main Wikka server, in order to
* produce raw wikka-formatted content with header and footer stripped.
*
* @package Actions
* @name FetchRemote
*
* @author {@link http://wikka.jsnx.com/DarTar DarTar}
* @version 0.6
* @since Wikka 1.1.X
*
* @input stringĀ $pageĀ optional: Starting page on the main Wikka server;
* default: HelpInfo
* can be overridden by a $_REQUEST["page"] parameter.
* @output prints fetched documentation pages
*
* @todo -Forced link rewriting: check regex for consistency with wikka formatters.
* -CamelCase link rewriting: check regex for consistency with wikka formatters.
* -Interwiki link rewriting.
*/
// SET DEFAULTS
$remote_server_root = "http://wikka.jsnx.com/"; # set remote server root
$defaultpage = 'HelpInfo'; # define default page to be fetched
if (isset($page)) $defaultpage = $page; # pick up action parameter
if (isset($_REQUEST["page"])) $defaultpage = $_REQUEST["page"]; # pick up URL parameter
$page = $defaultpage; # ready to roll
// PERFORM REDIRECTIONS
// redirect to main documentation page
if ($_POST["action"] == "Return to Wikka Documentation") $this->Redirect($this->GetPageTag());
// redirect to Wikka homepage on disconnection
if ($_POST["action"] == "Disconnect") $this->Redirect($this->GetConfigValue("root_page"));
// switch to local version of the page
if ($_POST["action"] == "See local version") $this->Redirect($page);
// automatically redirect to local page if it exists
// NOTE: the use of this feature is discouraged since it traps users 'locally'
// and prevents them from accessing recently updated versions of the Wikka documentation
//if ($this->LoadPage($page)) $this->Redirect($page);
// SET HEADER & FORM ELEMENTS
// header style
// to be replaced by a CSS selector in the definitive version
$style = "text-align: center; margin: 30px 25%; border: 1px dotted #333; background-color: #EEE; padding: 5px;";
// build form chunks
$form_local = "<input type='submit' name='action' value='See local version' />";
$form_main = "<input type='submit' name='action' value='Return to Wikka Documentation' />";
$form_disconnect = "<input type='submit' name='action' value='Disconnect' />";
$form_page = "<input type='hidden' name='page' value='".$page."' />";
$form_download = "<input type='submit' name='action' value='Download this page' />";
// TRY TO CONNECT
$remote_page = fopen($remote_server_root.$page."/raw", "r");
if (!$remote_page) {
// NO CONNECTION AVAILABLE
echo $this->Format("=====Wikka Documentation===== --- Visit the **[[http://wikka.jsnx.com/HelpInfo Wikka Documentation Project]]** --- --- ");
// if a local version of the starting page is available:
if ($this->LoadPage($page)) print $this->FormOpen().$form_local.$this->FormClose();
} else {
// CONNECTION ESTABLISHED
// fetch raw content of remote page
while (!feof($remote_page)) {
$content .= fgets($remote_page, 1024);
}
if (!$content) {
// missing or empty page: show error message
$header = "Sorry, **";
$header .= "\"\"<a href='".$this->Href("","","page=".$page)."'>".$page."</a>\"\"";
$header .= "** cannot be found on the [[".$remote_server_root.$page." Wikka server]]! --- --- ";
$form = $this->FormOpen().$form_page;
$form .= ($this->LoadPage($page))? $form_local : "";
$form .= $form_main.$this->FormClose();
} else {
// START LINK-REWRITING ENGINE
// regex pattern for forced links to *internal pages* on remote server
//$forced = "/\[\[([^ \/]+) ([^\]]+)\]\]/"; #original
//$forced = "/\[\[(\S*)(\s+(.+))?\]\]/"; #JW's suggestion
//$forced = "/\[\[([^\s\/]+)(\s+([^\]]+))?\]\]/"; # patched
$forced = "/\[\[([^\s\/]+)(\s+(.+))??\]\]/"; # this is clean and working
// regex pattern for CamelCase links to *internal pages* on remote server
$camel = "/[^a-z=>\|\"\[\/\{]([A-Z]+[a-z]+[A-Z][A-Za-z0-9]+)+/";
// rewrite forced links
//$content = preg_replace($forced, '""<a href="'.$this->Href('','',"page=\\1").'">'."\\2".'</a>""', $content);
// build callback function for marking CamelCase words within forced links
function MarkCamel($fmatches) {
global $wakka;
$camel = "/([A-Z]+[a-z]+[A-Z][A-Za-z0-9]+)+/";
$markedcamel = preg_replace($camel, "|||\\1|||", $fmatches[2]);
// 1) This is working
$output = '""<a href="'.$fmatches[1].'">'.$markedcamel.'</a>""';
// 2) This is NOT working
// How do I call a global function from within a callback function?
$output = '""<a href="'.$wakka->Href('','',"page=".$fmatches[1]).'">'.$markedcamel.'</a>""';
return $output;
}
// rewrite forced links
$content = preg_replace_callback($forced, 'MarkCamel', $content);
// rewrite camelcase links
$content = preg_replace($camel, ' ""<a href="'.$this->Href('','',"page=\\1").'">'."\\1".'</a>""', $content);
//strip "|||" markers from content
$content = str_replace("|||", "", $content);
if ($_POST["action"] == "Download this page") {
// SAVING FETCHED PAGE
if ($this->LoadPage($page)) {
// local page with this name already exists => display error message
// in the future we might show a form to ask if the local version should be overwritten
$header = "Sorry, a page named **[[".$page."]]** already exists on this site! --- ";
$form = $this->FormOpen().$form_main.$form_disconnect.$this->FormClose();
} else {
// local page does not exist => proceed
// write page to database and display message
$note = "fetched from the Wikka server";
$this->SavePage($page, $content, $note);
$header = "This page is now available on your site! --- --- ";
$form = $this->FormOpen().$form_page.$form_local.$form_main.$this->FormClose();
}
} else {
// display default header & form
$header = "You are currently browsing: **";
$header .= "\"\"<a href='".$this->Href("","","page=".$page)."'>".$page."</a>\"\"";
$header .= "** --- from the **[[".$this->GetPageTag()." Wikka Documentation Project]]** --- ";
$header .= "(fetched from the [[".$remote_server_root.$page." Wikka server]])";
$form = $this->FormOpen().$form_page;
$form .= ($this->LoadPage($page))? $form_local : $form_download;
$form .= $form_disconnect.$this->FormClose();
}
}
// PRINT HEADER AND CONTENT
print "<div style='".$style."'>".$this->Format($header).$form."</div>".$this->Format($content);
}
// CLOSE CONNECTION
fclose($remote_page);
?>
-- DarTar
CategoryDevelopment