Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge work on fixing the add url relationship form.

This merge contains the following commits:

Convert LinkType to a Moose class

Match attribute init_args to database fields

Fix building relate to URL form. Still needs to show hierarchy

Fix regressions in URL relationship form template

Convert MB::S::Link to be a Moose class

Display URL relationship types in a hierarchy

git-svn-id: http://svn.musicbrainz.org/mb_server/trunk@11251 b0b80210-5d09-0410-99dd-b4bd03f891c0
  • Loading branch information...
commit 18c9bf4761a21649fb18cc6271b2a18074cb8715 1 parent eccc75b
acid2 authored
View
26 lib/MusicBrainz/Server/Form/Relate/Url.pm
@@ -54,27 +54,27 @@ sub options_type
my $mb = new MusicBrainz;
$mb->Login;
- my $lt = new MusicBrainz::Server::LinkType($mb->{dbh}, [ $type, 'url' ]);
- my $root = $lt->Root;
+ my $lt = new MusicBrainz::Server::LinkType($mb->dbh, [ $type, 'url' ]);
+ my $root = $lt->root;
- my @options;
+ my @options = ('||', "[ Please select a relationship type ]");
- push @options, ('||', "[Please select a relationship type]");
-
- my @q = map { [$_,0] } $root;
- while (my $l = shift @q)
+ # @nodes is a list of [ node, hierarchy level ]
+ my @nodes = ( [ $root, 0] );
+ while (my $n = shift @nodes)
{
- unshift @q, map { [$_,$l->[1]+1] } $l->[0]->Children;
- next if ($l->[0]->name eq "ROOT");
- next if ($l->[0]->id == 32); # Add CC license -- don't show here, let people go to addcc
+ my $node = $n->[0];
+ unshift @nodes, map { [ $_, $n->[1] + 1 ] } $node->children;
+ next if ($node->name eq "ROOT");
+ next if ($node->id == 32); # Add CC license -- don't show here, let people go to addcc
- my $text = $l->[0]->GetLinkPhrase;
+ my $text = $node->link_phrase;
$text =~ s/\{(\w+:)/{/;
# add x times indentation like specified in the relationship type hierarchy
- $text = ("  " x $l->[1]) . $text;
+ $text = ("  " x $n->[1]) . $text;
- my $value = $l->[0]->id . "|" . $l->[0]->attributes;
+ my $value = $node->id . "|" . ($node->attributes || '');
push @options, ($value, $text);
}
View
355 lib/MusicBrainz/Server/Link.pm
@@ -1,34 +1,10 @@
-#!/usr/bin/perl -w
-# vi: set ts=4 sw=4 :
-#____________________________________________________________________________
-#
-# MusicBrainz -- the open internet music database
-#
-# Copyright (C) 2000 Robert Kaye
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-#
-# $Id$
-#____________________________________________________________________________
-
-use strict;
-
package MusicBrainz::Server::Link;
+use Moose;
+extends 'TableBase';
use Carp qw( croak );
-use base qw( TableBase );
+use MooseX::AttributeHelpers;
+
require MusicBrainz::Server::LinkEntity;
require MusicBrainz::Server::Attribute;
require MusicBrainz::Server::URL;
@@ -36,167 +12,207 @@ require MusicBrainz::Server::Artist;
require MusicBrainz::Server::Release;
require MusicBrainz::Server::Track;
-################################################################################
-# Bare Constructor
-################################################################################
-
-sub new
-{
- my ($class, $dbh, $types) = @_;
-
- my $self = $class->SUPER::new($dbh);
-
- # So far, links are always between two things. This may change one day.
- # if anything else other than two types are passed in, this object will
- # be an on-the-fly object (read: const object)
- if (defined $types && @$types == 2)
- {
- ref($types) eq "ARRAY" or return undef;
-
- MusicBrainz::Server::LinkEntity->ValidateTypes($types)
- or return undef;
- my @t = @$types;
- $self->{_types} = \@t;
- $self->{_table} = "l_" . join "_", @t;
- }
-
- $self;
-}
+=head1 ATTRIBUTES
-################################################################################
-# Properties
-################################################################################
+=head2 table
-sub Table { $_[0]{_table} }
+The table storing information about this link
-# Get/id implemented by TableBase
-sub Links { wantarray ? @{ $_[0]{_links} } : $_[0]{_links} }
-sub Types { wantarray ? @{ $_[0]{_types} } : $_[0]{_types} }
-sub GetNumberOfLinks{ scalar @{ $_[0]{_types} } }
-sub _GetLinkFields { map { "link$_" } (0 .. $_[0]->GetNumberOfLinks-1) }
-sub GetLinkType { $_[0]{link_type} }
-sub SetLinkType { $_[0]{link_type} = $_[1] }
-# Get/SetModPending in TableBase
+=cut
-# Set all the link IDs at once
-sub SetLinks
-{
- my ($self, $ids) = @_;
- croak "Wrong number of IDs passed to SetIDs"
- unless @$ids == $self->GetNumberOfLinks;
- $self->{_links} = [ @$ids ];
- @$self{ $self->_GetLinkFields } = @$ids;
-}
+has 'table' => (
+ isa => 'Str',
+ is => 'ro'
+);
-# Get the MusicBrainz::Server::LinkType object associated with this link
-sub LinkType
-{
- my $self = shift;
- my $l = MusicBrainz::Server::LinkType->new($self->dbh, $self->{_types});
- $l->newFromId($self->GetLinkType);
-}
+has 'links' => (
+ isa => 'ArrayRef',
+ is => 'rw',
+ trigger => sub {
+ my ($self, $ids) = @_;
-# Get the linked entities - all of them (as an array or array ref)
-sub Entities
-{
- my $self = shift;
- my @e;
- push @e, $self->Entity(0);
- push @e, $self->Entity(1);
- wantarray ? @e : \@e;
-}
+ croak "Wrong number of IDs passed to SetIDs"
+ unless @$ids == $self->number_of_links;
-# Get the linked entities - one at a time
-sub Entity
-{
- my ($self, $index) = @_;
- croak "No index passed" unless defined $index;
- croak "Bad index passed" unless $index >= 0 and $index < $self->GetNumberOfLinks;
+ @$self{ $self->_get_link_fields } = @$ids;
+ }
+);
+
+has 'types' => (
+ isa => 'ArrayRef',
+ is => 'ro',
+ metaclass => 'Collection::List',
+ provides => {
+ count => 'number_of_links',
+ }
+);
- return MusicBrainz::Server::LinkEntity->newFromTypeAndId(
- $self->{dbh},
- $self->{_types}->[$index],
- $self->{'link' . $index},
- );
-}
+has 'link_type' => (
+ isa => 'Int',
+ is => 'rw',
+);
-sub begin_date
-{
- my ($self, $new_date) = @_;
+has 'begin_date' => (
+ isa => 'Str',
+ is => 'rw',
+ initarg => 'begindate',
+);
- if (defined $new_date) { $self->{begindate} = $new_date; }
- return defined $self->{begindate} ? $self->{begindate} : '';
-}
+has 'end_date' => (
+ isa => 'Str',
+ is => 'rw',
+ initarg => 'enddate',
+);
sub begin_date_ymd
{
my $self = shift;
- return ('', '', '') unless $self->begin_date();
+ return ('', '', '') unless $self->begin_date;
return map { $_ == 0 ? '' : $_ } split(m/-/, $self->begin_date);
}
-sub end_date
-{
- my ($self, $new_date) = @_;
-
- if (defined $new_date) { $self->{enddate} = $new_date; }
- return defined $self->{enddate} ? $self->{enddate} : '';
-}
-
sub end_date_ymd
{
my $self = shift;
-
- return ('', '', '') unless $self->end_date();
+
+ return ('', '', '') unless $self->end_date;
return map { $_ == 0 ? '' : $_ } split(m/-/, $self->end_date);
}
-################################################################################
-# Data Retrieval
-################################################################################
-
-sub _new_from_row
-{
- my $this = shift;
- my $self = $this->SUPER::_new_from_row(@_)
- or return;
+=head1 METHODS
- while (my ($k, $v) = each %$this)
- {
- $self->{$k} = $v
- if substr($k, 0, 1) eq "_";
- }
- $self->{dbh} = $this->dbh;
-
- my $n = scalar @{ $self->{_types} };
- $self->{_links} = [ map { $self->{"link$_"} } 0..$n-1 ];
+=head2 newFromId
- bless $self, ref($this) || $this;
-}
+=cut
sub newFromId
{
my ($self, $id) = @_;
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
my $row = $sql->SelectSingleRowHash(
- "SELECT * FROM $self->{_table} WHERE id = ?",
+ "SELECT * FROM $table WHERE id = ?",
$id,
);
$self->_new_from_row($row);
}
+=head2 newFromMBId
+
+=cut
+
sub newFromMBId
{
my ($self, $id) = @_;
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
my $row = $sql->SelectSingleRowHash(
- "SELECT * FROM $self->{_table} WHERE mbid = ?",
+ "SELECT * FROM $table WHERE mbid = ?",
$id,
);
$self->_new_from_row($row);
}
+sub _new_from_row
+{
+ my ($self, $dbh, $row) = @_;
+ my $new = $self->SUPER::_new_from_row($dbh, $row);
+
+ $new->{links} = $self->links;
+ $new->{types} = $self->types;
+ $new->{table} = $self->table;
+
+ return $new;
+}
+
+sub BUILDARGS
+{
+ my $self = shift;
+ my $dbh = shift;;
+
+ if ((scalar @_ == 1 && ref $_[0] eq 'HASH') || scalar @_ == 0)
+ {
+ # The user has called with a hash reference, so use
+ # a normal Moose constructor
+ my $args = shift || {};
+ return {
+ dbh => $dbh,
+ %$args,
+ };
+ }
+ else
+ {
+ # So far, links are always between two things. This may change one day.
+ my $types = shift;
+ ref($types) eq "ARRAY" or die "$types must be an array reference";
+ scalar @$types == 2 or die "$types must have length 2";
+
+ MusicBrainz::Server::LinkEntity->ValidateTypes($types)
+ or die "$types contains invalid types";
+
+ return {
+ dbh => $dbh,
+ types => $types,
+ table => "l_" . join "_", @$types,
+ };
+ }
+}
+
+sub _get_link_fields
+{
+ my $self = shift;
+ map { "link$_" } (0 .. ($self->number_of_links - 1));
+}
+
+=head2 link_type
+
+Get the MusicBrainz::Server::LinkType object associated with this link
+
+=cut
+
+sub link_type
+{
+ my $self = shift;
+ my $l = MusicBrainz::Server::LinkType->new($self->dbh, $self->types);
+ $l->newFromId($self->link_type);
+}
+
+=head2 entities
+
+Get all the linked entities (as an array ref)
+
+=cut
+
+sub entities
+{
+ my $self = shift;
+ return [ map { $self->entity($_) } (0 .. 1) ];
+}
+
+=head2 entity $index
+
+Get a linked entities by index C<$index>
+
+=cut
+
+sub entity
+{
+ my ($self, $index) = @_;
+ croak "No index passed" unless defined $index;
+ croak "Bad index passed" unless $index >= 0 and $index < $self->number_of_links;
+
+ return MusicBrainz::Server::LinkEntity->newFromTypeAndId(
+ $self->dbh,
+ $self->types->[$index],
+ $self->{'link' . $index},
+ );
+}
+
+################################################################################
+# Data Retrieval
+################################################################################
+
sub FindLinkedEntities
{
my $self = shift;
@@ -425,30 +441,32 @@ sub Exists
my $datewhere = "";
- my @links = $self->Links;
- my @args = ($self->GetLinkType, @links);
- if ($self->begin_date() =~ /\S/)
+ my @links = @{ $self->links };
+ my @args = ($self->link_type, @links);
+ if ($self->begin_date =~ /\S/)
{
$datewhere .= " AND begindate = ?";
- push @args, $self->begin_date();
+ push @args, $self->begin_date;
}
- if ($self->end_date() =~ /\S/)
+ if ($self->end_date =~ /\S/)
{
$datewhere .= " AND enddate = ?";
- push @args, $self->end_date();
+ push @args, $self->end_date;
}
+ my $table = $self->table;
my $row = $sql->SelectSingleRowHash(
- "SELECT * FROM $self->{_table} WHERE link_type = ? AND link0 = ? AND link1 = ? $datewhere",
+ "SELECT * FROM $table WHERE link_type = ? AND link0 = ? AND link1 = ? $datewhere",
@args
);
# If both entity types are the same, test the inverse as well.
- my @types = $self->Types;
+ my @types = $self->types;
if (!$row && $types[0] eq $types[1])
{
+ my $table = $self->table;
$row = $sql->SelectSingleRowHash(
- "SELECT * FROM $self->{_table} WHERE link_type = ? AND link1 = ? AND link0 = ? $datewhere",
+ "SELECT * FROM $self->$table WHERE link_type = ? AND link1 = ? AND link0 = ? $datewhere",
@args
);
}
@@ -472,18 +490,18 @@ sub Insert
if ($$entities[1]->{type} eq 'url' and not $entities->[1]{'id'})
{
my $urlobj = MusicBrainz::Server::URL->new($self->dbh);
-
+
$urlobj->Insert($$entities[1]->{url}, $$entities[1]->{desc})
or return undef;
-
+
$$entities[1]->{obj} = $urlobj;
$$entities[1]->{id} = $urlobj->id;
}
# Make a $self which contains all of the desired properties
- $self = $self->new($self->dbh, scalar($link_type->Types));
- $self->SetLinkType($link_type->id);
- $self->SetLinks([ map { $_->{id} } @$entities ]);
+ $self = $self->new($self->dbh, $link_type->types);
+ $self->link_type($link_type->id);
+ $self->links([ map { $_->{id} } @$entities ]);
$self->begin_date($begindate);
$self->end_date($enddate);
@@ -491,19 +509,20 @@ sub Insert
if ($self->Exists);
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
$sql->Do(
- "INSERT INTO $self->{_table} (link_type"
- . (join "", map { ", $_" } $self->_GetLinkFields)
+ "INSERT INTO $table (link_type"
+ . (join "", map { ", $_" } $self->_get_link_fields)
. ", begindate, enddate) VALUES (?"
- . (", ?" x $self->GetNumberOfLinks)
+ . (", ?" x $self->number_of_links)
. ", ?, ?)",
- $self->GetLinkType,
- $self->Links,
+ $self->link_type,
+ @{ $self->links },
$begindate || undef,
$enddate || undef,
);
- $self->id($sql->GetLastInsertId($self->{_table}));
+ $self->id($sql->GetLastInsertId($self->table));
$self->has_mod_pending(0);
$self;
@@ -514,8 +533,9 @@ sub Update
my $self = shift;
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
$sql->Do(
- "UPDATE $self->{_table} SET link0 = ?, link1 = ?, begindate = ?, enddate = ?, link_type = ? where id = ?",
+ "UPDATE $table SET link0 = ?, link1 = ?, begindate = ?, enddate = ?, link_type = ? where id = ?",
$self->Links,
$self->begin_date || undef,
$self->end_date || undef,
@@ -531,8 +551,9 @@ sub Delete
my $self = shift;
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
$sql->Do(
- "DELETE FROM $self->{_table} WHERE id = ?",
+ "DELETE FROM $table WHERE id = ?",
$self->id,
);
@@ -714,8 +735,8 @@ sub _Remove
foreach my $row (@tables)
{
my ($table, $type1, $type2, $link) = @$row;
- $self->{_table} = $table;
- $self->{_types} = [$type1, $type2];
+ $self->table($table);
+ $self->types([$type1, $type2]);
my $rows = $sql->SelectListOfHashes("SELECT id, link0, link1 FROM $table WHERE $link = ?", $entityid);
foreach my $row (@$rows)
{
@@ -733,7 +754,7 @@ sub _Remove
sub _link_type_matches_entities
{
my ($link_type, $entities) = @_;
- my $a = join ",", $link_type->Types;
+ my $a = join ",", @{ $link_type->types };
my $b = join ",", map { $_->{type} } @{$entities};
return if $a eq $b;
local $Carp::CarpLevel = $Carp::CarpLevel + 1;
View
406 lib/MusicBrainz/Server/LinkType.pm
@@ -1,38 +1,16 @@
-#!/usr/bin/perl -w
-# vi: set ts=4 sw=4 :
-#____________________________________________________________________________
-#
-# MusicBrainz -- the open internet music database
-#
-# Copyright (C) 2000 Robert Kaye
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-#
-# $Id$
-#____________________________________________________________________________
-
-use strict;
-
package MusicBrainz::Server::LinkType;
+use Moose;
+extends 'TableBase';
-use base qw( TableBase );
require MusicBrainz::Server::LinkEntity;
-################################################################################
-# Ensure each link table has a "root" row
-################################################################################
+=head1 PACKAGE METHODS
+
+=head2 InsertDefaultRows
+
+Ensure each link table has a "root" row
+
+=cut
sub InsertDefaultRows
{
@@ -67,139 +45,191 @@ sub InsertDefaultRows
$sql->Commit;
}
-################################################################################
-# Bare Constructor
-################################################################################
+=head1 ATTRIBUTES
-sub new
-{
- my ($class, $dbh, $types) = @_;
+=head2 table
- ref($types) eq "ARRAY" or die;
- # So far, links are always between two things. This may change one day.
- @$types == 2 or die;
+The table in the database the represents data abut this link
- MusicBrainz::Server::LinkEntity->ValidateTypes($types)
- or die;
+=cut
- my $self = $class->SUPER::new($dbh);
- my @t = @$types;
- $self->{_types} = \@t;
- $self->{_table} = "lt_" . join "_", @t;
+has 'table' => (
+ isa => 'Str',
+ is => 'ro',
+);
- $self;
-}
+=head2 parent
-################################################################################
-# Properties
-################################################################################
+The ID of the links parent
-sub Table { $_[0]{_table} }
+=cut
-sub GetParentId { $_[0]->{parent} }
-sub SetParentId { $_[0]->{parent} = $_[1] }
-sub Parent { $_[0]->newFromId($_[0]->GetParentId) }
-sub Children { $_[0]->newFromParentId($_[0]->id) }
+has 'parent_id' => (
+ isa => 'Int',
+ is => 'rw',
+ initarg => 'parent',
+);
-sub Types { wantarray ? @{ $_[0]{_types} } : $_[0]{_types} }
-sub GetNumberOfLinks{ scalar @{ $_[0]{_types} } }
-sub GetLinkPhrase { $_[0]->{linkphrase} }
-sub SetLinkPhrase { $_[0]->{linkphrase} = $_[1]; }
-sub GetReverseLinkPhrase { $_[0]->{rlinkphrase} }
-sub SetReverseLinkPhrase { $_[0]->{rlinkphrase} = $_[1]; }
+=head2 types
-sub description
-{
- my ($self, $new_desc) = @_;
+The source and destination types of this link.
- if (defined $new_desc) { $self->{description} = $new_desc; }
- return $self->{description};
-}
+=cut
-sub attributes
-{
- my ($self, $new_attributes) = @_;
+has 'types' => (
+ isa => 'ArrayRef',
+ is => 'ro',
+ metaclass => 'Collection::List',
+ provides => {
+ count => 'number_of_links',
+ }
+);
- if (defined $new_attributes) { $self->{attribute} = $new_attributes; }
- return $self->{attribute};
-}
+=head2 link_phrase
-sub GetShortLinkPhrase { $_[0]->{shortlinkphrase} }
-sub SetShortLinkPhrase { $_[0]->{shortlinkphrase} = $_[1]; }
+The phrase joining the source type to the destination type.
-sub GetPriority { $_[0]->{priority} }
-sub SetPriority { $_[0]->{priority} = $_[1]; }
+=cut
-sub GetChildOrder { $_[0]->{childorder} }
-sub SetChildOrder { $_[0]->{childorder} = $_[1] }
+has 'link_phrase' => (
+ isa => 'Str',
+ is => 'rw',
+ init_arg => 'linkphrase'
+);
-sub PackTypes
-{
- my ($self, $types) = @_;
- $types ||= $self->Types;
- join "-", @$types;
-}
+=head2 reverse_link_phrase
+
+The link phrase joining the destination type to the source type
+
+=cut
+
+has 'reverse_link_phrase' => (
+ isa => 'Str',
+ is => 'rw',
+ init_arg => 'rlinkphrase',
+);
+
+=head2 description
+
+A description of this link type
+
+=cut
+
+has 'description' => (
+ isa => 'Str',
+ is => 'rw'
+);
+
+=head2 attributes
+
+Attributes set on this link type
+
+=cut
+
+has 'attributes' => (
+ isa => 'ArrayRef',
+ is => 'rw',
+);
+
+=head2 short_link_phrase
+
+A short phrase linking the source type to the destination type
+
+=cut
+
+has 'short_link_phrase' => (
+ isa => 'Str',
+ is => 'rw',
+ init_arg => 'shortlinkphrase',
+);
+
+has 'priority' => (
+ is => 'rw'
+);
+
+has 'child_order' => (
+ is => 'rw',
+ init_arg => 'childorder',
+);
+
+=head1 METHODS
+
+=head2 CONSTRUCTORS
+
+=head3 newFromPackedTypes $dbh, $packed
+
+Construct from types packed into a string. C<$dbh> is a database handle,
+C<$packed> contains the types of the link, packed into a string separated with
+'-'.
+
+=cut
sub newFromPackedTypes
{
my ($class, $dbh, $packed) = @_;
defined($packed) or return undef;
my @types = split /-/, $packed, -1;
- MusicBrainz::Server::LinkEntity->ValidateTypes(\@types)
- or return undef;
+
$class->new($dbh, \@types);
}
-################################################################################
-# Data Retrieval
-################################################################################
+=head2 newFromId $id
-sub _new_from_row
-{
- my $this = shift;
- my $self = $this->SUPER::_new_from_row(@_)
- or return;
+Create a new link from a row ID, C<$id>.
- while (my ($k, $v) = each %$this)
- {
- $self->{$k} = $v
- if substr($k, 0, 1) eq "_";
- }
- $self->{dbh} = $this->dbh;
-
- bless $self, ref($this) || $this;
-}
+=cut
sub newFromId
{
my ($self, $id) = @_;
+
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
my $row = $sql->SelectSingleRowHash(
- "SELECT * FROM $self->{_table} WHERE id = ?",
+ "SELECT * FROM $table WHERE id = ?",
$id,
);
+
$self->_new_from_row($row);
}
+=head2 newFromMBId $id
+
+Create a new link from a MusicBrainz Identifier, C<$id>.
+
+=cut
+
sub newFromMBId
{
my ($self, $id) = @_;
+
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
my $row = $sql->SelectSingleRowHash(
- "SELECT * FROM $self->{_table} WHERE mbid = ?",
- $id,
+ "SELECT * FROM $table WHERE mbid = ?",
+ $self->table, $id,
);
+
$self->_new_from_row($row);
}
+=head2 newFromParentId $parent_id
+
+Create a list of LinkType's, from a single parent ID, c<$parent_id>.
+
+=cut
+
sub newFromParentId
{
my ($self, $parentid) = @_;
+
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
my $rows = $sql->SelectListOfHashes(
- "SELECT * FROM $self->{_table} WHERE parent = ? AND id != parent ORDER BY childorder, name",
+ "SELECT * FROM $table WHERE parent = ? AND id != parent ORDER BY childorder, name",
$parentid,
);
+
map { $self->_new_from_row($_) } @$rows;
}
@@ -207,22 +237,123 @@ sub newFromParentIdAndChildName
{
my ($self, $parentid, $childname) = @_;
my $sql = Sql->new($self->dbh);
+ my $table = $self->table;
my $row = $sql->SelectSingleRowHash(
- "SELECT * FROM $self->{_table} WHERE parent = ? AND LOWER(name) = LOWER(?)",
+ "SELECT * FROM $table WHERE parent = ? AND LOWER(name) = LOWER(?)",
$parentid,
$childname,
);
$self->_new_from_row($row);
}
-################################################################################
-# Tree Hierarchy
-################################################################################
+sub BUILDARGS
+{
+ my $self = shift;
+ my $dbh = shift;;
+
+ if (scalar @_ == 1 && ref $_[0] eq 'HASH')
+ {
+ # The user has called with a hash reference, so use
+ # a normal Moose constructor
+ my $args = shift;
+ return {
+ dbh => $dbh,
+ %$args,
+ };
+ }
+ else
+ {
+ # So far, links are always between two things. This may change one day.
+ my $types = shift;
+ ref($types) eq "ARRAY" or die "$types must be an array reference";
+ scalar @$types == 2 or die "$types must have length 2";
+
+ MusicBrainz::Server::LinkEntity->ValidateTypes($types)
+ or die "$types contains invalid types";
+
+ return {
+ dbh => $dbh,
+ types => $types,
+ table => "lt_" . join "_", @$types,
+ };
+ }
+}
+
+sub _new_from_row
+{
+ my ($self, $dbh, $row) = @_;
+ my $new = $self->SUPER::_new_from_row($dbh, $row);
+
+ $new->{table} = $self->table;
+ $new->{types} = $self->types;
+
+ return $new;
+}
+
+=head2 parent
+
+The parent of this link, as an object
+
+=cut
+
+sub parent
+{
+ my $self = shift;
+ return $self->newFromId($self->parent_id);
+}
+
+=head2 children
+
+Return a list children objects
+
+=cut
+
+sub children
+{
+ my $self = shift;
+ return $self->newFromParentId($self->id);
+}
+
+=head2 pack_types $types
-sub Root { $_[0]->newFromId(0) or die }
-sub IsRoot { $_[0]->id == 0 }
+Pack C<$types> into a single string.
-sub PathFromRoot
+=cut
+
+sub pack_types
+{
+ my ($self, $types) = @_;
+ $types ||= $self->types;
+ join "-", @$types;
+}
+
+=head2 root
+
+Find the root node of this link type
+
+=cut
+
+sub root
+{
+ my $self = shift;
+ return $self->newFromId(0) or die;
+}
+
+=head2 is_root
+
+Check if this node is the root node
+
+=cut
+
+sub is_root { $_[0]->id == 0 }
+
+=head2 path_from_root
+
+Calculate the path from this node, to the root node
+
+=cut
+
+sub path_from_root
{
my ($self, $root) = @_;
my @path;
@@ -238,6 +369,12 @@ sub PathFromRoot
@path;
}
+=head2 named_child $child_name
+
+Find a node, that is a child of this node and has a given name, C<$child_name>.
+
+=cut
+
sub named_child
{
my ($self, $childname) = @_;
@@ -274,7 +411,7 @@ sub AddChild
}
sub InUse
-{
+{
my $self = shift;
my $sql = Sql->new($self->dbh);
my $table = "l_" . join "_", @{$self->{_types}};
@@ -306,22 +443,39 @@ sub Update
"UPDATE $self->{_table} SET parent = ?, childorder = ?, name = ?,
linkphrase = ?, rlinkphrase = ?, description = ?,
attribute = ?, shortlinkphrase = ?, priority = ? WHERE id = ?",
- $self->GetParentId,
- $self->GetChildOrder,
+ $self->parent_id,
+ $self->child_order,
$self->name,
- $self->GetLinkPhrase,
- $self->GetReverseLinkPhrase,
+ $self->link_phrase,
+ $self->reverse_link_phrase,
$self->description,
$self->attributes,
- $self->GetShortLinkPhrase,
- $self->GetPriority,
+ $self->short_link_phrase,
+ $self->priority,
$self->id,
);
}
-################################################################################
+=head1 LICENSE
+
+MusicBrainz -- the open internet music database
+
+Copyright (C) 2000 Robert Kaye
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-#InsertDefaultRows();
+=cut
1;
-# eof LinkType.pm
View
39 lib/MusicBrainz/Server/Moderation/MOD_ADD_LINK.pm
@@ -49,7 +49,7 @@ sub PreInsert
my $link = MusicBrainz::Server::Link->new(
$self->{dbh},
- scalar($linktype->Types),
+ scalar($linktype->types),
);
$link = $link->Insert($linktype, $entities, $begindate, $enddate);
@@ -62,9 +62,10 @@ sub PreInsert
my ($linkphrase, $rlinkphrase);
my $attr = MusicBrainz::Server::Attribute->new(
$self->{dbh},
- scalar($linktype->Types),
+ scalar($linktype->types),
$link->id
);
+
if ($attr)
{
if (!defined $attr->Insert([map { $_->{value} } @$attrs]))
@@ -72,7 +73,7 @@ sub PreInsert
$self->SuppressInsert;
return;
}
- ($linkphrase, $rlinkphrase) = $attr->ReplaceAttributes($linktype->{linkphrase}, $linktype->{rlinkphrase});
+ ($linkphrase, $rlinkphrase) = $attr->ReplaceAttributes($linktype->link_phrase, $linktype->reverse_link_phrase);
}
if (@$entities[0]->{type} eq 'album' || @$entities[0]->{type} eq 'track')
@@ -94,31 +95,31 @@ sub PreInsert
$self->artist(@$entities[0]->{obj}->id);
}
- $self->table($link->Table);
+ $self->table($link->table);
$self->column("id");
$self->row_id($link->id);
my %new = (
- linkid=>$link->id,
- linktypeid=>$linktype->{id},
- linktypename=>$linktype->{name},
- linktypephrase=>$linkphrase,
- rlinktypephrase=>$rlinkphrase,
- entity0id=>@$entities[0]->{id},
- entity0type=>@$entities[0]->{type},
- entity0name=>@$entities[0]->{name},
- entity1id=>@$entities[1]->{id},
- entity1type=>@$entities[1]->{type},
- entity1name=>@$entities[1]->{name},
- begindate=>$begindate,
- enddate=>$enddate,
+ linkid => $link->id,
+ linktypeid => $linktype->id,
+ linktypename => $linktype->name,
+ linktypephrase => $linkphrase,
+ rlinktypephrase => $rlinkphrase,
+ entity0id => @$entities[0]->{id},
+ entity0type => @$entities[0]->{type},
+ entity0name => @$entities[0]->{name},
+ entity1id => @$entities[1]->{id},
+ entity1type => @$entities[1]->{type},
+ entity1name => @$entities[1]->{name},
+ begindate => $begindate,
+ enddate => $enddate,
);
$new{url} = $url if ($url);
$self->new_data($self->ConvertHashToNew(\%new));
# finally some special ASIN URL handling (update album_amazon_asin table data)
- if ($linktype->{id} == MusicBrainz::Server::CoverArt->asin_link_type_id($self->{dbh}) &&
+ if ($linktype->id == MusicBrainz::Server::CoverArt->asin_link_type_id($self->{dbh}) &&
@$entities[0]->{type} eq 'album' &&
@$entities[1]->{type} eq 'url')
{
@@ -134,7 +135,7 @@ sub PreInsert
}
# now check to see if we need to tinker with generic cover art
- if ($linktype->{id} == MusicBrainz::Server::CoverArt->GetCoverArtLinkTypeId($self->{dbh}) &&
+ if ($linktype->id == MusicBrainz::Server::CoverArt->GetCoverArtLinkTypeId($self->{dbh}) &&
@$entities[0]->{type} eq 'album' &&
@$entities[1]->{type} eq 'url')
{
View
15 root/relate/url.tt
@@ -1,14 +1,15 @@
[% WRAPPER "${entity.entity_type}/layout.tt" title=l("Create Relationship: Relate to URL") %]
- [% WRAPPER 'components/box.tt' header=l("Relate {type} to <abbr title="Uniform Resource Locator">URL</abbr>", {type => ${entity.entity_type}}) %]
- [%- WRAPPER 'layout/guidelines.tt' -%]
- [%- create_guideline(l('Select the <abbr title="Uniform Resource Locator">URL</abbr> relationship type and enter the <abbr title="Uniform Resource Locator">URL</abbr> below. Please note that the <abbr title="Uniform Resource Locator">URL</abbr> must be a valid <abbr title="Uniform Resource Locator">URL</abbr>, including the http:// or ftp:// protocol portion of the <abbr title="Uniform Resource Locator">URL</abbr>')) %]
- [%- create_guideline('<strong>' _l('You may not link to digital audio files or sites where unauthorized copies of copyrighted works can be downloaded.') _ </strong>) %]
- [%- create_guideline(l('If you would like to link directly to a downloadable music track, please ensure that the music is licensed under some free license such as one of the <a href="http://creativecommons.org">Creative Commons licenses</a>, the <a href="http://www.eff.org/IP/Open_licenses/eff_oal.php"><abbr title="Electronic Frontier Foundation">EFF</abbr>'s Open Audio license</a>, or is otherwise legally available for free download by the general public.')) %]
+ [% WRAPPER 'components/box.tt' header=l('Relate {type} to URL', {type => ${entity.entity_type}}) %]
+ [%- WRAPPER 'layout/guidelines.tt' -%]
+ [%- create_guideline(l('Select the <abbr title="Uniform Resource Locator">URL</abbr> relationship type and enter the <abbr title="Uniform Resource Locator">URL</abbr> below. Please note that the <abbr title="Uniform Resource Locator">URL</abbr> must be a valid <abbr title="Uniform Resource Locator">URL</abbr>, including the http:// or ftp:// protocol portion of the <abbr title="Uniform Resource Locator">URL</abbr>.')) %]
+ [%- create_guideline('<strong>' _ l('You may not link to digital audio files or sites where unauthorized copies of copyrighted works can be downloaded.') _ '</strong>') %]
+ [%- create_guideline(l('If you would like to link directly to a downloadable music track, please ensure that the music is licensed under some free license such as one of the <a href="http://creativecommons.org">Creative Commons licenses</a>, the <a href="http://www.eff.org/IP/Open_licenses/eff_oal.php"><abbr title="Electronic Frontier Foundation">EFF</abbr>\'s Open Audio license</a>, or is otherwise legally available for free download by the general public.')) %]
[% END %]
+
[% WRAPPER "forms/form.tt" moderation=1 %]
- [% WRAPPER "forms/fieldset.tt" legend=l("<abbr title="Uniform Resource Locator">URL</abbr> Details") %]
+ [% WRAPPER "forms/fieldset.tt" legend=l('<abbr title="Uniform Resource Locator">URL</abbr> Details') %]
[% INCLUDE "forms/generic/row.tt" field=form.field('type') label=l("Type") %]
- [% INCLUDE "forms/generic/row.tt" field=form.field('url') label=l("<abbr title="Uniform Resource Locator">URL</abbr>") %]
+ [% INCLUDE "forms/generic/row.tt" field=form.field('url') label=l('<abbr title="Uniform Resource Locator">URL</abbr>') %]
[% INCLUDE "forms/generic/row.tt" field=form.field('description') label=l("Description") %]
[% END %]
[% END %]
Please sign in to comment.
Something went wrong with that request. Please try again.