1
|
<?php
|
2
|
/**
|
3
|
*
|
4
|
* @category WebsiteBaker
|
5
|
* @package modules
|
6
|
* @subpackage news
|
7
|
* @copyright 2004-2009, Ryan Djurovich
|
8
|
* @copyright 2009-2011, Website Baker Org. e.V.
|
9
|
* @link http://www.websitebaker2.org/
|
10
|
* @license http://www.gnu.org/licenses/gpl.html
|
11
|
* @platform WebsiteBaker 2.8.x
|
12
|
* @requirements PHP 5.2.2 and higher
|
13
|
* @version $Id: upgrade.php 1457 2011-06-25 17:18:50Z Luisehahne $
|
14
|
* @filesource $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/news/upgrade.php $
|
15
|
* @lastmodified $Date: 2011-06-25 19:18:50 +0200 (Sat, 25 Jun 2011) $
|
16
|
*
|
17
|
*/
|
18
|
|
19
|
// Must include code to stop this file being access directly
|
20
|
if(!defined('WB_URL')) { throw new Exception('illegal file access!! ['.$_SERVER['PHP_SELF'].']'); }
|
21
|
|
22
|
/* **** START UPGRADE ******************************************************* */
|
23
|
function mod_news_Upgrade()
|
24
|
{
|
25
|
global $database, $admin, $MESSAGE;
|
26
|
$callingScript = $_SERVER["SCRIPT_NAME"];
|
27
|
$tmp = 'upgrade-script.php';
|
28
|
$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
|
29
|
|
30
|
$sPagesPath = WB_PATH.PAGES_DIRECTORY;
|
31
|
$sPostsPath = $sPagesPath.'/posts';
|
32
|
// create /posts/ - directory if not exists
|
33
|
if(!file_exists($sPostsPath)) {
|
34
|
if(is_writable($sPagesPath)) {
|
35
|
make_dir(WB_PATH.PAGES_DIRECTORY.'/posts/');
|
36
|
}else {
|
37
|
if(!$globalStarted){
|
38
|
$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
|
39
|
}else {
|
40
|
echo $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
|
41
|
return;
|
42
|
}
|
43
|
}
|
44
|
if($globalStarted) {echo 'directory "'.PAGES_DIRECTORY.'/posts/" created.<br />'; }
|
45
|
}
|
46
|
// check if new fields must be added
|
47
|
$doImportDate = true;
|
48
|
if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_when')) {
|
49
|
if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_when',
|
50
|
'INT NOT NULL DEFAULT \'0\' AFTER `commenting`')) {
|
51
|
if($globalStarted){
|
52
|
echo $MESSAGE['RECORD_MODIFIED_FAILED'].'<br />';
|
53
|
return;
|
54
|
}else {
|
55
|
$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
|
56
|
}
|
57
|
}
|
58
|
if($globalStarted) { echo 'datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_when` added.<br />'; }
|
59
|
}else { $doImportDate = false; }
|
60
|
if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_by')) {
|
61
|
if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_by',
|
62
|
'INT NOT NULL DEFAULT \'0\' AFTER `created_when`')) {
|
63
|
if($globalStarted){
|
64
|
echo $MESSAGE['RECORD_MODIFIED_FAILED'].'<br />';
|
65
|
return;
|
66
|
}else {
|
67
|
$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
|
68
|
}
|
69
|
}
|
70
|
if($globalStarted) {echo 'datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_by` added.<br />'; }
|
71
|
}
|
72
|
// preset new fields `created_by` and `created_when` from existing values
|
73
|
if($doImportDate) {
|
74
|
$sql = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
|
75
|
$sql .= 'SET `created_by`=`posted_by`, `created_when`=`posted_when`';
|
76
|
$database->query($sql);
|
77
|
}
|
78
|
// now iterate through all existing accessfiles,
|
79
|
// write its creation date into database
|
80
|
$oDir = new DirectoryIterator($sPostsPath);
|
81
|
$count = 0;
|
82
|
foreach ($oDir as $fileinfo)
|
83
|
{
|
84
|
$fileName = $fileinfo->getFilename();
|
85
|
if((!$fileinfo->isDot()) &&
|
86
|
($fileName != 'index.php') &&
|
87
|
(substr_compare($fileName,PAGE_EXTENSION,(0-strlen(PAGE_EXTENSION)),strlen(PAGE_EXTENSION)) === 0)
|
88
|
)
|
89
|
{
|
90
|
// save creation date from old accessfile
|
91
|
if($doImportDate) {
|
92
|
$link = '/posts/'.preg_replace('/'.preg_quote(PAGE_EXTENSION).'$/i', '', $fileinfo->getFilename());
|
93
|
$sql = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
|
94
|
$sql .= 'SET `created_when`='.$fileinfo->getMTime().' ';
|
95
|
$sql .= 'WHERE `link`=\''.$link.'\'';
|
96
|
$database->query($sql);
|
97
|
}
|
98
|
// delete old access file
|
99
|
unlink($fileinfo->getPathname());
|
100
|
$count++;
|
101
|
}
|
102
|
}
|
103
|
unset($oDir);
|
104
|
if($globalStarted && $count > 0) {
|
105
|
echo 'save date of creation from '.$count.' old accessfiles and delete these files.<br />';
|
106
|
}
|
107
|
// ************************************************
|
108
|
// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
|
109
|
$sql = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
|
110
|
$sql .= 'SET `created_when`=`published_when` ';
|
111
|
$sql .= 'WHERE `published_when`<`created_when`';
|
112
|
$database->query($sql);
|
113
|
$sql = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
|
114
|
$sql .= 'SET `created_when`=`posted_when` ';
|
115
|
$sql .= 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
|
116
|
$database->query($sql);
|
117
|
// ************************************************
|
118
|
|
119
|
// rebuild all access-files
|
120
|
$count = 0;
|
121
|
// $backSteps = preg_replace('/^'.preg_quote(WB_PATH).'/', '', $sPostsPath);
|
122
|
$backSteps = preg_replace('@^'.preg_quote(WB_PATH).'@', '', $sPostsPath);
|
123
|
$backSteps = str_repeat( '../', substr_count($backSteps, '/'));
|
124
|
$sql = 'SELECT `page_id`,`post_id`,`section_id`,`link` ';
|
125
|
$sql .= 'FROM `'.TABLE_PREFIX.'mod_news_posts`';
|
126
|
$sql .= 'WHERE `link` != \'\'';
|
127
|
if( ($resPosts = $database->query($sql)) )
|
128
|
{
|
129
|
while( $recPost = $resPosts->fetchRow() )
|
130
|
{
|
131
|
$file = $sPagesPath.$recPost['link'].PAGE_EXTENSION;
|
132
|
$content =
|
133
|
'<?php'."\n".
|
134
|
'// *** This file is generated by WebsiteBaker Ver.'.VERSION."\n".
|
135
|
'// *** Creation date: '.date('c')."\n".
|
136
|
'// *** Do not modify this file manually'."\n".
|
137
|
'// *** WB will rebuild this file from time to time!!'."\n".
|
138
|
'// *************************************************'."\n".
|
139
|
"\t".'$page_id = '.$recPost['page_id'].';'."\n".
|
140
|
"\t".'$section_id = '.$recPost['section_id'].';'."\n".
|
141
|
"\t".'$post_id = '.$recPost['post_id'].';'."\n".
|
142
|
"\t".'$post_section = '.$recPost['section_id'].';'."\n".
|
143
|
// "\t".'define(\'POST_SECTION\', '.$recPost['section_id'].');'."\n".
|
144
|
// "\t".'define(\'POST_ID\', '.$recPost['post_id'].');'."\n".
|
145
|
"\t".'require(\''.$backSteps.'index.php\');'."\n".
|
146
|
'// *************************************************'."\n";
|
147
|
if( file_put_contents($file, $content) !== false ) {
|
148
|
// Chmod the file
|
149
|
change_mode($file);
|
150
|
}else {
|
151
|
if($globalStarted){
|
152
|
echo $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
|
153
|
return;
|
154
|
}else {
|
155
|
$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
|
156
|
}
|
157
|
}
|
158
|
$count++;
|
159
|
}
|
160
|
}
|
161
|
if($globalStarted) { echo 'created '.$count.' new accessfiles.'; }
|
162
|
if(!$globalStarted) { $admin->print_footer(); }
|
163
|
}
|
164
|
mod_news_Upgrade();
|
165
|
/* **** END UPGRADE ********************************************************* */
|