2017-12-20 20:49:11 +00:00
< ?
2018-10-22 18:03:03 +00:00
namespace Kabano ;
2017-12-20 20:49:11 +00:00
/**********************************************************
***********************************************************
**
** This class is to manage a wiki page object
**
***********************************************************
**********************************************************/
require_once ( $config [ 'third_folder' ] . " Md/MarkdownExtra.inc.php " );
class WikiPage
{
2019-01-30 18:49:26 +00:00
public $content_id = NULL ;
public $locale_id = NULL ;
public $version_id = NULL ;
2018-11-05 20:35:21 +00:00
public $permalink = NULL ;
2018-10-22 21:09:20 +00:00
public $version = 0 ;
public $locale = NULL ;
public $creation_date = NULL ;
public $update_date = NULL ;
public $author = NULL ;
public $is_public = NULL ;
public $is_archive = NULL ;
public $is_commentable = NULL ;
public $type = " wiki " ;
2018-08-21 18:15:53 +00:00
public $name = NULL ;
2017-12-20 20:49:11 +00:00
public $content = NULL ;
/*****
2018-10-22 21:09:20 +00:00
** Checks if a page at this ermalink exists and return the populated element
2017-12-20 20:49:11 +00:00
*****/
2018-10-22 21:09:20 +00:00
public function checkPermalink ( $permalink , $withArchive = 0 , $elementNb = 0 ) {
2017-12-20 20:49:11 +00:00
global $config ;
$con = pg_connect ( " host= " . $config [ 'SQL_host' ] . " dbname= " . $config [ 'SQL_db' ] . " user= " . $config [ 'SQL_user' ] . " password= " . $config [ 'SQL_pass' ])
or die ( " Could not connect to server \n " );
2019-01-30 18:49:26 +00:00
$query = " SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink= $ 1 AND type='wiki' " ;
2017-12-20 20:49:11 +00:00
if ( $withArchive == 0 ) {
2019-01-30 20:03:48 +00:00
$query .= " AND is_archive=FALSE AND is_public=TRUE " ;
2017-12-20 20:49:11 +00:00
}
2018-08-21 18:15:53 +00:00
$query .= " ORDER BY update_date DESC LIMIT 1 OFFSET $ 2 " ;
2017-12-20 20:49:11 +00:00
pg_prepare ( $con , " prepare1 " , $query )
or die ( " Cannot prepare statement \n " );
2018-10-22 21:09:20 +00:00
$result = pg_execute ( $con , " prepare1 " , array ( $permalink , $elementNb ))
2017-12-20 20:49:11 +00:00
or die ( " Cannot execute statement \n " );
pg_close ( $con );
if ( pg_num_rows ( $result ) == 1 ) {
2018-08-21 18:15:53 +00:00
$row = pg_fetch_assoc ( $result );
$this -> populate ( $row );
2017-12-20 20:49:11 +00:00
return 1 ;
}
else {
return 0 ;
}
}
/*****
2018-09-03 20:02:13 +00:00
** Populate the object using raw data from SQL
2017-12-20 20:49:11 +00:00
*****/
2018-10-22 21:09:20 +00:00
public function populate ( $row ) {
2019-01-30 18:49:26 +00:00
$this -> content_id = $row [ 'content_id' ];
$this -> locale_id = $row [ 'locale_id' ];
$this -> version_id = $row [ 'version_id' ];
2018-08-21 18:15:53 +00:00
$this -> permalink = $row [ 'permalink' ];
$this -> version = $row [ 'version' ];
$this -> locale = $row [ 'locale' ];
$this -> creation_date = $row [ 'creation_date' ];
$this -> update_date = $row [ 'update_date' ];
$this -> author = $row [ 'author' ];
$this -> is_public = $row [ 'is_public' ];
$this -> is_archive = $row [ 'is_archive' ];
$this -> is_commentable = $row [ 'is_commentable' ];
$this -> type = $row [ 'type' ];
$this -> name = $row [ 'name' ];
$this -> content = $row [ 'content' ];
2017-12-20 20:49:11 +00:00
}
/*****
** Edit a page by archiving the current one and inserting a new one ID
*****/
public function update () {
global $config ;
2018-10-30 20:45:12 +00:00
global $user ;
2018-11-04 07:15:56 +00:00
2019-01-30 18:49:26 +00:00
if ( $this -> content_id == 0 || $this -> locale_id == 0 || $this -> version_id == 0 )
2018-11-04 07:15:56 +00:00
die ( " Cannot update entry without giving ID " );
2018-10-22 21:09:20 +00:00
$this -> version ++ ;
2017-12-20 20:49:11 +00:00
$con = pg_connect ( " host= " . $config [ 'SQL_host' ] . " dbname= " . $config [ 'SQL_db' ] . " user= " . $config [ 'SQL_user' ] . " password= " . $config [ 'SQL_pass' ])
or die ( " Could not connect to server \n " );
2019-01-30 18:49:26 +00:00
$query = " UPDATE content_versions SET is_archive = TRUE WHERE locale_id = $ 1 " ;
2017-12-20 20:49:11 +00:00
pg_prepare ( $con , " prepare1 " , $query )
or die ( " Cannot prepare statement \n " );
2019-01-30 18:49:26 +00:00
$result = pg_execute ( $con , " prepare1 " , array ( $this -> locale_id ))
2017-12-20 20:49:11 +00:00
or die ( " Cannot execute statement \n " );
2019-01-30 18:49:26 +00:00
$query = " INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
( $ 1 , $ 2 , FALSE , $ 3 , $ 4 , $ 5 ) RETURNING id " ;
2017-12-20 20:49:11 +00:00
pg_prepare ( $con , " prepare2 " , $query )
or die ( " Cannot prepare statement \n " );
2019-01-30 18:49:26 +00:00
$result = pg_execute ( $con , " prepare2 " , array ( $this -> version , date ( 'r' ), $this -> name , $this -> content , $this -> locale_id ))
2017-12-20 20:49:11 +00:00
or die ( " Cannot execute statement \n " );
2019-01-30 18:49:26 +00:00
$this -> version_id = pg_fetch_assoc ( $result )[ 'id' ];
2018-11-04 07:15:56 +00:00
$query = " INSERT INTO content_contributors (content, contributor) VALUES
( $ 1 , $ 2 ) ON CONFLICT ( content , contributor ) DO NOTHING " ;
2019-01-30 18:49:26 +00:00
pg_prepare ( $con , " prepare3 " , $query )
2018-11-04 07:15:56 +00:00
or die ( " Cannot prepare statement \n " );
2019-01-30 18:49:26 +00:00
$result = pg_execute ( $con , " prepare3 " , array ( $this -> locale_id , $user -> id ))
2018-10-30 20:54:41 +00:00
or die ( " Cannot execute statement \n " );
2017-12-20 20:49:11 +00:00
pg_close ( $con );
error_log (
2018-10-22 21:09:20 +00:00
date ( 'r' ) . " \t " . $user -> name . " ( " . $user -> id . " ) \t UPDATE \t Edit wiki page ' " . $this -> permalink . " ' \r \n " ,
2017-12-20 20:49:11 +00:00
3 ,
$config [ 'logs_folder' ] . 'wiki.log' );
}
/*****
** Delete a page by archiving it
*****/
public function delete () {
global $config ;
global $user ;
$con = pg_connect ( " host= " . $config [ 'SQL_host' ] . " dbname= " . $config [ 'SQL_db' ] . " user= " . $config [ 'SQL_user' ] . " password= " . $config [ 'SQL_pass' ])
or die ( " Could not connect to server \n " );
2018-10-30 20:29:46 +00:00
$query = " UPDATE contents SET is_public=FALSE WHERE permalink= $ 1 AND type='wiki' " ;
pg_prepare ( $con , " prepare1 " , $query )
or die ( " Cannot prepare statement \n " );
$result = pg_execute ( $con , " prepare1 " , array ( $this -> permalink ))
or die ( " Cannot execute statement \n " );
pg_close ( $con );
error_log (
date ( 'r' ) . " \t " . $user -> name . " ( " . $user -> id . " ) \t DELETE \t Unpublish wiki page ' " . $this -> permalink . " ' \r \n " ,
3 ,
$config [ 'logs_folder' ] . 'wiki.log' );
}
/*****
** Restore a page from unpublishing it
*****/
public function restore () {
global $config ;
global $user ;
$con = pg_connect ( " host= " . $config [ 'SQL_host' ] . " dbname= " . $config [ 'SQL_db' ] . " user= " . $config [ 'SQL_user' ] . " password= " . $config [ 'SQL_pass' ])
or die ( " Could not connect to server \n " );
$query = " UPDATE contents SET is_public=TRUE WHERE permalink= $ 1 AND type='wiki' " ;
2017-12-20 20:49:11 +00:00
pg_prepare ( $con , " prepare1 " , $query )
or die ( " Cannot prepare statement \n " );
2018-10-22 21:09:20 +00:00
$result = pg_execute ( $con , " prepare1 " , array ( $this -> permalink ))
2017-12-20 20:49:11 +00:00
or die ( " Cannot execute statement \n " );
pg_close ( $con );
error_log (
2018-10-30 20:29:46 +00:00
date ( 'r' ) . " \t " . $user -> name . " ( " . $user -> id . " ) \t RESTORE \t Publish wiki page ' " . $this -> permalink . " ' \r \n " ,
2017-12-20 20:49:11 +00:00
3 ,
$config [ 'logs_folder' ] . 'wiki.log' );
}
/*****
** Create a page by archiving the current one and inserting a new one ID
*****/
public function insert () {
global $config ;
global $user ;
$con = pg_connect ( " host= " . $config [ 'SQL_host' ] . " dbname= " . $config [ 'SQL_db' ] . " user= " . $config [ 'SQL_user' ] . " password= " . $config [ 'SQL_pass' ])
or die ( " Could not connect to server \n " );
2019-01-30 18:49:26 +00:00
$query = " INSERT INTO contents (permalink, creation_date, is_public, is_commentable, type) VALUES
( $ 1 , $ 2 , TRUE , FALSE , 'wiki' ) RETURNING id " ;
2017-12-20 20:49:11 +00:00
2018-10-22 21:09:20 +00:00
pg_prepare ( $con , " prepare1 " , $query )
2017-12-20 20:49:11 +00:00
or die ( " Cannot prepare statement \n " );
2019-01-30 18:49:26 +00:00
$result = pg_execute ( $con , " prepare1 " , array ( $this -> permalink , date ( 'r' )))
2017-12-20 20:49:11 +00:00
or die ( " Cannot execute statement \n " );
2019-01-30 18:49:26 +00:00
$this -> content_id = pg_fetch_assoc ( $result )[ 'id' ];
$query = " INSERT INTO content_locales (content_id, locale, author) VALUES
( $ 1 , $ 2 , $ 3 ) RETURNING id " ;
pg_prepare ( $con , " prepare2 " , $query )
or die ( " Cannot prepare statement \n " );
$result = pg_execute ( $con , " prepare2 " , array ( $this -> content_id , $this -> locale , $user -> id ))
or die ( " Cannot execute statement \n " );
$this -> locale_id = pg_fetch_assoc ( $result )[ 'id' ];
$query = " INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
( '0' , $ 1 , FALSE , $ 2 , $ 3 , $ 4 ) RETURNING id " ;
pg_prepare ( $con , " prepare3 " , $query )
or die ( " Cannot prepare statement \n " );
$result = pg_execute ( $con , " prepare3 " , array ( date ( 'r' ), $this -> name , $this -> content , $this -> locale_id ))
or die ( " Cannot execute statement \n " );
$this -> version_id = pg_fetch_assoc ( $result )[ 'id' ];
2018-11-05 20:35:21 +00:00
2018-11-05 21:31:20 +00:00
$query = " INSERT INTO content_contributors (content, contributor) VALUES
( $ 1 , $ 2 ) " ;
2019-01-30 18:49:26 +00:00
pg_prepare ( $con , " prepare4 " , $query )
2018-11-05 21:31:20 +00:00
or die ( " Cannot prepare statement \n " );
2019-01-30 18:49:26 +00:00
$result = pg_execute ( $con , " prepare4 " , array ( $this -> locale_id , $user -> id ))
2018-11-05 21:31:20 +00:00
or die ( " Cannot execute statement \n " );
pg_close ( $con );
2017-12-20 20:49:11 +00:00
error_log (
2018-10-22 21:09:20 +00:00
date ( 'r' ) . " \t " . $user -> name . " ( " . $user -> id . " ) \t INSERT \t Create new wiki page ' " . $this -> permalink . " ' \r \n " ,
2017-12-20 20:49:11 +00:00
3 ,
$config [ 'logs_folder' ] . 'wiki.log' );
}
/*****
** Converts the Markdown content to HTML
*****/
public function md2html () {
$this -> content_html = \Michelf\MarkdownExtra :: defaultTransform ( $this -> content );
}
}
2018-10-22 21:09:20 +00:00
/**********************************************************
***********************************************************
**
** This class is to manage a wiki page object
**
***********************************************************
**********************************************************/
2017-12-20 20:49:11 +00:00
class WikiPages
{
2018-10-22 21:09:20 +00:00
public $objs = array ();
2017-12-20 20:49:11 +00:00
public $number = NULL ;
/*****
** Checks if a page at this URL exists and return the ID
*****/
public function getHistory ( $url ) {
global $config ;
$con = pg_connect ( " host= " . $config [ 'SQL_host' ] . " dbname= " . $config [ 'SQL_db' ] . " user= " . $config [ 'SQL_user' ] . " password= " . $config [ 'SQL_pass' ])
or die ( " Could not connect to server \n " );
2019-01-30 18:49:26 +00:00
$query = " SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink= $ 1 AND type='wiki' ORDER BY update_date DESC " ;
2017-12-20 20:49:11 +00:00
pg_prepare ( $con , " prepare1 " , $query )
or die ( " Cannot prepare statement \n " );
$result = pg_execute ( $con , " prepare1 " , array ( $url ))
or die ( " Cannot execute statement \n " );
pg_close ( $con );
$this -> number = pg_num_rows ( $result );
for ( $i = 0 ; $i < $this -> number ; $i ++ ) {
$row = pg_fetch_assoc ( $result , $i );
2018-10-22 21:09:20 +00:00
$this -> objs [ $i ] = new WikiPage ;
$this -> objs [ $i ] -> populate ( $row );
2017-12-20 20:49:11 +00:00
}
}
}
?>