1: <?php
2:
3: /*
4: * The MIT License
5: *
6: * Copyright 2014 Damien Doussaud (namide.com).
7: *
8: * Permission is hereby granted, free of charge, to any person obtaining a copy
9: * of this software and associated documentation files (the "Software"), to deal
10: * in the Software without restriction, including without limitation the rights
11: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12: * copies of the Software, and to permit persons to whom the Software is
13: * furnished to do so, subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in
16: * all copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24: * THE SOFTWARE.
25: */
26:
27: namespace Flea;
28:
29: /**
30: * Creates and saves all the pages in database
31: *
32: * @author Namide
33: */
34: class PageListCreate {
35:
36: private static $_INSTANCE = null;
37:
38: /**
39: * Add all pages of a directory
40: *
41: * @param type $dir Root directory
42: */
43: /* public function addPagesByDir( $dir )
44: {
45: $listOfPages = $this->addPageByDirRecurs( $dir, '' );
46: $this->db_insertPages($listOfPages);
47: } */
48:
49: /**
50: * Add all pages with the CSV
51: *
52: * @param type $dir Root directory
53: */
54: public function addPagesByCSV($dir) {
55: //$listOfPages = $this->addPageByDirRecurs( $dir, '' );
56:
57:
58: $csvName = $dir . 'pagelist.csv';
59: $csvFile = file($csvName);
60: $num = -1;
61: $head = [];
62: $pageList = [];
63: foreach ($csvFile as &$line) {
64:
65: if ($num < 0) {
66: $head = array_map('trim', str_getcsv($line));
67: } else {
68:
69: $page = new Page();
70:
71: $csvStr = str_getcsv($line);
72: foreach ($csvStr as $id => &$row) {
73: $row = trim($row);
74:
75: switch ($head[$id]) {
76: case 'path': $page->setName($row);
77: break;
78: case 'lang': $page->setLang($row);
79: break;
80: case 'url': $page->setPageUrl($row);
81: break;
82: case 'template': $page->setTemplate($row);
83: break;
84: case 'visible': $page->setVisible((bool) $row);
85: break;
86: case 'cachable': $page->setCachable((bool) $row);
87: break;
88: case 'type': $page->setType($row);
89: break;
90: case 'get.enable': $page->setGetEnabled((bool) $row);
91: break;
92: case 'get.explicit': $page->setGetExplicit((bool) $row);
93: break;
94: case 'header': $page->setPhpHeader($row);
95: break;
96: case 'date': $page->setDate($row);
97: break;
98:
99: case 'tags':
100:
101: if ($row !== '') {
102: $aTemp = array_map('trim', explode(';', $row));
103: $page->getTags()->addMultiple($aTemp);
104: break;
105: }
106:
107: case 'metas':
108:
109: if ($row !== '') {
110: $aTemp = explode(';', $row);
111: foreach ($aTemp as &$pair) {
112: $aTemp2 = explode(':', $pair);
113: if (count($aTemp2) > 1) {
114: $page->getMetas()->add(trim($aTemp2[1]), trim($aTemp2[0]));
115: } else if (_DEBUG) {
116: Debug::getInstance()->addError('The meta "' . $pair . '" must be a pair key:value in the CSV ' . $csvName);
117: }
118: }
119: }
120:
121: break;
122:
123: case '301':
124:
125: if ($row !== '') {
126: $aTemp = array_map('trim', explode(';', $row));
127: $page->getUrl301()->addMultiple($aTemp);
128: }
129: break;
130:
131: default:
132:
133: $aTemp = array_map('trim', explode(':', $head[$id]));
134: if ($aTemp[0] === 'meta' && count($aTemp) > 1) {
135: $page->getMetas()->add($row, $aTemp[1]);
136: } else if (_DEBUG) {
137: Debug::getInstance()->addError('label "' . $head[$id] . '" is not correct in the CSV ' . $csvName);
138: }
139: }
140: }
141:
142: $build = $dir . $page->getName() . '/' . $page->getLang() . '.php';
143: if (file_exists($build))
144: $page->setBuildFile($build);
145:
146: $pageList[] = $page;
147: }
148: $num++;
149: }
150:
151:
152: $this->db_insertPages($pageList);
153: }
154:
155: /**
156: * Add 301 redirections and other commands
157: *
158: * @param string $file File with commands
159: */
160: public function commands($file) {
161: $listOfPages = array();
162:
163: include $file;
164: if (isset($redirect301)) {
165: foreach ($redirect301 as $oldURL => $newURL) {
166: $page = new Page();
167: $page->setType(Page::$TYPE_REDIRECT301);
168: $page->setPageUrl($oldURL);
169: $page->setName('redirect301');
170: //$page->setHtmlBody( $newURL );
171: $page->setPhpHeader('Location: ' . $newURL);
172:
173: $page->setVisible(false);
174: $page->setCachable(false);
175:
176: array_push($listOfPages, $page);
177: }
178: }
179:
180: $this->db_insertPages($listOfPages);
181: }
182:
183: private function db_createPagesDB() {
184: $tableName = DataBase::objectToTableName(Page::getEmptyPage());
185:
186: $db = DataBase::getInstance(_DB_DSN_PAGE);
187:
188: $request = SqlQuery::getTemp(SqlQuery::$TYPE_CREATE);
189: $request->initCreate($tableName, Page::getEmptyPage()->getObjectVars());
190: $db->execute($request);
191:
192: $request->clean(SqlQuery::$TYPE_CREATE);
193: $request->initCreate($tableName . '_array', array('page_id' => 'TEXT', 'page_prop' => 'TEXT', 'key' => 'TEXT', 'value' => 'TEXT'));
194: $db->execute($request);
195: }
196:
197: private function db_insertPages(array $list) {
198: $tableName = DataBase::objectToTableName(Page::getEmptyPage());
199: $db = DataBase::getInstance(_DB_DSN_PAGE);
200:
201: if (!$db->exist($tableName)) {
202: $this->db_createPagesDB();
203: }
204:
205: $keys1 = array();
206: $keys2 = array();
207: $values1 = array();
208: $values2 = array();
209: $length1 = 1;
210: $length2 = 1;
211:
212: //$req1 = new SqlQuery( SqlQuery::$TYPE_INSERT );
213: //$req2 = new SqlQuery( SqlQuery::$TYPE_INSERT );
214:
215: foreach ($list as $page) {
216: $allVars = $page->getObjectVars();
217: $obj1 = array();
218: //$db->execute($request);
219:
220: foreach ($allVars as $key => $value) {
221: if (gettype($value) == 'array') {
222: foreach ($value as $key2 => $val2) {
223: $obj2 = array();
224: $obj2['page_id'] = $allVars['_id'];
225: $obj2['page_prop'] = $key;
226: $obj2['key'] = $key2;
227: $obj2['value'] = $val2;
228:
229: if (count($keys2) < 1) {
230: $keys2 = array_keys($obj2);
231: $length2 = count($keys2);
232: } else if ((count($values2) + 1) * $length2 > 999) {
233: $req = SqlQuery::getTemp(SqlQuery::$TYPE_MULTI_INSERT);
234: $req->initMultiInsertValues($tableName . '_array', $keys2, $values2);
235: $db->execute($req);
236: $values2 = array();
237: }
238:
239: $values2[] = array_values($obj2);
240:
241: /* $request->clean( SqlQuery::$TYPE_INSERT );
242: $request->initInsertValues( $tableName.'_array', $obj2 );
243: $db->execute($request); */
244: }
245: } elseif ($value !== null) {
246: $obj1[$key] = $value;
247: }
248: }
249:
250: if (count($keys1) < 1) {
251: $keys1 = array_keys($obj1);
252: $length1 = count($keys1);
253: } else if ((count($values1) + 1) * $length1 > 999) {
254: $req = SqlQuery::getTemp(SqlQuery::$TYPE_MULTI_INSERT);
255: $req->initMultiInsertValues($tableName, $keys1, $values1);
256: $db->execute($req);
257: $values1 = array();
258: }
259: $values1[] = array_values($obj1);
260: }
261:
262: if (count($values1) > 0) {
263: $req = SqlQuery::getTemp(SqlQuery::$TYPE_MULTI_INSERT);
264: $req->initMultiInsertValues($tableName, $keys1, $values1);
265: $db->execute($req);
266: }
267:
268: if (count($values2) > 0) {
269: $req->clean(SqlQuery::$TYPE_MULTI_INSERT);
270: $req->initMultiInsertValues($tableName . '_array', $keys2, $values2);
271: $db->execute($req);
272: }
273: }
274:
275: /**
276: * Add all pages of a directory recursivly
277: *
278: * @param type $dir Root directory
279: * @param type $fileDirRel Relative directory (for the recursivity)
280: * @return array List of the pages added
281: */
282: /* private function addPageByDirRecurs( $dir, $fileDirRel = '' )
283: {
284: $list = array();
285:
286: if ( !file_exists($dir) ) { return $list; }
287:
288: $dirOpen = opendir($dir);
289: while($file = @readdir($dirOpen))
290: {
291: if( $file != "." &&
292: $file != ".." &&
293: is_dir($dir.'/'.$file) )
294: {
295: $list1 = $this->addPageByDirRecurs( $dir.'/'.$file.'/', (($fileDirRel != '')?$fileDirRel.'/':'').$file );
296: $list2 = $this->createPage( (($fileDirRel != '')?$fileDirRel.'/':'').$file );
297:
298:
299: $list = array_merge($list, $list1, $list2);
300: }
301: }
302: closedir($dirOpen);
303:
304: return $list;
305: } */
306:
307: /**
308: * Add all the pages (by languages) in the folder
309: *
310: * @param string $folderName Name of the folder thats contain the page
311: * @return array List of the pages generated (differents languages)
312: */
313: /* private function createPage( $folderName )
314: {
315: $pages = array();
316:
317: $langList = LangList::getInstance();
318: $langs = $langList->getList();
319:
320: foreach ( $langs as $lang )
321: {
322: $filename = _CONTENT_DIRECTORY.$folderName.'/'.$lang.'-init.php';
323:
324: if( file_exists ( $filename ) )
325: {
326: $page = new Page();
327: $page->setLang( $lang );
328: $page->setName( $folderName );
329:
330: $this->initPage($page, $filename);
331:
332:
333: $buildFile = _CONTENT_DIRECTORY.$folderName.'/'.$lang.'-build.php';
334: if( file_exists ( $buildFile ) )
335: {
336: $page->setBuildFile($buildFile);
337: }
338:
339: array_push( $pages, $page );
340: }
341:
342: }
343:
344: return $pages;
345: } */
346:
347: /* private function initPage( Page &$page, $filename )
348: {
349: include $filename;
350:
351: if ( _DEBUG && !isset($url) )
352: {
353: Debug::getInstance()->addError( 'The initialisation of a page must to have an URL' );
354: }
355:
356: if ( isset($type) )
357: {
358: $page->setType($type);
359: if ( $type == Page::$TYPE_ERROR404 )
360: {
361: $this->_error404 = $page->getName();
362: $page->setVisible( false );
363: $page->setCachable( false );
364: $page->setPhpHeader( 'HTTP/1.0 404 Not Found' );
365: }
366: }
367:
368: if ( isset($url) ) { $page->setPageUrl($url) ; }
369: if ( isset($addUrl) ) { $page->getAdditionalUrls()->add($addUrl); }
370: if ( isset($addUrls) ) { $page->getAdditionalUrls()->addMultiple($addUrls); }
371: if ( isset($template) ) { $page->setTemplate($template) ; }
372:
373: if ( isset($visible) ) { $page->setVisible($visible) ; }
374: if ( isset($cachable) ) { $page->setCachable($cachable) ; }
375:
376: if ( isset($getEnabled) ) { $page->setGetEnabled($getEnabled) ; }
377: if ( isset($getExplicit) ) { $page->setGetExplicit($getExplicit) ; }
378: if ( isset($date) ) { $page->setDate($date) ; }
379:
380: if ( isset($htmlBody) ) { $page->setHtmlBody($htmlBody) ; }
381: if ( isset($htmlDescription) ) { $page->setHtmlDescription($htmlDescription) ; }
382: if ( isset($htmlHeader) ) { $page->setHtmlHeader($htmlHeader) ; }
383: if ( isset($htmlTitle) ) { $page->setHtmlTitle($htmlTitle) ; }
384: if ( isset($cover) ) { $page->setCover($cover) ; }
385:
386: if ( isset($phpHeader) ) { $page->setPhpHeader($phpHeader) ; }
387: if ( isset($format) ) { $page->setFormat($format) ; }
388:
389: if ( isset($tags) ) { $page->getTags()->addMultiple($tags) ; }
390: if ( isset($tag) ) { $page->getTags()->add($tag) ; }
391: if ( isset($contents) ) { $page->getContents()->addMultiple($contents); }
392:
393: return $page;
394: } */
395:
396: final private function __construct() {
397:
398: }
399:
400: final private function __clone() {
401: if (_DEBUG) {
402: Debug::getInstance()->addError('You can\'t clone a singleton');
403: }
404: }
405:
406: /**
407: * Instance of the PageList
408: *
409: * @return static Instance of the object PageListCreate
410: */
411: final public static function getInstance() {
412: if (PageListCreate::$_INSTANCE === null) {
413: PageListCreate::$_INSTANCE = new PageListCreate();
414: }
415: return PageListCreate::$_INSTANCE;
416: }
417:
418: }
419: