Moving from MediaWiki to SharePoint O365 - doRegularPages.php

This script will process the individual html files created from a MediaWiki xml dump by this script to final output files to be uploaded to SharePoint (except for templates, which remains a manual work).

Make sure you've created the folders as explained here, update the URL of your MediaWiki installation on line 5, the URL of your SharePoint site on line 7 and the path of your project directory on line 8.



  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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
ini_set('display_errors', 1);

global $wikiURL;
$wikiURL = "http://yourWikiURL/wiki/";
global $spSiteRelativeURL;
$spSiteRelativeURL = "https://yourInstallation.sharepoint.com/sites/yourSiteNumber/";
$localPath = "C:/something/UniServerZ/www/wikimigration/articles/";

//get article files
$files = scandir($localPath.'articlesSplit');
foreach($files as $filename){
 
 echo "$filename";
 
 //skip the "this" and "one up" files
 while(($filename == "." or $filename == "..")){
  echo "\r\n";
  continue 2;
 }
  
 //get the contents of the file, split out title and content
 $page = file_get_contents($localPath."articlesSplit/$filename");
 $title = substr($page,strlen("<h1>"),strpos($page,"</h1>")-strlen("<h1>"));
 $offlineContent = substr($page, strlen($title)+strlen("<h1></h1>"));
 
 //strip out categories: they are lost to us
 $offlineContent = stripCategories($offlineContent);
 
 //scan the content for templates and redirects
 if(strpos(strtolower($offlineContent), "#redirect") !== false){
  
  echo " contains redirect, formatting and sending... ";

  
  //get the link
  $link = substr($offlineContent,strpos($offlineContent,'[[')+2, strpos($offlineContent,']]') - (strpos($offlineContent,'[[')+2));
  
  //if the redirect uses an anchor for positioning, take it out
  if(strpos($link, '#') !== false)
   $link = substr($link, 0, strpos($link, '#'));
  
  //make sure the redirect link is uppercase and without unneccessary spaces
  $offlineContent = "#REDIRECT[[$link]]";
  
  //send prepared article to folder for uploading
  $data = "<h1>$title</h1>$offlineContent";
  file_put_contents($localPath."articlesToSend/$filename", $data) or die("issue with $filename");
  
  echo "done\r\n";
  //move file to handled folder
  if(rename($localPath."articlesSplit/$filename",$localPath."articlesSplit-handled/$filename") === false)
   die("issue with $filename\r\n");
 }
 else{
  //no difficult wiki syntax we need to save, so copy html instead
  echo " will be fetched from online... ";
  
  //get the online html
  $onlineContent = getContent($wikiURL,$title);
  
  //extract the article content from the page
  $articleStartPosition = strpos($onlineContent, '<div id="mw-content-text"');
  $articleStartPosition = strpos($onlineContent, '>', $articleStartPosition)+1;
  $articleEndPosition = strpos($onlineContent,'<div class="printfooter"',$articleStartPosition);
  $articleEndPosition = strrpos($onlineContent, '</div>',-1*(strlen($onlineContent) - $articleEndPosition));
  $onlineContent = substr($onlineContent, $articleStartPosition, $articleEndPosition - $articleStartPosition);
  
  //clean caching and other non-visible info
  $onlineContent = cleanCaching($onlineContent);
  
  //strip out edit links
  $onlineContent = stripEditLinks($onlineContent);
  
  //strip out TOC
  $onlineContent = stripTOC($onlineContent);
  
  //replace image and file links
  $onlineContent = replaceImageAndFileLinks($onlineContent);
  $onlineContent = replaceIntraWikiLinks($onlineContent);
  
  //replace math
  $onlineContent = replaceMath($onlineContent);
  
  //replace source code
  $onlineContent = replaceSourceCode($onlineContent, $offlineContent);
  
  //beautify tables
  $onlineContent = str_replace('class="wikitable"', 'style="margin: 1em 1em 1em 0;background-color: #f9f9f9;border: 1px #aaa solid;border-collapse: collapse;color: black;"', $onlineContent);
  $onlineContent = str_replace('<tr>','<tr style="border: 1px #aaa solid;">',$onlineContent);
  $onlineContent = str_replace('<td>','<td style="border: 1px #aaa solid;">',$onlineContent);
  $onlineContent = str_replace('<th>','<th style="border: 1px #aaa solid;">',$onlineContent);
  
  
  //make the title sharepoint-legal - change the "special characters" to double underscores
  $title=str_replace(":","__",$title);
  $title=str_replace("?","__",$title);
  $title=str_replace("/","__",$title);

  
  if(strpos($filename,'Template__') !== false){
   //templates moved to special folder for revision
   echo "but is template\r\n";
   file_put_contents($localPath."templates/template/$filename", $onlineContent);
   $origFilename = $filename;
   $filename = str_replace(".html", "_source.html", $filename);
   if(rename($localPath."articlesSplit/$origFilename",$localPath."templates/template/$filename") === false)
    die("issue with $filename\r\n");
  }
  elseif(strpos($offlineContent, '{{') !== false){
   //templatecallers moved to special folder for revision
   echo "but has template\r\n";
   file_put_contents($localPath."templates/caller/$filename", $onlineContent);
   $origFilename = $filename;
   $filename = str_replace(".html", "_source.html", $filename);
   if(rename($localPath."articlesSplit/$origFilename",$localPath."templates/caller/$filename") === false)
    die("issue with $filename\r\n");
  }
  else{
   //send prepared article to folder for uploading
   $data = "<h1>$title</h1>$onlineContent";
   file_put_contents($localPath."articlesToSend/$filename", $data) or die("issue with $filename");
 
   //move file to handled folder
   if(rename($localPath."articlesSplit/$filename",$localPath."articlesSplit-handled/$filename") === false)
    die("issue with $filename\r\n");
   
   echo "done\r\n";
  }
 }
}


/*
 * Some functions
 * */
function stripCategories($content){
 $newContent = $content;
 while(strpos(strtolower($content),"[[category:") !== false){
  $catStart = strpos(strtolower($content),"[[category:");
  $catEnd = strpos($content,"]]",$catStart);
  $newContent = substr($content,0,$catStart);
  $newContent.= substr($content,$catEnd+2);

  $content = $newContent;
 }
 while(strpos(strtolower($content),"[[category :") !== false){
  $catStart = strpos(strtolower($content),"[[category :");
  $catEnd = strpos($content,"]]",$catStart);
  $newContent = substr($content,0,$catStart);
  $newContent.= substr($content,$catEnd+2);

  $content = $newContent;
 }
 return $newContent;
}

function getContent($wikiURL,$title){
 $articleURL = $wikiURL.str_replace(" ","%20",$title);

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $articleURL);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_AUTOREFERER, true);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 $onlineContent = curl_exec($ch);
 
 if($onlineContent === false)
  die('Curl error: ' . curl_error($ch));
 
 curl_close($ch);
 
 return $onlineContent;
}

function cleanCaching($onlineContent){
 while(strpos($onlineContent, "<!--") !== false){
  $commentStart = strpos($onlineContent, "<!--");
  $commentEnd = strpos($onlineContent, "-->",$commentStart);
 
  $newContent = substr($onlineContent, 0, $commentStart);
  $newContent.= substr($onlineContent, $commentEnd+3);
 
  $onlineContent = $newContent;
 }
 return $onlineContent;
}

function stripEditLinks($onlineContent){
 while(strpos($onlineContent, '<span class="mw-editsection">') !== false){
  $editStart = strpos($onlineContent, '<span class="mw-editsection">');
 
  $lastSpan = $editStart + strlen('<span class="mw-editsection">');
  $openSpan = 1;
  while($openSpan > 0){
   $nextOpenSpan = strpos($onlineContent, '<span',$lastSpan);
   $nextCloseSpan = strpos($onlineContent, '</span>', $lastSpan);
    
   if($nextOpenSpan !== false and $nextOpenSpan < $nextCloseSpan){
    $openSpan++;
    $lastSpan = $nextOpenSpan+5;
   }
   else{
    $openSpan--;
    $lastSpan = $nextCloseSpan+7;
   }
  }
  $editEnd = $lastSpan;
 
  $newContent = substr($onlineContent, 0, $editStart);
  $newContent.= substr($onlineContent, $editEnd);
 
  $onlineContent = $newContent;
 }
 
 return $onlineContent;
}

function stripTOC($onlineContent){
 while(strpos($onlineContent, '<div id="toc"') !== false){
  $editStart = strpos($onlineContent, '<div id="toc"');
 
  $lastSpan = $editStart + strlen('<div id="toc"');
  $openSpan = 1;
  while($openSpan > 0){
   $nextOpenSpan = strpos($onlineContent, '<div',$lastSpan);
   $nextCloseSpan = strpos($onlineContent, '</div>', $lastSpan);
 
   if($nextOpenSpan !== false and $nextOpenSpan < $nextCloseSpan){
    $openSpan++;
    $lastSpan = $nextOpenSpan+5;
   }
   else{
    $openSpan--;
    $lastSpan = $nextCloseSpan+7;
   }
  }
  $editEnd = $lastSpan;
 
  $newContent = substr($onlineContent, 0, $editStart);
  $newContent.= substr($onlineContent, $editEnd);
 
  $onlineContent = $newContent;
 }
 
 return $onlineContent;
}

function replaceImageAndFileLinks($onlineContent){
 global $spSiteRelativeURL;
 while(strpos($onlineContent, '<a href="/wiki/File:') !== false){
  $linkStart = strpos($onlineContent, '<a href="/wiki/File:');
  $urlStart = strpos($onlineContent,'"',$linkStart)+1;
  $urlEnd = strpos($onlineContent, '"', $urlStart);
  $url = substr($onlineContent, $urlStart, $urlEnd-$urlStart);
 
  $linkEnd = strpos($onlineContent, '</a>', $urlEnd)+4;
  $imgStart = strpos($onlineContent, '<img',$urlEnd);
 
  if($imgStart !== false and $imgStart < $linkEnd){
   //link to image
   $imgSourceStart = strpos($onlineContent, 'src="',$imgStart)+5;
   $imgSourceEnd = strpos($onlineContent, '"', $imgSourceStart);
   $imgSource = substr($onlineContent,$imgSourceStart, $imgSourceEnd - $imgSourceStart);

   $imgFile = extractFileName($imgSource);
   
   $widthStart = strpos($onlineContent, 'width="', $imgStart) + 7;
   $widthEnd = strpos($onlineContent, '"', $widthStart);
   $width = substr($onlineContent, $widthStart, $widthEnd - $widthStart);
   
   $newContent = substr($onlineContent,0,$linkStart);
   $newContent.= '<img src="'.$spSiteRelativeURL.'SiteAssets/mw/'.$imgFile.'" width="'.$width.'">';
   $newContent.= substr($onlineContent, $linkEnd);
   
   $onlineContent = $newContent;
  }
  else{
   //link to file
   $link = substr($onlineContent, $linkStart, $linkEnd - $linkStart);
   $link = str_replace('/wiki/File:', $spSiteRelativeURL.'Shared Documents/', $link);
   $link = str_replace('File:','',$link);
   
   $newContent = substr($onlineContent,0,$linkStart);
   $newContent.= $link;
   $newContent.= substr($onlineContent, $linkEnd);
    
   $onlineContent = $newContent;
  }
 }
 
 return $onlineContent;
}

function extractFileName($imgSource){
 $nextSlash = strpos($imgSource, '/');
 $pathPiece = substr($imgSource, 0, $nextSlash);
 while(strpos($pathPiece, '.') === false){
  $prevSlash = $nextSlash+1;
  $nextSlash = strpos($imgSource,'/',$prevSlash);
  if($nextSlash === false) 
   $nextSlash = strlen($imgSource);
  $pathPiece = substr($imgSource, $prevSlash, $nextSlash - $prevSlash);
 }
 return $pathPiece;
}

function replaceIntraWikiLinks($onlineContent){
 while(strpos($onlineContent, '<a href="/wiki/') !== false){
  //existing article
  $linkStart = strpos($onlineContent, '<a href="/wiki/');
  $linkEnd = strpos($onlineContent, '</a>', $linkStart)+4;
 
  $nameStart= strpos($onlineContent, '>',$linkStart);
  $nameEnd = strpos($onlineContent, '<', $linkStart+1); 
  $name = substr($onlineContent,$nameStart+1, $nameEnd - ($nameStart+1));
  
  $titleStart= strpos($onlineContent, 'title="',$linkStart)+7;
  if($titleStart === false)
   $title = $name;
  else{
   $titleEnd = strpos($onlineContent, '"', $titleStart);
   $title = substr($onlineContent, $titleStart, $titleEnd - $titleStart);
  }
 
  $newContent = substr($onlineContent,0,$linkStart);
  
  //change the underscores back to spaces, but keep the double underscores
  $title=str_replace("__", "%%", $title);
  $title=str_replace("_", " ", $title);
  $title=str_replace("%%", "__", $title);
  //change the "special characters" to double underscores
  $title=str_replace(":","__",$title);
  $title=str_replace("?","__",$title);
  $title=str_replace("/","__",$title);
  
  //write out link
  if($title==$name)
   $newContent.= "[[$title]]";
  else
   $newContent.= "[[$title|$name]]";
  $newContent.= substr($onlineContent, $linkEnd);
 
  $onlineContent = $newContent;
 }
 while(strpos($onlineContent, '<a href="/w/') !== false){
  //non-existing article
  $linkStart = strpos($onlineContent, '<a href="/w/');
  $linkEnd = strpos($onlineContent, '</a>', $linkStart)+4;
 
  $nameStart= strpos($onlineContent, '>',$linkStart)+1;
  $nameEnd = strpos($onlineContent, '<', $nameStart);
 
  $name = substr($onlineContent,$nameStart, $nameEnd - $nameStart);
  //change the underscores back to spaces, but keep the double underscores
  $name=str_replace("__", "%%", $name);
  $name=str_replace("_", " ", $name);
  $name=str_replace("%%", "__", $name);
  //change the "special characters" to double underscores
  $name=str_replace(":","__",$name);
  $name=str_replace("?","__",$name);
  $name=str_replace("/","__",$name);
  
  //write out link
  $newContent = substr($onlineContent,0,$linkStart);
  $newContent.= "[[$name]]";
  $newContent.= substr($onlineContent, $linkEnd);
 
  $onlineContent = $newContent;
 
 }
 return $onlineContent;
}

function replaceMath($onlineContent){
 while(strpos($onlineContent,'<img class="tex" alt="')){
  $mathImgStart = strpos($onlineContent,'<img class="tex" alt="');
  $mathImgEnd = strpos($onlineContent,'>',$mathImgStart)+1;
  $mathImg = substr($onlineContent, $mathImgStart, $mathImgEnd-$mathImgStart);
  
  $mathFormulaStart = strpos($mathImg, 'alt="')+5;
  $mathFormulaEnd = strpos($mathImg, '"',$mathFormulaStart);
  $mathFormula = trim(substr($mathImg, $mathFormulaStart, $mathFormulaEnd-$mathFormulaStart));
  $mathFormula = str_replace("&#10;", "", $mathFormula);
  
  $onlineContent = str_replace($mathImg, "$$$mathFormula$$", $onlineContent);
 }
 return $onlineContent;
}

function replaceSourceCode($onlineContent, $offlineContent){
 //look for <div dir="ltr" class="mw-geshi mw-code mw-content-ltr"> - these are the online code elements
 while(strpos($onlineContent, '<div dir="ltr" class="mw-geshi mw-code mw-content-ltr">') !== false){
  
  //determine source element in online content
  $sourceStart = strpos($onlineContent, '<div dir="ltr" class="mw-geshi mw-code mw-content-ltr">');
  $lastDiv = $sourceStart + strlen('<div dir="ltr" class="mw-geshi mw-code mw-content-ltr">');
  $openDiv = 1;
  while($openDiv > 0){
   $nextOpenDiv = strpos($onlineContent, '<div',$lastDiv);
   $nextCloseDiv = strpos($onlineContent, '</div>', $lastDiv);
  
   if($nextOpenDiv !== false and $nextOpenDiv < $nextCloseDiv){
    $openDiv++;
    $lastDiv = $nextOpenDiv+4;
   }
   else{
    $openDiv--;
    $lastDiv = $nextCloseDiv+6;
   }
  }
  $sourceEnd = $lastDiv;
  
  //determine raw code from offlinecontent - first code element is right one because they're cut out after use
  $rawStart = strpos($offlineContent, '&lt;syntaxhighlight lang=&quot;');
  $sourceLangStart = $rawStart + strlen('&lt;syntaxhighlight lang=&quot;');
  $sourceLangEnd = strpos($offlineContent, '&quot;&gt;', $sourceLangStart);
  $sourceLanguage = substr($offlineContent, $sourceLangStart, $sourceLangEnd - $sourceLangStart);
  $codeStart = $sourceLangEnd + strlen('&quot;&gt;');
  $codeEnd = strpos($offlineContent,'&lt;/syntaxhighlight&gt;',$rawStart);
  $code = substr($offlineContent, $codeStart, $codeEnd-$codeStart);
  $rawEnd = $codeEnd + strlen('&lt;/syntaxhighlight&gt;');
  
  //create sharepoint source code element
  $codeElement = '<pre class="brush: '.$sourceLanguage . '">'. $code . '</pre>';
  
  //replace in onlinecontent and cut out of offline content
  $offlineContent = substr($offlineContent,0,$rawStart) . substr($offlineContent,$rawEnd);
  $onlineContent = substr($onlineContent, 0, $sourceStart) . $codeElement . substr($onlineContent, $sourceEnd);
 }
 return $onlineContent;
}
?>