This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/contrib/admin/main.php

180 lines
4.6 KiB
PHP
Raw Normal View History

<?php
2010-01-05 10:11:53 +00:00
/**
* Name: Admin Controls
* Author: Shish <webmaster@shishnet.org>
* Link: http://code.shishnet.org/shimmie2/
* License: GPLv2
* Description: Various things to make admins' lives easier
* Documentation:
* <p>Lowercase all tags:
* <br>Set all tags to lowercase for consistency
* <p>Recount tag use:
* <br>If the counts of images per tag get messed up somehow, this will reset them
* <p>Purge unused tags:
* <br>Get rid of all the tags that don't have any images associated with
* them (normally they were created as typos or spam); this is mostly for
* neatness, the performance gain is tiny...
* <p>Convert to InnoDB:
* <br>Convert your database tables to InnoDB, thus allowing shimmie to
* take advantage of useful InnoDB-only features (this should be done
* automatically, this button only exists as a backup). This only applies
* to MySQL -- all other databases come with useful features enabled
* as standard.
* <p>Database dump:
* <br>Download the contents of the database in plain text format, useful
* for backups.
*/
2009-07-21 03:18:40 +00:00
/**
* Sent when the admin page is ready to be added to
*/
class AdminBuildingEvent extends Event {
var $page;
public function AdminBuildingEvent($page) {
$this->page = $page;
}
}
2012-01-30 03:22:41 +00:00
class AdminPage extends SimpleExtension {
public function onPageRequest($event) {
global $page, $user;
2012-01-30 03:22:41 +00:00
if($event->page_matches("admin")) {
if(!$user->is_admin()) {
$this->theme->display_permission_denied($page);
}
else {
send_event(new AdminBuildingEvent($page));
}
}
2012-01-30 03:22:41 +00:00
if($event->page_matches("admin_utils")) {
2010-05-28 13:26:46 +00:00
if($user->is_admin() && $user->check_auth_token()) {
2009-05-08 11:43:45 +00:00
log_info("admin", "Util: {$_POST['action']}");
set_time_limit(0);
$redirect = false;
2009-01-04 19:18:37 +00:00
switch($_POST['action']) {
2010-12-23 14:00:50 +00:00
case 'delete by query':
$this->delete_by_query($_POST['query']);
$redirect = true;
break;
case 'lowercase all tags':
$this->lowercase_all_tags();
$redirect = true;
break;
case 'recount tag use':
$this->recount_tag_use();
$redirect = true;
break;
case 'purge unused tags':
$this->purge_unused_tags();
$redirect = true;
break;
case 'convert to innodb':
$this->convert_to_innodb();
$redirect = true;
break;
case 'database dump':
$this->dbdump($page);
break;
}
if($redirect) {
$page->set_mode("redirect");
$page->set_redirect(make_link("admin"));
}
}
}
2012-01-30 03:22:41 +00:00
}
2012-01-30 03:22:41 +00:00
public function onAdminBuilding($event) {
global $page;
$this->theme->display_page($page);
$this->theme->display_form($page);
}
2012-01-30 03:22:41 +00:00
public function onUserBlockBuilding($event) {
global $user;
if($user->is_admin()) {
$event->add_link("Board Admin", make_link("admin"));
}
}
2010-12-23 14:00:50 +00:00
private function delete_by_query($query) {
global $page, $user;
assert(strlen($query) > 1);
foreach(Image::find_images(0, 1000000, Tag::explode($query)) as $image) {
send_event(new ImageDeletionEvent($image));
}
}
private function lowercase_all_tags() {
global $database;
$database->execute("UPDATE tags SET tag=lower(tag)");
}
private function recount_tag_use() {
global $database;
2009-07-28 00:19:40 +00:00
$database->Execute("
UPDATE tags
SET count = COALESCE(
(SELECT COUNT(image_id) FROM image_tags WHERE tag_id=tags.id GROUP BY tag_id),
2009-07-28 00:19:40 +00:00
0
)");
}
private function purge_unused_tags() {
global $database;
$this->recount_tag_use();
$database->Execute("DELETE FROM tags WHERE count=0");
}
private function dbdump($page) {
$matches = array();
2012-02-02 03:53:03 +00:00
preg_match("#(\w+)://(\w+):(\w+)@([\w\.\-]+)/([\w_]+)(\?.*)?#", DATABASE_DSN, $matches);
$software = $matches[1];
$username = $matches[2];
$password = $matches[3];
$hostname = $matches[4];
$database = $matches[5];
switch($software) {
case 'mysql':
$cmd = "mysqldump -h$hostname -u$username -p$password $database";
break;
}
2009-01-04 19:18:37 +00:00
$page->set_mode("data");
$page->set_type("application/x-unknown");
$page->set_filename('shimmie-'.date('Ymd').'.sql');
$page->set_data(shell_exec($cmd));
}
private function check_for_orphanned_images() {
$orphans = array();
foreach(glob("images/*") as $dir) {
foreach(glob("$dir/*") as $file) {
$hash = str_replace("$dir/", "", $file);
if(!$this->db_has_hash($hash)) {
$orphans[] = $hash;
}
}
}
}
2011-01-01 19:00:30 +00:00
/*
private function convert_to_innodb() {
global $database;
if($database->engine->name == "mysql") {
$tables = $database->db->MetaTables();
foreach($tables as $table) {
log_info("upgrade", "converting $table to innodb");
$database->execute("ALTER TABLE $table TYPE=INNODB");
}
}
}
2011-01-01 19:00:30 +00:00
*/
}
?>