/
DateConverter.php
133 lines (100 loc) · 4.08 KB
/
DateConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
# examples:
# <dateConverter pagename='Calendars:TeamPage' /> - defaults to 'Public' and YYYYMMDD
# <dateConverter pagename='Calendars:TeamPage' calname='Team Vacation' />
# <dateConverter pagename='Calendars:TeamPage' calname='Team Vacation' newformat='LM D, YYYY' /> //ex: July 1, 2009
#
# require_once( "$IP/extensions/calendar/dateConverter.php" );
#
# newformat: format of converted events (YYYY MM DD M D SM LM) SM=short month, LM=longmonth
# pagename: wikipage name
# calname: calendar name (default: Public)
# redirect: add redirects to the old page name (default: no redirects)
#
# Note: if the desination page already exists during the convert/move, it will report as an invalid succesful
# Confirm MW environment
if (defined('MEDIAWIKI')) {
$wgExtensionFunctions[] = "wfDateConverter";
function wfDateConverter() {
global $wgParser;
$wgParser->setHook( "dateconverter", "convertCalendarDates" );
}
//CalendarEvents:NSC Interface Calendar/Public/1-1-2008 -Event 0
function convertCalendarDates( $paramstring, $params = array() ){
$converter = new convertCalendarDates;
// defaults
if( !isset($params['newformat'] ) ) $params['newformat'] = 'YYYYMMDD';
if( !isset($params['calname'] ) ) $params['calname'] = 'Public';
//return $converter->userDateFormat(7,5,2009,'YYYYMmDd'); //tester
$ret = $converter->convert( $params['newformat'],
$params['pagename'],
$params['calname'],
isset($params['redirect']),
isset($params['go']) );
return $ret;
}
require_once ("common.php");
class convertCalendarDates
{
function convert($newFormat, $pageName, $calName, $redirect, $go){
$search = "$pageName/$calName";
$pages = PrefixSearch::titleSearch( $search, 1000000); //search upto 1,000,000 events (no performace issue)
$count=$erroredCount = 0;
foreach($pages as $page) {
$retval = false;
$newPage = $this->convertToNewPage($page, $newFormat);
$article = new Article(Title::newFromText($page));
if( $newPage != '' ){
$fromTitle = Title::newFromText($page);
$toTitle = Title::newFromText($newPage);
$articleNew = new Article(Title::newFromText($newPage));
if( !$article->isRedirect() && !$articleNew->exists() ){
if($go){
$retval = $fromTitle->moveTo($toTitle, true, 'CalendarConversion', $redirect);
}else{
if($count < 10){
$testRun .= ' ' . $page . ' →→ ' . $newPage . '<br>';
}
}
}
}
}
unset($pages);
if($go){
$ret = "Conversion completed.";
}else{
$ret = "<b>Test Results, add '<i>go</i>' to the <i>dateConverter</i> tag to convert:</b><br>$testRun";
}
return $ret;
}
function convertToNewPage($page, $newFormat){
$arrPage= explode('/',$page);
$dateStr = trim( array_pop($arrPage) ); //get last element
$arrDateElements = explode('-',$dateStr); //arr[0]=month, arr[1]=day, arr[2]=year, arr[3]=eventid
if(count($arrDateElements) != 4) return '';
$eventID = array_pop($arrDateElements);
$newDate = $this->userDateFormat( trim($arrDateElements[0]),
trim($arrDateElements[1]),
trim($arrDateElements[2]),
trim($newFormat) );
$newDate .= " -$eventID";
$arrPage[] = $newDate;
$ret = implode('/', $arrPage);
return $ret;
}
function userDateFormat($month, $day, $year, $format='') {
if($format == '') $format = 'YYYYMMDD'; //default
$format = str_ireplace('YYYY',$year,$format);
$format = str_ireplace('MM', str_pad($month, 2, '0', STR_PAD_LEFT), $format);
$format = str_ireplace('DD', str_pad($day, 2, '0', STR_PAD_LEFT), $format);
$format = str_ireplace('D',$day,$format);
if( stripos($format,'SM') !== false || stripos($format,'LM') !== false ){
$format = str_ireplace('SM', Common::translate($month, 'month_short'), $format);
$format = str_ireplace('LM', Common::translate($month, 'month'), $format);
}else{
$format = str_ireplace('M',$month,$format);
}
return $format;
}
}
}//end mediawiki environment