3 * A UploadedList URL source class for crawlers
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class CrawlerUploadedListUrlSource extends BaseUrlSource implements UrlSource, Registerable {
28 private $csvFilePath = '';
31 * Last CSV file instance
33 private $lastCsvFileInstance = NULL;
36 * Stack for pushing data from this clas to another
38 private $stackSourceInstance = NULL;
41 * Stack name for a CSV file
43 const STACK_NAME_CSV_FILE = 'csv_file';
46 * "Imported" CSV files
48 private $csvFileImported = array();
51 * Protected constructor
55 protected function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
59 // "Cache" CSV path for faster usage
60 $this->csvFilePath = $this->getConfigInstance()->getConfigEntry('base_path') . '/' . $this->getConfigInstance()->getConfigEntry('csv_file_path');
62 // Initialize directory instance
63 $directoryInstance = ObjectFactory::createObjectByConfiguredName('directory_class', array($this->csvFilePath));
66 $this->setDirectoryInstance($directoryInstance);
68 // Init stack instance
69 $this->stackSourceInstance = ObjectFactory::createObjectByConfiguredName('crawler_uploaded_list_url_source_stack_class');
72 $this->getStackSourceInstance()->initStack(self::STACK_NAME_CSV_FILE);
76 * Creates an instance of this class
78 * @return $sourceInstance An instance of a Source class
80 public final static function createCrawlerUploadedListUrlSource () {
82 $sourceInstance = new CrawlerUploadedListUrlSource();
85 $sourceInstance->initSource('crawler', 'uploaded_list');
87 // Return the prepared instance
88 return $sourceInstance;
92 * Checks whether a CSV file is found
94 * @return $isFound Whether a CSV file is found
96 private function isCsvFileFound () {
97 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRAWLER-SOURCE [' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
99 // Is the instance valid?
100 if (!$this->getDirectoryInstance()->getDirectoryIteratorInstance()->valid()) {
102 $this->getDirectoryInstance()->getDirectoryIteratorInstance()->rewind();
106 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRAWLER-SOURCE [' . __METHOD__ . ':' . __LINE__ . ']: this->csvFileImported=' . print_r($this->csvFileImported, TRUE));
107 $directoryEntry = $this->getDirectoryInstance()->readDirectoryExcept(array_merge(array('.htaccess', '.', '..'), $this->csvFileImported));
109 // The read entry has not to be empty and extension must be '.csv'
110 if ((empty($directoryEntry)) || (substr($directoryEntry, -4, 4) != '.csv')) {
111 // Skip further processing
115 // Initialize CSV file instance
116 $this->lastCsvFileInstance = ObjectFactory::createObjectByConfiguredName('csv_file_class', array($this->csvFilePath . '/' . $directoryEntry));
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRAWLER-SOURCE [' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
124 * Initializes the import of the CSV file which is being processed by other task
127 * @throws NullPointerException If lastCsvFileInstance is not set
129 private function importCsvFile () {
130 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRAWLER-SOURCE [' . __METHOD__ . ':' . __LINE__ . ']: CALLED!');
132 // Is the instance set?
133 if (is_null($this->lastCsvFileInstance)) {
134 // This should not happen
135 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
139 $this->getStackSourceInstance()->pushNamed(self::STACK_NAME_CSV_FILE, $this->lastCsvFileInstance);
141 // ... and mark it as "imported"
142 array_push($this->csvFileImported, basename($this->lastCsvFileInstance->getFileName()));
144 // ... and finally NULL it (to save some RAM)
145 $this->lastCsvFileInstance = NULL;
147 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRAWLER-SOURCE [' . __METHOD__ . ':' . __LINE__ . ']: EXIT!');
151 * Getter for stackSourceInstance variable
153 * @return $stackSourceInstance An instance of an additional stack
155 public final function getStackSourceInstance () {
156 return $this->stackSourceInstance;
160 * Processes entries in the stack.
165 public function processStack () {
166 // Does the stack have some entries left?
167 if (!$this->isUrlStackEmpty()) {
169 $this->processNextEntry();
170 } elseif ($this->isCsvFileFound()) {
171 // A CSV file has been found and can maybe be imported.
172 $this->importCsvFile();
175 $this->partialStub('Please implement this method.');