Difference between revisions of "Extension:PublicCat.php"

From Organic Design wiki
(not ready yet)
(ready for debugging)
Line 8: Line 8:
 
define('PUBLICCAT_VERSION','0.0.0, 2007-06-16');
 
define('PUBLICCAT_VERSION','0.0.0, 2007-06-16');
  
$wgExtensionFunctions[]        = 'wfSetupPublicCat';
+
# Name of category in which to place articles to make them viewable from the public site
$wgHooks['LanguageGetMagic'][] = 'wfPublicCatLanguageGetMagic';
+
$wgPublicCat = 'Public';
  
# Anonymous users
+
# Pattern to match with domain to determine if client accessing private wiki or public website
$wgGroupPermissions['*']['createaccount']   = false;
+
$wgPublicCatPrivatePattern = '/^wiki\\./';
$wgGroupPermissions['*']['read']           = false;
+
 
$wgGroupPermissions['*']['edit']           = false;
+
# Disable all actions except view for anonymous users
$wgGroupPermissions['*']['createpage']     = false;
+
$wgGroupPermissions['*']['createaccount'] = false;
$wgGroupPermissions['*']['createtalk']     = false;
+
$wgGroupPermissions['*']['read']         = true;
 +
$wgGroupPermissions['*']['edit']         = false;
 +
$wgGroupPermissions['*']['createpage']   = false;
 +
$wgGroupPermissions['*']['createtalk']   = false;
 +
 
 +
# Allow unrestricted access to login, home and main css
 
$wgWhitelistRead = array('Special:Userlogin','Main Page','-');
 
$wgWhitelistRead = array('Special:Userlogin','Main Page','-');
 +
 +
$wgExtensionFunctions[] = 'wfSetupPublicCat';
  
 
$wgExtensionCredits['other'][] = array(
 
$wgExtensionCredits['other'][] = array(
Line 27: Line 34:
 
);
 
);
  
if (!ereg('^wiki.',$_SERVER['HTTP_HOST'])) {
+
# Determine if the request is private or public from the domain and pattern
if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw') $_REQUEST['action'] = 'view';
+
$wgPublicCatRequestIsPrivate = preg_match($wgPublicCatPrivatePattern,$_SERVER['HTTP_HOST']);
}
+
$wgPublicCatRequestIsPublic  = !$wgPublicCatRequestIsPrivate;
  
# Called from $wgExtensionFunctions array when initialising extensions
+
# If request is public, restrict action to view, raw or render
function wfSetupPublicCat() {
+
if ($wgPublicCatRequestIsPublic) {
 +
if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw' && $_REQUEST['action'] != 'render')
 +
$_REQUEST['action'] = 'view';
 
}
 
}
  
function wf404() {
+
# Function to die returing a 404 not found error
 +
function wfPublicCat404() {
 
header('HTTP/1.0 404 Not Found');
 
header('HTTP/1.0 404 Not Found');
 
$err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</t$
 
$err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</t$
Line 42: Line 52:
 
echo $err;
 
echo $err;
 
die;
 
die;
 +
}
 +
 +
# Function to check if passed a title is in a category
 +
function wfPublicCatInCat($title,$cat) {
 +
if (!is_object($title)) $title = Title::newFromText($title));
 +
$id      = $title->getArticleID()
 +
$db      = &wfGetDB(DB_SLAVE);
 +
$cl      = $db->tableName('categorylinks');
 +
$result  = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'");
 +
return is_array(mysql_fetch_row($result));
 +
}
 +
 +
# Return a 404 not found error if request is public but requested title is not in the public cat
 +
function wfSetupPublicCat() {
 +
global $wgPublicCatPattern,$wgPublicCatRequestIsPublic;
 +
if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($_REQUEST['title'],$wgPublicCatPattern) wf404();
 
}
 
}
  
 
?>
 
?>

Revision as of 00:54, 16 June 2007

<?php

  1. Extension:PublicCat
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:NadCategory:Extensions created with Template:Extension

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('PUBLICCAT_VERSION','0.0.0, 2007-06-16');

  1. Name of category in which to place articles to make them viewable from the public site

$wgPublicCat = 'Public';

  1. Pattern to match with domain to determine if client accessing private wiki or public website

$wgPublicCatPrivatePattern = '/^wiki\\./';

  1. Disable all actions except view for anonymous users

$wgGroupPermissions['*']['createaccount'] = false; $wgGroupPermissions['*']['read'] = true; $wgGroupPermissions['*']['edit'] = false; $wgGroupPermissions['*']['createpage'] = false; $wgGroupPermissions['*']['createtalk'] = false;

  1. Allow unrestricted access to login, home and main css

$wgWhitelistRead = array('Special:Userlogin','Main Page','-');

$wgExtensionFunctions[] = 'wfSetupPublicCat';

$wgExtensionCredits['other'][] = array( 'name' => 'PublicCat', 'author' => 'User:Nad', 'description' => 'Divides wiki into private and public by domain name, but allows unrestricted access to articles in a public category.', 'url' => 'http://www.mediawiki.org/wiki/Extension:PublicCat', 'version' => PUBLICCAT_VERSION );

  1. Determine if the request is private or public from the domain and pattern

$wgPublicCatRequestIsPrivate = preg_match($wgPublicCatPrivatePattern,$_SERVER['HTTP_HOST']); $wgPublicCatRequestIsPublic = !$wgPublicCatRequestIsPrivate;

  1. If request is public, restrict action to view, raw or render

if ($wgPublicCatRequestIsPublic) { if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw' && $_REQUEST['action'] != 'render') $_REQUEST['action'] = 'view'; }

  1. Function to die returing a 404 not found error

function wfPublicCat404() { header('HTTP/1.0 404 Not Found'); $err = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</t$

<body>

Not Found

The requested URL was not found on this server.

</body>$

if (in_array('Content-Encoding: gzip',headers_list())) $err = gzencode($err); echo $err; die; }

  1. Function to check if passed a title is in a category

function wfPublicCatInCat($title,$cat) { if (!is_object($title)) $title = Title::newFromText($title)); $id = $title->getArticleID() $db = &wfGetDB(DB_SLAVE); $cl = $db->tableName('categorylinks'); $result = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'"); return is_array(mysql_fetch_row($result)); }

  1. Return a 404 not found error if request is public but requested title is not in the public cat

function wfSetupPublicCat() { global $wgPublicCatPattern,$wgPublicCatRequestIsPublic; if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($_REQUEST['title'],$wgPublicCatPattern) wf404(); }

?>