]> git.mxchange.org Git - hub.git/blob - application/hub/main/source/urls/class_CrawlerUploadedListUrlSource.php
Continued:
[hub.git] / application / hub / main / source / urls / class_CrawlerUploadedListUrlSource.php
1 <?php
2 /**
3  * A UploadedList URL source class for crawlers
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2014 Crawler Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class CrawlerUploadedListUrlSource extends BaseUrlSource implements UrlSource, Registerable {
25         /**
26          * "Cached" CSV path
27          */
28         private $csvFilePath = '';
29
30         /**
31          * Last CSV file instance
32          */
33         private $lastCsvFileInstance = NULL;
34
35         /**
36          * Stack for pushing data from this clas to another
37          */
38         private $stackSourceInstance = NULL;
39
40         /**
41          * Stack name for a CSV file
42          */
43         const STACK_NAME_CSV_FILE = 'csv_file';
44
45         /**
46          * "Imported" CSV files
47          */
48         private $csvFileImported = array();
49
50         /**
51          * Protected constructor
52          *
53          * @return      void
54          */
55         protected function __construct () {
56                 // Call parent constructor
57                 parent::__construct(__CLASS__);
58
59                 // "Cache" CSV path for faster usage
60                 $this->csvFilePath = $this->getConfigInstance()->getConfigEntry('base_path') . '/' . $this->getConfigInstance()->getConfigEntry('csv_file_path');
61
62                 // Initialize directory instance
63                 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($this->csvFilePath));
64
65                 // Set it here
66                 $this->setDirectoryInstance($directoryInstance);
67
68                 // Init stack instance
69                 $this->stackSourceInstance = ObjectFactory::createObjectByConfiguredName('crawler_uploaded_list_url_source_stack_class');
70
71                 // Init stack
72                 $this->getStackSourceInstance()->initStack(self::STACK_NAME_CSV_FILE);
73         }
74
75         /**
76          * Creates an instance of this class
77          *
78          * @return      $sourceInstance         An instance of a Source class
79          */
80         public final static function createCrawlerUploadedListUrlSource () {
81                 // Get new instance
82                 $sourceInstance = new CrawlerUploadedListUrlSource();
83
84                 // Init source
85                 $sourceInstance->initSource('crawler', 'uploaded_list');
86
87                 // Return the prepared instance
88                 return $sourceInstance;
89         }
90
91         /**
92          * Checks whether a CSV file is found
93          *
94          * @return      $isFound        Whether a CSV file is found
95          */
96         private function isCsvFileFound () {
97                 // Is the instance valid?
98                 if (!$this->getDirectoryInstance()->getDirectoryIteratorInstance()->valid()) {
99                         // Then rewind it
100                         $this->getDirectoryInstance()->getDirectoryIteratorInstance()->rewind();
101                 } // END - if
102
103                 // Read next entry
104                 $directoryEntry = $this->getDirectoryInstance()->readDirectoryExcept(array_merge(array('.htaccess', '.', '..'), $this->csvFileImported));
105
106                 // The read entry has not to be empty and extension must be '.csv'
107                 if ((empty($directoryEntry)) || (substr($directoryEntry, -4, 4) != '.csv')) {
108                         // Skip further processing
109                         return FALSE;
110                 } // END - if
111
112                 // Initialize CSV file instance
113                 $this->lastCsvFileInstance = ObjectFactory::createObjectByConfiguredName('csv_file_class', array($this->csvFilePath . '/' . $directoryEntry));
114
115                 // Found an entry
116                 return TRUE;
117         }
118
119         /**
120          * Initializes the import of the CSV file which is being processed by other task
121          *
122          * @return      void
123          * @throws      NullPointerException    If lastCsvFileInstance is not set
124          */
125         private function importCsvFile () {
126                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRAWLER-SOURCE [' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
127
128                 // Is the instance set?
129                 if (is_null($this->lastCsvFileInstance)) {
130                         // This should not happen
131                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
132                 } // END - if
133
134                 // Stack this file
135                 $this->getStackSourceInstance()->pushNamed(self::STACK_NAME_CSV_FILE, $this->lastCsvFileInstance);
136
137                 // ... and mark it as "imported"
138                 array_push($this->csvFileImported, basename($this->lastCsvFileInstance->getFileName()));
139
140                 // ... and finally NULL it (to save some RAM)
141                 $this->lastCsvFileInstance = NULL;
142
143                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRAWLER-SOURCE [' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
144         }
145
146         /**
147          * Getter for stackSourceInstance variable
148          *
149          * @return      $stackSourceInstance    An instance of an additional stack
150          */
151         public final function stackSourceInstance () {
152                 return $this->stackSourceInstance;
153         }
154
155         /**
156          * Processes entries in the stack.
157          *
158          * @return      void
159          * @todo        ~10% done
160          */
161         public function processStack () {
162                 // Does the stack have some entries left?
163                 if (!$this->isUrlStackEmpty()) {
164                         // Handle next entry
165                         $this->processNextEntry();
166                 } elseif ($this->isCsvFileFound()) {
167                         // A CSV file has been found and can maybe be imported.
168                         $this->importCsvFile();
169                 }
170
171                 $this->partialStub('Please implement this method.');
172         }
173 }
174
175 // [EOF]
176 ?>