@ -1,12 +1,57 @@
< ? php
function ajax_customCategory ( $param , $content ) {
global $db ;
class CustomCategoryRepository {
function clearCache () {
}
if ( ! $db ) {
return null ;
function getCategory ( $id , $options = []) {
global $db ;
$stmt = $db -> prepare ( " select content from customCategory where id=:id " );
$stmt -> bindValue ( ':id' , $id , PDO :: PARAM_STR );
if ( $stmt -> execute ()) {
$row = $stmt -> fetch ( PDO :: FETCH_ASSOC );
$result = $row [ 'content' ];
$stmt -> closeCursor ();
return $result ;
}
}
if ( isset ( $param [ 'action' ]) && $param [ 'action' ] === 'list' ) {
function recordAccess ( $id ) {
global $db ;
if ( ! isset ( $_SESSION [ 'customCategoryAccess' ])) {
$_SESSION [ 'customCategoryAccess' ] = [];
}
// update access per session only once a day
if ( array_key_exists ( $id , $_SESSION [ 'customCategoryAccess' ]) && $_SESSION [ 'customCategoryAccess' ][ $id ] > time () - 86400 ) {
return ;
}
$_SESSION [ 'customCategoryAccess' ][ $id ] = time ();
$stmt = $db -> prepare ( " insert into customCategoryAccess (id) values (:id) " );
$stmt -> bindValue ( ':id' , $id );
$stmt -> execute ();
}
function saveCategory ( $content ) {
global $db ;
$id = md5 ( $content );
$stmt = $db -> prepare ( " insert or ignore into customCategory (id, content) values (:id, :content) " );
$stmt -> bindValue ( ':id' , $id , PDO :: PARAM_STR );
$stmt -> bindValue ( ':content' , $content , PDO :: PARAM_STR );
$result = $stmt -> execute ();
return $id ;
}
function list ( $options = []) {
global $db ;
// the popularity column counts every acess with declining value over time,
// it halves every year.
$stmt = $db -> prepare ( " select customCategory.id, customCategory.created, customCategory.content, t.accessCount, t.popularity, t.lastAccess from customCategory left join (select id, count(id) accessCount, sum(1/((julianday('2023-08-06 00:00:00') - julianday(ts))/365.25 + 1)) popularity, max(ts) lastAccess from customCategoryAccess group by id) t on customCategory.id=t.id order by popularity desc, created desc limit 25 " );
@ -31,52 +76,35 @@ function ajax_customCategory ($param, $content) {
$stmt -> closeCursor ();
return $data ;
}
}
if ( isset ( $param [ 'id' ])) {
$stmt = $db -> prepare ( " select content from customCategory where id=:id " );
$stmt -> bindValue ( ':id' , $param [ 'id' ], PDO :: PARAM_STR );
if ( $stmt -> execute ()) {
$row = $stmt -> fetch ( PDO :: FETCH_ASSOC );
$result = $row [ 'content' ];
$stmt -> closeCursor ();
customCategoryUpdateAccess ( $param [ 'id' ]);
$customCategoryRepository = new CustomCategoryRepository ();
return $result ;
}
function ajax_customCategory ( $param , $content ) {
global $db ;
global $customCategoryRepository ;
return false ;
if ( ! $db ) {
return null ;
}
if ( isset ( $param [ 'action' ]) && $param [ 'action' ] === 'save' ) {
$id = md5 ( $content );
$stmt = $db -> prepare ( " insert or ignore into customCategory (id, content) values (:id, :content) " );
$stmt -> bindValue ( ':id' , $id , PDO :: PARAM_STR );
$stmt -> bindValue ( ':content' , $content , PDO :: PARAM_STR );
$result = $stmt -> execute ();
customCategoryUpdateAccess ( $id );
return $result ;
if ( isset ( $param [ 'action' ]) && $param [ 'action' ] === 'list' ) {
return $customCategoryRepository -> list ( $param );
}
}
function customCategoryUpdateAccess ( $id ) {
global $db ;
if ( ! isset ( $_SESSION [ 'customCategoryAccess' ])) {
$_SESSION [ 'customCategoryAccess' ] = [];
}
if ( isset ( $param [ 'id' ])) {
$category = $customCategoryRepository -> getCategory ( $param [ 'id' ]);
if ( $category ) {
$customCategoryRepository -> recordAccess ( $param [ 'id' ]);
}
// update access per session only once a day
if ( array_key_exists ( $id , $_SESSION [ 'customCategoryAccess' ]) && $_SESSION [ 'customCategoryAccess' ][ $id ] > time () - 86400 ) {
return ;
return $category ;
}
$_SESSION [ 'customCategoryAccess' ][ $id ] = time ();
if ( isset ( $param [ 'action' ]) && $param [ 'action' ] === 'save' ) {
$id = $customCategoryRepository -> saveCategory ( $content );
$customCategoryRepository -> recordAccess ( $id );
$stmt = $db -> prepare ( " insert into customCategoryAccess (id) values (:id) " );
$stmt -> bindValue ( ':id' , $id );
$stmt -> execute ();
return $id ;
}
}