[SQL]

# Core
StartTrans=BEGIN
CommitTrans=COMMIT
CheckUser=SELECT userid,nickname,realname,realm,email FROM users WHERE email=? AND password=SHA1(?)
CheckUserOld=SELECT userid,nickname,realname,realm,email FROM users WHERE email=? AND password=OLD_PASSWORD(?)
CreateSession=INSERT INTO sessions (timeout,name,userid,realm,folderid,langcode,optionid,labyrinth) VALUES (?,?,?,?,1,?,?,?)
CheckSession=SELECT userid,name,realm,folderid,langcode,optionid FROM sessions WHERE labyrinth=?
DeleteSessions=DELETE FROM sessions WHERE timeout < ?
DeleteSession=DELETE FROM sessions WHERE labyrinth=?
CountSessions=SELECT DISTINCT s.userid,s.name as realname FROM sessions AS s \
    LEFT JOIN users AS u ON u.userid=s.userid
RetrieveSession=SELECT query FROM sessions WHERE labyrinth=?
StoreSession=UPDATE sessions SET query=? WHERE labyrinth=?
UpdateSession=UPDATE sessions SET $field=? WHERE labyrinth=?
UpdateSessionX=UPDATE sessions SET timeout=?,name=?,userid=?,realm=?,langcode=?,optionid=? WHERE labyrinth=?

AllRealms=SELECT * FROM realms
GetRealmByID=SELECT * FROM realms WHERE realmid=?
GetRealmByName=SELECT * FROM realms WHERE realm=?


# Permissions
AllAccess=SELECT * FROM access WHERE accessid <= ? ORDER BY accessid
GetPermission=SELECT a.folderid,a.groupid,a.userid,a.accessid \
    FROM acls AS a \
    INNER JOIN folders AS f ON a.folderid=f.folderid \
    WHERE a.folderid IN ($folders) AND (a.groupid IN ($groups) \
    OR a.userid = $user) \
    ORDER BY f.ref
UserACLs=SELECT * FROM acls AS l \
    INNER JOIN access AS a ON a.accessid=l.accessid \
    WHERE l.userid=?
UserACLCheck=SELECT * FROM acls WHERE userid=? AND accessid=? AND folderid=?
UserACLSave=INSERT INTO acls (userid,accessid,folderid) VALUES (?,?,?)
UserACLDelete=DELETE FROM acls WHERE userid=? AND accessid=? AND folderid=?
GroupACLs=SELECT * FROM acls AS l \
    INNER JOIN access AS a ON a.accessid=l.accessid \
    INNER JOIN groups AS g ON g.groupid=l.groupid \
    WHERE l.groupid=?
GroupACLSave=INSERT INTO acls (groupid,accessid,folderid) VALUES (?,?,?)
GroupACLDelete=DELETE FROM acls WHERE groupid=? AND accessid=? AND folderid=?


# Articles
AllArticles=SELECT * FROM articles $where ORDER BY createdate DESC $limit
GetArticlesLatest=SELECT title,userid,quickname FROM articles \
    WHERE sectionid=1 AND publish=3 ORDER BY createdate DESC
GetArticlesFrontPage=SELECT title,userid,quickname,snippet,imageid,createdate \
    FROM articles \
    WHERE sectionid=1 AND publish=3 AND front=1 \
    ORDER BY createdate DESC
GetArticleByName=SELECT * FROM articles WHERE quickname=?
GetArticleByID=SELECT * FROM articles WHERE articleid=?
AddArticle=INSERT INTO articles (folderid,title,userid,createdate, \
    sectionid,quickname,publish) VALUES (?,?,?,?,?,?,?)
SaveArticle=UPDATE articles SET folderid=?,title=?,userid=?, \
    sectionid=?,quickname=?,snippet=?,imageid=?,front=?,publish=?, \
    createdate=? \
    WHERE articleid=?
DeleteArticle=DELETE FROM articles WHERE articleid IN ($ids)
GetContent=SELECT p.*,i.link FROM paragraphs AS p \
    LEFT JOIN images AS i ON i.imageid=p.imageid \
    WHERE p.articleid=? ORDER BY p.orderno
AddContent=INSERT INTO paragraphs (articleid,orderno,type,imageid,href, \
    body,align)  \
    VALUES (?,?,?,?,?,?,?)
SaveContent=UPDATE paragraphs SET articleid=?,orderno=?,type=?,imageid=?, \
    href=?,body=?,align=? WHERE paraid=?
Relocate=UPDATE paragraphs SET orderno=? WHERE paraid=?
DeleteContent=DELETE FROM paragraphs WHERE paraid=?
DeleteArticleContent=DELETE FROM paragraphs WHERE articleid IN ($ids)
PromoteArticle=UPDATE articles SET publish=? WHERE articleid=?
FindTitle=SELECT articleid FROM articles WHERE title=?

CheckFrontPageArticles=SELECT articleid FROM articles \
    WHERE sectionid=1 AND publish=3 AND front=1
SetFrontPageArticle=UPDATE articles SET front=1 WHERE articleid=?
ClearFrontPageArticle=UPDATE articles SET front=0 WHERE articleid=?


# IP Address
FindIPAddress=SELECT * FROM ipindex WHERE ipaddr=?
AddIPAddress=INSERT INTO ipindex (author,type,ipaddr) VALUES (?,?,?)
SaveIPAddress=UPDATE ipindex SET author=?,type=? WHERE ipaddr=?


#Folders
AllFolders=SELECT * FROM folders ORDER BY folderid
GetFolder=SELECT * FROM folders WHERE folderid=?
GetFolderRef=SELECT ref FROM folders WHERE folderid=?
GetFolderIDs=SELECT folderid FROM folders WHERE ref IN ($folders)
GetFolderAccess=SELECT folderid FROM acls \
    (WHERE groupid IN ($groups) OR userid=$userid) AND accessid >= $access


#Hits
PageNow=SELECT now FROM updates WHERE pageid=0 AND area='site'
PageTimeStamp=SELECT pagets FROM updates WHERE area=? AND pageid=?
PageCounter=SELECT counter FROM hits WHERE area=? AND pageid=? AND photoid=?
AdminUpdates=SELECT * FROM updates ORDER BY pagets desc

GetUpdate=SELECT * FROM updates WHERE area=? AND pageid=?
SetUpdate=UPDATE updates SET now=? WHERE area=? AND pageid=?
AddUpdate=INSERT INTO updates (now,area,pageid) VALUES (?,?,?)

AddAHit=INSERT INTO hits (counter,area,pageid,photoid,query,createdate) VALUES (?,?,?,?,?,?)
GetAHit=SELECT SUM(counter) FROM hits WHERE area=? AND query=?
SumHits=SELECT area,query,pageid,photoid,SUM(counter) AS counter,count(*) AS number,max(createdate) AS createdate FROM hits WHERE createdate <= ? AND area=? GROUP BY query,pageid,photoid
DelHits=DELETE FROM hits WHERE createdate <= ? AND area=?
DelAHit=DELETE FROM hits WHERE createdate <= ? AND area=? AND pageid=? AND photoid=? AND query=?
GetHitAreas=SELECT area,count(*) AS count FROM hits GROUP BY area ORDER BY count

PageHitsAllTime=SELECT sum(h.counter) AS counter,h.area,h.query \
    FROM hits AS h \
    WHERE h.pageid=0 AND h.photoid=0 \
    GROUP BY h.area, h.query \
    ORDER BY counter DESC,h.area,h.query LIMIT 10
PageHitsLastMonth=SELECT sum(h.counter) AS counter,h.area,h.query \
    FROM hits AS h \
    WHERE h.pageid=0 AND h.photoid=0 AND h.createdate > ? \
    GROUP BY h.area, h.query \
    ORDER BY counter DESC,h.area,h.query LIMIT 20
AlbumHitsAllTime=SELECT * FROM hits WHERE pageid > 0 AND photoid = 0 LIMIT 10
AlbumHitsLastMonth=SELECT * FROM hits WHERE pageid > 0 AND photoid = 0 AND createdate > ? LIMIT 10
PhotoHitsAllTime=SELECT * FROM hits WHERE photoid > 0 LIMIT 10
PhotoHitsLastMonth=SELECT * FROM hits WHERE photoid > 0 AND createdate > ? LIMIT 10

AllHits=SELECT * FROM hits


#Images
GetAllImages=SELECT * FROM images
GetImagesByType=SELECT * FROM images WHERE type=? ORDER BY link,tag
GetDefaultImages=SELECT * FROM images WHERE tag='DEFAULT'
GetImageByID=SELECT * FROM images WHERE imageid=?
AddImage=INSERT INTO images (tag,link,type,href,dimensions) VALUES (?,?,?,?,?)
SaveImage=UPDATE images SET tag=?,link=?,type=?,href=?,dimensions=? WHERE imageid=?
DeleteImage=DELETE FROM images WHERE imageid=?
AllImageStock=SELECT * FROM imagestock


#Menus
AllMenus=SELECT * FROM menus
GetMenus=SELECT * FROM menus WHERE realmid=?
GetMenuByID=SELECT * FROM menus WHERE menuid=?
GetAllOptions=SELECT o.optionid,m.title,o.text FROM options AS o \
    INNER JOIN menus AS m ON o.menuid=m.menuid \
    WHERE o.menuid != ? AND o.text != '' ORDER BY m.title,o.name
GetOptions=SELECT * FROM options WHERE menuid=? ORDER BY orderno
FindOptions=SELECT * FROM options WHERE menuid IN ($ids)
AddMenu=INSERT INTO menus (name,title,typeid,realmid,parentid) VALUES (?,?,?,?,?)
AddOption=INSERT INTO options (menuid,orderno) VALUES (?,?)
SaveMenu=UPDATE menus SET name=?,title=?,typeid=?,realmid=?,parentid=? WHERE menuid=?
SaveOption=UPDATE options SET orderno=?,name=?,section=?,text=?,href=?,accessid=? WHERE optionid=?
DeleteMenu=DELETE FROM menus WHERE menuid IN ($ids)
DeleteOptions=DELETE FROM options WHERE optionid IN ($ids)

GetOptImages=SELECT * FROM optimages AS o \
    INNER JOIN images AS i ON o.imageid=i.imageid \
    WHERE optionid=? ORDER BY typeid
AddOptImage=INSERT INTO optimages (imageid,optionid,typeid) VALUES (?,?,?)
SaveOptImage=UPDATE optimages SET imageid=? WHERE optionid=? AND typeid=?
DeleteOptImages=DELETE FROM optimages WHERE optionid IN ($ids)


# Requests
AllRequests=SELECT * FROM requests ORDER BY section,command
AllSections=SELECT DISTINCT(section) FROM requests ORDER BY section
AllRequestSection=SELECT * FROM requests WHERE section=? ORDER BY section,command
GetRequestByID=SELECT * FROM requests WHERE requestid=?
GetRequest=SELECT * FROM requests WHERE section=? AND command=?
AddRequest=INSERT INTO requests (section,command,actions,layout,content,onsuccess,onerror,onfailure,secure,rewrite) VALUES (?,?,?,?,?,?,?,?,?,?)
SaveRequest=UPDATE requests SET section=?,command=?,actions=?,layout=?,content=?,onsuccess=?,onerror=?,onfailure=?,secure=?,rewrite=? WHERE requestid=?
DeleteRequests=DELETE FROM requests WHERE requestid IN ($ids)


#Metadata
MetaCloudArt=SELECT count(metadata) as count, metadata \
    FROM mxarticles AS x \
    INNER JOIN articles AS a ON a.articleid=x.articleid \
    WHERE sectionid IN ($ids) GROUP BY metadata;
MetaSearchArt=SELECT x.articleid AS id,a.* FROM mxarticles AS x \
    INNER JOIN articles AS a ON a.articleid=x.articleid \
    WHERE metadata IN ($meta) $where $limit
MetaDetailArt=SELECT DISTINCT(a.articleid) AS id,a.* FROM articles AS a \
    INNER JOIN paragraphs AS p ON a.articleid=p.articleid \
    INNER JOIN mxarticles AS x ON a.articleid=x.articleid \
    WHERE (a.title REGEXP '$data' OR p.body REGEXP '$data' OR x.metadata IN ($meta)) $where $limit
MetaDeleteArt=DELETE FROM mxarticles WHERE articleid=?
MetaUpdateArt=INSERT INTO mxarticles (articleid,metadata) VALUES (?,?)
MetaGetArt=SELECT * FROM mxarticles WHERE articleid=?

MetaSearchImage=SELECT * FROM photos AS i \
    INNER JOIN imetadata AS m ON i.photoid=m.imageid \
    WHERE tag IN ($meta) $where $limit
MetaUpdateImage=INSERT INTO imetadata (imageid,tag) VALUES (?,?)
MetaDeleteImage=DELETE FROM imetadata WHERE imageid=?
MetaGetImage=SELECT * FROM imetadata WHERE imageid=?


#Users
GetUserByID=SELECT u.*,i.link,i.tag \
    FROM users AS u \
    INNER JOIN images AS i ON u.imageid=i.imageid \
    WHERE u.userid=?
SearchUserNames=SELECT * FROM users AS u \
    LEFT JOIN images AS i ON u.imageid=i.imageid \
    WHERE (nickname LIKE ? OR realname LIKE ?) $where \
    ORDER BY $order
SearchUsers=SELECT * FROM users AS u \
    LEFT JOIN images AS i ON u.imageid=i.imageid \
    WHERE userid > 1 AND (nickname LIKE ? OR realname LIKE ?) $where \
    ORDER BY $order
AllUsers=SELECT * FROM users
NewUser=INSERT INTO users (password,accessid,search,realm,nickname,realname,email,imageid) VALUES (SHA1(?),?,?,?,?,?,?,?)
SaveUser=UPDATE users SET nickname=?,realname=?,email=?,imageid=? WHERE userid=?
AdminSaveUser=UPDATE users SET accessid=?,search=?,realm=?,nickname=?,realname=?,email=?,imageid=? WHERE userid=?
SaveUserInfo=UPDATE users SET aboutme=?,url=? WHERE userid=?
DeleteUsers=DELETE FROM users WHERE userid IN ($ids) AND accessid < 5

GetUserInfoByID=SELECT * FROM users WHERE userid=?

ChangePassword=UPDATE users SET password=SHA1(?) WHERE userid=?
BanUsers=UPDATE users SET password=? WHERE userid IN ($ids) AND accessid < 5
FindUser=SELECT userid,password,realname FROM users WHERE email = ?


# Groups
AllGroups=SELECT * FROM groups $where ORDER BY groupname
GroupCount=SELECT COUNT(linkid) AS count FROM ixusergroup WHERE groupid = ? AND type=1 GROUP BY groupid
GetGroup=SELECT * FROM groups WHERE groupid=?
LinkUsers=SELECT i.*,u.email,u.nickname,u.realname FROM ixusergroup AS i \
    INNER JOIN users AS u ON i.linkid=u.userid \
    WHERE i.type=1 AND i.groupid=? \
    ORDER BY u.realname
LinkedUsers=SELECT i.*,g.groupname,g.member FROM ixusergroup AS i \
    INNER JOIN groups AS g ON i.groupid=g.groupid \
    WHERE i.type=1 AND i.linkid=?
LinkGroups=SELECT i.*,g.groupname FROM ixusergroup AS i \
    INNER JOIN groups AS g ON i.linkid=g.groupid \
    WHERE i.type=2 AND i.groupid=?
AllGroupIndex=SELECT * FROM ixusergroup WHERE type=2
AddGroup=INSERT INTO groups (groupname,master) VALUES (?,0)
AddLinkIndex=INSERT INTO ixusergroup (type,linkid,groupid) VALUES (?,?,?)
SaveGroup=UPDATE groups SET groupname=? WHERE groupid=?
DeleteGroup=DELETE FROM groups WHERE groupid=?
DeleteGroupIndex=DELETE FROM ixusergroup WHERE groupid=?
DeleteLinkIndices=DELETE FROM ixusergroup WHERE linkid=? AND i.type=?
DeleteLinkIndex=DELETE FROM ixusergroup \
    WHERE type=? AND linkid=? AND groupid=?
GetGroupID=SELECT groupid FROM groups WHERE groupname=?
UserGroups=SELECT i.*,g.groupname FROM ixusergroup AS i \
    INNER JOIN groups AS g ON i.groupid=g.groupid \
    WHERE type=1 AND linkid=?

GetGroupUserMap=SELECT groupid FROM ixusergroup WHERE type=1 AND linkid=?
GetGroupParents=SELECT groupid FROM ixusergroup WHERE type=2 \
    AND linkid IN ($groups)


