4 * @subpackage Maintenance
7 define( 'REPORTING_INTERVAL', 10 );
9 require_once( 'includes/ImagePage.php' );
10 require_once( 'includes/CategoryPage.php' );
11 require_once( 'includes/RawPage.php' );
14 # Destination directory
18 var $noOverwrite = false;
20 # Show interlanguage links?
21 var $interwiki = true;
23 # Depth of HTML directory tree
26 # Directory that commons images are copied into
27 var $sharedStaticDirectory;
29 # Directory that the images are in, after copying
30 var $destUploadDirectory;
32 # Relative path to image directory
33 var $imageRel = 'upload';
35 # Copy commons images instead of symlinking
36 var $forceCopy = false;
38 # Make a copy of all images encountered
39 var $makeSnapshot = false;
41 # Don't image description pages in doEverything()
42 var $noSharedDesc = false;
44 # Make links assuming the script path is in the same directory as
46 var $alternateScriptPath = false;
48 # Original values of various globals
49 var $oldArticlePath = false, $oldCopyrightIcon = false;
51 # Has setupGlobals been called?
52 var $setupDone = false;
54 # Has to compress html pages
55 var $compress = false;
57 # List of raw pages used in the current article
61 var $skin = 'htmldump';
64 var $checkpointFile = false, $checkpoints = false;
66 var $startID = 1, $endID = false;
68 var $sliceNumerator = 1, $sliceDenominator = 1;
70 # Max page ID, lazy initialised
71 var $maxPageID = false;
74 var $udpProfile, $udpProfileCounter = 0, $udpProfileInit = false;
76 function DumpHTML( $settings = array() ) {
77 foreach ( $settings as $var => $value ) {
82 function loadCheckpoints() {
83 if ( $this->checkpoints !== false ) {
85 } elseif ( !$this->checkpointFile ) {
88 $lines = @file( $this->checkpointFile );
89 if ( $lines === false ) {
90 print "Starting new checkpoint file \"{$this->checkpointFile}\"\n";
91 $this->checkpoints = array();
93 $lines = array_map( 'trim', $lines );
94 $this->checkpoints = array();
95 foreach ( $lines as $line ) {
96 list( $name, $value ) = explode( '=', $line, 2 );
97 $this->checkpoints[$name] = $value;
104 function getCheckpoint( $type, $defValue = false ) {
105 if ( !$this->loadCheckpoints() ) {
108 if ( !isset( $this->checkpoints[$type] ) ) {
111 return $this->checkpoints[$type];
115 function setCheckpoint( $type, $value ) {
116 if ( !$this->checkpointFile ) {
119 $this->checkpoints[$type] = $value;
121 foreach ( $this->checkpoints as $type => $value ) {
122 $blob .= "$type=$value\n";
124 file_put_contents( $this->checkpointFile, $blob );
127 function doEverything() {
128 if ( $this->getCheckpoint( 'everything' ) == 'done' ) {
129 print "Checkpoint says everything is already done\n";
133 $this->doCategories();
134 $this->doRedirects();
135 if ( $this->sliceNumerator == 1 ) {
138 $this->doLocalImageDescriptions();
140 if ( !$this->noSharedDesc ) {
141 $this->doSharedImageDescriptions();
144 $this->setCheckpoint( 'everything', 'done' );
148 * Write a set of articles specified by start and end page_id
149 * Skip categories and images, they will be done separately
151 function doArticles() {
152 if ( $this->endID === false ) {
153 $end = $this->getMaxPageID();
157 $start = $this->startID;
159 # Start from the checkpoint
160 $cp = $this->getCheckpoint( 'article' );
161 if ( $cp == 'done' ) {
162 print "Articles already done\n";
164 } elseif ( $cp !== false ) {
166 print "Resuming article dump from checkpoint at page_id $start of $end\n";
168 print "Starting from page_id $start of $end\n";
171 # Move the start point to the correct slice if it isn't there already
172 $start = $this->modSliceStart( $start );
174 $this->setupGlobals();
176 $mainPageObj = Title::newMainPage();
177 $mainPage = $mainPageObj->getPrefixedDBkey();
179 for ( $id = $start, $i = 0; $id <= $end; $id += $this->sliceDenominator, $i++ ) {
180 wfWaitForSlaves( 20 );
181 if ( !( $i % REPORTING_INTERVAL) ) {
182 print "Processing ID: $id\r";
183 $this->setCheckpoint( 'article', $id );
185 if ( !($i % (REPORTING_INTERVAL*10) ) ) {
188 $title = Title::newFromID( $id );
190 $ns = $title->getNamespace() ;
191 if ( $ns != NS_CATEGORY && $ns != NS_MEDIAWIKI &&
192 $title->getPrefixedDBkey() != $mainPage ) {
193 $this->doArticle( $title );
197 $this->setCheckpoint( 'article', 'done' );
201 function doSpecials() {
204 $this->setupGlobals();
205 print "Special:Categories...";
206 $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
210 /** Write the main page as index.html */
211 function doMainPage() {
213 print "Making index.html ";
215 // Set up globals with no ../../.. in the link URLs
216 $this->setupGlobals( 0 );
218 $title = Title::newMainPage();
219 $text = $this->getArticleHTML( $title );
221 # Parse the XHTML to find the images
222 $images = $this->findImages( $text );
223 $this->copyImages( $images );
225 $file = fopen( "{$this->dest}/index.html", "w" );
227 print "\nCan't open index.html for writing\n";
230 fwrite( $file, $text );
235 function doImageDescriptions() {
236 $this->doLocalImageDescriptions();
237 if ( !$this->noSharedDesc ) {
238 $this->doSharedImageDescriptions();
243 * Dump image description pages that don't have an associated article, but do
246 function doLocalImageDescriptions() {
249 $dbr =& wfGetDB( DB_SLAVE );
251 $cp = $this->getCheckpoint( 'local image' );
252 if ( $cp == 'done' ) {
253 print "Local image descriptions already done\n";
255 } elseif ( $cp !== false ) {
256 print "Writing image description pages starting from $cp\n";
257 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
259 print "Writing image description pages for local images\n";
263 $this->setupGlobals();
267 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
268 array( 'ORDER BY' => 'img_name', 'LIMIT' => $chunkSize ) );
269 $numRows = $dbr->numRows( $res );
271 while ( $row = $dbr->fetchObject( $res ) ) {
272 # Update conds for the next chunk query
273 $conds = array( 'img_name > ' . $dbr->addQuotes( $row->img_name ) );
275 // Slice the result set with a filter
276 if ( !$this->sliceFilter( $row->img_name ) ) {
280 wfWaitForSlaves( 10 );
281 if ( !( ++$i % REPORTING_INTERVAL ) ) {
282 print "{$row->img_name}\n";
283 if ( $row->img_name !== 'done' ) {
284 $this->setCheckpoint( 'local image', $row->img_name );
287 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
288 if ( $title->getArticleID() ) {
289 // Already done by dumpHTML
292 $this->doArticle( $title );
294 $dbr->freeResult( $res );
295 } while ( $numRows );
297 $this->setCheckpoint( 'local image', 'done' );
302 * Dump images which only have a real description page on commons
304 function doSharedImageDescriptions() {
305 list( $start, $end ) = $this->sliceRange( 0, 255 );
307 $cp = $this->getCheckpoint( 'shared image' );
308 if ( $cp == 'done' ) {
309 print "Shared description pages already done\n";
311 } elseif ( $cp !== false ) {
312 print "Writing description pages for commons images starting from directory $cp/255\n";
315 print "Writing description pages for commons images\n";
318 $this->setupGlobals();
320 for ( $hash = $start; $hash <= $end; $hash++ ) {
321 $this->setCheckpoint( 'shared image', $hash );
323 $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
324 intval( $hash / 16 ), $hash );
325 $handle = @opendir( $dir );
326 while ( $handle && $file = readdir( $handle ) ) {
327 if ( $file[0] == '.' ) {
330 if ( !(++$i % REPORTING_INTERVAL ) ) {
334 $title = Title::makeTitleSafe( NS_IMAGE, $file );
335 $this->doArticle( $title );
341 $this->setCheckpoint( 'shared image', 'done' );
345 function doCategories() {
348 $this->setupGlobals();
349 $dbr =& wfGetDB( DB_SLAVE );
351 $cp = $this->getCheckpoint( 'category' );
352 if ( $cp == 'done' ) {
353 print "Category pages already done\n";
355 } elseif ( $cp !== false ) {
356 print "Resuming category page dump from $cp\n";
357 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
359 print "Starting category pages\n";
365 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
366 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
367 $numRows = $dbr->numRows( $res );
369 while ( $row = $dbr->fetchObject( $res ) ) {
370 // Set conditions for next chunk
371 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
373 // Filter pages from other slices
374 if ( !$this->sliceFilter( $row->cl_to ) ) {
378 wfWaitForSlaves( 10 );
379 if ( !(++$i % REPORTING_INTERVAL ) ) {
380 print "{$row->cl_to}\n";
381 if ( $row->cl_to != 'done' ) {
382 $this->setCheckpoint( 'category', $row->cl_to );
385 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
386 $this->doArticle( $title );
388 $dbr->freeResult( $res );
389 } while ( $numRows );
391 $this->setCheckpoint( 'category', 'done' );
395 function doRedirects() {
396 print "Doing redirects...\n";
399 $end = $this->getMaxPageID();
400 $cp = $this->getCheckpoint( 'redirect' );
401 if ( $cp == 'done' ) {
402 print "Redirects already done\n";
404 } elseif ( $cp !== false ) {
405 print "Resuming redirect generation from page_id $cp\n";
406 $start = intval( $cp );
411 $this->setupGlobals();
412 $dbr =& wfGetDB( DB_SLAVE );
415 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
416 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
418 'page_is_redirect' => 1,
419 "page_id BETWEEN $chunkStart AND $chunkEnd"
421 # Modulo slicing in SQL
422 if ( $this->sliceDenominator != 1 ) {
423 $n = intval( $this->sliceNumerator );
424 $m = intval( $this->sliceDenominator );
425 $conds[] = "page_id % $m = $n";
427 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
428 $conds, __METHOD__ );
430 while ( $row = $dbr->fetchObject( $res ) ) {
431 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
432 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
433 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
434 $this->setCheckpoint( 'redirect', $row->page_id );
436 $this->doArticle( $title );
438 $dbr->freeResult( $res );
440 $this->setCheckpoint( 'redirect', 'done' );
443 /** Write an article specified by title */
444 function doArticle( $title ) {
445 if ( $this->noOverwrite ) {
446 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
447 if ( file_exists( $fileName ) ) {
454 $this->rawPages = array();
455 $text = $this->getArticleHTML( $title );
457 if ( $text === false ) {
461 # Parse the XHTML to find the images
462 $images = $this->findImages( $text );
463 $this->copyImages( $images );
466 $this->writeArticle( $title, $text );
469 wfMkdirParents( "{$this->dest}/raw", 0755 );
470 foreach( $this->rawPages as $record ) {
471 list( $file, $title, $params ) = $record;
473 $path = "{$this->dest}/raw/$file";
474 if ( !file_exists( $path ) ) {
475 $article = new Article( $title );
476 $request = new FauxRequest( $params );
477 $rp = new RawPage( $article, $request );
478 $text = $rp->getRawText();
480 print "Writing $file\n";
481 $file = fopen( $path, 'w' );
483 print("Can't open file $path for writing\n");
486 fwrite( $file, $text );
491 wfIncrStats( 'dumphtml_article' );
494 /** Write the given text to the file identified by the given title object */
495 function writeArticle( $title, $text ) {
496 $filename = $this->getHashedFilename( $title );
498 # Temporary hack for current dump, this should be moved to
499 # getFriendlyName() at the earliest opportunity.
501 # Limit filename length to 255 characters, so it works on ext3.
502 # Titles are in fact limited to 255 characters, but dumpHTML
503 # adds a suffix which may put them over the limit.
504 $length = strlen( $filename );
505 if ( $length > 255 ) {
506 print "Warning: Filename too long ($length bytes). Skipping.\n";
510 $fullName = "{$this->dest}/$filename";
511 $fullDir = dirname( $fullName );
513 if ( $this->compress ) {
515 $text = gzencode( $text, 9 );
518 wfMkdirParents( $fullDir, 0755 );
520 wfSuppressWarnings();
521 $file = fopen( $fullName, 'w' );
525 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
529 fwrite( $file, $text );
533 /** Set up globals required for parsing */
534 function setupGlobals( $currentDepth = NULL ) {
535 global $wgUser, $wgStylePath, $wgArticlePath, $wgMathPath;
536 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
537 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
538 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
539 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
540 global $wgGenerateThumbnailOnParse;
542 static $oldLogo = NULL;
544 if ( !$this->setupDone ) {
545 $wgHooks['GetLocalURL'][] =& $this;
546 $wgHooks['GetFullURL'][] =& $this;
547 $wgHooks['SiteNoticeBefore'][] =& $this;
548 $wgHooks['SiteNoticeAfter'][] =& $this;
549 $this->oldArticlePath = $wgServer . $wgArticlePath;
552 if ( is_null( $currentDepth ) ) {
553 $currentDepth = $this->depth;
556 if ( $this->alternateScriptPath ) {
557 if ( $currentDepth == 0 ) {
560 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
563 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
566 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
569 # Allow for repeated setup
570 if ( !is_null( $oldLogo ) ) {
576 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
577 # If it's in the upload directory, rewrite it to the new upload directory
578 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
579 } elseif ( $wgLogo{0} == '/' ) {
580 # This is basically heuristic
581 # Rewrite an absolute logo path to one relative to the the script path
582 $wgLogo = $wgScriptPath . $wgLogo;
586 if ( !$this->setupDone ) {
587 $this->oldCopyrightIcon = $wgCopyrightIcon;
589 $wgCopyrightIcon = str_replace( 'src="/images',
590 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
592 $wgStylePath = "$wgScriptPath/skins";
593 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
594 $wgSharedUploadPath = "$wgUploadPath/shared";
596 $wgHideInterlanguageLinks = !$this->interwiki;
597 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
598 $wgEnableParserCache = false;
599 $wgMathPath = "$wgScriptPath/math";
600 $wgEnableSidebarCache = false;
601 $wgGenerateThumbnailOnParse = true;
603 if ( !empty( $wgRightsText ) ) {
604 $wgRightsUrl = "$wgScriptPath/COPYING.html";
608 $wgUser->setOption( 'skin', $this->skin );
609 $wgUser->setOption( 'editsection', 0 );
611 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
612 if ( realpath( $this->destUploadDirectory ) == realpath( $wgUploadDirectory ) ) {
613 print "Disabling image snapshot because the destination is the same as the source\n";
614 $this->makeSnapshot = false;
616 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
618 $this->setupDone = true;
621 /** Reads the content of a title object, executes the skin and captures the result */
622 function getArticleHTML( $title ) {
623 global $wgOut, $wgTitle, $wgArticle, $wgUser;
625 $linkCache =& LinkCache::singleton();
628 if ( is_null( $wgTitle ) ) {
632 $ns = $wgTitle->getNamespace();
633 if ( $ns == NS_SPECIAL ) {
634 $wgOut = new OutputPage;
635 $wgOut->setParserOptions( new ParserOptions );
636 SpecialPage::executePath( $wgTitle );
638 /** @todo merge with Wiki.php code */
639 if ( $ns == NS_IMAGE ) {
640 $wgArticle = new ImagePage( $wgTitle );
641 } elseif ( $ns == NS_CATEGORY ) {
642 $wgArticle = new CategoryPage( $wgTitle );
644 $wgArticle = new Article( $wgTitle );
646 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
648 return $this->getRedirect( $rt );
650 $wgOut = new OutputPage;
651 $wgOut->setParserOptions( new ParserOptions );
658 $sk =& $wgUser->getSkin();
660 $sk->outputPage( $wgOut );
661 $text = ob_get_contents();
667 function getRedirect( $rt ) {
668 $url = $rt->escapeLocalURL();
669 $text = $rt->getPrefixedText();
671 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
672 <html xmlns="http://www.w3.org/1999/xhtml">
674 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
675 <meta http-equiv="Refresh" content="0;url=$url" />
678 <p>Redirecting to <a href="$url">$text</a></p>
684 /** Returns image paths used in an XHTML document */
685 function findImages( $text ) {
686 global $wgOutputEncoding, $wgDumpImages;
687 $parser = xml_parser_create( $wgOutputEncoding );
688 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
690 $wgDumpImages = array();
691 xml_parse( $parser, $text );
692 xml_parser_free( $parser );
694 return $wgDumpImages;
698 * Copy a file specified by a URL to a given directory
700 * @param string $srcPath The source URL
701 * @param string $srcPathBase The base directory of the source URL
702 * @param string $srcDirBase The base filesystem directory of the source URL
703 * @param string $destDirBase The base filesystem directory of the destination URL
705 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
706 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
707 $sourceLoc = "$srcDirBase/$rel";
708 $destLoc = "$destDirBase/$rel";
709 #print "Copying $sourceLoc to $destLoc\n";
710 if ( !file_exists( $destLoc ) ) {
711 wfMkdirParents( dirname( $destLoc ), 0755 );
712 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
713 if ( !symlink( $sourceLoc, $destLoc ) ) {
714 print "Warning: unable to create symlink at $destLoc\n";
717 if ( !copy( $sourceLoc, $destLoc ) ) {
718 print "Warning: unable to copy $sourceLoc to $destLoc\n";
725 * Copy an image, and if it is a thumbnail, copy its parent image too
727 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
728 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
729 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
730 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
731 # The image was a thumbnail
732 # Copy the source image as well
733 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
734 $parts = explode( '/', $rel );
735 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
736 $newSrc = "$srcPathBase/$rel";
737 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
742 * Copy images (or create symlinks) from commons to a static directory.
743 * This is necessary even if you intend to distribute all of commons, because
744 * the directory contents is used to work out which image description pages
747 * Also copies math images, and full-sized images if the makeSnapshot option
751 function copyImages( $images ) {
752 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
753 $wgMathPath, $wgMathDirectory;
754 # Find shared uploads and copy them into the static directory
755 $sharedPathLength = strlen( $wgSharedUploadPath );
756 $mathPathLength = strlen( $wgMathPath );
757 $uploadPathLength = strlen( $wgUploadPath );
758 foreach ( $images as $escapedImage => $dummy ) {
759 $image = urldecode( $escapedImage );
761 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
762 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
763 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
764 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
765 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
766 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
771 function onGetFullURL( &$title, &$url, $query ) {
772 global $wgContLang, $wgArticlePath;
774 $iw = $title->getInterwiki();
775 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
776 if ( $title->getDBkey() == '' ) {
777 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
779 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
782 $url .= $this->compress ? ".gz" : "";
789 function onGetLocalURL( &$title, &$url, $query ) {
790 global $wgArticlePath;
792 if ( $title->isExternal() ) {
793 # Default is fine for interwiki
798 if ( $query != '' ) {
800 parse_str( $query, $params );
801 if ( isset($params['action']) && $params['action'] == 'raw' ) {
802 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
803 $file = 'gen.' . $params['gen'];
805 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
806 // Clean up Monobook.css etc.
808 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
809 $file = $matches[1] . '.' . $matches[2];
812 $this->rawPages[$file] = array( $file, $title, $params );
813 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
816 if ( $url === false ) {
817 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
819 $url .= $this->compress ? ".gz" : "";
823 function getHashedFilename( &$title ) {
824 if ( '' != $title->mInterwiki ) {
825 $dbkey = $title->getDBkey();
827 $dbkey = $title->getPrefixedDBkey();
830 $mainPage = Title::newMainPage();
831 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
835 return $this->getHashedDirectory( $title ) . '/' .
836 $this->getFriendlyName( $dbkey ) . '.html';
839 function getFriendlyName( $name ) {
841 # Replace illegal characters for Windows paths with underscores
842 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
844 # Work out lower case form. We assume we're on a system with case-insensitive
845 # filenames, so unless the case is of a special form, we have to disambiguate
846 if ( function_exists( 'mb_strtolower' ) ) {
847 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
849 $lowerCase = ucfirst( strtolower( $name ) );
852 # Make it mostly unique
853 if ( $lowerCase != $friendlyName ) {
854 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
856 # Handle colon specially by replacing it with tilde
857 # Thus we reduce the number of paths with hashes appended
858 $friendlyName = str_replace( ':', '~', $friendlyName );
860 return $friendlyName;
864 * Get a relative directory for putting a title into
866 function getHashedDirectory( &$title ) {
867 if ( '' != $title->getInterwiki() ) {
868 $pdbk = $title->getDBkey();
870 $pdbk = $title->getPrefixedDBkey();
873 # Find the first colon if there is one, use characters after it
874 $p = strpos( $pdbk, ':' );
875 if ( $p !== false ) {
876 $dbk = substr( $pdbk, $p + 1 );
877 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
882 # Split into characters
884 preg_match_all( '/./us', $dbk, $m );
887 $length = count( $chars );
890 for ( $i = 0; $i < $this->depth; $i++ ) {
894 if ( $i >= $length ) {
898 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
899 if ( function_exists( 'mb_strtolower' ) ) {
900 $dir .= mb_strtolower( $c );
902 $dir .= strtolower( $c );
905 $dir .= sprintf( "%02X", ord( $c ) );
913 * Calculate the start end end of a job based on the current slice
914 * @param integer $start
915 * @param integer $end
916 * @return array of integers
918 function sliceRange( $start, $end ) {
919 $count = $end - $start + 1;
920 $each = $count / $this->sliceDenominator;
921 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
922 if ( $this->sliceNumerator == $this->sliceDenominator ) {
925 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
927 return array( $sliceStart, $sliceEnd );
931 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
932 * @param integer $start
933 * @param integer $base The true start of the range; the minimum start
935 function modSliceStart( $start, $base = 1 ) {
936 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
940 * Determine whether a string belongs to the current slice, based on hash
942 function sliceFilter( $s ) {
943 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
949 function onSiteNoticeBefore( &$text ) {
953 function onSiteNoticeAfter( &$text ) {
958 function getMaxPageID() {
959 if ( $this->maxPageID === false ) {
960 $dbr =& wfGetDB( DB_SLAVE );
961 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
963 return $this->maxPageID;
969 if ( !$this->udpProfile ) {
972 if ( !$this->udpProfileInit ) {
973 $this->udpProfileInit = true;
974 } elseif ( $this->udpProfileCounter == 1 % $this->udpProfile ) {
975 $wgProfiler->getFunctionReport();
976 $wgProfiler = new DumpHTML_ProfilerStub;
978 if ( $this->udpProfileCounter == 0 ) {
979 $wgProfiler = new ProfilerSimpleUDP;
980 $wgProfiler->setProfileID( 'dumpHTML' );
982 $this->udpProfileCounter = ( $this->udpProfileCounter + 1 ) % $this->udpProfile;
986 class DumpHTML_ProfilerStub {
987 function profileIn() {}
988 function profileOut() {}
989 function getOutput() {}
991 function getFunctionReport() {}
994 /** XML parser callback */
995 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
996 global $wgDumpImages;
998 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
999 $wgDumpImages[$attribs['SRC']] = true;
1003 /** XML parser callback */
1004 function wfDumpEndTagHandler( $parser, $name ) {}