Difference between revisions of "Extension:PublicCat.php"

From Organic Design wiki
m
(update InCat)
Line 6: Line 6:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('PUBLICCAT_VERSION','1.0.6, 2008-05-20');
+
define('PUBLICCAT_VERSION','1.0.7, 2008-10-28');
  
 
# Name of category in which to place articles to make them viewable from the public site
 
# Name of category in which to place articles to make them viewable from the public site
Line 20: Line 20:
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:PublicCat',
 
'url'        => 'http://www.mediawiki.org/wiki/Extension:PublicCat',
 
'version'    => PUBLICCAT_VERSION
 
'version'    => PUBLICCAT_VERSION
);
+
);
  
 
# Hook in after article and title prepared
 
# Hook in after article and title prepared
Line 46: Line 46:
 
if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw' && $_REQUEST['action'] != 'render')
 
if ($_REQUEST['action'] != 'view' && $_REQUEST['action'] != 'raw' && $_REQUEST['action'] != 'render')
 
$_REQUEST['action'] = 'view';
 
$_REQUEST['action'] = 'view';
}
+
}
  
 
# Function to check if passed a title is in a category
 
# Function to check if passed a title is in a category
function wfPublicCatInCat(&$title,$cat) {
+
function inCat($title, $cat) {
if (!is_object($title)) $title = Title::newFromText($title);
+
if (!is_object($title)) $title = Title::newFromText($title));
if (!is_object($title)) return false;
+
$id = $title->getArticleID()
$id     = $title->getArticleID();
+
$dbr = &wfGetDB(DB_SLAVE);
$db      = &wfGetDB(DB_SLAVE);
+
$cat  = $dbr->addQuotes($cat);
$cl     = $db->tableName('categorylinks');
+
$cl = $db->tableName('categorylinks');
$result  = $db->query("SELECT 0 FROM $cl WHERE cl_from = $id AND cl_to = '$cat'");
+
return $dbr->selectRow($cl, '0', "cl_from = $id AND cl_to = $cat", __METHOD__);
if ($result instanceof ResultWrapper) $result = $result->result;
+
}
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
 
# Return a 404 not found error if request is public but requested title is not in the public cat
Line 66: Line 64:
 
wfHttpError(404,"404 Not Found","The requested URL was not found on this server!");
 
wfHttpError(404,"404 Not Found","The requested URL was not found on this server!");
 
return true;
 
return true;
}
+
}

Revision as of 21:36, 27 October 2008

<?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:Nad

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

define('PUBLICCAT_VERSION','1.0.7, 2008-10-28');

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

if (!isset($wgPublicCat)) $wgPublicCat = 'Public';

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

if (!isset($wgPublicCatPrivatePattern)) $wgPublicCatPrivatePattern = '/^wiki\\./';

$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. Hook in after article and title prepared
  2. - should be able to hook in earlier than this,
  3. - but note that it must be a hook that always executes, even for specialpages

$wgHooks['OutputPageBeforeHTML'][] = 'wfPublicCatValidate';

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

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

  1. Disable all actions except view for anonymous users

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

  1. Allow unrestricted access to login and css's

$wgWhitelistRead = array('Special:Userlogin','-'); if (eregi('\\.css$',$_REQUEST['title'])) $wgWhitelistRead[] = $_REQUEST['title'];

  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 check if passed a title is in a category

function inCat($title, $cat) { if (!is_object($title)) $title = Title::newFromText($title)); $id = $title->getArticleID() $dbr = &wfGetDB(DB_SLAVE); $cat = $dbr->addQuotes($cat); $cl = $db->tableName('categorylinks'); return $dbr->selectRow($cl, '0', "cl_from = $id AND cl_to = $cat", __METHOD__); }

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

function wfPublicCatValidate() { global $wgPublicCat,$wgPublicCatRequestIsPublic,$wgTitle; if ($wgPublicCatRequestIsPublic && !wfPublicCatInCat($wgTitle,$wgPublicCat)) wfHttpError(404,"404 Not Found","The requested URL was not found on this server!"); return true; }