| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * ownCloud - News | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * later. See the COPYING file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Alessandro Cosentino <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author Bernhard Posselt <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright Alessandro Cosentino 2012 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright Bernhard Posselt 2012, 2014 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace OCA\News\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use OCP\IConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use OCP\AppFramework\Db\DoesNotExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use OCP\AppFramework\Utility\ITimeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use OCA\News\Db\ItemMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use OCA\News\Db\StatusFlag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use OCA\News\Db\FeedType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use OCA\News\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class ItemService extends Service { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $statusFlag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $timeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $itemMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $systemConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 22 |  |     public function __construct(ItemMapper $itemMapper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                                 StatusFlag $statusFlag, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                                 ITimeFactory $timeFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                                 Config $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                                 IConfig $systemConfig){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 22 |  |         parent::__construct($itemMapper); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 22 |  |         $this->statusFlag = $statusFlag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 22 |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 22 |  |         $this->timeFactory = $timeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 22 |  |         $this->itemMapper = $itemMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 22 |  |         $this->systemConfig = $systemConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Returns all new items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param int $id the id of the feed, 0 for starred or all items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param int $type the type of the feed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param int $updatedSince a timestamp with the last modification date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * returns only items with a >= modified timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param boolean $showAll if unread items should also be returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return array of items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |     public function findAllNew($id, $type, $updatedSince, $showAll, $userId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |         $status = $this->statusFlag->typeToStatus($type, $showAll); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         switch($type){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |             case FeedType::FEED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |                 return $this->itemMapper->findAllNewFeed( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |                     $id, $updatedSince, $status, $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |             case FeedType::FOLDER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |                 return $this->itemMapper->findAllNewFolder( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |                     $id, $updatedSince, $status, $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |                 return $this->itemMapper->findAllNew( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |                     $updatedSince, $status, $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Returns all items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param int $id the id of the feed, 0 for starred or all items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param int $type the type of the feed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param int $limit how many items should be returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param int $offset the offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param boolean $showAll if unread items should also be returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @param boolean $oldestFirst if it should be ordered by oldest first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string[] $search an array of keywords that the result should | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * contain in either the author, title, link or body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return array of items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 4 |  |     public function findAll($id, $type, $limit, $offset, $showAll, $oldestFirst, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                             $userId, $search=[]){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  |         $status = $this->statusFlag->typeToStatus($type, $showAll); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         switch($type){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |             case FeedType::FEED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |                 return $this->itemMapper->findAllFeed( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |                     $id, $limit, $offset, $status, $oldestFirst, $userId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     $search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |             case FeedType::FOLDER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |                 return $this->itemMapper->findAllFolder( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |                     $id, $limit, $offset, $status, $oldestFirst, $userId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     $search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |                 return $this->itemMapper->findAll( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |                     $limit, $offset, $status, $oldestFirst, $userId, $search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Star or unstar an item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param int $feedId the id of the item's feed that should be starred | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param string $guidHash the guidHash of the item that should be starred | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param boolean $isStarred if true the item will be marked as starred, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * if false unstar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param string $userId the name of the user for security reasons | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @throws ServiceNotFoundException if the item does not exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 3 |  |     public function star($feedId, $guidHash, $isStarred, $userId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 3 |  |             $item = $this->itemMapper->findByGuidHash( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  |                 $guidHash, $feedId, $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 3 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 2 |  |             $item->setLastModified($this->timeFactory->getTime()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 2 |  |             if($isStarred){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |                 $item->setStarred(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |                 $item->setUnstarred(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 2 |  |             $this->itemMapper->update($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 3 |  |         } catch(DoesNotExistException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |             throw new ServiceNotFoundException($ex->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * Read or unread an item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param int $itemId the id of the item that should be read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param boolean $isRead if true the item will be marked as read, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * if false unread | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param string $userId the name of the user for security reasons | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @throws ServiceNotFoundException if the item does not exist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 2 |  |     public function read($itemId, $isRead, $userId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 2 |  |         $item = $this->find($itemId, $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 2 |  |         $item->setLastModified($this->timeFactory->getTime()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 2 |  |         if($isRead){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |             $item->setRead(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |             $item->setUnread(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 2 |  |         $this->itemMapper->update($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * Set all items read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param int $highestItemId all items below that are marked read. This is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * used to prevent marking items as read that the users hasn't seen yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  |     public function readAll($highestItemId, $userId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 1 |  |         $time = $this->timeFactory->getTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 1 |  |         $this->itemMapper->readAll($highestItemId, $time, $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * Set a folder read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @param int $folderId the id of the folder that should be marked read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @param int $highestItemId all items below that are marked read. This is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * used to prevent marking items as read that the users hasn't seen yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 1 |  |     public function readFolder($folderId, $highestItemId, $userId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 1 |  |         $time = $this->timeFactory->getTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 1 |  |         $this->itemMapper->readFolder( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 1 |  |             $folderId, $highestItemId, $time, $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * Set a feed read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @param int $feedId the id of the feed that should be marked read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @param int $highestItemId all items below that are marked read. This is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * used to prevent marking items as read that the users hasn't seen yet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 1 |  |     public function readFeed($feedId, $highestItemId, $userId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 1 |  |         $time = $this->timeFactory->getTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 1 |  |         $this->itemMapper->readFeed($feedId, $highestItemId, $time, $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      * This method deletes all unread feeds that are not starred and over the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * count of $this->autoPurgeCount starting by the oldest. This is to clean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      * up the database so that old entries don't spam your db. As criteria for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * old, the id is taken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 2 |  |     public function autoPurgeOld(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 2 |  |         $count = $this->config->getAutoPurgeCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 | 2 |  |         if ($count >= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 1 |  |             $this->itemMapper->deleteReadOlderThanThreshold($count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |      * Returns the newest item id, use this for marking feeds read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |      * @throws ServiceNotFoundException if there is no newest item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 | 2 |  |     public function getNewestItemId($userId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 | 2 |  |             return $this->itemMapper->getNewestItemId($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 | 1 |  |         } catch(DoesNotExistException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 | 1 |  |             throw new ServiceNotFoundException($ex->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |      * Returns the starred count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      * @return int the count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 | 1 |  |     public function starredCount($userId){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 | 1 |  |         return $this->itemMapper->starredCount($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |      * @param string $userId from which user the items should be taken | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      * @return array of items which are starred or unread | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 1 |  |     public function getUnreadOrStarred($userId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 | 1 |  |         return $this->itemMapper->findAllUnreadOrStarred($userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |      * Deletes all items of a user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |      * @param string $userId the name of the user | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 253 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 254 | 1 |  |     public function deleteUser($userId) { | 
            
                                                                        
                            
            
                                    
            
            
                | 255 | 1 |  |         $this->itemMapper->deleteUser($userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 256 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |      * Regenerates the search index for all items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |     public function generateSearchIndices($progressbar=null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         if ($progressbar) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |             $this->systemConfig->setSystemValue('maintenance', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |             $progressbar = $progressbar(count($rows)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |             $progressbar->setFormat('verbose'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |         $rows = $this->itemMapper->findAllItemIdsAndUsers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |         foreach ($rows as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |             $item = $this->find($row['id'], $row['user_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |             $item->generateSearchIndex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |             $this->itemMapper->update($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |             if ($progressbar) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |                 $progressbar->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         if ($progressbar) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |             $progressbar->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |             $this->systemConfig->setSystemValue('maintenance', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 288 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 289 |  |  |  | 
            
                        
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.