Project

General

Profile

« Previous | Next » 

Revision 1985

Added by Dietmar over 10 years ago

! /modules/news/Reorg.php completely recoded using class AccessFile()
! /modules/news/upgrade.php add accessfile report to output details in upgrade-script.php
! /modules/news/save_post.php search for missing accessfile folder and create missing one

View differences:

Reorg.php
1 1
<?php
2

  
2 3
/**
3 4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 5
 *
......
19 20
/**
20 21
 * Reorg.php
21 22
 *
22
 * @category     Module
23
 * @package      Module_news
24
 * @author       Werner v.d.Decken <wkl@isteam.de>
25
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
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>
26 27
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
27 28
 * @version      0.0.1
28 29
 * @revision     $Revision: $
29 30
 * @link         $HeadURL: $
30 31
 * @lastmodified $Date: $
31
 * @since        File available since 17.01.2013
32
 * @description  Reorganisation jobs for the 'news' module
32
 * @since        File available since 15.10.2013
33
 * @description  reorganisize all accessfiles of the addon 'news'
33 34
 */
34

  
35 35
class m_news_Reorg {
36 36

  
37
	protected $_oReg = null;
38
	protected $_sPagesDir = '';
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;
39 53

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

  
40 59
/**
41
 * Execute reorganisation
42
 * @return string all occured messages 
60
 * constructor
61
 * @param int $bDetailedLog  can be LOG_EXTENDED or LOG_SHORT
43 62
 */
44
	public function execute() 
63
	public function __construct($bDetailedLog = self::LOG_SHORT)
45 64
	{
46
		$sOutput = null;
47
		$this->_oReg = null;
48
		if(class_exists('WbAdaptor')) {
49
			$this->_oReg = WbAdaptor::getInstance();
50
			$sTmp = trim($this->_oReg->PagesDir, '/');
51
			$sTmp = ($sTmp == '' ? '' : $sTmp.'/');
52
			$this->_sPagesDir = $sTmp;
53
			$sOutput = $this->createAccessFiles();
54
		}
55
		// add here the requests for additional reorg methods
56
		return $sOutput;
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;
57 69
	}
58 70
/**
59
 * Creates all Accessfiles from DB
60
 * @return string all occured messages 
71
 * execute reorganisation
72
 * @return boolean
61 73
 */
62
	protected function createAccessFiles()
74
	public function execute()
63 75
	{
64
		$count = 0;
65
		$aReturnMsg = array();
66
		$sql  = 'SELECT `page_id`,`post_id`,`section_id`,`link` ';
67
		$sql .= 'FROM `'.$this->_oReg->TablePrefix.'mod_news_posts`';
68
		$sql .= 'WHERE `link` != \'\'';
69
		if(($oPosts = WbDatabase::getInstance()->query($sql))) {
70
			while($aPost = $oPosts->fetchRow(MYSQL_ASSOC))
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))
71 116
			{
72
				$sAccessFile = $this->_oReg->AppPath.$this->_sPagesDir
73
				               . trim(str_replace('\\', '/', $aPost['link']), '/')
74
				               . $this->_oReg->PageExtension;
75
				$aOptionalCommand = array(
76
					'$section_id   = '.$aPost['section_id'].';',
77
					'$post_section = '.$aPost['section_id'].';',
78
					'$post_id      = '.$aPost['post_id'].';'
79
				);
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()
80 182

  
81
				if(create_access_file($sAccessFile, $aPost['page_id'], 0, $aOptionalCommand)){
82
					$count++;	
83
				} else {
84
					$aReturnMsg[] = 'Can\'t create '.$sAccessFile;	
85
				}
86
			}
87
			$aReturnMsg[] = 'Number of new created access files: '.$count;
88
		}
89
		return $aReturnMsg;
90
	}
91
	// add here some additional reorg methods **************************************** *//
92
} // end of class Reorg
183
} // end of class m_news_Reorg
184

  

Also available in: Unified diff