commit 6288df98ff7956415701fb50e87016f57a05e868
parent fe248aa563c5784b5b757c3b5403f26def323a1b
Author: Felicitus <felicitus@felicitus.org>
Date: Sat, 1 Aug 2015 13:19:12 +0200
Moved tips of the day to their own bundle
Diffstat:
10 files changed, 211 insertions(+), 170 deletions(-)
diff --git a/app/AppKernel.php b/app/AppKernel.php
@@ -91,6 +91,7 @@ class AppKernel extends Kernel
$bundles[] = new PartKeepr\ManufacturerBundle\PartKeeprManufacturerBundle();
$bundles[] = new PartKeepr\ImageBundle\PartKeeprImageBundle();
$bundles[] = new PartKeepr\StorageLocationBundle\PartKeeprStorageLocationBundle();
+ $bundles[] = new PartKeepr\TipOfTheDayBundle\PartKeeprTipOfTheDayBundle();
return $bundles;
}
diff --git a/app/config/config.yml b/app/config/config.yml
@@ -707,3 +707,16 @@ services:
arguments: [ [ "@resource.tempfile.collection_operation.custom_post" ] ]
- method: "initItemOperations"
arguments: [ [ "@resource.tempfile.item_operation.get", "@resource.tempfile.item_operation.custom_get", "@resource.tempfile.item_operation.custom_get_mimetype", "@resource.tempfile.collection_operation.custom_post_webcam" ] ]
+
+ resource.tip_of_the_day:
+ parent: "api.resource"
+ arguments: [ "PartKeepr\\TipOfTheDayBundle\\Entity\\TipOfTheDay" ]
+ tags: [ { name: "api.resource" } ]
+ calls:
+ - method: "initFilters"
+ arguments: [ [ "@doctrine_reflection_service.search_filter" ] ]
+ - method: "initNormalizationContext"
+ arguments: [ { groups: [ "default" ] } ]
+ - method: "initDenormalizationContext"
+ arguments:
+ - { groups: [ "default" ] }+
\ No newline at end of file
diff --git a/cronjobs/UpdateTipsOfTheDay.php b/cronjobs/UpdateTipsOfTheDay.php
@@ -13,7 +13,7 @@ namespace PartKeepr\Cronjobs;
include(__DIR__."/../src/backend/PartKeepr/PartKeepr.php");
use PartKeepr\PartKeepr,
- PartKeepr\TipOfTheDay\TipOfTheDay,
+ PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay,
PartKeepr\CronLogger\CronLoggerManager;
PartKeepr::initialize();
diff --git a/src/PartKeepr/TipOfTheDayBundle/Entity/TipOfTheDay.php b/src/PartKeepr/TipOfTheDayBundle/Entity/TipOfTheDay.php
@@ -0,0 +1,126 @@
+<?php
+namespace PartKeepr\TipOfTheDayBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use PartKeepr\PartKeepr;
+use PartKeepr\Util\BaseEntity;
+use PartKeepr\Util\Configuration;
+use PartKeepr\Util\Serializable;
+
+/**
+ * Represents a tip of the day.
+ *
+ * Tips are stored on the central PartKeepr server in a wiki. However, we need to know a list of all tip pages
+ * because the API has a limit per day. So basically, we sync the tip names from the wiki to the local system several
+ * times a day and not each time an user logs in.
+ *
+ * Note: If you wish to link against a tip of the day, do it by name and not by id!
+ *
+ * @ORM\Entity
+ **/
+class TipOfTheDay extends BaseEntity implements Serializable
+{
+ /**
+ * @ORM\Column(type="string")
+ * @var string
+ */
+ private $name;
+
+ /**
+ * Sets the name for this tip
+ *
+ * @param string $name The name
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * Returns the name for this tip
+ *
+ * @return string The name
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * Syncronizes the tip database against the master wiki.
+ *
+ * @throws \Exception
+ */
+ public static function syncTips()
+ {
+ if (ini_get("allow_url_fopen") == 0) {
+ throw new \Exception("allow_url_fopen is disabled, but required to query the TipOfTheDay database.");
+ }
+
+ $url = Configuration::getOption("partkeepr.tipoftheday.api",
+ "http://partkeepr.org/wiki/api.php?action=query&list=categorymembers&cmtitle=Category:TipOfTheDay&format=json");
+
+ $tipsString = file_get_contents($url);
+
+
+ $aPageNames = self::extractPageNames($tipsString);
+
+ self::updateTipDatabase($aPageNames);
+ }
+
+ /**
+ * Updates the tip database. Expects an array of page names.
+ *
+ * This method clears all page names and re-creates them. This saves
+ * alot of engineering, because we don't need to match contents
+ * within the database against contents in an array.
+ *
+ * @param array $aPageNames The page names as array. Page names are stored as string.
+ */
+ private static function updateTipDatabase(array $aPageNames)
+ {
+ $dql = "DELETE FROM PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay";
+ $query = PartKeepr::getEM()->createQuery($dql);
+
+ $query->execute();
+
+ foreach ($aPageNames as $pageName) {
+ $tip = new TipOfTheDay();
+ $tip->setName($pageName);
+ PartKeepr::getEM()->persist($tip);
+ }
+
+ PartKeepr::getEM()->flush();
+ }
+
+ /**
+ * Extracts the page names from the mediawiki JSON returned.
+ *
+ * @param string $response The encoded json string
+ *
+ * @return array An array with the titles of each page
+ */
+ private static function extractPageNames($response)
+ {
+ $aTipsStructure = json_decode($response, true);
+ $aTips = $aTipsStructure["query"]["categorymembers"];
+
+ $aPageNames = array();
+
+ foreach ($aTips as $tip) {
+ $aPageNames[] = $tip["title"];
+ }
+
+ return $aPageNames;
+ }
+
+ /**
+ * (non-PHPdoc)
+ *
+ * @see PartKeepr\Util.Serializable::serialize()
+ */
+ public function serialize()
+ {
+ return array("name" => $this->getName());
+ }
+}+
\ No newline at end of file
diff --git a/src/PartKeepr/TipOfTheDayBundle/Entity/TipOfTheDayHistory.php b/src/PartKeepr/TipOfTheDayBundle/Entity/TipOfTheDayHistory.php
@@ -0,0 +1,51 @@
+<?php
+namespace PartKeepr\TipOfTheDayBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use PartKeepr\AuthBundle\Entity\User\User;
+use PartKeepr\Util\BaseEntity;
+
+/**
+ * Represents a tip of the day history entry.
+ *
+ * This entity stores each tip of the day the user has already seen.
+ *
+ * @ORM\Entity
+ **/
+class TipOfTheDayHistory extends BaseEntity
+{
+ /**
+ * @ORM\Column(type="string")
+ * @var string
+ */
+ private $name;
+
+ /**
+ * Defines the user
+ * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User")
+ *
+ * @var User
+ */
+ private $user;
+
+ /**
+ * Sets the user for this entry
+ *
+ * @param User $user
+ */
+ public function setUser(User $user)
+ {
+ $this->user = $user;
+ }
+
+ /**
+ * Sets the tip of the day name the user already has seen
+ *
+ * @param string $name The tip name
+ */
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+}+
\ No newline at end of file
diff --git a/src/PartKeepr/TipOfTheDayBundle/PartKeeprTipOfTheDayBundle.php b/src/PartKeepr/TipOfTheDayBundle/PartKeeprTipOfTheDayBundle.php
@@ -0,0 +1,8 @@
+<?php
+namespace PartKeepr\TipOfTheDayBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class PartKeeprTipOfTheDayBundle extends Bundle
+{
+}
diff --git a/src/backend/PartKeepr/PartKeepr.php b/src/backend/PartKeepr/PartKeepr.php
@@ -197,8 +197,8 @@ class PartKeepr {
'PartKeepr\Unit\Unit',
'PartKeepr\PartParameter\PartParameter',
- 'PartKeepr\TipOfTheDay\TipOfTheDay',
- 'PartKeepr\TipOfTheDay\TipOfTheDayHistory',
+ 'PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay',
+ 'PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory',
'PartKeepr\UserPreference\UserPreference',
'PartKeepr\SystemNotice\SystemNotice',
'PartKeepr\CronLogger\CronLogger'
diff --git a/src/backend/PartKeepr/TipOfTheDay/TipOfTheDay.php b/src/backend/PartKeepr/TipOfTheDay/TipOfTheDay.php
@@ -1,115 +0,0 @@
-<?php
-namespace PartKeepr\TipOfTheDay;
-
-use PartKeepr\Util\Serializable;
-
-use PartKeepr\PartKeepr;
-
-use PartKeepr\Util\Configuration;
-
-use PartKeepr\Util\BaseEntity,
- Doctrine\ORM\Mapping as ORM;
-
-/**
- * Represents a tip of the day.
- *
- * Tips are stored on the central PartKeepr server in a wiki. However, we need to know a list of all tip pages
- * because the API has a limit per day. So basically, we sync the tip names from the wiki to the local system several
- * times a day and not each time an user logs in.
- *
- * Note: If you wish to link against a tip of the day, do it by name and not by id!
- *
- * @ORM\Entity
- **/
-class TipOfTheDay extends BaseEntity implements Serializable {
- /**
- * @ORM\Column(type="string")
- * @var string
- */
- private $name;
-
- /**
- * Sets the name for this tip
- * @param string $name The name
- */
- public function setName ($name) {
- $this->name = $name;
- }
-
- /**
- * Returns the name for this tip
- * @return string The name
- */
- public function getName () {
- return $this->name;
- }
-
- /**
- * Syncronizes the tip database against the master wiki.
- * @throws \Exception
- */
- public static function syncTips () {
- if (ini_get("allow_url_fopen") == 0) {
- throw new \Exception("allow_url_fopen is disabled, but required to query the TipOfTheDay database.");
- }
-
- $url = Configuration::getOption("partkeepr.tipoftheday.api", "http://partkeepr.org/wiki/api.php?action=query&list=categorymembers&cmtitle=Category:TipOfTheDay&format=json");
-
- $tipsString = file_get_contents($url);
-
-
- $aPageNames = self::extractPageNames($tipsString);
-
- self::updateTipDatabase($aPageNames);
- }
-
- /**
- * Updates the tip database. Expects an array of page names.
- *
- * This method clears all page names and re-creates them. This saves
- * alot of engineering, because we don't need to match contents
- * within the database against contents in an array.
- *
- * @param array $aPageNames The page names as array. Page names are stored as string.
- */
- private static function updateTipDatabase (array $aPageNames) {
- $dql = "DELETE FROM PartKeepr\TipOfTheDay\TipOfTheDay";
- $query = PartKeepr::getEM()->createQuery($dql);
-
- $query->execute();
-
- foreach ($aPageNames as $pageName) {
- $tip = new TipOfTheDay();
- $tip->setName($pageName);
- PartKeepr::getEM()->persist($tip);
- }
-
- PartKeepr::getEM()->flush();
- }
-
- /**
- * Extracts the page names from the mediawiki JSON returned.
- * @param string $response The encoded json string
- * @return array An array with the titles of each page
- */
- private static function extractPageNames ($response) {
- $aTipsStructure = json_decode($response, true);
- $aTips = $aTipsStructure["query"]["categorymembers"];
-
- $aPageNames = array();
-
- foreach ($aTips as $tip) {
- $aPageNames[] = $tip["title"];
- }
-
- return $aPageNames;
- }
-
- /**
- * (non-PHPdoc)
- * @see PartKeepr\Util.Serializable::serialize()
- */
- public function serialize () {
- return array( "name" => $this->getName() );
- }
-}-
\ No newline at end of file
diff --git a/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayHistory.php b/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayHistory.php
@@ -1,45 +0,0 @@
-<?php
-namespace PartKeepr\TipOfTheDay;
-
-use Doctrine\ORM\Mapping as ORM;
-use PartKeepr\AuthBundle\Entity\User\User;
-use PartKeepr\Util\BaseEntity;
-
-/**
- * Represents a tip of the day history entry.
- *
- * This entity stores each tip of the day the user has already seen.
- *
- * @ORM\Entity
- **/
-class TipOfTheDayHistory extends BaseEntity {
- /**
- * @ORM\Column(type="string")
- * @var string
- */
- private $name;
-
- /**
- * Defines the user
- * @ORM\ManyToOne(targetEntity="PartKeepr\AuthBundle\Entity\User\User")
- * @var StorageLocation
- */
- private $user;
-
- /**
- * Sets the user for this entry
- * @param User $user
- */
- public function setUser (User $user) {
- $this->user = $user;
- }
-
- /**
- * Sets the tip of the day name the user already has seen
- * @param string $name The tip name
- */
- public function setName ($name) {
- $this->name = $name;
- }
-
-}-
\ No newline at end of file
diff --git a/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayService.php b/src/backend/PartKeepr/TipOfTheDay/TipOfTheDayService.php
@@ -5,6 +5,7 @@ use PartKeepr\PartKeepr;
use PartKeepr\Service\RestfulService;
use PartKeepr\Service\Service;
use PartKeepr\Session\SessionManager;
+use PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory;
use PartKeepr\Util\Configuration;
class TipOfTheDayService extends Service implements RestfulService {
@@ -18,8 +19,8 @@ class TipOfTheDayService extends Service implements RestfulService {
$url = Configuration::getOption("partkeepr.tipoftheday.wiki", "http://partkeepr.org/wiki/index.php/");
/* Extract all tips which aren't read */
- $dql = "SELECT d FROM PartKeepr\TipOfTheDay\TipOfTheDay d WHERE d.name NOT IN ";
- $dql .= "(SELECT dh.name FROM PartKeepr\TipOfTheDay\TipOfTheDayHistory dh WHERE dh.user = :user)";
+ $dql = "SELECT d FROM PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay d WHERE d.name NOT IN ";
+ $dql .= "(SELECT dh.name FROM PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory dh WHERE dh.user = :user)";
$query = PartKeepr::getEM()->createQuery($dql);
$query->setParameter("user", SessionManager::getCurrentSession()->getUser());
@@ -32,8 +33,8 @@ class TipOfTheDayService extends Service implements RestfulService {
}
/* Extract all tips which are read */
- $dql = "SELECT d FROM PartKeepr\TipOfTheDay\TipOfTheDay d WHERE d.name IN ";
- $dql .= "(SELECT dh.name FROM PartKeepr\TipOfTheDay\TipOfTheDayHistory dh WHERE dh.user = :user)";
+ $dql = "SELECT d FROM PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDay d WHERE d.name IN ";
+ $dql .= "(SELECT dh.name FROM PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory dh WHERE dh.user = :user)";
$query = PartKeepr::getEM()->createQuery($dql);
$query->setParameter("user", SessionManager::getCurrentSession()->getUser());
@@ -77,7 +78,7 @@ class TipOfTheDayService extends Service implements RestfulService {
* Marks all tips as unread for the current user
*/
public function markAllTipsAsUnread () {
- $dql = "DELETE FROM PartKeepr\TipOfTheDay\TipOfTheDayHistory th WHERE th.user = :user";
+ $dql = "DELETE FROM PartKeepr\TipOfTheDayBundle\Entity\TipOfTheDayHistory th WHERE th.user = :user";
$query = PartKeepr::getEM()->createQuery($dql);
$query->setParameter("user", $this->getUser());
$query->execute();