Issue #2041083 by marcingy, grisendo, herom: Move database query out of \Drupal\aggregator\FeedFormController.

8.0.x
Nathaniel Catchpole 2013-10-31 11:31:35 +00:00
parent d5de573d29
commit bb5048e7d8
6 changed files with 115 additions and 42 deletions

View File

@ -125,4 +125,11 @@ class CategoryStorageController implements CategoryStorageControllerInterface {
} }
} }
/**
* {@inheritdoc}
*/
public function loadAllKeyed() {
return $this->database->query('SELECT c.cid, c.title FROM {aggregator_category} c ORDER BY title')->fetchAllKeyed();
}
} }

View File

@ -87,5 +87,13 @@ interface CategoryStorageControllerInterface {
*/ */
public function updateItem($iid, array $cids); public function updateItem($iid, array $cids);
/**
* Loads all categories.
*
* @return array
* An array keyed on cid listing all available categories.
*/
public function loadAllKeyed();
} }

View File

@ -9,13 +9,53 @@ namespace Drupal\aggregator;
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Core\Entity\ContentEntityFormController; use Drupal\Core\Entity\ContentEntityFormController;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\aggregator\CategoryStorageControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Form controller for the aggregator feed edit forms. * Form controller for the aggregator feed edit forms.
*/ */
class FeedFormController extends ContentEntityFormController { class FeedFormController extends ContentEntityFormController {
/**
* The feed storage.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $feedStorageController;
/**
* The category storage controller.
*
* @var \Drupal\aggregator\CategoryStorageControllerInterface
*/
protected $categoryStorageController;
/**
* Constructs a FeedForm object.
*
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $feed_storage
* The feed storage.
* @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage_controller
* The category storage controller.
*/
public function __construct(EntityStorageControllerInterface $feed_storage, CategoryStorageControllerInterface $category_storage_controller) {
$this->feedStorageController = $feed_storage;
$this->categoryStorageController = $category_storage_controller;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.entity')->getStorageController('aggregator_feed'),
$container->get('aggregator.category.storage')
);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -58,11 +98,11 @@ class FeedFormController extends ContentEntityFormController {
// Handling of categories. // Handling of categories.
$options = array(); $options = array();
$values = array(); $values = array();
$categories = db_query('SELECT c.cid, c.title FROM {aggregator_category} c ORDER BY title'); $categories = $this->categoryStorageController->loadAllKeyed();
foreach ($categories as $category) { foreach ($categories as $cid => $title) {
$options[$category->cid] = String::checkPlain($category->title); $options[$cid] = String::checkPlain($title);
if (!empty($feed->categories) && in_array($category->cid, array_keys($feed->categories))) { if (!empty($feed->categories) && in_array($cid, array_keys($feed->categories))) {
$values[] = $category->cid; $values[] = $cid;
} }
} }
@ -85,13 +125,7 @@ class FeedFormController extends ContentEntityFormController {
public function validate(array $form, array &$form_state) { public function validate(array $form, array &$form_state) {
$feed = $this->buildEntity($form, $form_state); $feed = $this->buildEntity($form, $form_state);
// Check for duplicate titles. // Check for duplicate titles.
if ($feed->id()) { $result = $this->feedStorageController->getFeedDuplicates($feed);
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE (title = :title OR url = :url) AND fid <> :fid", array(':title' => $feed->label(), ':url' => $feed->url->value, ':fid' => $feed->id()));
}
else {
$result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $feed->label(), ':url' => $feed->url->value));
}
foreach ($result as $item) { foreach ($result as $item) {
if (strcasecmp($item->title, $feed->label()) == 0) { if (strcasecmp($item->title, $feed->label()) == 0) {
form_set_error('title', $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label()))); form_set_error('title', $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label())));

View File

@ -7,12 +7,11 @@
namespace Drupal\aggregator; namespace Drupal\aggregator;
use Drupal\aggregator\FeedInterface;
use Drupal\Core\Entity\FieldableDatabaseStorageController; use Drupal\Core\Entity\FieldableDatabaseStorageController;
use Drupal\aggregator\Entity\Feed;
use Drupal\Core\Entity\EntityInterface;
/** /**
* Controller class for aggregators feeds. * Controller class for aggregator's feeds.
* *
* This extends the Drupal\Core\Entity\DatabaseStorageController class, adding * This extends the Drupal\Core\Entity\DatabaseStorageController class, adding
* required special handling for feed entities. * required special handling for feed entities.
@ -39,7 +38,7 @@ class FeedStorageController extends FieldableDatabaseStorageController implement
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function saveCategories(Feed $feed, array $categories) { public function saveCategories(FeedInterface $feed, array $categories) {
foreach ($categories as $cid => $value) { foreach ($categories as $cid => $value) {
if ($value) { if ($value) {
$this->database->insert('aggregator_category_feed') $this->database->insert('aggregator_category_feed')
@ -62,4 +61,18 @@ class FeedStorageController extends FieldableDatabaseStorageController implement
->execute(); ->execute();
} }
/**
* {@inheritdoc}
*/
public function getFeedDuplicates(FeedInterface $feed) {
if ($feed->id()) {
$query = $this->database->query("SELECT title, url FROM {aggregator_feed} WHERE (title = :title OR url = :url) AND fid <> :fid", array(':title' => $feed->label(), ':url' => $feed->url->value, ':fid' => $feed->id()));
}
else {
$query = $this->database->query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $feed->label(), ':url' => $feed->url->value));
}
return $query->fetchAll();
}
} }

View File

@ -7,7 +7,7 @@
namespace Drupal\aggregator; namespace Drupal\aggregator;
use Drupal\aggregator\Entity\Feed; use Drupal\aggregator\FeedInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Entity\EntityStorageControllerInterface;
/** /**
@ -18,7 +18,7 @@ interface FeedStorageControllerInterface extends EntityStorageControllerInterfac
/** /**
* Loads the categories of a feed. * Loads the categories of a feed.
* *
* @param array $entities * @param array $feeds
* A list of feed entities keyed by feed id. Each entity will get a * A list of feed entities keyed by feed id. Each entity will get a
* categories property added. * categories property added.
*/ */
@ -27,12 +27,12 @@ interface FeedStorageControllerInterface extends EntityStorageControllerInterfac
/** /**
* Saves the categories of a feed. * Saves the categories of a feed.
* *
* @param Feed $feed * @param \Drupal\aggregator\Entity\FeedInterface $feed
* The feed entity. * The feed entity.
* @param array $categories * @param array $categories
* The array of categories. * The array of categories.
*/ */
public function saveCategories(Feed $feed, array $categories); public function saveCategories(FeedInterface $feed, array $categories);
/** /**
* Deletes the categories of a feed. * Deletes the categories of a feed.
@ -42,4 +42,15 @@ interface FeedStorageControllerInterface extends EntityStorageControllerInterfac
*/ */
public function deleteCategories(array $feeds); public function deleteCategories(array $feeds);
/**
* Provides a list of duplicate feeds.
*
* @param \Drupal\aggregator\Entity\FeedInterface $feed
* The feed entity.
*
* @return
* An array with the list of duplicated feeds.
*/
public function getFeedDuplicates(FeedInterface $feed);
} }

View File

@ -7,28 +7,21 @@
namespace Drupal\aggregator\Form; namespace Drupal\aggregator\Form;
use Drupal\aggregator\CategoryStorageControllerInterface;
use Drupal\aggregator\FeedStorageControllerInterface; use Drupal\aggregator\FeedStorageControllerInterface;
use Drupal\Component\Utility\Url; use Drupal\Component\Utility\Url;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Guzzle\Http\Exception\RequestException; use Guzzle\Http\Exception\RequestException;
use Guzzle\Http\Exception\BadResponseException; use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\Client; use Guzzle\Http\ClientInterface;
/** /**
* Imports feeds from OPML. * Imports feeds from OPML.
*/ */
class OpmlFeedAdd extends FormBase { class OpmlFeedAdd extends FormBase {
/**
* The database connection object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/** /**
* The entity query factory object. * The entity query factory object.
* *
@ -41,32 +34,39 @@ class OpmlFeedAdd extends FormBase {
* *
* @var \Drupal\aggregator\FeedStorageControllerInterface * @var \Drupal\aggregator\FeedStorageControllerInterface
*/ */
protected $feedStorage; protected $feedStorageController;
/** /**
* The HTTP client to fetch the feed data with. * The HTTP client to fetch the feed data with.
* *
* @var \Guzzle\Http\Client * @var \Guzzle\Http\ClientInterface
*/ */
protected $httpClient; protected $httpClient;
/**
* The category storage controller.
*
* @var \Drupal\aggregator\CategoryStorageControllerInterface
*/
protected $categoryStorageController;
/** /**
* Constructs a database object. * Constructs a database object.
* *
* @param \Drupal\Core\Database\Connection $database
* The database object.
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* The entity query object. * The entity query object.
* @param \Drupal\aggregator\FeedStorageControllerInterface $feed_storage * @param \Drupal\aggregator\FeedStorageControllerInterface $feed_storage
* The feed storage. * The feed storage.
* @param \Guzzle\Http\Client $http_client * @param \Guzzle\Http\ClientInterface $http_client
* The Guzzle HTTP client. * The Guzzle HTTP client.
* @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage_controller
* The category storage controller.
*/ */
public function __construct(Connection $database, QueryFactory $query_factory, FeedStorageControllerInterface $feed_storage, Client $http_client) { public function __construct(QueryFactory $query_factory, FeedStorageControllerInterface $feed_storage, ClientInterface $http_client, CategoryStorageControllerInterface $category_storage_controller) {
$this->database = $database;
$this->queryFactory = $query_factory; $this->queryFactory = $query_factory;
$this->feedStorage = $feed_storage; $this->feedStorageController = $feed_storage;
$this->httpClient = $http_client; $this->httpClient = $http_client;
$this->categoryStorageController = $category_storage_controller;
} }
/** /**
@ -74,10 +74,10 @@ class OpmlFeedAdd extends FormBase {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('database'),
$container->get('entity.query'), $container->get('entity.query'),
$container->get('entity.manager')->getStorageController('aggregator_feed'), $container->get('entity.manager')->getStorageController('aggregator_feed'),
$container->get('http_default_client') $container->get('http_default_client'),
$container->get('aggregator.category.storage')
); );
} }
@ -115,7 +115,7 @@ class OpmlFeedAdd extends FormBase {
); );
// Handling of categories. // Handling of categories.
$options = array_map('check_plain', $this->database->query("SELECT cid, title FROM {aggregator_category} ORDER BY title")->fetchAllKeyed()); $options = array_map('check_plain', $this->categoryStorageController->loadAllKeyed());
if ($options) { if ($options) {
$form['category'] = array( $form['category'] = array(
'#type' => 'checkboxes', '#type' => 'checkboxes',
@ -192,7 +192,7 @@ class OpmlFeedAdd extends FormBase {
$ids = $query $ids = $query
->condition($condition) ->condition($condition)
->execute(); ->execute();
$result = $this->feedStorage->loadMultiple($ids); $result = $this->feedStorageController->loadMultiple($ids);
foreach ($result as $old) { foreach ($result as $old) {
if (strcasecmp($old->label(), $feed['title']) == 0) { if (strcasecmp($old->label(), $feed['title']) == 0) {
drupal_set_message($this->t('A feed named %title already exists.', array('%title' => $old->label())), 'warning'); drupal_set_message($this->t('A feed named %title already exists.', array('%title' => $old->label())), 'warning');
@ -204,7 +204,7 @@ class OpmlFeedAdd extends FormBase {
} }
} }
$new_feed = $this->feedStorage->create(array( $new_feed = $this->feedStorageController->create(array(
'title' => $feed['title'], 'title' => $feed['title'],
'url' => $feed['url'], 'url' => $feed['url'],
'refresh' => $form_state['values']['refresh'], 'refresh' => $form_state['values']['refresh'],