a bunch of image functions moved to the image class
This commit is contained in:
parent
b6480f076e
commit
93b7467fdd
@ -151,7 +151,7 @@ class Database {
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
// misc {{{
|
||||
// safety wrapping {{{
|
||||
public function execute($query, $args=array()) {
|
||||
$result = $this->db->Execute($query, $args);
|
||||
if($result === False) {
|
||||
@ -173,7 +173,7 @@ class Database {
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public function get_row($query, $args=array()) {
|
||||
$result = $this->db->GetRow($query, $args);
|
||||
if($result === False) {
|
||||
@ -215,70 +215,6 @@ class Database {
|
||||
}
|
||||
// }}}
|
||||
// tags {{{
|
||||
public function resolve_alias($tag) {
|
||||
assert(is_string($tag));
|
||||
$newtag = $this->db->GetOne("SELECT newtag FROM aliases WHERE oldtag=?", array($tag));
|
||||
if(!empty($newtag)) {
|
||||
return $newtag;
|
||||
} else {
|
||||
return $tag;
|
||||
}
|
||||
}
|
||||
|
||||
public function resolve_wildcard($tag) {
|
||||
if(strpos($tag, "%") === false && strpos($tag, "_") === false) {
|
||||
return array($tag);
|
||||
}
|
||||
else {
|
||||
$newtags = $this->db->GetCol("SELECT tag FROM tags WHERE tag LIKE ?", array($tag));
|
||||
if(count($newtags) > 0) {
|
||||
$resolved = $newtags;
|
||||
} else {
|
||||
$resolved = array($tag);
|
||||
}
|
||||
return $resolved;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function sanitise($tag) {
|
||||
assert(is_string($tag));
|
||||
$tag = preg_replace("/[\s?*]/", "", $tag);
|
||||
$tag = preg_replace("/\.+/", ".", $tag);
|
||||
$tag = preg_replace("/^(\.+[\/\\\\])+/", "", $tag);
|
||||
return $tag;
|
||||
}
|
||||
|
||||
public function delete_tags_from_image($image_id) {
|
||||
assert(is_numeric($image_id));
|
||||
$this->execute("UPDATE tags SET count = count - 1 WHERE id IN (SELECT tag_id FROM image_tags WHERE image_id = ?)", array($image_id));
|
||||
$this->execute("DELETE FROM image_tags WHERE image_id=?", array($image_id));
|
||||
}
|
||||
|
||||
public function set_tags($image_id, $tags) {
|
||||
assert(is_numeric($image_id));
|
||||
$tags = tag_explode($tags);
|
||||
|
||||
$tags = array_map(array($this, 'resolve_alias'), $tags);
|
||||
$tags = array_map(array($this, 'sanitise'), $tags);
|
||||
$tags = array_iunique($tags); // remove any duplicate tags
|
||||
|
||||
// delete old
|
||||
$this->delete_tags_from_image($image_id);
|
||||
|
||||
// insert each new tag
|
||||
foreach($tags as $tag) {
|
||||
$this->execute("INSERT IGNORE INTO tags(tag) VALUES (?)", array($tag));
|
||||
$this->execute("INSERT INTO image_tags(image_id, tag_id) VALUES(?, (SELECT id FROM tags WHERE tag = ?))", array($image_id, $tag));
|
||||
$this->execute("UPDATE tags SET count = count + 1 WHERE tag = ?", array($tag));
|
||||
}
|
||||
}
|
||||
|
||||
public function set_source($image_id, $source) {
|
||||
assert(is_numeric($image_id));
|
||||
if(empty($source)) $source = null;
|
||||
$this->execute("UPDATE images SET source=? WHERE id=?", array($source, $image_id));
|
||||
}
|
||||
// }}}
|
||||
}
|
||||
?>
|
||||
|
@ -26,6 +26,9 @@ class Image {
|
||||
var $posted;
|
||||
var $source;
|
||||
|
||||
/*
|
||||
* Constructors and other instance creators
|
||||
*/
|
||||
public function Image($row=null) {
|
||||
global $config;
|
||||
global $database;
|
||||
@ -83,6 +86,9 @@ class Image {
|
||||
return $images;
|
||||
}
|
||||
|
||||
/*
|
||||
* Image-related utility functions
|
||||
*/
|
||||
public static function count_images(Config $config, Database $database, $tags=array()) {
|
||||
if(count($tags) == 0) {
|
||||
return $database->db->GetOne("SELECT COUNT(*) FROM images");
|
||||
@ -100,6 +106,9 @@ class Image {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Accessors & mutators
|
||||
*/
|
||||
public function get_next($tags=array(), $next=true) {
|
||||
assert(is_array($tags));
|
||||
assert(is_bool($next));
|
||||
@ -130,13 +139,6 @@ class Image {
|
||||
return $this->get_next($tags, false);
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
$this->database->execute("DELETE FROM images WHERE id=?", array($this->id));
|
||||
|
||||
unlink($this->get_image_filename());
|
||||
unlink($this->get_thumb_filename());
|
||||
}
|
||||
|
||||
public function get_owner() {
|
||||
return User::by_id($this->config, $this->database, $this->owner_id);
|
||||
}
|
||||
@ -220,6 +222,86 @@ class Image {
|
||||
public function get_source() {
|
||||
return $this->source;
|
||||
}
|
||||
|
||||
public function set_source($source) {
|
||||
if(empty($source)) $source = null;
|
||||
$this->database->execute("UPDATE images SET source=? WHERE id=?", array($source, $this->id));
|
||||
}
|
||||
|
||||
public function delete_tags_from_image() {
|
||||
$this->database->execute(
|
||||
"UPDATE tags SET count = count - 1 WHERE id IN ".
|
||||
"(SELECT tag_id FROM image_tags WHERE image_id = ?)", array($this->id));
|
||||
$this->database->execute("DELETE FROM image_tags WHERE image_id=?", array($this->id));
|
||||
}
|
||||
|
||||
public function set_tags($tags) {
|
||||
$tags = tag_explode($tags);
|
||||
|
||||
$tags = array_map(array($this, 'resolve_alias'), $tags);
|
||||
$tags = array_map(array($this, 'sanitise'), $tags);
|
||||
$tags = array_iunique($tags); // remove any duplicate tags
|
||||
|
||||
// delete old
|
||||
$this->delete_tags_from_image();
|
||||
|
||||
// insert each new tag
|
||||
foreach($tags as $tag) {
|
||||
$this->database->execute(
|
||||
"INSERT IGNORE INTO tags(tag) VALUES (?)",
|
||||
array($tag));
|
||||
$this->database->execute(
|
||||
"INSERT INTO image_tags(image_id, tag_id) ".
|
||||
"VALUES(?, (SELECT id FROM tags WHERE tag = ?))",
|
||||
array($image_id, $tag));
|
||||
$this->database->execute(
|
||||
"UPDATE tags SET count = count + 1 WHERE tag = ?",
|
||||
array($tag));
|
||||
}
|
||||
}
|
||||
|
||||
public function resolve_alias($tag) {
|
||||
assert(is_string($tag));
|
||||
$newtag = $this->database->db->GetOne("SELECT newtag FROM aliases WHERE oldtag=?", array($tag));
|
||||
if(!empty($newtag)) {
|
||||
return $newtag;
|
||||
} else {
|
||||
return $tag;
|
||||
}
|
||||
}
|
||||
|
||||
public function resolve_wildcard($tag) {
|
||||
if(strpos($tag, "%") === false && strpos($tag, "_") === false) {
|
||||
return array($tag);
|
||||
}
|
||||
else {
|
||||
$newtags = $this->database->db->GetCol("SELECT tag FROM tags WHERE tag LIKE ?", array($tag));
|
||||
if(count($newtags) > 0) {
|
||||
$resolved = $newtags;
|
||||
} else {
|
||||
$resolved = array($tag);
|
||||
}
|
||||
return $resolved;
|
||||
}
|
||||
}
|
||||
|
||||
public function sanitise($tag) {
|
||||
assert(is_string($tag));
|
||||
$tag = preg_replace("/[\s?*]/", "", $tag);
|
||||
$tag = preg_replace("/\.+/", ".", $tag);
|
||||
$tag = preg_replace("/^(\.+[\/\\\\])+/", "", $tag);
|
||||
return $tag;
|
||||
}
|
||||
|
||||
/*
|
||||
* Other actions
|
||||
*/
|
||||
public function delete() {
|
||||
$this->database->execute("DELETE FROM images WHERE id=?", array($this->id));
|
||||
|
||||
unlink($this->get_image_filename());
|
||||
unlink($this->get_thumb_filename());
|
||||
}
|
||||
|
||||
public function parse_link_template($tmpl, $_escape="url_escape") {
|
||||
// don't bother hitting the database if it won't be used...
|
||||
|
@ -6,11 +6,11 @@
|
||||
*
|
||||
*/
|
||||
class SourceSetEvent extends Event {
|
||||
var $image_id;
|
||||
var $image;
|
||||
var $source;
|
||||
|
||||
public function SourceSetEvent($image_id, $source) {
|
||||
$this->image_id = $image_id;
|
||||
public function SourceSetEvent($image, $source) {
|
||||
$this->image = $image;
|
||||
$this->source = $source;
|
||||
}
|
||||
}
|
||||
@ -23,11 +23,11 @@ class SourceSetEvent extends Event {
|
||||
*
|
||||
*/
|
||||
class TagSetEvent extends Event {
|
||||
var $image_id;
|
||||
var $image;
|
||||
var $tags;
|
||||
|
||||
public function TagSetEvent($image_id, $tags) {
|
||||
$this->image_id = $image_id;
|
||||
public function TagSetEvent($image, $tags) {
|
||||
$this->image = $image;
|
||||
$this->tags = tag_explode($tags);
|
||||
}
|
||||
}
|
||||
@ -61,10 +61,9 @@ class TagEdit implements Extension {
|
||||
|
||||
if($event instanceof ImageInfoSetEvent) {
|
||||
if($this->can_tag()) {
|
||||
global $database;
|
||||
send_event(new TagSetEvent($event->image_id, $_POST['tag_edit__tags']));
|
||||
send_event(new TagSetEvent($event->image, $_POST['tag_edit__tags']));
|
||||
if($this->can_source()) {
|
||||
send_event(new SourceSetEvent($event->image_id, $_POST['tag_edit__source']));
|
||||
send_event(new SourceSetEvent($event->image, $_POST['tag_edit__source']));
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -73,18 +72,15 @@ class TagEdit implements Extension {
|
||||
}
|
||||
|
||||
if($event instanceof TagSetEvent) {
|
||||
global $database;
|
||||
$database->set_tags($event->image_id, $event->tags);
|
||||
$event->image->set_tags($event->tags);
|
||||
}
|
||||
|
||||
if($event instanceof SourceSetEvent) {
|
||||
global $database;
|
||||
$database->set_source($event->image_id, $event->source);
|
||||
$event->image->set_source($event->source);
|
||||
}
|
||||
|
||||
if($event instanceof ImageDeletionEvent) {
|
||||
global $database;
|
||||
$database->delete_tags_from_image($event->image->id);
|
||||
$event->image->delete_tags_from_image();
|
||||
}
|
||||
|
||||
if($event instanceof AdminBuildingEvent) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user