2004-06-02 12:45:55 +00:00
< ? php
/* MySQL database backend
( Glue between Moodle and ewiki Database )
Adapted by Michael Schneider
*/
/// Glue
$ewiki_plugins [ " database " ][ 0 ] = " ewiki_database_moodle " ;
/// #-- predefine some of the configuration constants
define ( " EWIKI_NAME " , $wiki_entry -> pagename );
define ( " EWIKI_CONTROL_LINE " , 0 );
define ( " EWIKI_LIST_LIMIT " , 25 );
define ( " EWIKI_DEFAULT_LANG " , current_language ());
define ( " EWIKI_HTML_CHARS " , 1 );
define ( " EWIKI_DB_TABLE_NAME " , " wiki_pages " );
function ewiki_database_moodle ( $action , & $args , $sw1 , $sw2 ) {
global $wiki , $wiki_entry , $CFG ;
#-- result array
$r = array ();
switch ( $action ) {
/* Returns database entry as array for the page whose name was given
with the " id " key in the $args array , usually fetches the latest
version of a page , unless a specific " version " was requested in
the $args array .
*/
# Ugly, but we need to choose which wiki we are about to change/read
case " GET " :
2005-03-09 15:14:27 +00:00
$id = " ' " . anydb_escape_string ( $args [ " id " ]) . " ' " ;
2004-06-02 12:45:55 +00:00
( $version = 0 + @ $args [ " version " ]) and ( $version = " AND (version= $version ) " ) or ( $version = " " );
# $result = mysql_query("SELECT * FROM " . EWIKI_DB_TABLE_NAME
# . " WHERE (pagename=$id) $version ORDER BY version DESC LIMIT 1"
#);
#if ($result && ($r = mysql_fetch_array($result, MYSQL_ASSOC))) {
# $r["id"] = $r["pagename"];
# unset($r["pagename"]);
#}
#if (strlen($r["meta"])) {
# $r["meta"] = @unserialize($r["meta"]);
#}
$select = " (pagename= $id ) AND wiki= " . $wiki_entry -> id . " $version " ;
$sort = " version DESC " ;
2005-03-05 04:14:51 +00:00
if ( $result_arr = get_records_select ( EWIKI_DB_TABLE_NAME , $select , $sort , " * " , 0 , 1 )) {
//Iterate to get the first (and unique!)
foreach ( $result_arr as $obj ) {
$result_obj = $obj ;
}
2005-02-21 17:55:54 +00:00
}
2004-06-02 12:45:55 +00:00
if ( $result_obj ) {
2005-02-21 17:55:54 +00:00
//Convert to array
$r = get_object_vars ( $result_obj );
2004-06-02 12:45:55 +00:00
$r [ " id " ] = $r [ " pagename " ];
unset ( $r [ " pagename " ]);
$r [ " meta " ] = @ unserialize ( $r [ " meta " ]);
}
break ;
/* Increases the hit counter for the page name given in $args array
with " id " index key .
*/
case " HIT " :
2005-03-09 15:14:27 +00:00
#mysql_query("UPDATE " . EWIKI_DB_TABLE_NAME . " SET hits=(hits+1) WHERE pagename='" . anydb_escape_string($args["id"]) . "'");
2004-06-02 12:45:55 +00:00
# set_field does not work because of the "hits+1" construct
2005-03-09 15:14:27 +00:00
#print "DO ".anydb__escape_string($args["id"]); exit;
execute_sql ( " UPDATE " . $CFG -> prefix . EWIKI_DB_TABLE_NAME . " SET hits=(hits+1) WHERE pagename=' " . anydb_escape_string ( $args [ " id " ]) . " ' and wiki= " . $wiki_entry -> id , 0 );
2004-06-02 12:45:55 +00:00
break ;
/* Stores the $data array into the database , while not overwriting
existing entries ( using WRITE ); returns 0 on failure and 1 if
saved correctly .
*/
case " OVERWRITE " :
$COMMAND = " REPLACE " ;
break ;
case " WRITE " :
$COMMAND = " WRITE " ;
$args [ " pagename " ] = $args [ " id " ];
unset ( $args [ " id " ]);
if ( is_array ( $args [ " meta " ])) {
$args [ " meta " ] = serialize ( $args [ " meta " ]);
}
#$sql1 = $sql2 = "";
#foreach ($args as $index => $value) {
# if (is_int($index)) {
# continue;
# }
# $a = ($sql1 ? ', ' : '');
# $sql1 .= $a . $index;
2005-03-09 15:14:27 +00:00
# $sql2 .= $a . "'" . anydb_escape_string($value) . "'";
2004-06-02 12:45:55 +00:00
#}
#strlen(@$COMMAND) || ($COMMAND = "INSERT");
foreach ( $args as $index => $value ) {
if ( is_int ( $index )) {
continue ;
}
2005-03-09 15:14:27 +00:00
$args [ $index ] = anydb_escape_string ( $value );
2004-06-02 12:45:55 +00:00
}
$args [ " wiki " ] = $wiki_entry -> id ;
# Check if Record exists
if ( $COMMAND == " REPLACE " ) {
if ( count_records ( EWIKI_DB_TABLE_NAME , " wiki " , $wiki_entry -> id , " pagename " , $args [ " pagename " ], " version " , $args [ " version " ])) {
delete_record ( EWIKI_DB_TABLE_NAME , " wiki " , $wiki_entry -> id , " pagename " , $args [ " pagename " ], " version " , $args [ " version " ]);
}
}
# Write
2006-01-22 23:54:09 +00:00
$result = insert_record ( EWIKI_DB_TABLE_NAME , $args , false );
2004-06-02 12:45:55 +00:00
#$result = mysql_query("$COMMAND INTO " . EWIKI_DB_TABLE_NAME .
# " (" . $sql1 . ") VALUES (" . $sql2 . ")"
#);
#return($result && mysql_affected_rows() ?1:0);
return $result ;
break ;
/* Checks for existence of the WikiPages whose names are given in
the $args array . Returns an array with the specified WikiPageNames
associated with values of " 0 " or " 1 " ( stating if the page exists
in the database ) . For images / binary db entries returns the " meta "
field instead of an " 1 " .
*/
case " FIND " :
$select = " " ;
foreach ( array_values ( $args ) as $id ) {
if ( strlen ( $id )) {
$r [ $id ] = 0 ;
$select .= ( $select ? " OR " : " " ) .
2005-03-09 15:14:27 +00:00
" (pagename=' " . anydb_escape_string ( $id ) . " ') " ;
2004-06-02 12:45:55 +00:00
}
}
if ( $select ) {
$select = " ( " . $select . " ) AND wiki= " . $wiki_entry -> id ;
$result = get_records_select ( EWIKI_DB_TABLE_NAME , $select );
#$sql = "SELECT pagename AS id, meta FROM " .
# EWIKI_DB_TABLE_NAME . " WHERE $sql "
#);
#while ($result && ($row = mysql_fetch_row($result))) {
# $r[$row[0]] = strpos($row[1], 's:5:"image"') ? $row[1] : 1;
while ( list ( $key , $val ) = @ each ( $result )) {
$r [ $val -> pagename ] = strpos ( $val -> meta , 's:5:"image"' ) ? $val -> meta : 1 ;
}
}
break ;
/* Counts the number of Versions
*/
case " COUNTVERSIONS " :
$sql = " SELECT pagename AS id, count(*) as versioncount " .
" FROM " . $CFG -> prefix . EWIKI_DB_TABLE_NAME .
" WHERE wiki = " . $wiki_entry -> id .
2006-10-04 10:03:02 +00:00
" GROUP BY pagename " ;
2004-06-02 12:45:55 +00:00
#print "$sql";
$result = get_records_sql ( $sql );
while ( list ( $key , $val ) = each ( $result )) {
$r [ $key ] = $val -> versioncount ;
}
break ;
/* Returns an array of __all__ pages , where each entry is made up
of the fields from the database requested with the $args array ,
e . g . array ( " flags " , " meta " , " lastmodified " );
*/
case " GETALL " :
2005-03-09 15:14:27 +00:00
switch ( $CFG -> dbtype ) {
case 'postgres7' :
$sql = " SELECT pagename AS id, " .
implode ( " , " , $args ) .
" FROM " . $CFG -> prefix . EWIKI_DB_TABLE_NAME .
" WHERE wiki = " . $wiki_entry -> id .
" GROUP BY pagename, " . implode ( " , " , $args );
break ;
default :
$sql = " SELECT pagename AS id, " .
implode ( " , " , $args ) .
" FROM " . $CFG -> prefix . EWIKI_DB_TABLE_NAME .
" WHERE wiki = " . $wiki_entry -> id .
" GROUP BY id, version " ;
}
2004-06-02 12:45:55 +00:00
#print "$sql";
$result = get_records_sql ( $sql );
$r = new ewiki_dbquery_result ( $args );
$drop = " " ;
#while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
# $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
# if ($i != $drop) {
# $drop = $i;
# $r->add($row);
# }
#}
#print "<pre>"; print_r($result); print "</pre>";
2004-06-06 12:07:28 +00:00
if ( ! $result ) {
$result = array ();
}
2004-06-02 12:45:55 +00:00
while ( list ( $key , $val ) = each ( $result )) {
$row = get_object_vars ( $val );
$i = EWIKI_CASE_INSENSITIVE ? strtolower ( $row [ " id " ]) : $row [ " id " ];
if ( $i != $drop ) {
$drop = $i ;
$r -> add ( $row );
}
}
break ;
/* Returns array of database entries ( also arrays ), where the one
specified column matches the specified content string , for example
$args = array ( " content " => " text...piece " )
is not guaranteed to only search / return the latest version of a page
*/
case " SEARCH " :
$field = implode ( " " , array_keys ( $args ));
$content = strtolower ( implode ( " " , $args ));
if ( $field == " id " ) { $field = " pagename " ; }
2006-10-31 19:17:19 +00:00
$sql = " SELECT pagename AS id, version, flags " .
( EWIKI_DBQUERY_BUFFER && ( $field != " pagename " ) ? " , $field " : " " ) .
" FROM " . $CFG -> prefix . EWIKI_DB_TABLE_NAME .
" WHERE $field " . sql_ilike () . " '% " . anydb_escape_string ( $content ) . " %' and wiki= " . $wiki_entry -> id .
" ORDER BY id, version ASC " ;
2004-06-02 12:45:55 +00:00
$result = get_records_sql ( $sql );
2005-03-09 15:14:27 +00:00
2004-06-02 12:45:55 +00:00
$r = new ewiki_dbquery_result ( array ( " id " , " version " , $field ));
$drop = " " ;
#while ($result && ($row = mysql_fetch_array($result, MYSQL_ASSOC))) {
# $i = EWIKI_CASE_INSENSITIVE ? strtolower($row["id"]) : $row["id"];
# if ($i != $drop) {
# $drop = $i;
# $r->add($row);
# }
#}
while ( list ( $key , $val ) = @ each ( $result )) {
$row = get_object_vars ( $val );
$i = EWIKI_CASE_INSENSITIVE ? strtolower ( $row [ " id " ]) : $row [ " id " ];
if ( $i != $drop ) {
$drop = $i ;
$r -> add ( $row );
}
}
break ;
case " DELETE " :
2005-03-09 15:14:27 +00:00
$id = anydb_escape_string ( $args [ " id " ]);
2004-06-02 12:45:55 +00:00
$version = $args [ " version " ];
#mysql_query("DELETE FROM " . EWIKI_DB_TABLE_NAME ."
# WHERE pagename='$id' AND version=$version");
2004-09-12 14:41:49 +00:00
# print "DELETING wiki:".$wiki_entry->id."Pagename: $id Version: $version <br />\n";
2004-06-02 12:45:55 +00:00
delete_records ( EWIKI_DB_TABLE_NAME , " wiki " , $wiki_entry -> id , " pagename " , $id , " version " , $version );
break ;
case " INIT " :
#mysql_query("CREATE TABLE " . EWIKI_DB_TABLE_NAME ."
# (pagename VARCHAR(160) NOT NULL,
# version INTEGER UNSIGNED NOT NULL DEFAULT 0,
# flags INTEGER UNSIGNED DEFAULT 0,
# content MEDIUMTEXT,
# author VARCHAR(100) DEFAULT 'ewiki',
# created INTEGER UNSIGNED DEFAULT ".time().",
# lastmodified INTEGER UNSIGNED DEFAULT 0,
# refs MEDIUMTEXT,
# meta MEDIUMTEXT,
# hits INTEGER UNSIGNED DEFAULT 0,
# PRIMARY KEY id (pagename, version) )
# ");
#echo mysql_error();
break ;
default :
}
return ( $r );
}
2005-03-09 15:14:27 +00:00
function anydb_escape_string ( $s ) {
global $CFG ;
$type = ( $CFG -> dbtype );
switch ( $CFG -> dbtype ) {
case 'mysql' :
$s = mysql_escape_string ( $s );
break ;
case 'postgres7' :
$s = pg_escape_string ( $s );
break ;
default :
$s = addslashes ( $s );
}
return ( $s );
}