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()) {
|
public function execute($query, $args=array()) {
|
||||||
$result = $this->db->Execute($query, $args);
|
$result = $this->db->Execute($query, $args);
|
||||||
if($result === False) {
|
if($result === False) {
|
||||||
@ -215,70 +215,6 @@ class Database {
|
|||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
// tags {{{
|
// 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 $posted;
|
||||||
var $source;
|
var $source;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Constructors and other instance creators
|
||||||
|
*/
|
||||||
public function Image($row=null) {
|
public function Image($row=null) {
|
||||||
global $config;
|
global $config;
|
||||||
global $database;
|
global $database;
|
||||||
@ -83,6 +86,9 @@ class Image {
|
|||||||
return $images;
|
return $images;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Image-related utility functions
|
||||||
|
*/
|
||||||
public static function count_images(Config $config, Database $database, $tags=array()) {
|
public static function count_images(Config $config, Database $database, $tags=array()) {
|
||||||
if(count($tags) == 0) {
|
if(count($tags) == 0) {
|
||||||
return $database->db->GetOne("SELECT COUNT(*) FROM images");
|
return $database->db->GetOne("SELECT COUNT(*) FROM images");
|
||||||
@ -100,6 +106,9 @@ class Image {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Accessors & mutators
|
||||||
|
*/
|
||||||
public function get_next($tags=array(), $next=true) {
|
public function get_next($tags=array(), $next=true) {
|
||||||
assert(is_array($tags));
|
assert(is_array($tags));
|
||||||
assert(is_bool($next));
|
assert(is_bool($next));
|
||||||
@ -130,13 +139,6 @@ class Image {
|
|||||||
return $this->get_next($tags, false);
|
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() {
|
public function get_owner() {
|
||||||
return User::by_id($this->config, $this->database, $this->owner_id);
|
return User::by_id($this->config, $this->database, $this->owner_id);
|
||||||
}
|
}
|
||||||
@ -221,6 +223,86 @@ class Image {
|
|||||||
return $this->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") {
|
public function parse_link_template($tmpl, $_escape="url_escape") {
|
||||||
// don't bother hitting the database if it won't be used...
|
// don't bother hitting the database if it won't be used...
|
||||||
$safe_tags = "";
|
$safe_tags = "";
|
||||||
|
@ -6,11 +6,11 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class SourceSetEvent extends Event {
|
class SourceSetEvent extends Event {
|
||||||
var $image_id;
|
var $image;
|
||||||
var $source;
|
var $source;
|
||||||
|
|
||||||
public function SourceSetEvent($image_id, $source) {
|
public function SourceSetEvent($image, $source) {
|
||||||
$this->image_id = $image_id;
|
$this->image = $image;
|
||||||
$this->source = $source;
|
$this->source = $source;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -23,11 +23,11 @@ class SourceSetEvent extends Event {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class TagSetEvent extends Event {
|
class TagSetEvent extends Event {
|
||||||
var $image_id;
|
var $image;
|
||||||
var $tags;
|
var $tags;
|
||||||
|
|
||||||
public function TagSetEvent($image_id, $tags) {
|
public function TagSetEvent($image, $tags) {
|
||||||
$this->image_id = $image_id;
|
$this->image = $image;
|
||||||
$this->tags = tag_explode($tags);
|
$this->tags = tag_explode($tags);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -61,10 +61,9 @@ class TagEdit implements Extension {
|
|||||||
|
|
||||||
if($event instanceof ImageInfoSetEvent) {
|
if($event instanceof ImageInfoSetEvent) {
|
||||||
if($this->can_tag()) {
|
if($this->can_tag()) {
|
||||||
global $database;
|
send_event(new TagSetEvent($event->image, $_POST['tag_edit__tags']));
|
||||||
send_event(new TagSetEvent($event->image_id, $_POST['tag_edit__tags']));
|
|
||||||
if($this->can_source()) {
|
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 {
|
else {
|
||||||
@ -73,18 +72,15 @@ class TagEdit implements Extension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($event instanceof TagSetEvent) {
|
if($event instanceof TagSetEvent) {
|
||||||
global $database;
|
$event->image->set_tags($event->tags);
|
||||||
$database->set_tags($event->image_id, $event->tags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($event instanceof SourceSetEvent) {
|
if($event instanceof SourceSetEvent) {
|
||||||
global $database;
|
$event->image->set_source($event->source);
|
||||||
$database->set_source($event->image_id, $event->source);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($event instanceof ImageDeletionEvent) {
|
if($event instanceof ImageDeletionEvent) {
|
||||||
global $database;
|
$event->image->delete_tags_from_image();
|
||||||
$database->delete_tags_from_image($event->image->id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if($event instanceof AdminBuildingEvent) {
|
if($event instanceof AdminBuildingEvent) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user