Skip to content

Instantly share code, notes, and snippets.

@ebernhardson
Created August 15, 2014 19:51
Show Gist options
  • Save ebernhardson/221b53d9f31cadc6ea24 to your computer and use it in GitHub Desktop.
Save ebernhardson/221b53d9f31cadc6ea24 to your computer and use it in GitHub Desktop.
<?php
namespace Flow\Data;
use Flow\Exception\FlowException;
use Flow\Model\PostRevision;
use Flow\Model\Workflow;
use Flow\WatchedTopicItems;
use WatchedItem;
abstract class AbstractTopicInsertListener {
public function __construct( $changeTypes ) {
$this->changeTypes = $changeTypes;
}
public function onAfterInsert( $object, array $row, array $metadata ) {
if ( !$object instanceof PostRevision ) {
wfWarn( __METHOD__ . ': Object is no PostRevision instance' );
return;
}
if ( !in_array( $row['rev_change_type'], $this->changeTypes ) ) {
return;
}
if ( !isset( $metadata['workflow'] ) ) {
wfWarn( __METHOD__ . ': Missing required metadata: workflow' );
return;
}
if ( $metadata['workflow']->getType() !== 'topic' ) {
wfWarn( __METHOD__ . ': Expected "topic" workflow but received "' . $metadata['workflow']->getType() . '"' );
return;
}
$this->onAfterInsertExpectedChange( $metadata['workflow'] );
}
abstract protected function onAfterInsertExpectedChange( Workflow $workflow );
// do nothing
public function onAfterLoad( $object, array $new ) {}
public function onAfterUpdate( $object, array $old, array $new, array $metadata ) {}
public function onAfterRemove( $object, array $old, array $metadata ) {}
}
/**
* Auto subscribe the user to the article title when one of the actions
* specified in the constructor is inserted.
*/
class WatchTopicListener extends AbstractTopicInsertListener {
public function __construct( array $changeTypes, WatchedTopicItems $watchedTopicItems ) {
parent::__construct( $changeTypes );
$this->watchedTopicItems = $watchedTopicItems;
}
protected function onAfterInsertExpectedChange( Workflow $workflow ) {
$title = $workflow->getArticleTitle();
if ( $title ) {
WatchedItem::fromUserTitle( $this->watchedTopicItems->getUser(), $title )
->addWatch();
$this->watchedTopicItems->addOverrideWatched( $title );
}
}
}
/**
* Auto subscribe watchers of the owner title to the article title
* when one of the actions specified in the constructor is inserted.
*/
class SubscribeWatchersListener extends AbstractTopicInsertListener {
protected function onAfterInsertExpectedChange( Workflow $workflow ) {
JobQueueGroup::singleton()->push( new SubscribeWatchersJob(
// page the topic was created on
$workflow->getOwnerTitle(),
array(
// page to subscribe to
'subscribe' => $workflow->getArticleTitle(),
)
) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment