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