Project

General

Profile

1
<?php
2

    
3
/**
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19

    
20
/**
21
 * Reorg.php
22
 *
23
 * @category     Addon
24
 * @package      news_package
25
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
26
 * @author       Manuela v.d.Decken <manuela@isteam.de>
27
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
28
 * @version      0.0.1
29
 * @revision     $Revision: $
30
 * @link         $HeadURL: $
31
 * @lastmodified $Date: $
32
 * @since        File available since 15.10.2013
33
 * @description  reorganisize all accessfiles of the addon 'news'
34
 */
35

    
36
class m_news_Reorg extends ModuleReorgAbstract{
37

    
38
/** root directory for accessfiles */
39
	protected $sAccessFilesRoot = '';
40
/** sub directory for accessfiles
41
 * @description  This is needed to correct db::x_mod_news_posts::link entries of former versions<br />
42
 *               It can be removed after <b>all</b> modules accessing news are modified to access
43
 *               without a hardcoded subdirectory name.
44
 */
45
	protected $sAccessFilesSubdir = 'posts/';
46

    
47
/**
48
 * execute reorganisation
49
 * @return boolean
50
 */
51
	public function execute()
52
	{
53
/**
54
 * @description Structure of report array.<br />
55
 *              (int) number of 'FilesDeleted'<br />
56
 *              (int) number of 'FilesCreated'<br />
57
 *              (array) 'Success'<br />
58
 *              (array) 'Failed'
59
 */
60
	// reset report
61
		$this->aReport = array( 'FilesDeleted'=>0,
62
		                        'FilesCreated'=>0,
63
		                        'Success'=>array(),
64
		                        'Failed'=>array()
65
		                      );
66
	// build AccessFilesRoot
67
		$this->sAccessFilesRoot = $this->oReg->AppPath.$this->oReg->PagesDir.$this->sAccessFilesSubdir;
68
	// delete old accessfiles
69
		$this->deleteAll();
70
	// recreate new accessfiles
71
		$this->rebuildAll();
72
	// return true if all is successful done
73
		return (sizeof($this->aReport['Failed']) == 0);
74
	}
75
/**
76
 * deleteAll
77
 * @throws AccessFileException
78
 * @description delete all accessfiles and its children in $sAccessFilesRoot
79
 */
80
	protected function deleteAll()
81
	{
82
	// scan start directory for access files
83
		$aMatches = glob($this->sAccessFilesRoot . '*'.$this->oReg->PageExtension);
84
		if(is_array($aMatches))
85
		{
86
			foreach($aMatches as $sItem)
87
			{
88
			// sanitize itempath
89
				$sItem = str_replace('\\', '/', $sItem);
90
				if(AccessFileHelper::isAccessFile($sItem))
91
				{
92
				// delete accessfiles only
93
					if(is_writable($sItem) && @unlink($sItem))
94
					{
95
					// if file is successful deleted
96
						if($this->bDetailedLog)
97
						{
98
							$this->aReport['Success'][] = 'File successful removed : '.str_replace($this->oReg->AppPath, '', $sItem);
99
						}
100
					// increment successful counter
101
						$this->aReport['FilesDeleted']++;
102
					}else
103
					{
104
					// if failed
105
						$this->aReport['Failed'][] = 'Delete file failed : '.str_replace($this->oReg->AppPath, '', $sItem);
106
					}
107
				} // endif
108
			} // endforeach
109
		}else
110
		{
111
			$this->aReport['Failed'][] = 'Directory scan failed : '.str_replace($this->oReg->AppPath, '', $this->sAccessFilesRoot);
112
		}
113
	} // end of function deleteAll()
114
/**
115
 * rebuildAll
116
 * @return integer  number of successful deleted files
117
 * @throws AccessFileException
118
 * @description rebuild all accessfiles from database
119
 */
120
	protected function rebuildAll()
121
	{
122
		$sql = 'SELECT `page_id`, `post_id`, `section_id`, `link`, `title` '
123
		     . 'FROM `'.$this->oDb->TablePrefix.'mod_news_posts` '
124
		     . 'WHERE `link`!=\'\'';
125
		if(($oPosts = $this->oDb->query($sql)))
126
		{
127
			while(($aPost = $oPosts->fetchRow(MYSQL_ASSOC)))
128
			{
129
			// sanitize link if there is an old value in database from former versions
130
				$aPost['link'] = preg_replace( '/^'.preg_quote($this->sAccessFilesSubdir, '/').'/',
131
				                               '',
132
				                               trim( str_replace('\\', '/', $aPost['link']), '/')
133
				                             );
134

    
135
			// compose name of accessfile
136
				$sAccFileName = $this->sAccessFilesRoot.$aPost['link'].$this->oReg->PageExtension;
137

    
138
				try
139
				{
140
				// create new object
141
					$oAccFile = new AccessFile($this->sAccessFilesRoot, $aPost['link'], $aPost['page_id']);
142
					$oAccFile->addVar('section_id',   $aPost['section_id'], AccessFile::VAR_INT);
143
					$oAccFile->addVar('post_id',      $aPost['post_id'],    AccessFile::VAR_INT);
144
					$oAccFile->addVar('post_section', $aPost['section_id'], AccessFile::VAR_INT);
145
					$oAccFile->write();
146
				// destroy object if its file is written
147
					unset($oAccFile);
148
					if($this->bDetailedLog)
149
					{
150
						$this->aReport['Success'][] = 'File successful created : '.str_replace($this->oReg->AppPath, '', $sAccFileName);
151
					}
152
				// increment successful counter
153
					$this->aReport['FilesCreated']++;
154
				}catch(AccessFileException $e)
155
				{
156
				// if failed
157
					$this->aReport['Failed'][] = ($this->bDetailedLog ? $e : $e->getMessage());
158
				}
159
			} // endwhile
160
		} // endif
161
	} // end of function rebuildAll()
162

    
163
} // end of class m_news_Reorg
164

    
(1-1/34)