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
class m_news_Reorg {
36

    
37
/** registry object */
38
	protected $oReg = null;
39
/** database object */
40
	protected $oDb  = null;
41
/** root directory for accessfiles */
42
	protected $sAccessFilesRoot = '';
43
/** sub directory for accessfiles
44
 * @description  This is needed to correct db::x_mod_news_posts::link entries of former versions<br />
45
 *               It can be removed after <b>all</b> modules accessing news are modified to access
46
 *               without a hardcoded subdirectory name.
47
 */
48
	protected $sAccessFilesSubdir = 'posts/';
49
/** collector of return values */
50
	protected $aReport = null;
51
/** set kind of return values */
52
	protected $bDetailedLog = false;
53

    
54
/** show extended log entries */
55
	const LOG_EXTENDED = true;
56
/** show minimal log entries */
57
	const LOG_SHORT    = false;
58

    
59
/**
60
 * constructor
61
 * @param int $bDetailedLog  can be LOG_EXTENDED or LOG_SHORT
62
 */
63
	public function __construct($bDetailedLog = self::LOG_SHORT)
64
	{
65
		$this->bDetailedLog     = $bDetailedLog;
66
		$this->oDb              = WbDatabase::getInstance();
67
		$this->oReg             = WbAdaptor::getInstance();
68
		$this->sAccessFilesRoot = $this->oReg->AppPath.$this->oReg->PagesDir.$this->sAccessFilesSubdir;
69
	}
70
/**
71
 * execute reorganisation
72
 * @return boolean
73
 */
74
	public function execute()
75
	{
76
	// reset areport
77
		$this->aReport = array( 'FilesDeleted'=>0,
78
		                        'FilesCreated'=>0,
79
		                        'Success'=>array(),
80
		                        'Failed'=>array()
81
		                      );
82
	// delete old accessfiles
83
		$this->deleteAll();
84
	// recreate new accessfiles
85
		$this->rebuildAll();
86
	// return true if all is successful done
87
		return (sizeof($this->aReport['Failed']) == 0);
88
	}
89
/**
90
 * getReport
91
 * @return array
92
 * @description a report about the whoole reorganisation<br />
93
 *              returns an array including<br />
94
 *              (int) number of 'FilesDeleted'<br />
95
 *              (int) number of 'FilesCreated'<br />
96
 *              (array) 'Success'<br />
97
 *              (array) 'Failed'
98
 */
99
	public function getReport()
100
	{
101
		return $this->aReport;
102
	}
103
/**
104
 * deleteAll
105
 * @throws AccessFileException
106
 * @description delete all accessfiles and its children in $sAccessFilesRoot
107
 */
108
	protected function deleteAll()
109
	{
110
	// scan start directory for access files
111
		foreach (glob($this->sAccessFilesRoot . '*'.$this->oReg->PageExtension, GLOB_MARK) as $sItem)
112
		{
113
		// sanitize itempath
114
            $sItem = str_replace('\\', '/', $sItem);
115
			if(AccessFileHelper::isAccessFile($sItem))
116
			{
117
			// delete accessfiles only
118
				if(is_writable($sItem) && @unlink($sItem))
119
				{
120
				// if file is successful deleted
121
					if($this->bDetailedLog)
122
					{
123
						$this->aReport['Success'][] = 'File successful removed : '.str_replace($this->oReg->AppPath, '', $sItem);
124
					}
125
				// increment successful counter
126
					$this->aReport['FilesDeleted']++;
127
				}else
128
				{
129
				// if failed
130
					$this->aReport['Failed'][] = 'Delete file failed : '.str_replace($this->oReg->AppPath, '', $sItem);
131
				}
132
			} // endif
133
		} // endforeach
134
	} // end of function deleteAll()
135
/**
136
 * rebuildAll
137
 * @return integer  number of successful deleted files
138
 * @throws AccessFileException
139
 * @description rebuild all accessfiles from database
140
 */
141
	protected function rebuildAll()
142
	{
143
		$sql = 'SELECT `page_id`, `post_id`, `section_id`, `link`, `title` '
144
		     . 'FROM `'.$this->oDb->TablePrefix.'mod_news_posts` '
145
		     . 'WHERE `link`!=\'\'';
146
		if(($oPosts = $this->oDb->query($sql)))
147
		{
148
			while(($aPost = $oPosts->fetchRow(MYSQL_ASSOC)))
149
			{
150
			// sanitize link if there is an old value in database from former versions
151
				$aPost['link'] = trim( str_replace('\\', '/', $aPost['link']), '/');
152
				$aPost['link'] = preg_replace( '/^'.preg_quote($this->sAccessFilesSubdir, '/').'/',
153
				                               '',
154
				                               trim( str_replace('\\', '/', $aPost['link']), '/')
155
				                             );
156
			// compose name of accessfile
157
				$sAccFileName = $this->sAccessFilesRoot.$aPost['link'].$this->oReg->PageExtension;
158
				try
159
				{
160
				// create new object
161
					$oAccFile = new AccessFile($sAccFileName, $aPost['page_id']);
162
					$oAccFile->addVar('section_id',   $aPost['section_id'], AccessFile::VAR_INT);
163
					$oAccFile->addVar('post_id',      $aPost['post_id'],    AccessFile::VAR_INT);
164
					$oAccFile->addVar('post_section', $aPost['section_id'], AccessFile::VAR_INT);
165
					$oAccFile->write();
166
				// destroy object if its file is written
167
					unset($oAccFile);
168
					if($this->bDetailedLog)
169
					{
170
						$this->aReport['Success'][] = 'File successful created : '.str_replace($this->oReg->AppPath, '', $sAccFileName);
171
					}
172
				// increment successful counter
173
					$this->aReport['FilesCreated']++;
174
				}catch(AccessFileException $e)
175
				{
176
				// if failed
177
					$this->aReport['Failed'][] = ($this->bDetailedLog ? $e : $e->getMessage());
178
				}
179
			} // endwhile
180
		} // endif
181
	} // end of function rebuildAll()
182

    
183
} // end of class m_news_Reorg
184

    
(1-1/33)