[Home]Wikipedia PHP script/The alt main script

HomePage | Wikipedia PHP script | Recent Changes | Preferences

The script below is wrapped with "pre" tags for display (remove them to use the script). Save it as wiki.php in a directory to get started.

see also /The mysql sql ddl and /The wiki stylesheet


<?
###########  based on script by Magnus Manske
#              just a bit easier to set up on _your_ system
#              and just as GPL'ed as ever (see http://gnu.org/gpl)
####################################################################### Constant declarations


$db_user="mysqluser" ;
$db_passwd="mysqlpasswd" ;
$db_server="127.0.0.1" ;
$db_name="my_sql_db" ;

$wiki_user="WIKI_user" ;
$wiki_cur="WIKI_cur" ;
$wiki_old="WIKI_old" ;

$wiki_php="wiki.php" ;
$wiki_upload="upload.php" ;
$wiki_title="PHP Wiki" ;


function getSecureTitle ( $s ) {
	$s=str_replace(" ","_",$s);
	$s=strtoupper(substr($s,0,1)).substr($s,1);
	return $s ;
	}

function getDBconnection () {
	$connection=mysql_connect ( $GLOBALS['db_server'] , $GLOBALS['db_user'] , $GLOBALS['db_passwd'] ) ;
	return $connection ;
	}

####################################################################### USER FUNCTIONS

function getCurrentUserName () {
	global $USERNAME , $USERPASSWORD , $USERLOGGEDIN ;
	global $REMOTE_ADDR ;
	if ( $USERLOGGEDIN == "YES" ) return $USERNAME ;
	else return $REMOTE_ADDR ;
	}

function doesUserExist ( $un ) {
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "SELECT * FROM ". $GLOBALS['wiki_user'] . " WHERE user_name=\"$un\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	if ( $s = mysql_fetch_object ( $result ) ) $ret = true ;
	else $ret = false ;
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $ret ;
	}

function getUserSetting ( $un , $s ) {
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "SELECT * FROM " . $GLOBALS['wiki_user'] . " WHERE user_name=\"$un\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	$t = mysql_fetch_object ( $result ) ;
	$ret = $t->$s ;
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $ret ;
	}

function changeUserSetting ( $un , $s , $v ) {
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "UPDATE " . $GLOBALS['wiki_user'] . " SET $s = \"$v\" WHERE user_name = \"$un\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	mysql_close ( $connection ) ;
	}

function checkUserPassword ( $un , $up ) {
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "SELECT * FROM " . $GLOBALS['wiki_user'] . " WHERE user_name=\"$un\" AND user_password=\"$up\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	if ( $s = mysql_fetch_object ( $result ) ) {
		setcookie ( "USERID" , "$s->user_id" ) ;
		$ret = true ;
		}
	else $ret = false ;
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $ret ;
	}

function addNewUser ( $un , $up , $ur ) {
	if ( doesUserExist ( $un ) ) return ;
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "INSERT INTO " . $GLOBALS['wiki_user'] . " (user_name, user_password, user_rights) VALUES (\"$un\", \"$up\", \"$ur\")" ;
	$result = mysql_query ( $sql , $connection ) ;

	$sql = "SELECT * FROM " . $GLOBALS['wiki_user'] . " WHERE user_name=\"$un\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	setcookie ( "USERNAME" , "$s->user_name" ) ;
	setcookie ( "USERPASSWORD" , "$s->user_password" ) ;
	setcookie ( "USERID" , "$s->user_id" ) ;
	setcookie ( "USERLOGGEDIN" , "YES" ) ;
	mysql_free_result ( $result ) ;

	mysql_close ( $connection ) ;
	}

####################################################################### ARTICLE DATABASE INTERFACE

function acquireTopic ( $s ) {
	global $title ;
	$s=getSecureTitle($s);
	$s=strtolower($s);
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "select * from " . $GLOBALS['wiki_cur'] . " where cur_title='$s'" ;
	$result = mysql_query ( $sql , $connection ) ;
	if ( $s = mysql_fetch_object ( $result ) ) {
		$title=$s->cur_title ;
		$s = $s->cur_text ;
		}
	else {
		$s = "" ;
		}
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $s ;
	}

function acquireOldTopic ( $s , $id ) {
	global $title ;
	$s=getSecureTitle($s);
	$s=strtolower($s);
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "select * from " . $GLOBALS['wiki_old'] . " where old_title='$title' and old_id=$id" ;
	$result = mysql_query ( $sql , $connection ) ;
	if ( $s = mysql_fetch_object ( $result ) ) {
		$title=$s->old_title ;
		$s = $s->old_text ;
		}
	else {
		$s = "nothing available" ;
		}
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $s ;
	}

function saveTopic ( $txt , $com , $min ) {
	global $title ;
	global $USERLOGGEDIN , $USERID ;
	$s=getSecureTitle($title);
	$s=strtolower($s);
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$txt = str_replace ( "\r" , "" , $txt ) ;

	$sql = "update " . $GLOBALS['wiki_cur'] . " set cur_text='$txt' where cur_title='$title'" ;
	$result = mysql_query ( $sql , $connection ) ;

	$sql = "update " . $GLOBALS['wiki_cur'] . " set cur_comment='$com' where cur_title='$title'" ;
	$result = mysql_query ( $sql , $connection ) ;

	$sql = "update " . $GLOBALS['wiki_cur'] . " set cur_minor_edit=1 where cur_title='$title'" ;
	if ( $min == "on" ) $result = mysql_query ( $sql , $connection ) ;

	$id = $USERID ;
	if ( $id == "" or $USERLOGGEDIN != "YES" ) $id = "0" ;
	$sql = "update " . $GLOBALS['wiki_cur'] . " set cur_user='$id' where cur_title='$title'" ;
	$result = mysql_query ( $sql , $connection ) ;

	$un = getCurrentUserName () ;
	$sql = "update " . $GLOBALS['wiki_cur'] . " set cur_user_text='$un' where cur_title='$title'" ;
	$result = mysql_query ( $sql , $connection ) ;

	mysql_close ( $connection ) ;
	}

function addPlainTopic ( $t ) {
	global $title ;
	$s=getSecureTitle($title);
	$s=strtolower($s);
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;

	$sql = "insert into " . $GLOBALS['wiki_cur'] . " ( cur_title, cur_text ) VALUES ( '$t' , '' )" ;
	$result = mysql_query ( $sql , $connection ) ;

	mysql_close ( $connection ) ;
	}

function backupTopic ( $t ) {
	global $title ;
	$s=getSecureTitle($title);
	$s=strtolower($s);
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;

	# Reading current version
	$sql = "select * from " . $GLOBALS['wiki_cur'] . " where cur_title='$t'" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;

	$o_title = $s->cur_title ;
	$o_text = $s->cur_text ;
	$o_comment = $s->cur_comment ;
	$o_user = $s->cur_user ;
	$o_user_text = $s->cur_user_text ;
	$o_old_version = $s->cur_old_version ;
	$o_timestamp = $s->cur_timestamp ;
	$o_minor_edit = $s->cur_minor_edit ;

	$o_text = str_replace ( '"' , '\"' , $o_text ) ;

	mysql_free_result ( $result ) ;

	# Adding data to "old" table
	$sql = "insert into " . $GLOBALS['wiki_old'] . " ( old_title, old_text , old_comment , old_user, old_user_text , old_old_version , old_timestamp , old_minor_edit ) VALUES ( \"$o_title\" , \"$o_text\" , \"$o_comment\" , \"$o_user\" , \"$o_user_text\" , \"$o_old_version\" , \"$o_timestamp\" , \"$o_minor_edit\" )" ;
	$result = mysql_query ( $sql , $connection ) ;

	# Get old id
	$sql = "select * from " . $GLOBALS['wiki_old'] . " where old_title='$o_title' and old_old_version='$o_old_version'" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$n_old_version = $s->old_id ;
	mysql_free_result ( $result ) ;

	# Update current version
	$sql = "update " . $GLOBALS['wiki_cur'] . " set cur_old_version='$n_old_version' where cur_title='$title'" ;
	$result = mysql_query ( $sql , $connection ) ;	

	mysql_close ( $connection ) ;
	}

function doesTopicExist ( $s ) {
	$s=getSecureTitle($s);
	$s=strtolower($s);
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "select * from " . $GLOBALS['wiki_cur'] . " where cur_title=\"$s\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	if ( $s = mysql_fetch_object ( $result ) ) $ret = true ;
	else $ret = false ;
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $s ;
	}

function getTopicSetting ( $tt , $s ) {
	$tt = getSecureTitle ( $tt ) ;
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "SELECT * FROM " . $GLOBALS['wiki_cur'] . " WHERE cur_title=\"$tt\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	if ( $t = mysql_fetch_object ( $result ) ) $ret = $t->$s ;
	else $ret = "NOSUCHTHING" ; # This topic or property doesn't exist
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $ret ;
	}

function changeTopicSetting ( $tt , $s , $v ) {
	$secureTitle = getSecureTItle ( $tt ) ;
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "UPDATE " . $GLOBALS['wiki_cur'] . " SET $s = \"$v\" WHERE cur_title = \"$secureTitle\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	mysql_close ( $connection ) ;
	}


####################################################################### PARSER FUNCTIONS

function replaceAllEntries ( $s , $f1 , $f2 , $r1 , $r2 ) {
	while ( eregi($f1,$s) && eregi($f2,$s) ) {
		$pieces1=spliti($f1,$s,2);
		$pieces2=spliti($f2,$pieces1[1],2);
		$middle=$pieces2[0] ;
		$s=$pieces1[0].$r1.$middle.$r2.$pieces2[1];
		}
	return $s ;
	}

# DISPLAY PARSER ; INCOMPLETE!!!!
function parseContent ( $s ) {
	global $title ;
	$s = str_replace ( "\r" , "" , $s ) ;
	if ( !strpos ( $title , "/" ) and !strpos ( $s , "/Talk" ) ) $s .= "\n----\n[[/Talk]]" ;

	# Replace {{{variable}}}
	$var=date("m"); $s = str_replace ( "{{{CURRENTMONTH}}}" , $var , $s ) ;
	$var=date("F"); $s = str_replace ( "{{{CURRENTMONTHNAME}}}" , $var , $s ) ;
	$var=date("d"); $s = str_replace ( "{{{CURRENTDAY}}}" , $var , $s ) ;
	$var=date("l"); $s = str_replace ( "{{{CURRENTDAYNAME}}}" , $var , $s ) ;
	$var=date("Y"); $s = str_replace ( "{{{CURRENTYEAR}}}" , $var , $s ) ;

	# Replace [[ and ]] with internal links
	$tag1="\[\[";
	$tag2="\]\]";
	while ( eregi($tag1,$s) && eregi($tag2,$s) ) {
		$pieces1=spliti($tag1,$s,2);
		$pieces2=spliti($tag2,$pieces1[1],2);
		$middle=$pieces2[0] ;
		$original = $middle ;
		$linkto=getSecureTitle($middle);

		if ( strstr ( $middle , "|" ) ) { # show left part, link to right part
			$pos = strpos ( $middle , "|" ) ;
			$linkto = trim ( substr ( $middle , 0 , $pos ) ) ;
			$middle = trim ( substr ( $middle , $pos+1 , 9999 ) ) ;
			}

		if ( substr($linkto,0,1)=="/" ) $linkto = $title.$linkto ;

		if ( substr_count ( $linkto , "/" ) < 2 ) {		
			if ( doesTopicExist($linkto) ) $middle="<a href=\"" . $GLOBALS['wiki_php'] . "?title=$linkto&action=view\">$middle</a>" ;
			else {
				if ( strstr($middle," ") ) $middle="[$middle]" ;
				$middle="$middle<a href=\"" . $GLOBALS['wiki_php'] . "?title=$linkto&action=edit\">?</a>" ;
				}
			} else $middle = "$original" ;
		$s=$pieces1[0].$middle.$pieces2[1];
		}


	# Replace '''
	$s = replaceAllEntries ( $s , "\'\'\'" , "\'\'\'" , "<b>" , "</b>" ) ;

	# Replace ''
	$s = replaceAllEntries ( $s , "\'\'" , "\'\'" , "<i>" , "</i>" ) ;

	# Replace *
	$s = replaceAllEntries ( $s , "\n\*" , "\n" , "<ul><li>" , "</li></ul>\n" ) ;
	$s = replaceAllEntries ( $s , "<ul><li>\*" , "</li></ul>" , "<ul><li><ul><li>" , "</li></ul></li></ul>\n" ) ;
	$s = str_replace ( "</ul>\n" , "</ul>" , $s ) ;
	while ( strstr ( $s , "</li></ul><ul><li>" ) or strstr ( $s , "</li><li><ul>" ) ) {
		$s = str_replace ( "</li></ul><ul><li>" , "</li><li>" , $s ) ;
		$s = str_replace ( "</li><li><ul>" , "<ul>" , $s ) ;
		}


	# Replace #
	$s = replaceAllEntries ( $s , "\n\#" , "\n" , "<ol><li>" , "</li></ol>\n" ) ;
	$s = replaceAllEntries ( $s , "<ol><li>\#" , "</li></ol>" , "<ol><li><ol><li>" , "</li></ol></li></ol>\n" ) ;
	$s = str_replace ( "</ol>\n" , "</ol>" , $s ) ;
	while ( strstr ( $s , "</li></ol><ol><li>" ) or strstr ( $s , "</li><li><ol>" ) ) {
		$s = str_replace ( "</li></ol><ol><li>" , "</li><li>" , $s ) ;
		$s = str_replace ( "</li><li><ol>" , "<ol>" , $s ) ;
		}

	# Courier
	$s = replaceAllEntries ( $s , "\n " , "\n" , "\n <font face=\"courier\">" , "</font>\n" ) ;


	# Line by line
	$arr = explode ( "\n" , $s ) ;
	$narr = array () ;

	$dp = false ;
	foreach ( $arr as $x ) {
		$y = $x ;
		if ( substr ( $y , 0 , 4 ) == "http" ) $y = "<a href=\"$y\">$y</a>" ;
		if ( substr ( $y , 0 , 1 ) == ":" ) {
			$y = "<dt><dd>".substr ( $y , 1 , 99999 ) ;
			if ( !$dp ) $y = "<DL>".$y ;
			$dp = true ;
		} else if ( $dp ) {
			$y .= "</DL>" ;
			$dp = false ;
			}
		if ( substr ( $y , 0 , 4 ) == "----" ) $y = "<hr>" ;
		if ( substr ( $y , 0 , 4 ) == "<hr>" ) $footnote = 1 ;

		# Outside links
		$footnote = 1 ;
		$tag1="\[http://";
		$tag2="\]";
		while ( eregi($tag1,$y) && eregi($tag2,$y) ) {
			$pieces1=spliti($tag1,$y,2);
			$pieces2=spliti($tag2,$pieces1[1],2);
			$linkto=trim($pieces2[0]) ;

			if ( strpos ( $linkto , " " ) ) {
				$middle = substr ( $linkto , strpos ( $linkto , " " ) + 1 , 99999 ) ;
				$linkto = substr ( $linkto , 0 , strpos ( $linkto , " " ) ) ;
			} else {
				$middle = $footnote ;
				$footnote++ ;
				}

			$y=$pieces1[0]."<a href=\"http://$linkto\">[$middle]</a>".$pieces2[1];
			}


		if ( $y == "" ) $y = "</p><p>" ;
		array_push ( $narr , $y ) ;
		}

	$s = implode ( "\n" , $narr ) ;
	
	# Final
	$s = "<p>$s</p>" ;
	$s = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">".$s ;

	return $s ;
	}

function getCurrentUserText () {
	global $USERNAME , $USERPASSWORD , $USERLOGGEDIN ;
	global $REMOTE_ADDR ;
#	if ( $USERLOGGEDIN != "YES" and $USERNAME != "" and $USERPASSWORD != "" ) {
#		if ( checkUserPassword ( $USERNAME , $USERPASSWORD ) ) setcookie ( "USERLOGGEDIN" , "YES" ) ;
#		$USERLOGGEDIN = "YES" ;
#		}
	if ( $USERLOGGEDIN != "YES" or $USERNAME == "" ) {
		$u = "$REMOTE_ADDR<br>\n<a href=\"" . $GLOBALS['wiki_php'] . "?action=login\">log in</a>" ;
		}
	else {
		$u = "$USERNAME<br>\n<a href=\"" . $GLOBALS['wiki_php'] . "?action=logout\">log out</a>" ;
		$u .= " <a href=\"" . $GLOBALS['wiki_php'] . "?action=prefs\">Preferences</a>" ;
		}
	return $u ;
	}

########### RIGHTS MANAGEMENT

function canEdit( $tt ) {
	global $USERNAME , $USERLOGGEDIN , $action ;
	$restrictions = getTopicSetting ( $tt , "cur_restrictions" ) ;
	if ( $restrictions == "" ) return true ; # No restrictions, OK to edit for everyone
	if ( $restrictions == "NOSUCHTHING" ) {
		$stt = strtolower ( $tt ) ;
		if ( $stt == "recentchanges" ) return false ;
		if ( $action == "revisions" ) return false ;
		if ( $action == "statistics" ) return false ;
		if ( $action == "restrictions" ) return false ;
		if ( $action == "prefs" ) return false ;
		return true ; # New topic
		}
	if ( $USERLOGGEDIN != "YES" ) return false ; # Restrictions, but not logged in -> No edit, bad dog!
	$resArr = explode ( "," , $restrictions ) ;
	$rights = ",".getUserSetting ( $USERNAME , "user_rights" )."," ;

	$allowed = false ;
	foreach ( $resArr as $x ) {
		$y = ",is_$x," ;
		if ( strstr ( $rights , $y ) ) $allowed = true ;
		}
	return $allowed ;
	}

function canRestrict ( $tt ) {
	global $USERNAME , $USERLOGGEDIN , $dosearch ;
	if ( $USERLOGGEDIN != "YES" ) return false ; # Not logged in
	if ( $dosearch == 1 ) return false ; # Search page
	if ( !doesTopicExist ( $tt ) ) return false ; # No such topic
	$rights = ",".getUserSetting ( $USERNAME , "user_rights" )."," ;
	$allowed = false ;
	if ( strstr ( $rights , ",is_editor," ) ) $allowed = true ;
	if ( strstr ( $rights , ",is_sysop," ) ) $allowed = true ;
	return $allowed ;
	}


#######################################
# OUTPUT PROCEDURES
#######################################

function getHeaderFooterParts () {
	global $title , $action , $oid ;
	global $USERNAME ;

	$secureTitle = getSecureTitle ( $title ) ;
	$ret = "" ;
	
	$special = false ;
	if ( $title == "recentchanges" ) $special = true ;
	if ( $action == "revisions" or $action == "statistics" or $action == "restrictions" ) $special = true ;
	if ( $action == "prefs" or $action == "edituserrights" ) $special = true ;

	$ret .= "<a href=\"" . $GLOBALS['wiki_php'] . "?title=MainPage&action=view\">Main page</a> | " ;
	$ret .= "<a href=\"" . $GLOBALS['wiki_php'] . "?title=recentchanges&action=view\">Recent changes</a>" ;
	if ( !$special ) $ret .= " | <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=revisions\">Other versions</a>" ;
	if ( !$special and strstr ( $title , "/" ) ) {
		$parent = substr($title , 0 , strrpos($title,"/")) ;
		$sparent = getSecureTitle ( $parent ) ;
		$ret .= " | <a href=\"" . $GLOBALS['wiki_php'] . "?title=$sparent&action=view\">$parent</a>" ;
		}

	if ( $action == "view" and !$special and canEdit($title) ) $ret .= " | <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=edit\">Edit this page</a>" ;
	if ( $action == "view_old_article" ) $ret .= " | <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=view_old_source&oid=$oid\">View this source</a>" ;
	if ( $action == "view_old_source" ) $ret .= " | <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=view_old_article&oid=$oid\">View this article</a>" ;
	$ret .= " | <a href=\"./" . $GLOBALS['wiki_upload'] . "\" target=\"_blank\">Upload files</a>" ;
	if ( $action != "statistics" ) $ret .= " | <a href=\"" . $GLOBALS['wiki_php'] . "?action=statistics\">Statistics</a>" ;

	if ( !$special and canRestrict($title) ) $ret .= " | <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=restrictions\">Change restrictions</a>" ;

	return $ret ;
	}

function getStandardHeader () {
	global $title , $action , $oid ;
	global $USERNAME ;
	
	$special = false ;
	if ( $title == "recentchanges" ) $special = true ;
	if ( $action == "revisions" or $action == "statistics" or $action == "restrictions" ) $special = true ;
	if ( $action == "prefs" or $action == "edituserrights" ) $special = true ;

	$secureTitle = getSecureTitle ( $title ) ;
	$hversion = "" ;
	if ( $action == "view_old_article" or $action == "view_old_source" ) $hversion = " (Older version)" ;

	$userName = getCurrentUserText () ;

	$hpre = "<table width=\"100%\"><tr><td><h1>" ;
	$hpost = "</h1></td><td align=right><p class=\"user\">User : $userName</p></td></tr></table>" ;

	if ( $action == "view" or $action == "view_old_article" or $action == "view_old_source" or $special ) {
		if ( $title == "recentchanges" ) $thebody = "Recent Changes" ;
		else if ( $action == "revisions" ) $thebody = "History of $title" ;
		else if ( $action == "statistics" ) $thebody = "Statistics (".date("l, F d, Y H:i:s").", PST)" ;
		else if ( $action == "edituserrights" ) $thebody = "Edit user access rights here" ;
		else if ( $action == "restrictions" ) $thebody = "Restrictions of $title" ;
		else if ( $action == "prefs" ) $thebody = "Preferences for $USERNAME" ;
		else $thebody = "<a href=\"" . $GLOBALS['wiki_php'] . "?$action=search&search=$secureTitle&dosearch=1\">$title</a>$hversion" ;
		$head = $hpre.$thebody.$hpost ;
	} else if ( $action == "edit" or $action == "preview" ) {
		$head = $hpre."Editing $title".$hpost ;
		}

	$head .= getHeaderFooterParts() ;
	$head .= "<hr>" ;
	return $head ;
	}

function getStandardFooter () {
	$ret = "<FORM><hr>" ;
	$ret .= getHeaderFooterParts () ;
	$ret .= "<br>Search: <INPUT TYPE=text NAME=search SIZE=20><INPUT TYPE=hidden NAME=dosearch VALUE=1></FORM>" ;
	return $ret ;
	}

######## APPLY RESTRICTIONS TO AN ARTICLE
function restrictions () {
	global $title , $therestrictions ;
	$secureTitle = getSecureTitle ( $title ) ;
	if ( !canRestrict ( $title ) ) return "You are not allowed to restrict this article. Follow <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle\">this link</a> to go back." ;

	if ( isset ( $therestrictions ) ) {
		changeTopicSetting ( $title , "cur_restrictions" , $therestrictions ) ;
		$ret="<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=" . $GLOBALS['wiki_php'] . "?title=$title&action=view\">" ;
		unset ( $therestrictions ) ;
	} else {
		$ret = "" ;
		$ret .= getStandardHeader () ;
		$r = getTopicSetting ( $title , "cur_restrictions" ) ;
		$ret .= "<FORM action=\"" . $GLOBALS['wiki_php'] . "?title=$title&action=restrictions\" method=post>\n" ;
		$ret .= "Restrictions : <INPUT TABINDEX=1 TYPE=text NAME=therestrictions VALUE=\"$r\" SIZE=80><br>\n" ;
		$ret .= "<INPUT TYPE=SUBMIT NAME=changeprefs value=\"Save new restrictions\">\n" ;
		$ret .= "</FORM>\n" ;
		}

	return $ret ;
	}

############################################
# BASIC FUNCTIONS
############################################

function view () {
	global $title , $action ;

	$content = acquireTopic ( $title ) ;
	$content = parseContent ( $content ) ;

	$secureTitle = getSecureTitle ( $title ) ;
	$head = getStandardHeader () ;
	$content = $head.$content."\n" ;
	$content .= getStandardFooter () ;
	return $content ;
	}

function view_old_article ( $mode="parsed" ) {
	global $title , $action , $oid ;
	if ( $oid == "" ) return "NO OID GIVEN" ;

	$content = acquireOldTopic ( $title , $oid ) ;
	if ( $mode == "parsed" )
		$content = parseContent ( $content ) ;
	else if ( $mode == "source" )
		$content = "<textarea name=newtext rows=20 cols=65 STYLE=\"width:100%\" wrap=virtual>$content</textarea>" ;

	$secureTitle = getSecureTitle ( $title ) ;
	$head = getStandardHeader () ;

	$content = $head.$content ;
	$content .= "\n<hr>\n" ;

	return $content ;
	}

function edit () {
	global $title , $action ;
	global $newtext , $comment , $recent_edit ;

	if ( ! $comment ) $comment = "*" ;
	if ( $recent_edit ) $recent_edit = "on" ;
	else $recent_edit = "off" ;

	$realTitle=$title ;
	$secureTitle = getSecureTitle ( $title ) ;

	# Checking clearance
	if ( !canEdit($title) ) return "You are not allowed to edit this article. Follow <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle\">this link</a> to go back." ;

	if ( $newtext ) {
		$content = $newtext ;
		$content = str_replace ( "\\\"" , "\"" , $content ) ;
		$content = str_replace ( "\\'" , "'" , $content ) ;
		}
	else $content = acquireTopic ( $title ) ;

	$content = str_replace ( "\r" , "" , $content ) ;

	$source = $content ;

	$head = getStandardHeader () ;
	$head .= "<form action=\"" . $GLOBALS['wiki_php'] . "?title=$title&action=edited\" method=post>\n";

	if ( $content == "" ) $content = "Describe the new page here.\n" ;

	$content =  "<textarea name=newtext rows=20 cols=65 STYLE=\"width:100%\" wrap=virtual>$content</textarea><br>\n" ;

	$content .= "Summary:<INPUT TYPE=text NAME=comment VALUE=\"$comment\" SIZE=60 MAXLENGTH=200><br>\n" ;
	$content .= "<INPUT TYPE=checkbox NAME=\"minor_edit\" VALUE=\"on\">This change is a minor edit.<br>\n" ;
	$content .= "<input type=submit name=save value=\"Save changes\">\n" ;
	$content .= "<input type=submit name=preview value=\"Preview changes\">\n" ;

	$content .= "</form>\n" ;

	if ( $action=="preview" ) {
		$source = parseContent ( $source ) ; 
		$content .= "<hr>\n" ;
		$content .= "<font size=\"+3\">PREVIEW</font><br><br>\n" ;
		$content .= $source ;
		$content .= "\n<hr><b>Remember, this is just a preview!</b>\n" ;
		}

	unset ( $recent_edit ) ;
	unset ( $comment ) ;
	unset ( $newtext ) ;

	$content = $head.$content ;
	return $content ;
	}

function edited () {
	global $action , $preview , $title , $save ;
	global $newtext , $comment , $minor_edit ;

	$secureTitle = getSecureTitle ( $title ) ;
	# Checking clearance
	if ( !canEdit($title) ) return "You are not allowed to edit this article. Follow <a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle\">this link</a> to go back." ;

	if ( doesTopicExist ( $title ) ) {
		# Backup old version
		backupTopic ( $title ) ;
	} else {
		# New topic
		addPlainTopic ( $title ) ;
		}
	
	saveTopic ( $newtext , $comment , $minor_edit ) ;

	unset ( $preview ) ;
	unset ( $newtext ) ;
	unset ( $save ) ;

	$action="view" ;
	$ret="<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=" . $GLOBALS['wiki_php'] . "?title=$title&action=view\">" ;
	return $ret ;
	}

function MySQLtimestamp ( $edit_time ) {
	$qh = substr ( $edit_time ,  8 , 2 ) ;
	$qm = substr ( $edit_time , 10 , 2 ) ;
	$qs = substr ( $edit_time , 12 , 2 ) ;
	$qo = substr ( $edit_time ,  4 , 2 ) ;
	$qd = substr ( $edit_time ,  6 , 2 ) ;
	$qy = substr ( $edit_time ,  0 , 4 ) ;
	$edit_time = date ( "F d, Y, H:i:s" , mktime ( $qh , $qm , $qs , $qo , $qd , $qy ) ) ;
	if ( $edit_time == "" ) $edit_time = "<unknown>" ;
	return $edit_time ;
	}

function currentMySQLtime () {
	return date ( "YmdHis" ) ;
	}

############################################
# HIGHER BRAIN FUNCTIONS
############################################

function showRecentChanges () {
	global $title ;
	$s=getSecureTitle($s);
	$s=strtolower($s);
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "SELECT * FROM " . $GLOBALS['wiki_cur'] . " ORDER BY cur_timestamp DESC LIMIT 100" ;
	$result = mysql_query ( $sql , $connection ) ;

	$output .= getStandardHeader () ;

	$output .= "<table width=\"100%\" border=1>\n" ;
	$output .= "<tr><th width=150 nowrap>Title</th><th width=180 nowrap>Other Version</th><th width=180 nowrap>Time</th><th>User</th><th>Last comment</th></tr>";
	while ( $s = mysql_fetch_object ( $result ) ) {
		$secureTitle=getSecureTitle($s->cur_title);
		$edit_time = MySQLtimestamp ( $s->cur_timestamp ) ;
		$comment=$s->cur_comment ;
		if ( $s->cur_minor_edit == 1 ) $comment = "<i>[edit]</i> ".$comment ;
		$cuser=$s->cur_user_text ;
		if ( $cuser == "" ) $cuser = "<unknown>" ;
		$output .= "<tr>" ;
		$output .= "<td width=150 nowrap><a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=view\">$s->cur_title</a></td>";
		$output .= "<td width=180 nowrap><a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=revisions\">Other versions of this article</a>" ;
		$output .= "<td width=180 nowrap>$edit_time</td>" ;
		$output .= "<td width=120 nowrap>$cuser</td>" ;
		$output .= "<td>$comment</td>" ;
		$output .= "</tr>\n" ;
	}
	$output .= "</table>\n" ;
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;
	return $output ;
	}

function revisions () {
	global $title ;
	if ( !doesTopicExist ( $title ) ) return "There is no topic $title." ;
	
	$ret .= getStandardHeader () ;

	$s=getSecureTitle($title);
	$s=strtolower($s);
	$stitle=$s ;
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "select * from " . $GLOBALS['wiki_cur'] . " where cur_title='$stitle'" ;
	$result = mysql_query ( $sql , $connection ) ;

	$s = mysql_fetch_object ( $result ) ;
	$id = $s->cur_id ;
	$next = $s->cur_old_version ;
	$comment = $s->cur_comment ;
	$user_text = $s->cur_user_text ;
	$edit_time = $s->cur_timestamp ;
	$release = "current" ;
	
	$ret .= "<table width=\"100%\" border=1>\n" ;
	$ret .= "<tr><th nowrap width=60><center><b>History</b></center></th>";
	$ret .= "<th nowrap width=10><b>Article</b></th>" ;
	$ret .= "<th nowrap width=10><b>Source</b></th>" ;
	$ret .= "<th nowrap width=10><b>User</b></th>" ;
	$ret .= "<th nowrap width=10><b>Time</b></th>" ;
	$ret .= "<th nowrap width=\"100%\"><b>Comment</b></th>" ;
	$ret .= "</tr>\n" ;
	do {
		$oid = $next ;
		if ( $release == "current" ) $oid = $release ;

		$edit_time = MySQLtimestamp ( $edit_time ) ;

		if ( $user_text == "" ) $user_text = "<unknown>" ;
		$ret .= "<tr>" ;
		$ret .= "<td nowrap><center>$release</center></td>" ;
		$ret .= "<td nowrap><a href=\"" . $GLOBALS['wiki_php'] . "?title=$title&action=view_old_article&oid=$oid\">Go to this article version</a></td>";
		$ret .= "<td nowrap><a href=\"" . $GLOBALS['wiki_php'] . "?title=$title&action=view_old_source&oid=$oid\">Go to this source version</a></td>";
		$ret .= "<td nowrap>$user_text</td>" ;
		$ret .= "<td nowrap>$edit_time</td>" ;
		$ret .= "<td>$comment</td>" ;
		$ret .= "</tr>\n" ;

		if ( $release != "current" ) $next = $s->old_old_version ;
		if ( $release == "current" ) $release = 0 ;
		$release = $release + 1 ;
		if ( $next != 0 ) {
			mysql_free_result ( $result ) ;
			$sql = "select * from " . $GLOBALS['wiki_old'] . " where old_id=$next" ;
			$result = mysql_query ( $sql , $connection ) ;
			$s = mysql_fetch_object ( $result ) ;
			$comment = $s->old_comment ;
			$user_text = $s->old_user_text ;
			$edit_time = $s->old_timestamp ;
			}
		} while ( $next != 0 ) ;
	$ret .= "</table>\n" ;
	$ret .= getStandardFooter () ;

	mysql_close ( $connection ) ;
	return $ret ;
	}

function doSearch () {
	global $search ;

	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$sql = "SELECT * FROM " . $GLOBALS['wiki_cur'] . " WHERE cur_text LIKE \"%$search%\" OR cur_title LIKE \"%$search%\" ORDER BY cur_title" ;
	$result = mysql_query ( $sql , $connection ) ;

	$output="<h1>Search results</h1>\n";
	$output .= getStandardHeader () ;
	$output .= "<br><table width=\"100%\" border=1>\n" ;
	$output .= "<tr><th width=150 nowrap>Title</th><th width=180 nowrap>Other Version</th><th width=180 nowrap>Time</th><th>User</th><th>Last comment</th></tr>";
	while ( $s = mysql_fetch_object ( $result ) ) {
		$secureTitle=getSecureTitle($s->cur_title);
		$edit_time = MySQLtimestamp ( $s->cur_timestamp ) ;
		$comment=$s->cur_comment ;
		if ( $s->cur_minor_edit == 1 ) $comment = "<i>[edit]</i> ".$comment ;
		$cuser=$s->cur_user_text ;
		if ( $cuser == "" ) $cuser = "<unknown>" ;
		$output .= "<tr>" ;
		$output .= "<td width=150 nowrap><a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=view\">$s->cur_title</a></td>";
		$output .= "<td width=180 nowrap><a href=\"" . $GLOBALS['wiki_php'] . "?title=$secureTitle&action=revisions\">Other versions of this article</a>" ;
		$output .= "<td width=180 nowrap>$edit_time</td>" ;
		$output .= "<td width=120 nowrap>$cuser</td>" ;
		$output .= "<td>$comment</td>" ;
		$output .= "</tr>\n" ;
	}
	$output .= "</table>\n" ;
	mysql_free_result ( $result ) ;
	mysql_close ( $connection ) ;

	$output .= getStandardFooter () ;
	return $output ;
	}

function login () {
	global $USERNAME , $USERPASSWORD , $USERLOGGEDIN ;
	$ret = "<font size=\"+3\">Log in</font><hr>\n" ;
	if ( $USERLOGGEDIN == "YES" ) $ret .= "$USERNAME, you are already logged in!<br>\n" ;
	$ret .= "<FORM action=\"" . $GLOBALS['wiki_php'] . "?action=loginattempt\" method=post><font face=courier>\n" ;
	$ret .= "Your current user name : <INPUT TABINDEX=1 TYPE=text NAME=user_name VALUE=\"$USERNAME\" SIZE=20><br>\n" ;

	$pwd = $USERPASSWORD ;
	if ( !doesUserExist($USERNAME) ) $pwd = "" ;

	$ret .= "Your current password  : <INPUT TABINDEX=2 TYPE=password NAME=user_password VALUE=\"$pwd\" SIZE=20><br>\n" ;
	$ret .= "<INPUT TABINDEX=3 TYPE=checkbox NAME=user_remember_password>Remember my password (as a cookie).<br>\n" ;
	$ret .= "<input TABINDEX=4 type=submit name=dologin value=\"Log in\">\n" ;
	$ret .= "</font></FORM>\n" ;
	$ret .= "<hr>Return to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a> without logging in" ;

	return $ret ;
	}

function loginattempt () {
	global $user_name , $user_password , $user_remember_password , $newuser ;
	global $USERNAME , $USERPASSWORD , $USERLOGGEDIN ;
	
	if ( $newuser == "YES" and !doesUserExist ( $user_name ) ) {
		addNewUser ( $user_name , $user_password , "" ) ;
		$ret .= "Congratulations, $user_name! You were added to the user list.<br>\n" ;
		$ret .= "Check your preferences <a href=\"" . $GLOBALS['wiki_php'] . "?action=prefs\">here</a>!<br>\n" ;
		$ret .= "Or go directly to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a>.\n" ;
	} else if ( checkUserPassword ( $user_name , $user_password ) ) { # Correct log-in
		setcookie ( "USERNAME" , $user_name ) ;
		if ( $user_remember_password == "on" ) setcookie ( "USERPASSWORD" , $user_password ) ;
		else setcookie ( "USERPASSWORD" , "" ) ;
		setcookie ( "USERLOGGEDIN" , "YES" ) ;
		$ret .= "$USERNAME, you have been successfully logged in!<br>\n" ;
		$ret .= "<hr>Return to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a>" ;
	} else { #Wrong log-in
		$ret .= "Sorry, your login was incorrect. You can :<br>\n" ;
		$ret .= "- <a href=\"" . $GLOBALS['wiki_php'] . "?action=login\">Try again</a>.<br>\n" ;
		$ret .= "- Go to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a> without logging in.<br>\n" ;
		if ( !doesUserExist ( $user_name ) ) {
			$ret .= "- Create a new user \"$user_name\", with the password \"$user_password\"." ;
			$ret .= "<FORM action=\"" . $GLOBALS['wiki_php'] . "?action=loginattempt\" method=post>\n" ;
			$ret .= "<input type=submit name=createnewuser value=\"Create user $user_name\">\n" ;
			$ret .= "<INPUT TYPE=HIDDEN NAME=user_name VALUE=\"$user_name\">\n" ;
			$ret .= "<INPUT TYPE=HIDDEN NAME=user_password VALUE=\"$user_password\">\n" ;
			$ret .= "<INPUT TYPE=HIDDEN NAME=newuser VALUE=\"YES\">\n" ;
			$ret .= "</FORM>\n" ;
			}
		}
	
	unset ( $newuser ) ;
	return $ret ;
	}

function logout () {
	global $USERNAME , $USERPASSWORD , $USERLOGGEDIN , $USERID ;
	setcookie ( "USERLOGGEDIN" , "NO" ) ;
	$ret = "<font size=\"+3\">Goodbye, $USERNAME!</font><br>\n" ;
	$ret .= "Return to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a>" ;
	return $ret ;
	}

function prefs () {
	global $changeprefs , $u_email , $u_password ;
	global $USERNAME , $USERPASSWORD , $USERLOGGEDIN , $USERID ;
	if ( $USERLOGGEDIN != "YES" ) return "You are not logged in. <a href=\"" . $GLOBALS['wiki_php'] . "?action=login\">Log in</a> or return to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a>" ;
	$ret = getStandardHeader () ;

	if ( $changeprefs ) { # Save new settings
		changeUserSetting ( $USERNAME , "user_email" , $u_email ) ;
		changeUserSetting ( $USERNAME , "user_password" , $u_password ) ;
		$ret .= "Settings are changed.<br>\n" ;
		}

	$uemail = getUserSetting ( $USERNAME , "user_email" ) ;
	$ur = getUserSetting ( $USERNAME , "user_rights" ) ;
	$ret .= "<font face=courier>\n" ;
	$ret .= "<FORM action=\"" . $GLOBALS['wiki_php'] . "?action=prefs\" method=post>\n" ;
	$ret .= "<p>Your user ID  : $USERID</p>\n" ;
	$ret .= "<p>Your rights   : $ur</p>\n" ;
	$ret .= "<p>Your email    : <INPUT TABINDEX=1 TYPE=text NAME=u_email VALUE=\"$uemail\" SIZE=20></p>\n" ;
	$ret .= "<p>Your password : <INPUT TABINDEX=1 TYPE=text NAME=u_password VALUE=\"$USERPASSWORD\" SIZE=20></p>\n" ;
	$ret .= "<INPUT TYPE=SUBMIT NAME=changeprefs value=\"Save settings\">\n" ;
	$ret .= "</FORM>\n" ;
	$ret .= "</font>\n" ;

	$rights = ",".getUserSetting ( $USERNAME , "user_rights" )."," ;
	if ( strstr ( $rights , ",is_editor" ) or strstr ( $rights , ",is_sysop" ) ) {
		$ret .= "<hr><font color=red>You are allowed to <a href=\"" . $GLOBALS['wiki_php'] . "?action=editUserRights\">edit user rights</a>!</font>" ;
		}

	$ret .= getStandardFooter () ;

	return $ret ;
	}

######## EDIT USER RIGHTS
function editUserRights () {
	global $title , $editusername , $newuserrights , $USERLOGGEDIN , $USERNAME ;
	$secureTitle = getSecureTitle ( $title ) ;
	if ( !$USERLOGGEDIN ) return "You are not logged in. <a href=\"" . $GLOBALS['wiki_php'] . "?action=login\">Log in</a> or return to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a>" ;

	# AUTHENTIFICATION
	$rights = ",".getUserSetting ( $USERNAME , "user_rights" )."," ;
	if ( strstr ( $rights , ",is_editor," ) or strstr ( $rights , ",is_sysop" ) ) $isEditor = true ;
	else $isEditor = false ;
	if ( strstr ( $rights , ",is_sysop," ) or strstr ( $rights , ",is_sysop" ) ) $isSysop = true ;
	else $isSysop = false ;
	if ( !$isSysop and !isEditor ) return "You are neither an editor nor a sysop. Return to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a>" ;

	$ret = "" ;
	if ( isset ( $editusername ) ) {
		if ( isset ( $newuserrights ) ) {
			changeUserSetting ( $editusername , "user_rights" , $newuserrights ) ;
			$ret="<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=" . $GLOBALS['wiki_php'] . "?action=prefs\">" ;
		} else {
		$ret .= getStandardHeader () ;
		$ret .= "<font size=\"+2\">Editing rights of user $editusername</font><br>" ;
		$r = getUserSetting ( $editusername , "user_rights" ) ;
		$ret .= "<FORM action=\"" . $GLOBALS['wiki_php'] . "?action=editUserRights&editusername=$editusername\" method=post>\n" ;
		$ret .= "User rights : <INPUT TABINDEX=1 TYPE=text NAME=newuserrights VALUE=\"$r\" SIZE=80><br>\n" ;
		$ret .= "<INPUT TYPE=SUBMIT NAME=changeprefs value=\"Save new user rights\">\n" ;
		$ret .= "</FORM>\n" ;
			}
		unset ( $editusername ) ;
		unset ( $newuserrights ) ;
	} else {
		$ret .= getStandardHeader () ;
		$connection=getDBconnection() ;
		mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
		$sql = "SELECT * FROM " . $GLOBALS['wiki_user'] ;
		if ( !$isSysop ) $sql .= " WHERE user_rights NOT LIKE \"is_sysop\"" ;
		$sql .= " ORDER BY user_name" ;
		$result = mysql_query ( $sql , $connection ) ;
		while ( $s = mysql_fetch_object ( $result ) ) {
			$t = $s->user_name ;
			$t = "<a href=\"" . $GLOBALS['wiki_php'] . "?action=editUserRights&editusername=$t\">$t</a>" ;
			$ret .= "Edit the rights of $t ($s->user_rights)<br>\n" ;
			}
		mysql_free_result ( $result ) ;
		mysql_close ( $connection ) ;
		$ret .= getStandardFooter () ;
		}

	return $ret ;
	}


function statistics () {
	$ret = getStandardHeader () ;
	$connection=getDBconnection() ;
	mysql_select_db ( $GLOBALS['db_name'] , $connection ) ;
	$ret = getStandardHeader() ;
	$ret .= "<h2>Article statistics</h2><ul>" ;

	$nf1 = "<font color=red><b>" ;
	$nf2 = "</b></font>" ;

	# TOTAL	
	$sql = "SELECT COUNT(*) AS number FROM ". $GLOBALS['wiki_cur'] ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$totalPages = $s->number ;
	$ret .= "<li>There are $nf1$totalPages$nf2 pages in the database</li>" ;
	mysql_free_result ( $result ) ;

	# /TALK
	$sql = "SELECT COUNT(*) as number FROM " . $GLOBALS['wiki_cur'] . " WHERE cur_title LIKE \"%/Talk\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$talkPages = $s->number ;
	$ret .= "<li>There are $nf1$talkPages$nf2 <b>/Talk</b> pages</li>" ;
	mysql_free_result ( $result ) ;

	# , NOT /TALK
	$sql = "SELECT COUNT(*) as number FROM " . $GLOBALS['wiki_cur'] . " WHERE cur_title NOT LIKE \"%/Talk\" AND cur_text LIKE \"%,%\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$commaPages = $s->number ;
	$ret .= "<li>There are $nf1$commaPages$nf2 with a comma that are <i>not</i> <b>/Talk</b> pages</li>" ;
	mysql_free_result ( $result ) ;

	# WIKIPEDIA NOT /TALK
	$sql = "SELECT COUNT(*) as number FROM " . $GLOBALS['wiki_cur'] . " WHERE cur_title NOT LIKE \"%/Talk\" AND cur_title LIKE \"%ikipedia%\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$wikiPages = $s->number ;
	$ret .= "<li>There are $nf1$wikiPages$nf2 that have \"ikipedia\" in the title and are <i>not</i> <b>/Talk</b> pages</li>" ;
	mysql_free_result ( $result ) ;

	# WIKIPEDIA NOT /TALK
	$sql = "SELECT COUNT(*) as number FROM " . $GLOBALS['wiki_cur'] . " WHERE cur_title LIKE \"%/%\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$subPages = $s->number - $talkPages;
	$ret .= "<li>There are $nf1$subPages$nf2 subpages that are <i>not</i> <b>/Talk</b> pages</li>" ;
	mysql_free_result ( $result ) ;

	# RESULT
	$x = $commaPages - $wikiPages ; # Comma (no /Talk) - wiki pages = articles, including subpages
	$ret .= "<li>That means there are about $nf1$x$nf2 articles, including subpages (except <b>/Talk</b>).</li>" ;
	$y = $x - $subPages ;
	$ret .= "<li>Or, there are about $nf1$y$nf2 articles, not counting any subpages!</li>" ;
	$z = $totalPages - $talkPages - $commaPages ;
	$ret .= "<li>Finally, there are about $nf1$z$nf2 junk pages :-(</li>" ;

	# OLD PAGES
	$sql = "SELECT COUNT(*) as number FROM " . $GLOBALS['wiki_old'] ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$oldPages = $s->number - $talkPages;
	$p = round ( $oldPages / $totalPages , 2 ) ;
	$ret .= "<li>And, there are $nf1$oldPages$nf2 old page versions in the database, giving an average of $p old pages on every active page.</li>" ;
	mysql_free_result ( $result ) ;


	$ret .= "</ul><hr>" ;
	$ret .= "<h2>User statistics</h2><ul>" ;
	
	# USERS
	$sql = "SELECT COUNT(*) as number FROM " . $GLOBALS['wiki_user'] ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$numUser = $s->number ;
	$ret .= "<li>There are currently $nf1$numUser$nf2 users signed up.</li>" ;
	mysql_free_result ( $result ) ;
	
	# EDITORS AND SYSOPS
	$sql = "SELECT COUNT(*) as number FROM " . $GLOBALS['wiki_user'] . " WHERE user_rights LIKE \"%is_editor%\" OR user_rights LIKE \"%is_sysop%\"" ;
	$result = mysql_query ( $sql , $connection ) ;
	$s = mysql_fetch_object ( $result ) ;
	$numEditors = $s->number ;
	$ret .= "<li>$nf1$numEditors$nf2 of them have editor or sysop status.</li>" ;
	mysql_free_result ( $result ) ;

	mysql_close ( $connection ) ;
	$ret .= "</ul>" ;
	$ret .= getStandardFooter () ;
	return $ret ;
	}

#############################
# MAIN PROGRAM
#############################

	global $title , $action , $doSearch ;
	if ( $title == "" ) $title="MainPage" ;
	if ( $action == "" ) $action = "view" ;
	$action = strtolower ( $action ) ;

	if ( $action == "edited" && $preview ) $action="preview" ;
	unset ( $preview ) ;

	if ( $action == "view_old_article" and $oid == "current" ) $action = "view" ;
	if ( $action == "view_old_source" and $oid == "current" ) $action = "edit" ;
	if ( $dosearch == 1 ) $action = "search" ;

	$ltitle=strtolower($title);
	if ( $ltitle=="recentchanges" ) $ret = showRecentChanges() ;
	else if ( $dosearch == 1 ) $ret = doSearch () ;
	else if ( $action == "statistics" ) $ret = statistics() ;
	else if ( $action == "restrictions" ) $ret = restrictions() ;
	else if ( $action == "edituserrights" ) $ret = editUserRights() ;
	else if ( $action == "prefs" ) $ret = prefs() ;
	else if ( $action == "login" ) $ret = login() ;
	else if ( $action == "loginattempt" ) $ret = loginattempt() ;
	else if ( $action == "logout" ) $ret = logout() ;
	else if ( $action == "view" ) $ret = view() ;
	else if ( $action == "edit" ) $ret = edit() ;
	else if ( $action == "preview" ) $ret = edit() ;
	else if ( $action == "edited" ) $ret = edited() ;
	else if ( $action == "revisions" ) $ret = revisions() ;
	else if ( $action == "view_old_article" ) $ret = view_old_article( "parsed" ) ;
	else if ( $action == "view_old_source" ) $ret = view_old_article( "source" ) ;
	else { # No valid action!
		$ret = "<font size=\"+4\">ILLEGAL COMMAND!</font><br>\n" ;
		$ret .= "Return to the <a href=\"" . $GLOBALS['wiki_php'] . "\">Main Page</a>" ;
		}
	print "<html>\n<head>\n";
    print "<title>$wiki_title</title>\n" ;
    print "<link rel=\"stylesheet\" href=\"/css/wiki.css\">\n" ;
    print "</head>\n<body>" ;
	echo $ret ;
    print "</body>\n</html>\n" ;
	unset ( $oid ) ;
	unset ( $doSearch ) ;
	unset ( $editusername ) ;
?>

HomePage | Wikipedia PHP script | Recent Changes | Preferences
This page is read-only | View other revisions
Last edited August 29, 2001 7:08 am by 12.9.138.xxx (diff)
Search: