support for checkpointing
[lhc/web/wiklou.git] / maintenance / dumpHTML.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 define( 'REPORTING_INTERVAL', 10 );
8
9 require_once( 'includes/ImagePage.php' );
10 require_once( 'includes/CategoryPage.php' );
11 require_once( 'includes/RawPage.php' );
12
13 class DumpHTML {
14 # Destination directory
15 var $dest;
16
17 # Show interlanguage links?
18 var $interwiki = true;
19
20 # Depth of HTML directory tree
21 var $depth = 3;
22
23 # Directory that commons images are copied into
24 var $sharedStaticDirectory;
25
26 # Directory that the images are in, after copying
27 var $destUploadDirectory;
28
29 # Relative path to image directory
30 var $imageRel = 'upload';
31
32 # Copy commons images instead of symlinking
33 var $forceCopy = false;
34
35 # Make a copy of all images encountered
36 var $makeSnapshot = false;
37
38 # Make links assuming the script path is in the same directory as
39 # the destination
40 var $alternateScriptPath = false;
41
42 # Original values of various globals
43 var $oldArticlePath = false, $oldCopyrightIcon = false;
44
45 # Has setupGlobals been called?
46 var $setupDone = false;
47
48 # List of raw pages used in the current article
49 var $rawPages;
50
51 # Skin to use
52 var $skin = 'htmldump';
53
54 # Checkpoint stuff
55 var $checkpointFile = false, $checkpoints = false;
56
57 function DumpHTML( $settings ) {
58 foreach ( $settings as $var => $value ) {
59 $this->$var = $value;
60 }
61 }
62
63 function loadCheckpoints() {
64 if ( $this->checkpoints !== false ) {
65 return true;
66 } elseif ( !$this->checkpointFile ) {
67 return false;
68 } else {
69 $lines = @file( $this->checkpointFile );
70 if ( $lines === false ) {
71 print "Starting new checkpoint file \"{$this->checkpointFile}\"\n";
72 $this->checkpoints = array();
73 } else {
74 $lines = array_map( 'trim', $lines );
75 $this->checkpoints = array();
76 foreach ( $lines as $line ) {
77 list( $name, $value ) = explode( '=', $line, 2 );
78 $this->checkpoints[$name] = $value;
79 }
80 }
81 return true;
82 }
83 }
84
85 function getCheckpoint( $type, $defValue = false ) {
86 if ( !$this->loadCheckpoints() ) {
87 return false;
88 }
89 if ( !isset( $this->checkpoints[$type] ) ) {
90 return false;
91 } else {
92 return $this->checkpoints[$type];
93 }
94 }
95
96 function setCheckpoint( $type, $value ) {
97 if ( !$this->checkpointFile ) {
98 return;
99 }
100 $this->checkpoints[$type] = $value;
101 $blob = '';
102 foreach ( $this->checkpoints as $type => $value ) {
103 $blob .= "$type=$value\n";
104 }
105 file_put_contents( $this->checkpointFile, $blob );
106 }
107
108 function doEverything() {
109 if ( $this->getCheckpoint( 'everything' ) == 'done' ) {
110 print "Checkpoint says everything is already done\n";
111 return;
112 }
113 $this->doArticles();
114 $this->doLocalImageDescriptions();
115 $this->doSharedImageDescriptions();
116 $this->doCategories();
117 $this->doRedirects();
118 $this->doSpecials();
119
120 $this->setCheckpoint( 'everything', 'done' );
121 }
122
123 /**
124 * Write a set of articles specified by start and end page_id
125 * Skip categories and images, they will be done separately
126 */
127 function doArticles() {
128 $fname = 'DumpHTML::doArticles';
129
130 $cp = $this->getCheckpoint( 'article' );
131 if ( $cp == 'done' ) {
132 print "Articles already done\n";
133 return;
134 } elseif ( $cp !== false ) {
135 print "Resuming article dump from checkpoint at page_id $cp of {$this->endID}\n";
136 $start = $cp;
137 } else {
138 print "Starting from page_id {$this->startID} of {$this->endID}\n";
139 $start = $this->startID;
140 }
141
142 $this->setupGlobals();
143
144 if ( $this->endID === false ) {
145 $dbr =& wfGetDB( DB_SLAVE );
146 $this->endID = $dbr->selectField( 'page', 'max(page_id)', false, $fname );
147 }
148
149
150 $mainPageObj = Title::newMainPage();
151 $mainPage = $mainPageObj->getPrefixedDBkey();
152
153 for ($id = $start; $id <= $this->endID; $id++) {
154 wfWaitForSlaves( 20 );
155 if ( !($id % REPORTING_INTERVAL) ) {
156 print "Processing ID: $id\r";
157 $this->setCheckpoint( 'article', $id );
158 }
159 if ( !($id % (REPORTING_INTERVAL*10) ) ) {
160 print "\n";
161 }
162 $title = Title::newFromID( $id );
163 if ( $title ) {
164 $ns = $title->getNamespace() ;
165 if ( $ns != NS_CATEGORY && $title->getPrefixedDBkey() != $mainPage ) {
166 $this->doArticle( $title );
167 }
168 }
169 }
170 $this->setCheckpoint( 'article', 'done' );
171 print "\n";
172 }
173
174 function doSpecials() {
175 $this->doMainPage();
176
177 $this->setupGlobals();
178 print "Special:Categories...";
179 $this->doArticle( Title::makeTitle( NS_SPECIAL, 'Categories' ) );
180 print "\n";
181 }
182
183 /** Write the main page as index.html */
184 function doMainPage() {
185
186 print "Making index.html ";
187
188 // Set up globals with no ../../.. in the link URLs
189 $this->setupGlobals( 0 );
190
191 $title = Title::newMainPage();
192 $text = $this->getArticleHTML( $title );
193
194 # Parse the XHTML to find the images
195 $images = $this->findImages( $text );
196 $this->copyImages( $images );
197
198 $file = fopen( "{$this->dest}/index.html", "w" );
199 if ( !$file ) {
200 print "\nCan't open index.html for writing\n";
201 return false;
202 }
203 fwrite( $file, $text );
204 fclose( $file );
205 print "\n";
206 }
207
208 function doImageDescriptions() {
209 $this->doLocalImageDescriptions();
210 $this->doSharedImageDescriptions();
211 }
212
213 /**
214 * Dump image description pages that don't have an associated article, but do
215 * have a local image
216 */
217 function doLocalImageDescriptions() {
218 global $wgSharedUploadDirectory;
219
220 $dbr =& wfGetDB( DB_SLAVE );
221
222 $cp = $this->getCheckpoint( 'local image' );
223 if ( $cp == 'done' ) {
224 print "Local image descriptions already done\n";
225 return;
226 } elseif ( $cp !== false ) {
227 print "Writing image description pages starting from $cp\n";
228 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
229 } else {
230 print "Writing image description pages for local images\n";
231 $conds = false;
232 }
233
234 $this->setupGlobals();
235
236 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
237 array( 'ORDER BY' => 'img_name' ) );
238
239 $i = 0;
240 $num = $dbr->numRows( $res );
241 while ( $row = $dbr->fetchObject( $res ) ) {
242 wfWaitForSlaves( 10 );
243 if ( !( ++$i % REPORTING_INTERVAL ) ) {
244 print "Done $i of $num\r";
245 if ( $row->img_name !== 'done' ) {
246 $this->setCheckpoint( 'local image', $row->img_name );
247 }
248 }
249 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
250 if ( $title->getArticleID() ) {
251 // Already done by dumpHTML
252 continue;
253 }
254 $this->doArticle( $title );
255 }
256 $this->setCheckpoint( 'local image', 'done' );
257 print "\n";
258 }
259
260 /**
261 * Dump images which only have a real description page on commons
262 */
263 function doSharedImageDescriptions() {
264 $cp = $this->getCheckpoint( 'shared image' );
265 if ( $cp == 'done' ) {
266 print "Shared description pages already done\n";
267 return;
268 } elseif ( $cp !== false ) {
269 print "Writing description pages for commons images starting from directory $cp/255\n";
270 $start = $cp;
271 } else {
272 print "Writing description pages for commons images\n";
273 $start = 0;
274 }
275
276 $this->setupGlobals();
277 $i = 0;
278 for ( $hash = $start; $hash < 256; $hash++ ) {
279 $this->setCheckpoint( 'shared image', $hash );
280
281 $dir = sprintf( "%01x/%02x", intval( $hash / 16 ), $hash );
282 $paths = array_merge( glob( "{$this->sharedStaticDirectory}/$dir/*" ),
283 glob( "{$this->sharedStaticDirectory}/thumb/$dir/*" ) );
284
285 foreach ( $paths as $path ) {
286 $file = basename( $path );
287 if ( !(++$i % REPORTING_INTERVAL ) ) {
288 print "$i\r";
289 }
290
291 $title = Title::makeTitle( NS_IMAGE, $file );
292 $this->doArticle( $title );
293 }
294 }
295 $this->setCheckpoint( 'shared image', 'done' );
296 print "\n";
297 }
298
299 function doCategories() {
300 $fname = 'DumpHTML::doCategories';
301 $this->setupGlobals();
302 $dbr =& wfGetDB( DB_SLAVE );
303 $sql = 'SELECT DISTINCT cl_to FROM ' . $dbr->tableName( 'categorylinks' );
304
305 $cp = $this->getCheckpoint( 'category' );
306 if ( $cp == 'done' ) {
307 print "Category pages already done\n";
308 return;
309 } elseif ( $cp !== false ) {
310 print "Resuming category page dump from $cp";
311 $sql .= ' WHERE cl_to >= ' . $dbr->addQuotes( $cp );
312 }
313
314 $sql .= ' ORDER BY cl_to';
315 print "Selecting categories...";
316 $res = $dbr->query( $sql, $fname );
317
318 print "\nWriting " . $dbr->numRows( $res ). " category pages\n";
319 $i = 0;
320 while ( $row = $dbr->fetchObject( $res ) ) {
321 wfWaitForSlaves( 10 );
322 if ( !(++$i % REPORTING_INTERVAL ) ) {
323 print "$i\r";
324 if ( $row->cl_to != 'done' ) {
325 $this->setCheckpoint( 'category', $row->cl_to );
326 }
327 }
328 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
329 $this->doArticle( $title );
330 }
331 $this->setCheckpoint( 'category', 'done' );
332 print "\n";
333 }
334
335 function doRedirects() {
336 print "Doing redirects...\n";
337 $fname = 'DumpHTML::doRedirects';
338 $conds = array( 'page_is_redirect' => 1 );
339
340 $cp = $this->getCheckpoint( 'redirect' );
341 if ( $cp == 'done' ) {
342 print "Redirects already done\n";
343 return;
344 } elseif ( $cp !== false ) {
345 print "Resuming redirect generation from page_id $cp\n";
346 $conds[] = 'page_id > ' . intval( $cp );
347 }
348
349 $this->setupGlobals();
350 $dbr =& wfGetDB( DB_SLAVE );
351 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
352 $conds, $fname );
353 $num = $dbr->numRows( $res );
354 print "$num redirects to do...\n";
355 $i = 0;
356 while ( $row = $dbr->fetchObject( $res ) ) {
357 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
358 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
359 print "Done $i of $num\n";
360 $this->setCheckpoint( 'redirect', $row->page_id );
361 }
362 $this->doArticle( $title );
363 }
364 $this->setCheckpoint( 'redirect', 'done' );
365 }
366
367 /** Write an article specified by title */
368 function doArticle( $title ) {
369 global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
370 global $wgUploadDirectory;
371
372 $this->rawPages = array();
373 $text = $this->getArticleHTML( $title );
374
375 if ( $text === false ) {
376 return;
377 }
378
379 # Parse the XHTML to find the images
380 $images = $this->findImages( $text );
381 $this->copyImages( $images );
382
383 # Write to file
384 $this->writeArticle( $title, $text );
385
386 # Do raw pages
387 wfMkdirParents( "{$this->dest}/raw", 0755 );
388 foreach( $this->rawPages as $record ) {
389 list( $file, $title, $params ) = $record;
390
391 $path = "{$this->dest}/raw/$file";
392 if ( !file_exists( $path ) ) {
393 $article = new Article( $title );
394 $request = new FauxRequest( $params );
395 $rp = new RawPage( $article, $request );
396 $text = $rp->getRawText();
397
398 print "Writing $file\n";
399 $file = fopen( $path, 'w' );
400 if ( !$file ) {
401 print("Can't open file $fullName for writing\n");
402 continue;
403 }
404 fwrite( $file, $text );
405 fclose( $file );
406 }
407 }
408 }
409
410 /** Write the given text to the file identified by the given title object */
411 function writeArticle( &$title, $text ) {
412 $filename = $this->getHashedFilename( $title );
413 $fullName = "{$this->dest}/$filename";
414 $fullDir = dirname( $fullName );
415
416 wfMkdirParents( $fullDir, 0755 );
417
418 wfSuppressWarnings();
419 $file = fopen( $fullName, 'w' );
420 wfRestoreWarnings();
421
422 if ( !$file ) {
423 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
424 return;
425 }
426
427 fwrite( $file, $text );
428 fclose( $file );
429 }
430
431 /** Set up globals required for parsing */
432 function setupGlobals( $currentDepth = NULL ) {
433 global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
434 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
435 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
436 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
437 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
438
439 static $oldLogo = NULL;
440
441 if ( !$this->setupDone ) {
442 $wgHooks['GetLocalURL'][] =& $this;
443 $wgHooks['GetFullURL'][] =& $this;
444 $this->oldArticlePath = $wgServer . $wgArticlePath;
445 }
446
447 if ( is_null( $currentDepth ) ) {
448 $currentDepth = $this->depth;
449 }
450
451 if ( $this->alternateScriptPath ) {
452 if ( $currentDepth == 0 ) {
453 $wgScriptPath = '.';
454 } else {
455 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
456 }
457 } else {
458 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
459 }
460
461 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
462
463 # Logo image
464 # Allow for repeated setup
465 if ( !is_null( $oldLogo ) ) {
466 $wgLogo = $oldLogo;
467 } else {
468 $oldLogo = $wgLogo;
469 }
470
471 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
472 # If it's in the upload directory, rewrite it to the new upload directory
473 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
474 } elseif ( $wgLogo{0} == '/' ) {
475 # This is basically heuristic
476 # Rewrite an absolute logo path to one relative to the the script path
477 $wgLogo = $wgScriptPath . $wgLogo;
478 }
479
480 # Another ugly hack
481 if ( !$this->setupDone ) {
482 $this->oldCopyrightIcon = $wgCopyrightIcon;
483 }
484 $wgCopyrightIcon = str_replace( 'src="/images',
485 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
486
487 $wgStylePath = "$wgScriptPath/skins";
488 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
489 $wgSharedUploadPath = "$wgUploadPath/shared";
490 $wgMaxCredits = -1;
491 $wgHideInterlanguageLinks = !$this->interwiki;
492 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
493 $wgEnableParserCache = false;
494 $wgMathPath = "$wgScriptPath/math";
495 $wgEnableSidebarCache = false;
496
497 if ( !empty( $wgRightsText ) ) {
498 $wgRightsUrl = "$wgScriptPath/COPYING.html";
499 }
500
501 $wgUser = new User;
502 $wgUser->setOption( 'skin', $this->skin );
503 $wgUser->setOption( 'editsection', 0 );
504
505 if ( $this->makeSnapshot ) {
506 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
507 if ( realpath( $this->destUploadDirectory == $wgUploadDirectory ) ) {
508 $this->makeSnapshot = false;
509 }
510 }
511
512 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
513
514 $this->setupDone = true;
515 }
516
517 /** Reads the content of a title object, executes the skin and captures the result */
518 function getArticleHTML( &$title ) {
519 global $wgOut, $wgTitle, $wgArticle, $wgUser;
520
521 $linkCache =& LinkCache::singleton();
522 $linkCache->clear();
523 $wgTitle = $title;
524 if ( is_null( $wgTitle ) ) {
525 return false;
526 }
527
528 $ns = $wgTitle->getNamespace();
529 if ( $ns == NS_SPECIAL ) {
530 $wgOut = new OutputPage;
531 $wgOut->setParserOptions( new ParserOptions );
532 SpecialPage::executePath( $wgTitle );
533 } else {
534 /** @todo merge with Wiki.php code */
535 if ( $ns == NS_IMAGE ) {
536 $wgArticle = new ImagePage( $wgTitle );
537 } elseif ( $ns == NS_CATEGORY ) {
538 $wgArticle = new CategoryPage( $wgTitle );
539 } else {
540 $wgArticle = new Article( $wgTitle );
541 }
542 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
543 if ( $rt != NULL ) {
544 return $this->getRedirect( $rt );
545 } else {
546 $wgOut = new OutputPage;
547 $wgOut->setParserOptions( new ParserOptions );
548
549 $wgArticle->view();
550 }
551 }
552
553 $sk =& $wgUser->getSkin();
554 ob_start();
555 $sk->outputPage( $wgOut );
556 $text = ob_get_contents();
557 ob_end_clean();
558
559 return $text;
560 }
561
562 function getRedirect( $rt ) {
563 $url = $rt->escapeLocalURL();
564 $text = $rt->getPrefixedText();
565 return <<<ENDTEXT
566 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
567 <html xmlns="http://www.w3.org/1999/xhtml">
568 <head>
569 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
570 <meta http-equiv="Refresh" content="0;url=$url" />
571 </head>
572 <body>
573 <p>Redirecting to <a href="$url">$text</a></p>
574 </body>
575 </html>
576 ENDTEXT;
577 }
578
579 /** Returns image paths used in an XHTML document */
580 function findImages( $text ) {
581 global $wgOutputEncoding, $wgDumpImages;
582 $parser = xml_parser_create( $wgOutputEncoding );
583 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
584
585 $wgDumpImages = array();
586 xml_parse( $parser, $text );
587 xml_parser_free( $parser );
588
589 return $wgDumpImages;
590 }
591
592 /**
593 * Copy a file specified by a URL to a given directory
594 *
595 * @param string $srcPath The source URL
596 * @param string $srcPathBase The base directory of the source URL
597 * @param string $srcDirBase The base filesystem directory of the source URL
598 * @param string $destDirBase The base filesystem directory of the destination URL
599 */
600 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
601 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
602 $sourceLoc = "$srcDirBase/$rel";
603 $destLoc = "$destDirBase/$rel";
604 #print "Copying $sourceLoc to $destLoc\n";
605 if ( !file_exists( $destLoc ) ) {
606 wfMkdirParents( dirname( $destLoc ), 0755 );
607 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
608 symlink( $sourceLoc, $destLoc );
609 } else {
610 copy( $sourceLoc, $destLoc );
611 }
612 }
613 }
614
615 /**
616 * Copy an image, and if it is a thumbnail, copy its parent image too
617 */
618 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
619 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
620 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
621 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
622 # The image was a thumbnail
623 # Copy the source image as well
624 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
625 $parts = explode( '/', $rel );
626 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
627 $newSrc = "$srcPathBase/$rel";
628 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
629 }
630 }
631
632 /**
633 * Copy images (or create symlinks) from commons to a static directory.
634 * This is necessary even if you intend to distribute all of commons, because
635 * the directory contents is used to work out which image description pages
636 * are needed.
637 *
638 * Also copies math images, and full-sized images if the makeSnapshot option
639 * is specified.
640 *
641 */
642 function copyImages( $images ) {
643 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
644 $wgMathPath, $wgMathDirectory;
645 # Find shared uploads and copy them into the static directory
646 $sharedPathLength = strlen( $wgSharedUploadPath );
647 $mathPathLength = strlen( $wgMathPath );
648 $uploadPathLength = strlen( $wgUploadPath );
649 foreach ( $images as $escapedImage => $dummy ) {
650 $image = urldecode( $escapedImage );
651
652 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
653 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
654 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
655 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
656 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
657 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
658 }
659 }
660 }
661
662 function onGetFullURL( &$title, &$url, $query ) {
663 global $wgContLang, $wgArticlePath;
664
665 $iw = $title->getInterwiki();
666 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
667 if ( $title->getDBkey() == '' ) {
668 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
669 } else {
670 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
671 $wgArticlePath );
672 }
673 return false;
674 } else {
675 return true;
676 }
677 }
678
679 function onGetLocalURL( &$title, &$url, $query ) {
680 global $wgArticlePath;
681
682 if ( $title->isExternal() ) {
683 # Default is fine for interwiki
684 return true;
685 }
686
687 $url = false;
688 if ( $query != '' ) {
689 parse_str( $query, $params );
690 if ( isset($params['action']) && $params['action'] == 'raw' ) {
691 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
692 $file = 'gen.' . $params['gen'];
693 } else {
694 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
695 // Clean up Monobook.css etc.
696 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
697 $file = $matches[1] . '.' . $matches[2];
698 }
699 }
700 $this->rawPages[$file] = array( $file, $title, $params );
701 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
702 }
703 }
704 if ( $url === false ) {
705 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
706 }
707
708 return false;
709 }
710
711 function getHashedFilename( &$title ) {
712 if ( '' != $title->mInterwiki ) {
713 $dbkey = $title->getDBkey();
714 } else {
715 $dbkey = $title->getPrefixedDBkey();
716 }
717
718 $mainPage = Title::newMainPage();
719 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
720 return 'index.html';
721 }
722
723 return $this->getHashedDirectory( $title ) . '/' .
724 $this->getFriendlyName( $dbkey ) . '.html';
725 }
726
727 function getFriendlyName( $name ) {
728 global $wgLang;
729 # Replace illegal characters for Windows paths with underscores
730 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
731
732 # Work out lower case form. We assume we're on a system with case-insensitive
733 # filenames, so unless the case is of a special form, we have to disambiguate
734 if ( function_exists( 'mb_strtolower' ) ) {
735 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
736 } else {
737 $lowerCase = ucfirst( strtolower( $name ) );
738 }
739
740 # Make it mostly unique
741 if ( $lowerCase != $friendlyName ) {
742 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
743 }
744 # Handle colon specially by replacing it with tilde
745 # Thus we reduce the number of paths with hashes appended
746 $friendlyName = str_replace( ':', '~', $friendlyName );
747
748 return $friendlyName;
749 }
750
751 /**
752 * Get a relative directory for putting a title into
753 */
754 function getHashedDirectory( &$title ) {
755 if ( '' != $title->getInterwiki() ) {
756 $pdbk = $title->getDBkey();
757 } else {
758 $pdbk = $title->getPrefixedDBkey();
759 }
760
761 # Find the first colon if there is one, use characters after it
762 $p = strpos( $pdbk, ':' );
763 if ( $p !== false ) {
764 $dbk = substr( $pdbk, $p + 1 );
765 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
766 } else {
767 $dbk = $pdbk;
768 }
769
770 # Split into characters
771 preg_match_all( '/./us', $dbk, $m );
772
773 $chars = $m[0];
774 $length = count( $chars );
775 $dir = '';
776
777 for ( $i = 0; $i < $this->depth; $i++ ) {
778 if ( $i ) {
779 $dir .= '/';
780 }
781 if ( $i >= $length ) {
782 $dir .= '_';
783 } else {
784 $c = $chars[$i];
785 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
786 if ( function_exists( 'mb_strtolower' ) ) {
787 $dir .= mb_strtolower( $c );
788 } else {
789 $dir .= strtolower( $c );
790 }
791 } else {
792 $dir .= sprintf( "%02X", ord( $c ) );
793 }
794 }
795 }
796 return $dir;
797 }
798
799 }
800
801 /** XML parser callback */
802 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
803 global $wgDumpImages;
804
805 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
806 $wgDumpImages[$attribs['SRC']] = true;
807 }
808 }
809
810 /** XML parser callback */
811 function wfDumpEndTagHandler( $parser, $name ) {}
812
813 # vim: syn=php
814 ?>