* Link: http://code.shishnet.org/shimmie2/ * License: GPLv2 * Description: Various things to make admins' lives easier * Documentation: *
Lowercase all tags:
*
Set all tags to lowercase for consistency
*
Recount tag use:
*
If the counts of images per tag get messed up somehow, this will reset them
*
Purge unused tags:
*
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...
*
Convert to InnoDB:
*
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.
*
Database dump:
*
Download the contents of the database in plain text format, useful
* for backups.
*/
/**
* Sent when the admin page is ready to be added to
*/
class AdminBuildingEvent extends Event {
var $page;
public function AdminBuildingEvent(Page $page) {
$this->page = $page;
}
}
class AdminPage extends Extension {
public function onPageRequest(PageRequestEvent $event) {
global $page, $user;
if($event->page_matches("admin")) {
if(!$user->is_admin()) {
$this->theme->display_permission_denied();
}
else {
send_event(new AdminBuildingEvent($page));
}
}
if($event->page_matches("admin_utils")) {
if($user->is_admin() && $user->check_auth_token()) {
log_info("admin", "Util: {$_POST['action']}");
set_time_limit(0);
$redirect = false;
switch($_POST['action']) {
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;
case 'reset image ids':
$this->reset_imageids();
$redirect = true;
break;
case 'image dump':
$this->imgdump($page);
break;
}
if($redirect) {
$page->set_mode("redirect");
$page->set_redirect(make_link("admin"));
}
}
}
}
public function onAdminBuilding(AdminBuildingEvent $event) {
global $page;
$this->theme->display_page($page);
$this->theme->display_form($page);
}
public function onUserBlockBuilding(UserBlockBuildingEvent $event) {
global $user;
if($user->is_admin()) {
$event->add_link("Board Admin", make_link("admin"));
}
}
private function delete_by_query(/*array(string)*/ $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;
$database->Execute("
UPDATE tags
SET count = COALESCE(
(SELECT COUNT(image_id) FROM image_tags WHERE tag_id=tags.id GROUP BY tag_id),
0
)");
}
private function purge_unused_tags() {
global $database;
$this->recount_tag_use();
$database->Execute("DELETE FROM tags WHERE count=0");
}
private function dbdump(Page $page) {
$matches = array();
preg_match("#^(?P