| 1 | package Silki::Schema::Wiki; | ||
| 2 | { | ||
| 3 | $Silki::Schema::Wiki::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | use autodie; | ||
| 10 | |||
| 11 | use Archive::Tar::Wrapper; | ||
| 12 | use Data::Dumper qw( Dumper ); | ||
| 13 | use DateTime::Format::Pg; | ||
| 14 | use Fey::Literal; | ||
| 15 | use Fey::Object::Iterator::FromSelect; | ||
| 16 | use Fey::SQL; | ||
| 17 | use File::Spec; | ||
| 18 | use List::AllUtils qw( uniq ); | ||
| 19 | use Path::Class qw( dir file ); | ||
| 20 | use Silki::Config; | ||
| 21 | use Silki::I18N qw( loc ); | ||
| 22 | use Silki::JSON; | ||
| 23 | use Silki::Schema; | ||
| 24 | use Silki::Schema::Account; | ||
| 25 | use Silki::Schema::Domain; | ||
| 26 | use Silki::Schema::File; | ||
| 27 | use Silki::Schema::Page; | ||
| 28 | use Silki::Schema::Permission; | ||
| 29 | use Silki::Schema::Role; | ||
| 30 | use Silki::Schema::TextSearchResult; | ||
| 31 | use Silki::Schema::UserWikiRole; | ||
| 32 | use Silki::Schema::WantedPage; | ||
| 33 | use Silki::Schema::WikiRolePermission; | ||
| 34 | use Silki::Wiki::Exporter; | ||
| 35 | use Silki::Wiki::Importer; | ||
| 36 | use Silki::Types qw( Bool CodeRef File HashRef Int Str ValidPermissionType ); | ||
| 37 | |||
| 38 | use Fey::ORM::Table; | ||
| 39 | use MooseX::ClassAttribute; | ||
| 40 | use MooseX::Params::Validate qw( pos_validated_list validated_list ); | ||
| 41 | |||
| 42 | with 'Silki::Role::Schema::URIMaker'; | ||
| 43 | |||
| 44 | with 'Silki::Role::Schema::SystemLogger' => | ||
| 45 | { methods => [ 'insert', 'delete' ] }; | ||
| 46 | |||
| 47 | with 'Silki::Role::Schema::DataValidator' => { | ||
| 48 | steps => [ | ||
| 49 | '_title_is_unique', | ||
| 50 | '_title_is_valid', | ||
| 51 | '_short_name_is_unique', | ||
| 52 | ], | ||
| 53 | }; | ||
| 54 | |||
| 55 | my $Schema = Silki::Schema->Schema(); | ||
| 56 | |||
| 57 | has_policy 'Silki::Schema::Policy'; | ||
| 58 | |||
| 59 | has_table( $Schema->table('Wiki') ); | ||
| 60 | |||
| 61 | has_one( $Schema->table('Domain') ); | ||
| 62 | |||
| 63 | has_one creator => ( table => $Schema->table('User') ); | ||
| 64 | |||
| 65 | has_many pages => ( | ||
| 66 | table => $Schema->table('Page'), | ||
| 67 | order_by => [ $Schema->table('Page')->column('title') ], | ||
| 68 | ); | ||
| 69 | |||
| 70 | has permissions => ( | ||
| 71 | is => 'ro', | ||
| 72 | isa => HashRef [ HashRef [Bool] ], | ||
| 73 | lazy => 1, | ||
| 74 | builder => '_build_permissions', | ||
| 75 | init_arg => undef, | ||
| 76 | clearer => '_clear_permissions', | ||
| 77 | ); | ||
| 78 | |||
| 79 | has non_member_can_edit => ( | ||
| 80 | is => 'ro', | ||
| 81 | isa => Bool, | ||
| 82 | lazy => 1, | ||
| 83 | builder => '_build_non_member_can_edit', | ||
| 84 | init_arg => undef, | ||
| 85 | clearer => '_clear_non_member_can_edit', | ||
| 86 | ); | ||
| 87 | |||
| 88 | has permissions_name => ( | ||
| 89 | is => 'ro', | ||
| 90 | isa => Str, | ||
| 91 | lazy => 1, | ||
| 92 | builder => '_build_permissions_name', | ||
| 93 | init_arg => undef, | ||
| 94 | clearer => '_clear_permissions_name', | ||
| 95 | ); | ||
| 96 | |||
| 97 | has front_page_title => ( | ||
| 98 | is => 'ro', | ||
| 99 | isa => Str, | ||
| 100 | lazy => 1, | ||
| 101 | init_arg => 1, | ||
| 102 | builder => '_build_front_page_title', | ||
| 103 | ); | ||
| 104 | |||
| 105 | class_has _RecentChangesSelect => ( | ||
| 106 | is => 'ro', | ||
| 107 | isa => 'Fey::SQL::Select', | ||
| 108 | lazy => 1, | ||
| 109 | builder => '_BuildRecentChangesSelect', | ||
| 110 | ); | ||
| 111 | |||
| 112 | class_has _DistinctRecentChangesSelect => ( | ||
| 113 | is => 'ro', | ||
| 114 | isa => 'Fey::SQL::Select', | ||
| 115 | lazy => 1, | ||
| 116 | builder => '_BuildDistinctRecentChangesSelect', | ||
| 117 | ); | ||
| 118 | |||
| 119 | class_has _FilesSelect => ( | ||
| 120 | is => 'ro', | ||
| 121 | isa => 'Fey::SQL::Select', | ||
| 122 | lazy => 1, | ||
| 123 | builder => '_BuildFilesSelect', | ||
| 124 | ); | ||
| 125 | |||
| 126 | class_has _OrphanedPagesSelect => ( | ||
| 127 | is => 'ro', | ||
| 128 | isa => 'Fey::SQL::Select', | ||
| 129 | lazy => 1, | ||
| 130 | builder => '_BuildOrphanedPagesSelect', | ||
| 131 | ); | ||
| 132 | |||
| 133 | class_has _WantedPagesSelect => ( | ||
| 134 | is => 'ro', | ||
| 135 | isa => 'Fey::SQL::Select', | ||
| 136 | lazy => 1, | ||
| 137 | builder => '_BuildWantedPagesSelect', | ||
| 138 | ); | ||
| 139 | |||
| 140 | class_has _RecentlyViewedPagesSelect => ( | ||
| 141 | is => 'ro', | ||
| 142 | isa => 'Fey::SQL::Select', | ||
| 143 | lazy => 1, | ||
| 144 | builder => '_BuildRecentlyViewedPagesSelect', | ||
| 145 | ); | ||
| 146 | |||
| 147 | class_has _MembersSelect => ( | ||
| 148 | is => 'ro', | ||
| 149 | isa => 'Fey::SQL::Select', | ||
| 150 | lazy => 1, | ||
| 151 | builder => '_BuildMembersSelect', | ||
| 152 | ); | ||
| 153 | |||
| 154 | class_has _ActiveUsersSelect => ( | ||
| 155 | is => 'ro', | ||
| 156 | isa => 'Fey::SQL::Select', | ||
| 157 | lazy => 1, | ||
| 158 | builder => '_BuildActiveUsersSelect', | ||
| 159 | ); | ||
| 160 | |||
| 161 | class_has _PagesByTagSelect => ( | ||
| 162 | is => 'ro', | ||
| 163 | isa => 'Fey::SQL::Select', | ||
| 164 | lazy => 1, | ||
| 165 | builder => '_BuildPagesByTagSelect', | ||
| 166 | ); | ||
| 167 | |||
| 168 | class_has _PagesByTagCountSelect => ( | ||
| 169 | is => 'ro', | ||
| 170 | isa => 'Fey::SQL::Select', | ||
| 171 | lazy => 1, | ||
| 172 | builder => '_BuildPagesByTagCountSelect', | ||
| 173 | ); | ||
| 174 | |||
| 175 | class_has _PopularTagsSelect => ( | ||
| 176 | is => 'ro', | ||
| 177 | isa => 'Fey::SQL::Select', | ||
| 178 | lazy => 1, | ||
| 179 | builder => '_BuildPopularTagsSelect', | ||
| 180 | ); | ||
| 181 | |||
| 182 | class_has _PagesEditedByUserSelect => ( | ||
| 183 | is => 'ro', | ||
| 184 | does => 'Fey::Role::SQL::ReturnsData', | ||
| 185 | lazy => 1, | ||
| 186 | builder => '_BuildPagesEditedByUserSelect', | ||
| 187 | ); | ||
| 188 | |||
| 189 | class_has _PagesEditedByUserCountSelect => ( | ||
| 190 | is => 'ro', | ||
| 191 | does => 'Fey::Role::SQL::ReturnsData', | ||
| 192 | lazy => 1, | ||
| 193 | builder => '_BuildPagesEditedByUserCountSelect', | ||
| 194 | ); | ||
| 195 | |||
| 196 | class_has _PagesCreatedByUserSelect => ( | ||
| 197 | is => 'ro', | ||
| 198 | does => 'Fey::Role::SQL::ReturnsData', | ||
| 199 | lazy => 1, | ||
| 200 | builder => '_BuildPagesCreatedByUserSelect', | ||
| 201 | ); | ||
| 202 | |||
| 203 | class_has _PagesCreatedByUserCountSelect => ( | ||
| 204 | is => 'ro', | ||
| 205 | does => 'Fey::Role::SQL::ReturnsData', | ||
| 206 | lazy => 1, | ||
| 207 | builder => '_BuildPagesCreatedByUserCountSelect', | ||
| 208 | ); | ||
| 209 | |||
| 210 | class_has _PublicWikiCountSelect => ( | ||
| 211 | is => 'ro', | ||
| 212 | isa => 'Fey::SQL::Select', | ||
| 213 | lazy => 1, | ||
| 214 | builder => '_BuildPublicWikiCountSelect', | ||
| 215 | ); | ||
| 216 | |||
| 217 | class_has _PublicWikiSelect => ( | ||
| 218 | is => 'ro', | ||
| 219 | isa => 'Fey::SQL::Select', | ||
| 220 | lazy => 1, | ||
| 221 | builder => '_BuildPublicWikiSelect', | ||
| 222 | ); | ||
| 223 | |||
| 224 | class_has _AllWikiSelect => ( | ||
| 225 | is => 'ro', | ||
| 226 | isa => 'Fey::SQL::Select', | ||
| 227 | lazy => 1, | ||
| 228 | builder => '_BuildAllWikiSelect', | ||
| 229 | ); | ||
| 230 | |||
| 231 | class_has _MaxRevisionSelect => ( | ||
| 232 | is => 'ro', | ||
| 233 | isa => 'Fey::SQL::Select', | ||
| 234 | lazy => 1, | ||
| 235 | builder => '_BuildMaxRevisionSelect', | ||
| 236 | ); | ||
| 237 | |||
| 238 | class_has _MinRevisionSelect => ( | ||
| 239 | is => 'ro', | ||
| 240 | isa => 'Fey::SQL::Select', | ||
| 241 | lazy => 1, | ||
| 242 | builder => '_BuildMinRevisionSelect', | ||
| 243 | ); | ||
| 244 | |||
| 245 | query revision_count => ( | ||
| 246 | select => __PACKAGE__->_DistinctRevisionCountSelect(), | ||
| 247 | bind_params => sub { $_[0]->wiki_id() }, | ||
| 248 | ); | ||
| 249 | |||
| 250 | query tag_count => ( | ||
| 251 | select => __PACKAGE__->_TagCountSelect(), | ||
| 252 | bind_params => sub { $_[0]->wiki_id() }, | ||
| 253 | ); | ||
| 254 | |||
| 255 | query page_count => ( | ||
| 256 | select => __PACKAGE__->_PageCountSelect(), | ||
| 257 | bind_params => sub { $_[0]->wiki_id() }, | ||
| 258 | ); | ||
| 259 | |||
| 260 | query orphaned_page_count => ( | ||
| 261 | select => __PACKAGE__->_OrphanedPageCountSelect(), | ||
| 262 | bind_params => sub { $_[0]->wiki_id(), $_[0]->front_page_title() }, | ||
| 263 | ); | ||
| 264 | |||
| 265 | query wanted_page_count => ( | ||
| 266 | select => __PACKAGE__->_WantedPageCountSelect(), | ||
| 267 | bind_params => sub { $_[0]->wiki_id() }, | ||
| 268 | ); | ||
| 269 | |||
| 270 | query member_count => ( | ||
| 271 | select => __PACKAGE__->_MemberCountSelect(), | ||
| 272 | bind_params => sub { $_[0]->wiki_id() }, | ||
| 273 | ); | ||
| 274 | |||
| 275 | query file_count => ( | ||
| 276 | select => __PACKAGE__->_FileCountSelect(), | ||
| 277 | bind_params => sub { $_[0]->wiki_id() }, | ||
| 278 | ); | ||
| 279 | |||
| 280 | with 'Silki::Role::Schema::Serializes'; | ||
| 281 | |||
| 282 | my $FrontPage = <<'EOF'; | ||
| 283 | Welcome to your new wiki. | ||
| 284 | |||
| 285 | A wiki is a set of web pages that can be read and edited by a group of people. You use simple syntax to add things like *italics* and **bold** to the text. Wikis are designed to make linking to other pages easy. | ||
| 286 | |||
| 287 | For more information about wikis in general and Silki in particular, click on the Help link in the upper right. | ||
| 288 | |||
| 289 | You can also play around on the ((Scratch Pad)) page. | ||
| 290 | EOF | ||
| 291 | |||
| 292 | my $Scratch = <<'EOF'; | ||
| 293 | Feel free experiment with the wiki here. | ||
| 294 | EOF | ||
| 295 | |||
| 296 | around insert => sub { | ||
| 297 | my $orig = shift; | ||
| 298 | my $class = shift; | ||
| 299 | my %p = @_; | ||
| 300 | |||
| 301 | if ( !exists $p{short_name} ) { | ||
| 302 | my $name = $p{title}; | ||
| 303 | $name =~ s/ +/-/g; | ||
| 304 | $p{short_name} = lc $name; | ||
| 305 | } | ||
| 306 | |||
| 307 | $p{user_id} = $p{user}->user_id(); | ||
| 308 | |||
| 309 | $p{account_id} = Silki::Schema::Account->DefaultAccount()->account_id(); | ||
| 310 | |||
| 311 | my $wiki; | ||
| 312 | |||
| 313 | my $skip_default = delete $p{skip_default_pages}; | ||
| 314 | |||
| 315 | $class->SchemaClass()->RunInTransaction( | ||
| 316 | sub { | ||
| 317 | $wiki = $class->$orig(%p); | ||
| 318 | |||
| 319 | unless ($skip_default) { | ||
| 320 | Silki::Schema::Page->insert_with_content( | ||
| 321 | title => 'Front Page', | ||
| 322 | content => $FrontPage, | ||
| 323 | wiki_id => $wiki->wiki_id(), | ||
| 324 | user_id => $wiki->user_id(), | ||
| 325 | can_be_renamed => 0, | ||
| 326 | ); | ||
| 327 | |||
| 328 | Silki::Schema::Page->insert_with_content( | ||
| 329 | title => 'Scratch Pad', | ||
| 330 | content => $Scratch, | ||
| 331 | wiki_id => $wiki->wiki_id(), | ||
| 332 | user_id => $wiki->user_id(), | ||
| 333 | can_be_renamed => 0, | ||
| 334 | ); | ||
| 335 | } | ||
| 336 | } | ||
| 337 | ); | ||
| 338 | |||
| 339 | return $wiki; | ||
| 340 | }; | ||
| 341 | |||
| 342 | sub _system_log_values_for_insert { | ||
| 343 | my $class = shift; | ||
| 344 | my %p = @_; | ||
| 345 | |||
| 346 | my $msg = 'Created wiki: ' . $p{title}; | ||
| 347 | |||
| 348 | return ( | ||
| 349 | message => $msg, | ||
| 350 | data_blob => \%p, | ||
| 351 | ); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub _system_log_values_for_delete { | ||
| 355 | my $self = shift; | ||
| 356 | |||
| 357 | my $msg = 'Deleted wiki: ' . $self->title(); | ||
| 358 | |||
| 359 | return ( | ||
| 360 | message => $msg, | ||
| 361 | ); | ||
| 362 | } | ||
| 363 | |||
| 364 | sub _title_is_unique { | ||
| 365 | my $self = shift; | ||
| 366 | my $p = shift; | ||
| 367 | my $is_insert = shift; | ||
| 368 | |||
| 369 | return | ||
| 370 | if !$is_insert && exists $p->{title} && $p->{title} eq $self->title(); | ||
| 371 | |||
| 372 | return unless __PACKAGE__->new( title => $p->{title} ); | ||
| 373 | |||
| 374 | return { | ||
| 375 | field => 'title', | ||
| 376 | message => loc( | ||
| 377 | 'The title you provided is already in use by another wiki.' | ||
| 378 | ), | ||
| 379 | }; | ||
| 380 | } | ||
| 381 | |||
| 382 | sub _title_is_valid { | ||
| 383 | my $self = shift; | ||
| 384 | my $p = shift; | ||
| 385 | |||
| 386 | return unless exists $p->{title}; | ||
| 387 | |||
| 388 | if ( $p->{title} =~ /\)\)/ ) { | ||
| 389 | return { | ||
| 390 | message => loc( | ||
| 391 | q{Wiki titles cannot contain the characters "))", since this conflicts with the wiki link syntax.} | ||
| 392 | ), | ||
| 393 | }; | ||
| 394 | } | ||
| 395 | |||
| 396 | if ( $p->{title} =~ /\// ) { | ||
| 397 | return { | ||
| 398 | message => loc( | ||
| 399 | q{Wiki titles cannot contain a slash (/), since this conflicts with the syntax to link to another wiki.} | ||
| 400 | ), | ||
| 401 | }; | ||
| 402 | } | ||
| 403 | |||
| 404 | return; | ||
| 405 | } | ||
| 406 | |||
| 407 | sub _short_name_is_unique { | ||
| 408 | my $self = shift; | ||
| 409 | my $p = shift; | ||
| 410 | my $is_insert = shift; | ||
| 411 | |||
| 412 | return | ||
| 413 | if !$is_insert | ||
| 414 | && exists $p->{short_name} | ||
| 415 | && $p->{short_name} eq $self->short_name(); | ||
| 416 | |||
| 417 | return unless __PACKAGE__->new( short_name => $p->{short_name} ); | ||
| 418 | |||
| 419 | return { | ||
| 420 | message => loc( | ||
| 421 | 'The title you provided generates the same URI as an existing wiki.' | ||
| 422 | ), | ||
| 423 | }; | ||
| 424 | } | ||
| 425 | |||
| 426 | sub _base_uri_path { | ||
| 427 | my $self = shift; | ||
| 428 | |||
| 429 | return '/wiki/' . $self->short_name(); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub uri_for_member { | ||
| 433 | my $self = shift; | ||
| 434 | my $user = shift; | ||
| 435 | my $view = shift; | ||
| 436 | |||
| 437 | my $view_base = 'user/' . $user->user_id(); | ||
| 438 | $view_base .= q{/} . $view if defined $view; | ||
| 439 | |||
| 440 | return $self->uri( view => $view_base ); | ||
| 441 | } | ||
| 442 | |||
| 443 | sub add_user { | ||
| 444 | my $self = shift; | ||
| 445 | my ( $user, $role ) = validated_list( | ||
| 446 | \@_, | ||
| 447 | user => { isa => 'Silki::Schema::User' }, | ||
| 448 | role => { | ||
| 449 | isa => 'Silki::Schema::Role', | ||
| 450 | default => Silki::Schema::Role->Member(), | ||
| 451 | }, | ||
| 452 | ); | ||
| 453 | |||
| 454 | return if $user->is_system_user(); | ||
| 455 | |||
| 456 | return if $role->name() eq 'Guest' || $role->name() eq 'Authenticated'; | ||
| 457 | |||
| 458 | my $uwr = Silki::Schema::UserWikiRole->new( | ||
| 459 | user_id => $user->user_id(), | ||
| 460 | wiki_id => $self->wiki_id(), | ||
| 461 | ); | ||
| 462 | |||
| 463 | if ($uwr) { | ||
| 464 | $uwr->update( role_id => $role->role_id() ); | ||
| 465 | } | ||
| 466 | else { | ||
| 467 | Silki::Schema::UserWikiRole->insert( | ||
| 468 | user_id => $user->user_id(), | ||
| 469 | wiki_id => $self->wiki_id(), | ||
| 470 | role_id => $role->role_id(), | ||
| 471 | ); | ||
| 472 | } | ||
| 473 | |||
| 474 | return; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub remove_user { | ||
| 478 | my $self = shift; | ||
| 479 | my ($user) = validated_list( | ||
| 480 | \@_, | ||
| 481 | user => { isa => 'Silki::Schema::User' }, | ||
| 482 | ); | ||
| 483 | |||
| 484 | return if $user->is_system_user(); | ||
| 485 | |||
| 486 | my $uwr = Silki::Schema::UserWikiRole->new( | ||
| 487 | user_id => $user->user_id(), | ||
| 488 | wiki_id => $self->wiki_id(), | ||
| 489 | ); | ||
| 490 | |||
| 491 | $uwr->delete() if $uwr; | ||
| 492 | |||
| 493 | return; | ||
| 494 | } | ||
| 495 | |||
| 496 | sub _build_permissions { | ||
| 497 | my $self = shift; | ||
| 498 | |||
| 499 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 500 | |||
| 501 | #<<< | ||
| 502 | $select | ||
| 503 | ->select( $Schema->table('Role')->column('name'), | ||
| 504 | $Schema->table('Permission')->column('name'), | ||
| 505 | ) | ||
| 506 | ->from( $Schema->table('Permission'), | ||
| 507 | $Schema->table('WikiRolePermission') ) | ||
| 508 | ->from( $Schema->table('Role'), | ||
| 509 | $Schema->table('WikiRolePermission') ) | ||
| 510 | ->where( $Schema->table('WikiRolePermission')->column('wiki_id'), | ||
| 511 | '=', $self->wiki_id() ); | ||
| 512 | #>>> | ||
| 513 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 514 | |||
| 515 | my %perms; | ||
| 516 | for my $row ( | ||
| 517 | @{ | ||
| 518 | $dbh->selectall_arrayref( | ||
| 519 | $select->sql($dbh), {}, $select->bind_params() | ||
| 520 | ) | ||
| 521 | } | ||
| 522 | ) { | ||
| 523 | $perms{ $row->[0] }{ $row->[1] } = 1; | ||
| 524 | } | ||
| 525 | |||
| 526 | return \%perms; | ||
| 527 | } | ||
| 528 | |||
| 529 | sub _build_non_member_can_edit { | ||
| 530 | my $self = shift; | ||
| 531 | |||
| 532 | my $permissions = $self->permissions(); | ||
| 533 | |||
| 534 | return $permissions->{Guest}{Edit} || $permissions->{Authenticated}{Edit}; | ||
| 535 | } | ||
| 536 | |||
| 537 | { | ||
| 538 | my %Sets = ( | ||
| 539 | 'public' => { | ||
| 540 | Guest => [qw( Read Edit )], | ||
| 541 | Authenticated => [qw( Read Edit )], | ||
| 542 | Member => [qw( Read Edit Upload )], | ||
| 543 | Admin => [qw( Read Edit Delete Upload Invite Manage )], | ||
| 544 | }, | ||
| 545 | 'public-authenticate-to-edit' => { | ||
| 546 | Guest => [qw( Read )], | ||
| 547 | Authenticated => [qw( Read Edit )], | ||
| 548 | Member => [qw( Read Edit Upload )], | ||
| 549 | Admin => [qw( Read Edit Delete Upload Invite Manage )], | ||
| 550 | }, | ||
| 551 | 'public-read-only' => { | ||
| 552 | Guest => [qw( Read )], | ||
| 553 | Authenticated => [qw( Read )], | ||
| 554 | Member => [qw( Read Edit Upload )], | ||
| 555 | Admin => [qw( Read Edit Delete Upload Invite Manage )], | ||
| 556 | }, | ||
| 557 | 'private' => { | ||
| 558 | Guest => [], | ||
| 559 | Authenticated => [], | ||
| 560 | Member => [qw( Read Edit Upload )], | ||
| 561 | Admin => [qw( Read Edit Delete Upload Invite Manage )], | ||
| 562 | }, | ||
| 563 | ); | ||
| 564 | |||
| 565 | sub set_permissions { | ||
| 566 | my $self = shift; | ||
| 567 | my ($type) | ||
| 568 | = pos_validated_list( \@_, { isa => ValidPermissionType } ); | ||
| 569 | |||
| 570 | $self->_set_permissions_from_set( $Sets{$type} ); | ||
| 571 | } | ||
| 572 | |||
| 573 | my %SetsAsHashes; | ||
| 574 | for my $name ( keys %Sets ) { | ||
| 575 | for my $role ( keys %{ $Sets{$name} } ) { | ||
| 576 | next unless @{ $Sets{$name}{$role} }; | ||
| 577 | $SetsAsHashes{$name}{$role} | ||
| 578 | = { map { $_ => 1 } @{ $Sets{$name}{$role} } }; | ||
| 579 | } | ||
| 580 | } | ||
| 581 | |||
| 582 | sub _build_permissions_name { | ||
| 583 | my $self = shift; | ||
| 584 | |||
| 585 | local $Data::Dumper::Sortkeys = 1; | ||
| 586 | my $perms = Dumper( $self->permissions() ); | ||
| 587 | |||
| 588 | for my $name ( keys %SetsAsHashes ) { | ||
| 589 | return $name if $perms eq Dumper( $SetsAsHashes{$name} ); | ||
| 590 | } | ||
| 591 | |||
| 592 | return 'custom'; | ||
| 593 | } | ||
| 594 | } | ||
| 595 | |||
| 596 | { | ||
| 597 | my $Delete = Silki::Schema->SQLFactoryClass()->new_delete(); | ||
| 598 | |||
| 599 | #<<< | ||
| 600 | $Delete | ||
| 601 | ->delete() | ||
| 602 | ->from( $Schema->table('WikiRolePermission') ) | ||
| 603 | ->where( $Schema->table('WikiRolePermission')->column('wiki_id'), | ||
| 604 | '=', Fey::Placeholder->new() ); | ||
| 605 | #>>> | ||
| 606 | sub _set_permissions_from_set { | ||
| 607 | my $self = shift; | ||
| 608 | my $set = shift; | ||
| 609 | |||
| 610 | my @inserts; | ||
| 611 | for my $role_name ( keys %{$set} ) { | ||
| 612 | my $role = Silki::Schema::Role->$role_name(); | ||
| 613 | |||
| 614 | for my $perm_name ( @{ $set->{$role_name} } ) { | ||
| 615 | my $perm = Silki::Schema::Permission->$perm_name(); | ||
| 616 | |||
| 617 | push @inserts, { | ||
| 618 | wiki_id => $self->wiki_id(), | ||
| 619 | role_id => $role->role_id(), | ||
| 620 | permission_id => $perm->permission_id(), | ||
| 621 | }; | ||
| 622 | } | ||
| 623 | } | ||
| 624 | |||
| 625 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($Delete)->dbh(); | ||
| 626 | my $trans = sub { | ||
| 627 | $dbh->do( $Delete->sql($dbh), {}, $self->wiki_id() ); | ||
| 628 | Silki::Schema::WikiRolePermission->insert_many(@inserts); | ||
| 629 | }; | ||
| 630 | |||
| 631 | Silki::Schema->RunInTransaction($trans); | ||
| 632 | |||
| 633 | $self->_clear_permissions(); | ||
| 634 | $self->_clear_permissions_name(); | ||
| 635 | $self->_clear_non_member_can_edit(); | ||
| 636 | |||
| 637 | return; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | sub _build_front_page_title { | ||
| 642 | my $self = shift; | ||
| 643 | |||
| 644 | # XXX - needs i18n | ||
| 645 | return 'Front Page'; | ||
| 646 | } | ||
| 647 | |||
| 648 | sub _RevisionCountSelect { | ||
| 649 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 650 | |||
| 651 | my ( $page_t, $page_revision_t ) | ||
| 652 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 653 | |||
| 654 | my $count = Fey::Literal::Function->new( | ||
| 655 | 'COUNT', | ||
| 656 | $page_revision_t->column('page_id') | ||
| 657 | ); | ||
| 658 | |||
| 659 | #<<< | ||
| 660 | $select | ||
| 661 | ->select($count)->from( $page_t, $page_revision_t ) | ||
| 662 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ); | ||
| 663 | #>>> | ||
| 664 | return $select; | ||
| 665 | } | ||
| 666 | |||
| 667 | sub _DistinctRevisionCountSelect { | ||
| 668 | my $class = shift; | ||
| 669 | |||
| 670 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 671 | |||
| 672 | my ( $page_t, $page_revision_t ) | ||
| 673 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 674 | |||
| 675 | my $max_revision = $class->_MaxRevisionSelect(); | ||
| 676 | |||
| 677 | my $count = Fey::Literal::Function->new( | ||
| 678 | 'COUNT', | ||
| 679 | $page_revision_t->column('page_id'), | ||
| 680 | ); | ||
| 681 | |||
| 682 | my $count_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 683 | |||
| 684 | #<<< | ||
| 685 | $count_select | ||
| 686 | ->select($count) | ||
| 687 | ->from( $page_t, $page_revision_t ) | ||
| 688 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 689 | ->and ( $page_revision_t->column('revision_number'), | ||
| 690 | '=', $max_revision ); | ||
| 691 | #>>> | ||
| 692 | return $count_select; | ||
| 693 | } | ||
| 694 | |||
| 695 | sub revisions { | ||
| 696 | my $self = shift; | ||
| 697 | my ( $limit, $offset ) = validated_list( | ||
| 698 | \@_, | ||
| 699 | limit => { isa => Int, optional => 1 }, | ||
| 700 | offset => { isa => Int, default => 0 }, | ||
| 701 | ); | ||
| 702 | |||
| 703 | my $select = $self->_DistinctRecentChangesSelect()->clone(); | ||
| 704 | $select->limit( $limit, $offset ); | ||
| 705 | |||
| 706 | return Fey::Object::Iterator::FromSelect->new( | ||
| 707 | classes => [ 'Silki::Schema::Page', 'Silki::Schema::PageRevision' ], | ||
| 708 | select => $select, | ||
| 709 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 710 | bind_params => [ $self->wiki_id() ], | ||
| 711 | ); | ||
| 712 | } | ||
| 713 | |||
| 714 | sub _BuildRecentChangesSelect { | ||
| 715 | my $class = shift; | ||
| 716 | |||
| 717 | my $page_t = $Schema->table('Page'); | ||
| 718 | |||
| 719 | my $pages_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 720 | |||
| 721 | #<<< | ||
| 722 | $pages_select | ||
| 723 | ->select( $page_t, $Schema->table('PageRevision') ) | ||
| 724 | ->from( $page_t, $Schema->table('PageRevision') ) | ||
| 725 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 726 | ->order_by( | ||
| 727 | $Schema->table('PageRevision')->column('creation_datetime'), 'DESC', | ||
| 728 | $Schema->table('Page')->column('title'), 'ASC', | ||
| 729 | ); | ||
| 730 | #>>> | ||
| 731 | return $pages_select; | ||
| 732 | } | ||
| 733 | |||
| 734 | # This gets recently changed pages but only shows each page once, in its most | ||
| 735 | # recent revision. | ||
| 736 | sub _BuildDistinctRecentChangesSelect { | ||
| 737 | my $class = shift; | ||
| 738 | |||
| 739 | my ( $page_t, $page_revision_t ) | ||
| 740 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 741 | |||
| 742 | my $max_func = Fey::Literal::Function->new( | ||
| 743 | 'MAX', | ||
| 744 | $Schema->table('PageRevision')->column('revision_number') | ||
| 745 | ); | ||
| 746 | |||
| 747 | my $max_revision = $class->_MaxRevisionSelect(); | ||
| 748 | |||
| 749 | my $pages_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 750 | |||
| 751 | #<<< | ||
| 752 | $pages_select | ||
| 753 | ->select( $page_t, $Schema->table('PageRevision') ) | ||
| 754 | ->from( $page_t, $Schema->table('PageRevision') ) | ||
| 755 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 756 | ->and ( | ||
| 757 | $Schema->table('PageRevision')->column('revision_number'), | ||
| 758 | '=', $max_revision | ||
| 759 | )->order_by( | ||
| 760 | $Schema->table('PageRevision')->column('creation_datetime'), 'DESC', | ||
| 761 | $page_t->column('title'), 'ASC', | ||
| 762 | ); | ||
| 763 | #>>> | ||
| 764 | return $pages_select; | ||
| 765 | } | ||
| 766 | |||
| 767 | sub orphaned_pages { | ||
| 768 | my $self = shift; | ||
| 769 | my ( $limit, $offset ) = validated_list( | ||
| 770 | \@_, | ||
| 771 | limit => { isa => Int, optional => 1 }, | ||
| 772 | offset => { isa => Int, default => 0 }, | ||
| 773 | ); | ||
| 774 | |||
| 775 | my $select = $self->_OrphanedPagesSelect()->clone(); | ||
| 776 | $select->limit( $limit, $offset ); | ||
| 777 | |||
| 778 | return Fey::Object::Iterator::FromSelect->new( | ||
| 779 | classes => [ 'Silki::Schema::Page', 'Silki::Schema::PageRevision' ], | ||
| 780 | select => $select, | ||
| 781 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 782 | bind_params => [ $self->wiki_id(), $self->front_page_title() ], | ||
| 783 | ); | ||
| 784 | } | ||
| 785 | |||
| 786 | sub _PageCountSelect { | ||
| 787 | my $class = shift; | ||
| 788 | |||
| 789 | my $page_t = $Schema->table('Page'); | ||
| 790 | |||
| 791 | my $count = Fey::Literal::Function->new( | ||
| 792 | 'COUNT', | ||
| 793 | $page_t->column('page_id') | ||
| 794 | ); | ||
| 795 | |||
| 796 | my $pages_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 797 | |||
| 798 | #<<< | ||
| 799 | $pages_select | ||
| 800 | ->select($count) | ||
| 801 | ->from($page_t) | ||
| 802 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ); | ||
| 803 | #>>> | ||
| 804 | return $pages_select; | ||
| 805 | } | ||
| 806 | |||
| 807 | sub _OrphanedPageCountSelect { | ||
| 808 | my $class = shift; | ||
| 809 | |||
| 810 | my $page_link_t = $Schema->table('PageLink'); | ||
| 811 | |||
| 812 | my $linked_pages = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 813 | |||
| 814 | #<<< | ||
| 815 | $linked_pages | ||
| 816 | ->select( $page_link_t->column('to_page_id') ) | ||
| 817 | ->from( $page_link_t ); | ||
| 818 | #>>> | ||
| 819 | my $page_t = $Schema->table('Page'); | ||
| 820 | |||
| 821 | my $count = Fey::Literal::Function->new( | ||
| 822 | 'COUNT', | ||
| 823 | $page_t->column('page_id') | ||
| 824 | ); | ||
| 825 | |||
| 826 | my $pages_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 827 | |||
| 828 | #<<< | ||
| 829 | $pages_select | ||
| 830 | ->select($count) | ||
| 831 | ->from($page_t) | ||
| 832 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 833 | ->and( $page_t->column( 'title' ), '!=', Fey::Placeholder->new() ) | ||
| 834 | ->and( $page_t->column('page_id'), 'NOT IN', $linked_pages ); | ||
| 835 | #>>> | ||
| 836 | return $pages_select; | ||
| 837 | } | ||
| 838 | |||
| 839 | sub _BuildOrphanedPagesSelect { | ||
| 840 | my $class = shift; | ||
| 841 | |||
| 842 | my $page_link_t = $Schema->table('PageLink'); | ||
| 843 | |||
| 844 | my $linked_pages = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 845 | $linked_pages->select( $page_link_t->column('to_page_id') ) | ||
| 846 | ->from($page_link_t); | ||
| 847 | |||
| 848 | my $page_t = $Schema->table('Page'); | ||
| 849 | |||
| 850 | my $pages_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 851 | |||
| 852 | #<<< | ||
| 853 | $pages_select | ||
| 854 | ->select($page_t) | ||
| 855 | ->from($page_t) | ||
| 856 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 857 | ->and( $page_t->column( 'title' ), '!=', Fey::Placeholder->new() ) | ||
| 858 | ->and( $page_t->column('page_id'), 'NOT IN', $linked_pages ) | ||
| 859 | ->order_by( | ||
| 860 | $page_t->column('title'), 'ASC', | ||
| 861 | ); | ||
| 862 | #>>> | ||
| 863 | return $pages_select; | ||
| 864 | } | ||
| 865 | |||
| 866 | sub _WantedPageCountSelect { | ||
| 867 | my $class = shift; | ||
| 868 | |||
| 869 | my $pending_page_link_t = $Schema->table('PendingPageLink'); | ||
| 870 | |||
| 871 | my $distinct = Fey::Literal::Term->new( | ||
| 872 | 'DISTINCT ', | ||
| 873 | $pending_page_link_t->column('to_page_title') | ||
| 874 | ); | ||
| 875 | my $count = Fey::Literal::Function->new( 'COUNT', $distinct ); | ||
| 876 | |||
| 877 | my $wanted_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 878 | |||
| 879 | #<<< | ||
| 880 | $wanted_select | ||
| 881 | ->select($count) | ||
| 882 | ->from($pending_page_link_t) | ||
| 883 | ->where( $pending_page_link_t->column('to_wiki_id'), '=', | ||
| 884 | Fey::Placeholder->new() ); | ||
| 885 | #>>> | ||
| 886 | return $wanted_select; | ||
| 887 | } | ||
| 888 | |||
| 889 | sub wanted_pages { | ||
| 890 | my $self = shift; | ||
| 891 | my ( $limit, $offset ) = validated_list( | ||
| 892 | \@_, | ||
| 893 | limit => { isa => Int, optional => 1 }, | ||
| 894 | offset => { isa => Int, default => 0 }, | ||
| 895 | ); | ||
| 896 | |||
| 897 | my $select = $self->_WantedPagesSelect()->clone(); | ||
| 898 | $select->limit( $limit, $offset ); | ||
| 899 | |||
| 900 | return Fey::Object::Iterator::FromSelect->new( | ||
| 901 | classes => ['Silki::Schema::WantedPage'], | ||
| 902 | select => $select, | ||
| 903 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 904 | bind_params => [ $self->wiki_id() ], | ||
| 905 | attribute_map => { | ||
| 906 | 0 => { | ||
| 907 | class => 'Silki::Schema::WantedPage', | ||
| 908 | attribute => 'title', | ||
| 909 | }, | ||
| 910 | 1 => { | ||
| 911 | class => 'Silki::Schema::WantedPage', | ||
| 912 | attribute => 'wiki_id', | ||
| 913 | }, | ||
| 914 | 2 => { | ||
| 915 | class => 'Silki::Schema::WantedPage', | ||
| 916 | attribute => 'wanted_count', | ||
| 917 | }, | ||
| 918 | }, | ||
| 919 | ); | ||
| 920 | } | ||
| 921 | |||
| 922 | sub _BuildWantedPagesSelect { | ||
| 923 | my $class = shift; | ||
| 924 | |||
| 925 | my $pending_page_link_t = $Schema->table('PendingPageLink'); | ||
| 926 | |||
| 927 | my $count = Fey::Literal::Function->new( | ||
| 928 | 'COUNT', | ||
| 929 | $pending_page_link_t->column('from_page_id') | ||
| 930 | ); | ||
| 931 | |||
| 932 | my $wanted_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 933 | |||
| 934 | #<<< | ||
| 935 | $wanted_select | ||
| 936 | ->select( $pending_page_link_t->columns( 'to_page_title', 'to_wiki_id' ), $count ) | ||
| 937 | ->from( $pending_page_link_t ) | ||
| 938 | ->where( $pending_page_link_t->column('to_wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 939 | ->group_by( $pending_page_link_t->columns( 'to_page_title', 'to_wiki_id' ) ) | ||
| 940 | ->order_by( | ||
| 941 | $count, 'DESC', | ||
| 942 | $pending_page_link_t->column('to_page_title'), 'ASC', | ||
| 943 | ); | ||
| 944 | #>>> | ||
| 945 | return $wanted_select; | ||
| 946 | } | ||
| 947 | |||
| 948 | sub recently_viewed_pages { | ||
| 949 | my $self = shift; | ||
| 950 | my ( $cutoff, $limit, $offset ) = validated_list( | ||
| 951 | \@_, | ||
| 952 | cutoff => { | ||
| 953 | isa => 'DateTime', | ||
| 954 | default => DateTime->today()->subtract( months => 1 ) | ||
| 955 | }, | ||
| 956 | limit => { isa => Int, optional => 1 }, | ||
| 957 | offset => { isa => Int, default => 0 }, | ||
| 958 | ); | ||
| 959 | |||
| 960 | my $select = $self->_RecentlyViewedPagesSelect()->clone(); | ||
| 961 | $select->limit( $limit, $offset ); | ||
| 962 | |||
| 963 | return Fey::Object::Iterator::FromSelect->new( | ||
| 964 | classes => ['Silki::Schema::Page'], | ||
| 965 | select => $select, | ||
| 966 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 967 | bind_params => [ | ||
| 968 | $self->wiki_id(), | ||
| 969 | DateTime::Format::Pg->format_datetime($cutoff) | ||
| 970 | ], | ||
| 971 | ); | ||
| 972 | } | ||
| 973 | |||
| 974 | sub _BuildRecentlyViewedPagesSelect { | ||
| 975 | my $class = shift; | ||
| 976 | |||
| 977 | my ( $page_t, $page_view_t ) = $Schema->tables( 'Page', 'PageView' ); | ||
| 978 | |||
| 979 | my $count = Fey::Literal::Function->new( | ||
| 980 | 'COUNT', | ||
| 981 | $page_view_t->column('page_id') | ||
| 982 | ); | ||
| 983 | |||
| 984 | my $viewed_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 985 | #<<< | ||
| 986 | $viewed_select | ||
| 987 | ->select( $page_t, $count ) | ||
| 988 | ->from( $page_t, $page_view_t ) | ||
| 989 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 990 | ->and ( $page_view_t->column('view_datetime'), '>=', Fey::Placeholder->new() ) | ||
| 991 | ->group_by( $page_t->columns() ) | ||
| 992 | ->order_by( | ||
| 993 | $count, 'DESC', | ||
| 994 | $page_t->column('title'), 'ASC', | ||
| 995 | ); | ||
| 996 | #>>> | ||
| 997 | return $viewed_select; | ||
| 998 | } | ||
| 999 | |||
| 1000 | sub _MemberCountSelect { | ||
| 1001 | my $class = shift; | ||
| 1002 | |||
| 1003 | my $uwr_t = $Schema->table('UserWikiRole'); | ||
| 1004 | |||
| 1005 | my $count | ||
| 1006 | = Fey::Literal::Function->new( 'COUNT', $uwr_t->column('user_id') ); | ||
| 1007 | |||
| 1008 | my $member_count_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1009 | |||
| 1010 | #<<< | ||
| 1011 | $member_count_select | ||
| 1012 | ->select($count) | ||
| 1013 | ->from($uwr_t) | ||
| 1014 | ->where( $uwr_t->column('wiki_id'), '=', Fey::Placeholder->new() ); | ||
| 1015 | #>>> | ||
| 1016 | return $member_count_select; | ||
| 1017 | } | ||
| 1018 | |||
| 1019 | sub _FileCountSelect { | ||
| 1020 | my $class = shift; | ||
| 1021 | |||
| 1022 | my ( $file_t, $page_t ) = $Schema->tables( 'File', 'Page' ); | ||
| 1023 | |||
| 1024 | my $count | ||
| 1025 | = Fey::Literal::Function->new( 'COUNT', $file_t->column('file_id') ); | ||
| 1026 | |||
| 1027 | my $file_count_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1028 | |||
| 1029 | #<<< | ||
| 1030 | $file_count_select | ||
| 1031 | ->select($count) | ||
| 1032 | ->from( $file_t, $page_t ) | ||
| 1033 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ); | ||
| 1034 | #>>> | ||
| 1035 | return $file_count_select; | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | sub files { | ||
| 1039 | my $self = shift; | ||
| 1040 | my ( $limit, $offset ) = validated_list( | ||
| 1041 | \@_, | ||
| 1042 | limit => { isa => Int, optional => 1 }, | ||
| 1043 | offset => { isa => Int, default => 0 }, | ||
| 1044 | ); | ||
| 1045 | |||
| 1046 | my $select = $self->_FilesSelect()->clone(); | ||
| 1047 | $select->limit( $limit, $offset ); | ||
| 1048 | |||
| 1049 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1050 | classes => ['Silki::Schema::File'], | ||
| 1051 | select => $select, | ||
| 1052 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 1053 | bind_params => [ $self->wiki_id() ], | ||
| 1054 | ); | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | sub _BuildFilesSelect { | ||
| 1058 | my $class = shift; | ||
| 1059 | |||
| 1060 | my ( $file_t, $page_t ) = $Schema->tables( 'File', 'Page' ); | ||
| 1061 | |||
| 1062 | my $files_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1063 | |||
| 1064 | #<<< | ||
| 1065 | $files_select | ||
| 1066 | ->select($file_t) | ||
| 1067 | ->from( $file_t, $page_t ) | ||
| 1068 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1069 | ->order_by( $file_t->column('filename'), 'ASC' ); | ||
| 1070 | #>>> | ||
| 1071 | return $files_select; | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | sub members { | ||
| 1075 | my $self = shift; | ||
| 1076 | my ( $limit, $offset ) = validated_list( | ||
| 1077 | \@_, | ||
| 1078 | limit => { isa => Int, optional => 1 }, | ||
| 1079 | offset => { isa => Int, default => 0 }, | ||
| 1080 | ); | ||
| 1081 | |||
| 1082 | my $select = $self->_MembersSelect()->clone(); | ||
| 1083 | $select->limit( $limit, $offset ); | ||
| 1084 | |||
| 1085 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1086 | classes => [ 'Silki::Schema::User', 'Silki::Schema::Role' ], | ||
| 1087 | select => $select, | ||
| 1088 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 1089 | bind_params => [ $self->wiki_id() ], | ||
| 1090 | ); | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | sub _BuildMembersSelect { | ||
| 1094 | my $class = shift; | ||
| 1095 | |||
| 1096 | my $user_t = $Schema->table('User'); | ||
| 1097 | my $uwr_t = $Schema->table('UserWikiRole'); | ||
| 1098 | my $role_t = $Schema->table('Role'); | ||
| 1099 | |||
| 1100 | my $members_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1101 | |||
| 1102 | #<<< | ||
| 1103 | $members_select | ||
| 1104 | ->select( $user_t, $role_t ) | ||
| 1105 | ->from( $user_t, $uwr_t ) | ||
| 1106 | ->from( $uwr_t, $role_t ) | ||
| 1107 | ->where( $uwr_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1108 | ->order_by( $role_t->column('name'), 'ASC', | ||
| 1109 | $user_t->column('display_name'), 'ASC', | ||
| 1110 | $user_t->column('email_address'), 'ASC', | ||
| 1111 | ); | ||
| 1112 | #>>> | ||
| 1113 | return $members_select; | ||
| 1114 | } | ||
| 1115 | |||
| 1116 | sub active_users { | ||
| 1117 | my $self = shift; | ||
| 1118 | my ( $limit, $offset ) = validated_list( | ||
| 1119 | \@_, | ||
| 1120 | limit => { isa => Int, optional => 1 }, | ||
| 1121 | offset => { isa => Int, default => 0 }, | ||
| 1122 | ); | ||
| 1123 | |||
| 1124 | my $select = $self->_ActiveUsersSelect()->clone(); | ||
| 1125 | $select->limit( $limit, $offset ); | ||
| 1126 | |||
| 1127 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1128 | classes => ['Silki::Schema::User'], | ||
| 1129 | select => $select, | ||
| 1130 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 1131 | bind_params => [ $self->wiki_id(), $select->bind_params() ], | ||
| 1132 | ); | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | sub _BuildActiveUsersSelect { | ||
| 1136 | my $class = shift; | ||
| 1137 | |||
| 1138 | my $user_t = $Schema->table('User'); | ||
| 1139 | my $page_t = $Schema->table('Page'); | ||
| 1140 | my $page_revision_t = $Schema->table('PageRevision'); | ||
| 1141 | |||
| 1142 | my $username_or_display_name = Fey::Literal::Term->new( | ||
| 1143 | q{CASE WHEN display_name = '' THEN username ELSE display_name END}); | ||
| 1144 | |||
| 1145 | my $max = Fey::Literal::Function->new( | ||
| 1146 | 'MAX', | ||
| 1147 | $page_revision_t->column('creation_datetime') | ||
| 1148 | ); | ||
| 1149 | |||
| 1150 | my $users_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1151 | |||
| 1152 | #<<< | ||
| 1153 | $users_select | ||
| 1154 | ->select( $user_t, $username_or_display_name, $max ) | ||
| 1155 | ->distinct() | ||
| 1156 | ->from( $user_t, $page_revision_t ) | ||
| 1157 | ->from( $page_revision_t, $page_t ) | ||
| 1158 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1159 | ->where( $user_t->column('is_system_user'), '=', 0 ) | ||
| 1160 | ->where( | ||
| 1161 | $page_revision_t->column('creation_datetime'), | ||
| 1162 | '>=', | ||
| 1163 | DateTime::Format::Pg->format_datetime( | ||
| 1164 | DateTime->today()->subtract( months => 1 ) | ||
| 1165 | ) | ||
| 1166 | ) | ||
| 1167 | ->group_by( $user_t->columns(), $username_or_display_name ) | ||
| 1168 | ->order_by( $max, 'DESC', $username_or_display_name, ); | ||
| 1169 | #>>> | ||
| 1170 | return $users_select; | ||
| 1171 | } | ||
| 1172 | |||
| 1173 | # This is a rather complicated query. The end result is something like this .. | ||
| 1174 | # | ||
| 1175 | # SELECT | ||
| 1176 | # *, | ||
| 1177 | # TS_HEADLINE(title || E'\n' || content, "page") AS "headline" | ||
| 1178 | # FROM | ||
| 1179 | # ( | ||
| 1180 | # SELECT | ||
| 1181 | # "Page"."is_archived", | ||
| 1182 | # "Page"."page_id", | ||
| 1183 | # ..., | ||
| 1184 | # "PageRevision"."comment", | ||
| 1185 | # "PageRevision"."creation_datetime", | ||
| 1186 | # ..., | ||
| 1187 | # TS_RANK("PageSearchableText"."ts_text", "page") AS "rank" | ||
| 1188 | # FROM | ||
| 1189 | # "Page" JOIN "PageRevision" ON ("PageRevision"."page_id" = "Page"."page_id") | ||
| 1190 | # JOIN "PageSearchableText" ON ("PageSearchableText"."page_id" = "Page"."page_id") | ||
| 1191 | # WHERE | ||
| 1192 | # "PageSearchableText"."ts_text" @@ to_tsquery(?) | ||
| 1193 | # AND | ||
| 1194 | # "Page"."wiki_id" = ? | ||
| 1195 | # AND | ||
| 1196 | # "PageRevision"."revision_number" = | ||
| 1197 | # ( SELECT | ||
| 1198 | # MAX("PageRevision"."revision_number") AS "FUNCTION0" | ||
| 1199 | # FROM | ||
| 1200 | # "PageRevision" | ||
| 1201 | # WHERE | ||
| 1202 | # "PageRevision"."page_id" = "Page"."page_id" ) | ||
| 1203 | # ORDER BY | ||
| 1204 | # "rank" DESC, "Page"."title" ASC | ||
| 1205 | # OFFSET 0 | ||
| 1206 | # ) | ||
| 1207 | # AS "SUBSELECT0" | ||
| 1208 | # | ||
| 1209 | # Part of the reason for the complication is that we want to generate the | ||
| 1210 | # headline (TS_HEADLINE) only after applying the OFFSET clause. If we don't do | ||
| 1211 | # this, then we generate the headline for every match, regardless of how many | ||
| 1212 | # are being displayed. See | ||
| 1213 | # http://www.postgresql.org/docs/8.3/static/textsearch-controls.html#TEXTSEARCH-HEADLINE | ||
| 1214 | # for details. | ||
| 1215 | # | ||
| 1216 | # The innermost select clause on PageRevision.revision_number ensures that | ||
| 1217 | # we only retrieve the most recent revision for a page. | ||
| 1218 | |||
| 1219 | sub text_search { | ||
| 1220 | my $self = shift; | ||
| 1221 | my ( $query, $limit, $offset ) = validated_list( | ||
| 1222 | \@_, | ||
| 1223 | query => { isa => Str }, | ||
| 1224 | limit => { isa => Int, optional => 1 }, | ||
| 1225 | offset => { isa => Int, default => 0 }, | ||
| 1226 | ); | ||
| 1227 | |||
| 1228 | my $page_t = $Schema->table('Page'); | ||
| 1229 | my $page_revision_t = $Schema->table('PageRevision'); | ||
| 1230 | my $pst_t = $Schema->table('PageSearchableText'); | ||
| 1231 | |||
| 1232 | my $max_revision = $self->_MaxRevisionSelect(); | ||
| 1233 | |||
| 1234 | my $ts_query = Fey::Literal::Function->new( 'TO_TSQUERY', $query ); | ||
| 1235 | |||
| 1236 | my $rank = Fey::Literal::Function->new( | ||
| 1237 | 'TS_RANK', | ||
| 1238 | $pst_t->column('ts_text'), | ||
| 1239 | $ts_query, | ||
| 1240 | ); | ||
| 1241 | |||
| 1242 | $rank->set_alias_name('rank'); | ||
| 1243 | |||
| 1244 | my $search_select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1245 | |||
| 1246 | #<<< | ||
| 1247 | $search_select | ||
| 1248 | ->select( $page_t, $page_revision_t, $rank ) | ||
| 1249 | ->from( $page_t, $page_revision_t ) | ||
| 1250 | ->from( $page_t, $pst_t ) | ||
| 1251 | ->where( $pst_t->column('ts_text'), '@@', $ts_query ) | ||
| 1252 | ->and ( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1253 | ->and ( $page_revision_t->column('revision_number'), | ||
| 1254 | '=', $max_revision ) | ||
| 1255 | ->order_by( $rank, 'DESC', | ||
| 1256 | $page_t->column('title'), 'ASC' ); | ||
| 1257 | #>>> | ||
| 1258 | $search_select->limit( $limit, $offset ); | ||
| 1259 | |||
| 1260 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1261 | |||
| 1262 | my $marker = Silki::Schema::TextSearchResult->HighlightMarker(); | ||
| 1263 | my $result = Fey::Literal::Function->new( | ||
| 1264 | 'TS_HEADLINE', | ||
| 1265 | Fey::Literal::Term->new( q{title || E'\n' || content}, ), | ||
| 1266 | $ts_query, | ||
| 1267 | "StartSel = $marker, StopSel = $marker", | ||
| 1268 | ); | ||
| 1269 | $result->set_alias_name('result'); | ||
| 1270 | |||
| 1271 | my $star = Fey::Literal::Term->new('*'); | ||
| 1272 | $star->set_can_have_alias(0); | ||
| 1273 | |||
| 1274 | #<<< | ||
| 1275 | $select | ||
| 1276 | ->select( $star, $result ) | ||
| 1277 | ->from($search_select); | ||
| 1278 | #>>> | ||
| 1279 | my $x = 0; | ||
| 1280 | my %attribute_map; | ||
| 1281 | |||
| 1282 | # This matches the order of the columns in the $search_select defined | ||
| 1283 | # above. | ||
| 1284 | for my $col_name ( sort map { $_->name() } $page_t->columns() ) { | ||
| 1285 | $attribute_map{ $x++ } = { | ||
| 1286 | class => 'Silki::Schema::Page', | ||
| 1287 | attribute => $col_name, | ||
| 1288 | }; | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | for my $col_name ( sort map { $_->name() } $page_revision_t->columns() ) { | ||
| 1292 | $attribute_map{ $x++ } = { | ||
| 1293 | class => 'Silki::Schema::PageRevision', | ||
| 1294 | attribute => $col_name, | ||
| 1295 | }; | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | # Need to skip the rank item in the row | ||
| 1299 | $attribute_map{ ++$x } = { | ||
| 1300 | class => 'Silki::Schema::TextSearchResult', | ||
| 1301 | attribute => 'result', | ||
| 1302 | }; | ||
| 1303 | |||
| 1304 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1305 | classes => [ | ||
| 1306 | 'Silki::Schema::Page', | ||
| 1307 | 'Silki::Schema::PageRevision', | ||
| 1308 | 'Silki::Schema::TextSearchResult' | ||
| 1309 | ], | ||
| 1310 | select => $select, | ||
| 1311 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 1312 | bind_params => [ $self->wiki_id() ], | ||
| 1313 | attribute_map => \%attribute_map, | ||
| 1314 | ); | ||
| 1315 | } | ||
| 1316 | |||
| 1317 | sub pages_tagged { | ||
| 1318 | my $self = shift; | ||
| 1319 | my ( $tag_name, $limit, $offset ) = validated_list( | ||
| 1320 | \@_, | ||
| 1321 | tag => { isa => Str }, | ||
| 1322 | limit => { isa => Int, optional => 1 }, | ||
| 1323 | offset => { isa => Int, default => 0 }, | ||
| 1324 | ); | ||
| 1325 | |||
| 1326 | my $tag = $self->_find_tag_by_name($tag_name) | ||
| 1327 | or return; | ||
| 1328 | |||
| 1329 | my $select = $self->_PagesByTagSelect()->clone(); | ||
| 1330 | |||
| 1331 | $select->limit( $limit, $offset ); | ||
| 1332 | |||
| 1333 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1334 | classes => [ 'Silki::Schema::Page', 'Silki::Schema::PageRevision' ], | ||
| 1335 | select => $select, | ||
| 1336 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 1337 | bind_params => [ $tag->tag_id() ], | ||
| 1338 | ); | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | sub _BuildPagesByTagSelect { | ||
| 1342 | my $class = shift; | ||
| 1343 | |||
| 1344 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1345 | |||
| 1346 | my ( $page_t, $page_revision_t, $page_tag_t ) | ||
| 1347 | = $Schema->tables( 'Page', 'PageRevision', 'PageTag' ); | ||
| 1348 | |||
| 1349 | my $max_revision = $class->_MaxRevisionSelect(); | ||
| 1350 | |||
| 1351 | #<<< | ||
| 1352 | $select | ||
| 1353 | ->select( $page_t, $page_revision_t ) | ||
| 1354 | ->from( $page_t, $page_revision_t ) | ||
| 1355 | ->from( $page_t, $page_tag_t ) | ||
| 1356 | ->where( $page_tag_t->column('tag_id'), '=', Fey::Placeholder->new() ) | ||
| 1357 | ->and ( $page_revision_t->column('revision_number'), | ||
| 1358 | '=', $max_revision ) | ||
| 1359 | ->order_by( $page_t->column('title') ); | ||
| 1360 | #>>> | ||
| 1361 | return $select; | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | sub pages_tagged_count { | ||
| 1365 | my $self = shift; | ||
| 1366 | my ($tag_name) = validated_list( | ||
| 1367 | \@_, | ||
| 1368 | tag => { isa => Str }, | ||
| 1369 | ); | ||
| 1370 | |||
| 1371 | my $tag = $self->_find_tag_by_name($tag_name) | ||
| 1372 | or return 0; | ||
| 1373 | |||
| 1374 | my $select = $self->_PagesByTagCountSelect(); | ||
| 1375 | |||
| 1376 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1377 | |||
| 1378 | my $vals = $dbh->selectrow_arrayref( | ||
| 1379 | $select->sql($dbh), | ||
| 1380 | {}, | ||
| 1381 | $tag->tag_id(), | ||
| 1382 | ); | ||
| 1383 | |||
| 1384 | return $vals ? $vals->[0] : 0; | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | sub _BuildPagesByTagCountSelect { | ||
| 1388 | my $class = shift; | ||
| 1389 | |||
| 1390 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1391 | |||
| 1392 | my ($page_tag_t) = $Schema->table('PageTag'); | ||
| 1393 | |||
| 1394 | my $count = Fey::Literal::Function->new( | ||
| 1395 | 'COUNT', | ||
| 1396 | $page_tag_t->column('page_id'), | ||
| 1397 | ); | ||
| 1398 | |||
| 1399 | #<<< | ||
| 1400 | $select | ||
| 1401 | ->select($count) | ||
| 1402 | ->from($page_tag_t) | ||
| 1403 | ->where( $page_tag_t->column('tag_id'), '=', Fey::Placeholder->new() ); | ||
| 1404 | #>>> | ||
| 1405 | return $select; | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | sub _find_tag_by_name { | ||
| 1409 | my $self = shift; | ||
| 1410 | my $name = shift; | ||
| 1411 | |||
| 1412 | return Silki::Schema::Tag->new( | ||
| 1413 | tag => $name, | ||
| 1414 | wiki_id => $self->wiki_id(), | ||
| 1415 | ); | ||
| 1416 | } | ||
| 1417 | |||
| 1418 | sub popular_tags { | ||
| 1419 | my $self = shift; | ||
| 1420 | my ( $limit, $offset ) = validated_list( | ||
| 1421 | \@_, | ||
| 1422 | limit => { isa => Int, optional => 1 }, | ||
| 1423 | offset => { isa => Int, default => 0 }, | ||
| 1424 | ); | ||
| 1425 | |||
| 1426 | my $select = $self->_PopularTagsSelect()->clone(); | ||
| 1427 | $select->limit( $limit, $offset ); | ||
| 1428 | |||
| 1429 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1430 | classes => ['Silki::Schema::Tag'], | ||
| 1431 | select => $select, | ||
| 1432 | dbh => Silki::Schema->DBIManager()->source_for_sql($select)->dbh(), | ||
| 1433 | bind_params => [ $self->wiki_id() ], | ||
| 1434 | ); | ||
| 1435 | } | ||
| 1436 | |||
| 1437 | sub _BuildPopularTagsSelect { | ||
| 1438 | my $class = shift; | ||
| 1439 | |||
| 1440 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1441 | |||
| 1442 | my ( $tag_t, $page_tag_t ) = $Schema->tables( 'Tag', 'PageTag' ); | ||
| 1443 | |||
| 1444 | my $count = Fey::Literal::Function->new( | ||
| 1445 | 'COUNT', | ||
| 1446 | $page_tag_t->column('page_id'), | ||
| 1447 | ); | ||
| 1448 | |||
| 1449 | #<<< | ||
| 1450 | $select | ||
| 1451 | ->select( $tag_t, $count ) | ||
| 1452 | ->from( $tag_t, $page_tag_t ) | ||
| 1453 | ->where( $tag_t->column('wiki_id'), '=', | ||
| 1454 | Fey::Placeholder->new() ) | ||
| 1455 | ->group_by( $tag_t->columns() ) | ||
| 1456 | ->order_by( $count, 'DESC', | ||
| 1457 | $tag_t->column('tag'), 'ASC' ); | ||
| 1458 | #>>> | ||
| 1459 | return $select; | ||
| 1460 | } | ||
| 1461 | |||
| 1462 | sub _TagCountSelect { | ||
| 1463 | my $class = shift; | ||
| 1464 | |||
| 1465 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1466 | |||
| 1467 | my ( $page_t, $page_tag_t, $tag_t ) | ||
| 1468 | = $Schema->tables( 'Page', 'PageTag', 'Tag' ); | ||
| 1469 | |||
| 1470 | my $distinct = Fey::Literal::Term->new( | ||
| 1471 | 'DISTINCT ', | ||
| 1472 | $page_tag_t->column('tag_id'), | ||
| 1473 | ); | ||
| 1474 | |||
| 1475 | my $count = Fey::Literal::Function->new( 'COUNT', $distinct ); | ||
| 1476 | |||
| 1477 | #<<< | ||
| 1478 | $select | ||
| 1479 | ->select($count) | ||
| 1480 | ->from( $page_t, $page_tag_t ) | ||
| 1481 | ->from( $page_tag_t, $tag_t ) | ||
| 1482 | ->where( $tag_t->column('wiki_id'), '=', | ||
| 1483 | Fey::Placeholder->new() ); | ||
| 1484 | #>>> | ||
| 1485 | return $select; | ||
| 1486 | } | ||
| 1487 | |||
| 1488 | sub pages_edited_by_user { | ||
| 1489 | my $self = shift; | ||
| 1490 | my ( $user, $limit, $offset ) = validated_list( | ||
| 1491 | \@_, | ||
| 1492 | user => { isa => 'Silki::Schema::User' }, | ||
| 1493 | limit => { isa => Int, optional => 1 }, | ||
| 1494 | offset => { isa => Int, default => 0 }, | ||
| 1495 | ); | ||
| 1496 | |||
| 1497 | my $select = $self->_PagesEditedByUserSelect()->clone(); | ||
| 1498 | $select->limit( $limit, $offset ); | ||
| 1499 | |||
| 1500 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1501 | |||
| 1502 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1503 | classes => [ 'Silki::Schema::Page', 'Silki::Schema::PageRevision' ], | ||
| 1504 | select => $select, | ||
| 1505 | dbh => $dbh, | ||
| 1506 | bind_params => [ $self->wiki_id(), $user->user_id() ], | ||
| 1507 | ); | ||
| 1508 | } | ||
| 1509 | |||
| 1510 | sub _BuildPagesEditedByUserSelect { | ||
| 1511 | my $class = shift; | ||
| 1512 | |||
| 1513 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1514 | |||
| 1515 | my ( $page_t, $page_revision_t ) | ||
| 1516 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 1517 | |||
| 1518 | my $max_revision = $class->_MaxRevisionSelect()->clone(); | ||
| 1519 | $max_revision->and( | ||
| 1520 | $page_revision_t->column('user_id'), '=', | ||
| 1521 | Fey::Placeholder->new() | ||
| 1522 | ); | ||
| 1523 | |||
| 1524 | #<<< | ||
| 1525 | $select | ||
| 1526 | ->select( $page_t, $page_revision_t ) | ||
| 1527 | ->from( $page_t, $page_revision_t ) | ||
| 1528 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1529 | ->and ( $page_revision_t->column('revision_number'), | ||
| 1530 | '=', $max_revision ) | ||
| 1531 | ->order_by( $page_revision_t->column('creation_datetime'), 'DESC', | ||
| 1532 | $page_t->column('title'), 'ASC' ); | ||
| 1533 | #>>> | ||
| 1534 | return $select; | ||
| 1535 | } | ||
| 1536 | |||
| 1537 | sub pages_edited_by_user_count { | ||
| 1538 | my $self = shift; | ||
| 1539 | my ($user) = pos_validated_list( \@_, { isa => 'Silki::Schema::User' } ); | ||
| 1540 | |||
| 1541 | my $select = $self->_PagesEditedByUserCountSelect(); | ||
| 1542 | |||
| 1543 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1544 | |||
| 1545 | my $vals = $dbh->selectrow_arrayref( | ||
| 1546 | $select->sql($dbh), | ||
| 1547 | {}, | ||
| 1548 | $self->wiki_id(), $user->user_id(), | ||
| 1549 | ); | ||
| 1550 | |||
| 1551 | return $vals ? $vals->[0] : 0; | ||
| 1552 | } | ||
| 1553 | |||
| 1554 | sub _BuildPagesEditedByUserCountSelect { | ||
| 1555 | my $class = shift; | ||
| 1556 | |||
| 1557 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1558 | |||
| 1559 | my ( $page_t, $page_revision_t ) | ||
| 1560 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 1561 | |||
| 1562 | my $distinct = Fey::Literal::Term->new( | ||
| 1563 | 'DISTINCT ', | ||
| 1564 | Fey::Literal::Function->new( | ||
| 1565 | 'COUNT', $page_revision_t->column('page_id') | ||
| 1566 | ) | ||
| 1567 | ); | ||
| 1568 | |||
| 1569 | #<<< | ||
| 1570 | $select | ||
| 1571 | ->select($distinct) | ||
| 1572 | ->from( $page_t, $page_revision_t ) | ||
| 1573 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1574 | ->and ( $page_revision_t->column('user_id'), '=', Fey::Placeholder->new() ); | ||
| 1575 | #>>> | ||
| 1576 | return $select; | ||
| 1577 | } | ||
| 1578 | |||
| 1579 | sub pages_created_by_user { | ||
| 1580 | my $self = shift; | ||
| 1581 | my ( $user, $limit, $offset ) = validated_list( | ||
| 1582 | \@_, | ||
| 1583 | user => { isa => 'Silki::Schema::User' }, | ||
| 1584 | limit => { isa => Int, optional => 1 }, | ||
| 1585 | offset => { isa => Int, default => 0 }, | ||
| 1586 | ); | ||
| 1587 | |||
| 1588 | my $select = $self->_PagesCreatedByUserSelect()->clone(); | ||
| 1589 | $select->limit( $limit, $offset ); | ||
| 1590 | |||
| 1591 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1592 | |||
| 1593 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1594 | classes => [ 'Silki::Schema::Page', 'Silki::Schema::PageRevision' ], | ||
| 1595 | select => $select, | ||
| 1596 | dbh => $dbh, | ||
| 1597 | bind_params => [ $self->wiki_id(), $user->user_id() ], | ||
| 1598 | ); | ||
| 1599 | } | ||
| 1600 | |||
| 1601 | sub _BuildPagesCreatedByUserSelect { | ||
| 1602 | my $class = shift; | ||
| 1603 | |||
| 1604 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1605 | |||
| 1606 | my $min_revision = $class->_MinRevisionSelect(); | ||
| 1607 | |||
| 1608 | my ( $page_t, $page_revision_t ) | ||
| 1609 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 1610 | |||
| 1611 | #<<< | ||
| 1612 | $select | ||
| 1613 | ->select( $page_t, $page_revision_t ) | ||
| 1614 | ->from( $page_t, $page_revision_t ) | ||
| 1615 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1616 | ->and ( $page_t->column('user_id'), '=', Fey::Placeholder->new() ) | ||
| 1617 | ->and ( $page_revision_t->column('revision_number'), | ||
| 1618 | '=', $min_revision ) | ||
| 1619 | ->order_by( $page_revision_t->column('creation_datetime'), 'DESC', | ||
| 1620 | $page_t->column('title'), 'ASC' ); | ||
| 1621 | #>>> | ||
| 1622 | return $select; | ||
| 1623 | } | ||
| 1624 | |||
| 1625 | sub pages_created_by_user_count { | ||
| 1626 | my $self = shift; | ||
| 1627 | my ($user) = pos_validated_list( \@_, { isa => 'Silki::Schema::User' } ); | ||
| 1628 | |||
| 1629 | my $select = $self->_PagesCreatedByUserCountSelect(); | ||
| 1630 | |||
| 1631 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1632 | |||
| 1633 | my $vals = $dbh->selectrow_arrayref( | ||
| 1634 | $select->sql($dbh), | ||
| 1635 | {}, | ||
| 1636 | $self->wiki_id(), $user->user_id(), | ||
| 1637 | ); | ||
| 1638 | |||
| 1639 | return $vals ? $vals->[0] : 0; | ||
| 1640 | } | ||
| 1641 | |||
| 1642 | sub _BuildPagesCreatedByUserCountSelect { | ||
| 1643 | my $class = shift; | ||
| 1644 | |||
| 1645 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1646 | |||
| 1647 | my $page_t = $Schema->table('Page'); | ||
| 1648 | |||
| 1649 | my $count | ||
| 1650 | = Fey::Literal::Function->new( 'COUNT', $page_t->column('page_id') ); | ||
| 1651 | |||
| 1652 | #<<< | ||
| 1653 | $select | ||
| 1654 | ->select($count) | ||
| 1655 | ->from($page_t) | ||
| 1656 | ->where( $page_t->column('wiki_id'), '=', Fey::Placeholder->new() ) | ||
| 1657 | ->and ( $page_t->column('user_id'), '=', Fey::Placeholder->new() ); | ||
| 1658 | #>>> | ||
| 1659 | return $select; | ||
| 1660 | } | ||
| 1661 | |||
| 1662 | sub PublicWikiCount { | ||
| 1663 | my $class = shift; | ||
| 1664 | |||
| 1665 | my $select = $class->_PublicWikiCountSelect(); | ||
| 1666 | |||
| 1667 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1668 | |||
| 1669 | my $vals = $dbh->selectrow_arrayref( | ||
| 1670 | $select->sql($dbh), {}, | ||
| 1671 | $select->bind_params() | ||
| 1672 | ); | ||
| 1673 | |||
| 1674 | return $vals ? $vals->[0] : 0; | ||
| 1675 | } | ||
| 1676 | |||
| 1677 | sub PublicWikis { | ||
| 1678 | my $class = shift; | ||
| 1679 | my ( $limit, $offset ) = validated_list( | ||
| 1680 | \@_, | ||
| 1681 | limit => { isa => Int, optional => 1 }, | ||
| 1682 | offset => { isa => Int, default => 0 }, | ||
| 1683 | ); | ||
| 1684 | |||
| 1685 | my $select = $class->_PublicWikiSelect()->clone(); | ||
| 1686 | $select->limit( $limit, $offset ); | ||
| 1687 | |||
| 1688 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1689 | |||
| 1690 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1691 | classes => 'Silki::Schema::Wiki', | ||
| 1692 | select => $select, | ||
| 1693 | dbh => $dbh, | ||
| 1694 | bind_params => [ $select->bind_params() ], | ||
| 1695 | ); | ||
| 1696 | } | ||
| 1697 | |||
| 1698 | { | ||
| 1699 | my $guest = Silki::Schema::Role->Guest(); | ||
| 1700 | my $read = Silki::Schema::Permission->Read(); | ||
| 1701 | |||
| 1702 | my ( $wiki_t, $wrp_t ) = $Schema->tables( 'Wiki', 'WikiRolePermission' ); | ||
| 1703 | |||
| 1704 | my $base = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1705 | |||
| 1706 | #<<< | ||
| 1707 | $base | ||
| 1708 | ->from( $wiki_t, $wrp_t ) | ||
| 1709 | ->where( $wrp_t->column('role_id'), '=', $guest->role_id() ) | ||
| 1710 | ->and ( $wrp_t->column('permission_id'), '=', $read->permission_id() ); | ||
| 1711 | #>>> | ||
| 1712 | sub _BuildPublicWikiCountSelect { | ||
| 1713 | my $class = shift; | ||
| 1714 | |||
| 1715 | my $select = $base->clone(); | ||
| 1716 | |||
| 1717 | my $distinct = Fey::Literal::Term->new( | ||
| 1718 | 'DISTINCT ', | ||
| 1719 | $wiki_t->column('wiki_id') | ||
| 1720 | ); | ||
| 1721 | my $count = Fey::Literal::Function->new( 'COUNT', $distinct ); | ||
| 1722 | |||
| 1723 | $select->select($count); | ||
| 1724 | |||
| 1725 | return $select; | ||
| 1726 | } | ||
| 1727 | |||
| 1728 | sub _BuildPublicWikiSelect { | ||
| 1729 | my $class = shift; | ||
| 1730 | |||
| 1731 | my $select = $base->clone(); | ||
| 1732 | |||
| 1733 | $select->select($wiki_t)->distinct() | ||
| 1734 | ->order_by( $wiki_t->column('title') ); | ||
| 1735 | |||
| 1736 | return $select; | ||
| 1737 | } | ||
| 1738 | } | ||
| 1739 | |||
| 1740 | sub All { | ||
| 1741 | my $class = shift; | ||
| 1742 | my ( $limit, $offset ) = validated_list( | ||
| 1743 | \@_, | ||
| 1744 | limit => { isa => Int, optional => 1 }, | ||
| 1745 | offset => { isa => Int, default => 0 }, | ||
| 1746 | ); | ||
| 1747 | |||
| 1748 | my $select = $class->_AllWikiSelect()->clone(); | ||
| 1749 | $select->limit( $limit, $offset ); | ||
| 1750 | |||
| 1751 | my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh(); | ||
| 1752 | |||
| 1753 | return Fey::Object::Iterator::FromSelect->new( | ||
| 1754 | classes => 'Silki::Schema::Wiki', | ||
| 1755 | select => $select, | ||
| 1756 | dbh => $dbh, | ||
| 1757 | bind_params => [ $select->bind_params() ], | ||
| 1758 | ); | ||
| 1759 | } | ||
| 1760 | |||
| 1761 | sub _BuildAllWikiSelect { | ||
| 1762 | my $class = shift; | ||
| 1763 | |||
| 1764 | my $select = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1765 | |||
| 1766 | my $wiki_t = $Schema->table('Wiki'); | ||
| 1767 | |||
| 1768 | #<<< | ||
| 1769 | $select | ||
| 1770 | ->select($wiki_t) | ||
| 1771 | ->from($wiki_t) | ||
| 1772 | ->order_by( $wiki_t->column('title') ); | ||
| 1773 | #>>> | ||
| 1774 | return $select; | ||
| 1775 | } | ||
| 1776 | |||
| 1777 | sub _BuildMaxRevisionSelect { | ||
| 1778 | my $class = shift; | ||
| 1779 | |||
| 1780 | my ( $page_t, $page_revision_t ) | ||
| 1781 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 1782 | |||
| 1783 | my $max_func = Fey::Literal::Function->new( | ||
| 1784 | 'MAX', | ||
| 1785 | $page_revision_t->column('revision_number'), | ||
| 1786 | ); | ||
| 1787 | |||
| 1788 | my $max_revision = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1789 | |||
| 1790 | #<<< | ||
| 1791 | $max_revision | ||
| 1792 | ->select($max_func) | ||
| 1793 | ->from( $Schema->table('PageRevision') ) | ||
| 1794 | ->where( $Schema->table('PageRevision')->column('page_id'), | ||
| 1795 | '=', $page_t->column('page_id') | ||
| 1796 | ); | ||
| 1797 | #>>> | ||
| 1798 | return $max_revision; | ||
| 1799 | } | ||
| 1800 | |||
| 1801 | sub _BuildMinRevisionSelect { | ||
| 1802 | my $class = shift; | ||
| 1803 | |||
| 1804 | my ( $page_t, $page_revision_t ) | ||
| 1805 | = $Schema->tables( 'Page', 'PageRevision' ); | ||
| 1806 | |||
| 1807 | my $min_func = Fey::Literal::Function->new( | ||
| 1808 | 'MIN', | ||
| 1809 | $page_revision_t->column('revision_number'), | ||
| 1810 | ); | ||
| 1811 | |||
| 1812 | my $min_revision = Silki::Schema->SQLFactoryClass()->new_select(); | ||
| 1813 | |||
| 1814 | #<<< | ||
| 1815 | $min_revision | ||
| 1816 | ->select($min_func) | ||
| 1817 | ->from( $Schema->table('PageRevision') ) | ||
| 1818 | ->where( $Schema->table('PageRevision')->column('page_id'), | ||
| 1819 | '=', $page_t->column('page_id') | ||
| 1820 | ); | ||
| 1821 | #>>> | ||
| 1822 | return $min_revision; | ||
| 1823 | } | ||
| 1824 | |||
| 1825 | sub export { | ||
| 1826 | my $self = shift; | ||
| 1827 | |||
| 1828 | return Silki::Wiki::Exporter->new( | ||
| 1829 | @_, | ||
| 1830 | wiki => $self, | ||
| 1831 | )->tarball(); | ||
| 1832 | } | ||
| 1833 | |||
| 1834 | sub import_tarball { | ||
| 1835 | my $self = shift; | ||
| 1836 | |||
| 1837 | return Silki::Wiki::Importer->new( | ||
| 1838 | @_, | ||
| 1839 | )->imported_wiki(); | ||
| 1840 | } | ||
| 1841 | |||
| 1842 | __PACKAGE__->meta()->make_immutable(); | ||
| 1843 | |||
| 1844 | 1; | ||
| 1845 | |||
| 1846 | # ABSTRACT: Represents a wiki | ||
| 1847 | |||
| 1848 | __END__ |