Project

General

Profile

1 1866 Luisehahne
<?php
2 1985 Luisehahne
3 1866 Luisehahne
/**
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 1985 Luisehahne
 * @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 1866 Luisehahne
 * @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 1985 Luisehahne
 * @since        File available since 15.10.2013
33
 * @description  reorganisize all accessfiles of the addon 'news'
34 1866 Luisehahne
 */
35
36 1990 darkviper
class m_news_Reorg extends ModuleReorgAbstract{
37
38 1985 Luisehahne
/** 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 1880 Luisehahne
47 1866 Luisehahne
/**
48 1985 Luisehahne
 * execute reorganisation
49
 * @return boolean
50 1866 Luisehahne
 */
51 1985 Luisehahne
	public function execute()
52 1866 Luisehahne
	{
53 1990 darkviper
/**
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 1985 Luisehahne
		$this->aReport = array( 'FilesDeleted'=>0,
62
		                        'FilesCreated'=>0,
63
		                        'Success'=>array(),
64
		                        'Failed'=>array()
65
		                      );
66 1990 darkviper
	// build AccessFilesRoot
67
		$this->sAccessFilesRoot = $this->oReg->AppPath.$this->oReg->PagesDir.$this->sAccessFilesSubdir;
68 1985 Luisehahne
	// 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 1990 darkviper
		$aMatches = glob($this->sAccessFilesRoot . '*'.$this->oReg->PageExtension);
84
		if(is_array($aMatches))
85 1985 Luisehahne
		{
86 1991 darkviper
			foreach($aMatches as $sItem)
87 1866 Luisehahne
			{
88 1990 darkviper
			// sanitize itempath
89
				$sItem = str_replace('\\', '/', $sItem);
90
				if(AccessFileHelper::isAccessFile($sItem))
91 1985 Luisehahne
				{
92 1990 darkviper
				// delete accessfiles only
93
					if(is_writable($sItem) && @unlink($sItem))
94 1985 Luisehahne
					{
95 1990 darkviper
					// 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 1985 Luisehahne
					}
107 1990 darkviper
				} // endif
108
			} // endforeach
109
		}else
110
		{
111
			$this->aReport['Failed'][] = 'Directory scan failed : '.str_replace($this->oReg->AppPath, '', $this->sAccessFilesRoot);
112
		}
113 1985 Luisehahne
	} // 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 2045 Luisehahne
135 1985 Luisehahne
			// compose name of accessfile
136
				$sAccFileName = $this->sAccessFilesRoot.$aPost['link'].$this->oReg->PageExtension;
137 2045 Luisehahne
138 1985 Luisehahne
				try
139
				{
140
				// create new object
141 2045 Luisehahne
					$oAccFile = new AccessFile($this->sAccessFilesRoot, $aPost['link'], $aPost['page_id']);
142 1985 Luisehahne
					$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 1880 Luisehahne
163 1985 Luisehahne
} // end of class m_news_Reorg