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;
73 function DumpHTML( $settings = array() ) {
74 foreach ( $settings as $var => $value ) {
79 function loadCheckpoints() {
80 if ( $this->checkpoints !== false ) {
82 } elseif ( !$this->checkpointFile ) {
85 $lines = @file( $this->checkpointFile );
86 if ( $lines === false ) {
87 print "Starting new checkpoint file \"{$this->checkpointFile}\"\n";
88 $this->checkpoints = array();
90 $lines = array_map( 'trim', $lines );
91 $this->checkpoints = array();
92 foreach ( $lines as $line ) {
93 list( $name, $value ) = explode( '=', $line, 2 );
94 $this->checkpoints[$name] = $value;
101 function getCheckpoint( $type, $defValue = false ) {
102 if ( !$this->loadCheckpoints() ) {
105 if ( !isset( $this->checkpoints[$type] ) ) {
108 return $this->checkpoints[$type];
112 function setCheckpoint( $type, $value ) {
113 if ( !$this->checkpointFile ) {
116 $this->checkpoints[$type] = $value;
118 foreach ( $this->checkpoints as $type => $value ) {
119 $blob .= "$type=$value\n";
121 file_put_contents( $this->checkpointFile, $blob );
124 function doEverything() {
125 if ( $this->getCheckpoint( 'everything' ) == 'done' ) {
126 print "Checkpoint says everything is already done\n";
130 $this->doCategories();
131 $this->doRedirects();
132 if ( $this->sliceNumerator == 1 ) {
135 $this->doLocalImageDescriptions();
137 if ( !$this->noSharedDesc ) {
138 $this->doSharedImageDescriptions();
141 $this->setCheckpoint( 'everything', 'done' );
145 * Write a set of articles specified by start and end page_id
146 * Skip categories and images, they will be done separately
148 function doArticles() {
149 if ( $this->endID === false ) {
150 $end = $this->getMaxPageID();
154 $start = $this->startID;
156 # Start from the checkpoint
157 $cp = $this->getCheckpoint( 'article' );
158 if ( $cp == 'done' ) {
159 print "Articles already done\n";
161 } elseif ( $cp !== false ) {
163 print "Resuming article dump from checkpoint at page_id $start of $end\n";
165 print "Starting from page_id $start of $end\n";
168 # Move the start point to the correct slice if it isn't there already
169 $start = $this->modSliceStart( $start );
171 $this->setupGlobals();
173 $mainPageObj = Title::newMainPage();
174 $mainPage = $mainPageObj->getPrefixedDBkey();
176 for ( $id = $start, $i = 0; $id <= $end; $id += $this->sliceDenominator, $i++ ) {
177 wfWaitForSlaves( 20 );
178 if ( !( $i % REPORTING_INTERVAL) ) {
179 print "Processing ID: $id\r";
180 $this->setCheckpoint( 'article', $id );
182 if ( !($i % (REPORTING_INTERVAL*10) ) ) {
185 $title = Title::newFromID( $id );
187 $ns = $title->getNamespace() ;
188 if ( $ns != NS_CATEGORY && $ns != NS_MEDIAWIKI &&
189 $title->getPrefixedDBkey() != $mainPage ) {
190 $this->doArticle( $title );
194 $this->setCheckpoint( 'article', 'done' );
198 function doSpecials() {
201 $this->setupGlobals();
202 print "Special:Categories...";
203 $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
207 /** Write the main page as index.html */
208 function doMainPage() {
210 print "Making index.html ";
212 // Set up globals with no ../../.. in the link URLs
213 $this->setupGlobals( 0 );
215 $title = Title::newMainPage();
216 $text = $this->getArticleHTML( $title );
218 # Parse the XHTML to find the images
219 $images = $this->findImages( $text );
220 $this->copyImages( $images );
222 $file = fopen( "{$this->dest}/index.html", "w" );
224 print "\nCan't open index.html for writing\n";
227 fwrite( $file, $text );
232 function doImageDescriptions() {
233 $this->doLocalImageDescriptions();
234 if ( !$this->noSharedDesc ) {
235 $this->doSharedImageDescriptions();
240 * Dump image description pages that don't have an associated article, but do
243 function doLocalImageDescriptions() {
244 global $wgSharedUploadDirectory;
247 $dbr =& wfGetDB( DB_SLAVE );
249 $cp = $this->getCheckpoint( 'local image' );
250 if ( $cp == 'done' ) {
251 print "Local image descriptions already done\n";
253 } elseif ( $cp !== false ) {
254 print "Writing image description pages starting from $cp\n";
255 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
257 print "Writing image description pages for local images\n";
261 $this->setupGlobals();
265 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
266 array( 'ORDER BY' => 'img_name', 'LIMIT' => $chunkSize ) );
267 $numRows = $dbr->numRows( $res );
269 while ( $row = $dbr->fetchObject( $res ) ) {
270 # Update conds for the next chunk query
271 $conds = array( 'img_name > ' . $dbr->addQuotes( $row->img_name ) );
273 // Slice the result set with a filter
274 if ( !$this->sliceFilter( $row->img_name ) ) {
278 wfWaitForSlaves( 10 );
279 if ( !( ++$i % REPORTING_INTERVAL ) ) {
280 print "{$row->img_name}\n";
281 if ( $row->img_name !== 'done' ) {
282 $this->setCheckpoint( 'local image', $row->img_name );
285 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
286 if ( $title->getArticleID() ) {
287 // Already done by dumpHTML
290 $this->doArticle( $title );
292 $dbr->freeResult( $res );
293 } while ( $numRows );
295 $this->setCheckpoint( 'local image', 'done' );
300 * Dump images which only have a real description page on commons
302 function doSharedImageDescriptions() {
303 list( $start, $end ) = $this->sliceRange( 0, 255 );
305 $cp = $this->getCheckpoint( 'shared image' );
306 if ( $cp == 'done' ) {
307 print "Shared description pages already done\n";
309 } elseif ( $cp !== false ) {
310 print "Writing description pages for commons images starting from directory $cp/255\n";
313 print "Writing description pages for commons images\n";
316 $this->setupGlobals();
318 for ( $hash = $start; $hash <= $end; $hash++ ) {
319 $this->setCheckpoint( 'shared image', $hash );
321 $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
322 intval( $hash / 16 ), $hash );
323 $handle = @opendir( $dir );
324 while ( $handle && $file = readdir( $handle ) ) {
325 if ( $file[0] == '.' ) {
328 if ( !(++$i % REPORTING_INTERVAL ) ) {
332 $title = Title::makeTitleSafe( NS_IMAGE, $file );
333 $this->doArticle( $title );
339 $this->setCheckpoint( 'shared image', 'done' );
343 function doCategories() {
346 $this->setupGlobals();
347 $dbr =& wfGetDB( DB_SLAVE );
349 $cp = $this->getCheckpoint( 'category' );
350 if ( $cp == 'done' ) {
351 print "Category pages already done\n";
353 } elseif ( $cp !== false ) {
354 print "Resuming category page dump from $cp\n";
355 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
357 print "Starting category pages\n";
363 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
364 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
365 $numRows = $dbr->numRows( $res );
367 while ( $row = $dbr->fetchObject( $res ) ) {
368 // Set conditions for next chunk
369 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
371 // Filter pages from other slices
372 if ( !$this->sliceFilter( $row->cl_to ) ) {
376 wfWaitForSlaves( 10 );
377 if ( !(++$i % REPORTING_INTERVAL ) ) {
378 print "{$row->cl_to}\n";
379 if ( $row->cl_to != 'done' ) {
380 $this->setCheckpoint( 'category', $row->cl_to );
383 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
384 $this->doArticle( $title );
386 $dbr->freeResult( $res );
387 } while ( $numRows );
389 $this->setCheckpoint( 'category', 'done' );
393 function doRedirects() {
394 print "Doing redirects...\n";
397 $end = $this->getMaxPageID();
398 $cp = $this->getCheckpoint( 'redirect' );
399 if ( $cp == 'done' ) {
400 print "Redirects already done\n";
402 } elseif ( $cp !== false ) {
403 print "Resuming redirect generation from page_id $cp\n";
404 $start = intval( $cp );
409 $this->setupGlobals();
410 $dbr =& wfGetDB( DB_SLAVE );
413 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
414 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
416 'page_is_redirect' => 1,
417 "page_id BETWEEN $chunkStart AND $chunkEnd"
419 # Modulo slicing in SQL
420 if ( $this->sliceDenominator != 1 ) {
421 $n = intval( $this->sliceNumerator );
422 $m = intval( $this->sliceDenominator );
423 $conds[] = "page_id % $m = $n";
425 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
426 $conds, __METHOD__ );
428 while ( $row = $dbr->fetchObject( $res ) ) {
429 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
430 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
431 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
432 $this->setCheckpoint( 'redirect', $row->page_id );
434 $this->doArticle( $title );
436 $dbr->freeResult( $res );
438 $this->setCheckpoint( 'redirect', 'done' );
441 /** Write an article specified by title */
442 function doArticle( $title ) {
443 global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
444 global $wgUploadDirectory;
446 if ( $this->noOverwrite ) {
447 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
448 if ( file_exists( $fileName ) ) {
453 $this->rawPages = array();
454 $text = $this->getArticleHTML( $title );
456 if ( $text === false ) {
460 # Parse the XHTML to find the images
461 $images = $this->findImages( $text );
462 $this->copyImages( $images );
465 $this->writeArticle( $title, $text );
468 wfMkdirParents( "{$this->dest}/raw", 0755 );
469 foreach( $this->rawPages as $record ) {
470 list( $file, $title, $params ) = $record;
472 $path = "{$this->dest}/raw/$file";
473 if ( !file_exists( $path ) ) {
474 $article = new Article( $title );
475 $request = new FauxRequest( $params );
476 $rp = new RawPage( $article, $request );
477 $text = $rp->getRawText();
479 print "Writing $file\n";
480 $file = fopen( $path, 'w' );
482 print("Can't open file $fullName for writing\n");
485 fwrite( $file, $text );
490 wfIncrStats( 'dumphtml_article' );
493 /** Write the given text to the file identified by the given title object */
494 function writeArticle( &$title, $text ) {
495 $filename = $this->getHashedFilename( $title );
497 # Temporary hack for current dump, this should be moved to
498 # getFriendlyName() at the earliest opportunity.
500 # Limit filename length to 255 characters, so it works on ext3.
501 # Titles are in fact limited to 255 characters, but dumpHTML
502 # adds a suffix which may put them over the limit.
503 $length = strlen( $filename );
504 if ( $length > 255 ) {
505 print "Warning: Filename too long ($length bytes). Skipping.\n";
509 $fullName = "{$this->dest}/$filename";
510 $fullDir = dirname( $fullName );
512 if ( $this->compress ) {
514 $text = gzencode( $text, 9 );
517 wfMkdirParents( $fullDir, 0755 );
519 wfSuppressWarnings();
520 $file = fopen( $fullName, 'w' );
524 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
528 fwrite( $file, $text );
532 /** Set up globals required for parsing */
533 function setupGlobals( $currentDepth = NULL ) {
534 global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
535 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
536 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
537 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
538 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
539 global $wgGenerateThumbnailOnParse;
541 static $oldLogo = NULL;
543 if ( !$this->setupDone ) {
544 $wgHooks['GetLocalURL'][] =& $this;
545 $wgHooks['GetFullURL'][] =& $this;
546 $wgHooks['SiteNoticeBefore'][] =& $this;
547 $wgHooks['SiteNoticeAfter'][] =& $this;
548 $this->oldArticlePath = $wgServer . $wgArticlePath;
551 if ( is_null( $currentDepth ) ) {
552 $currentDepth = $this->depth;
555 if ( $this->alternateScriptPath ) {
556 if ( $currentDepth == 0 ) {
559 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
562 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
565 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
568 # Allow for repeated setup
569 if ( !is_null( $oldLogo ) ) {
575 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
576 # If it's in the upload directory, rewrite it to the new upload directory
577 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
578 } elseif ( $wgLogo{0} == '/' ) {
579 # This is basically heuristic
580 # Rewrite an absolute logo path to one relative to the the script path
581 $wgLogo = $wgScriptPath . $wgLogo;
585 if ( !$this->setupDone ) {
586 $this->oldCopyrightIcon = $wgCopyrightIcon;
588 $wgCopyrightIcon = str_replace( 'src="/images',
589 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
591 $wgStylePath = "$wgScriptPath/skins";
592 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
593 $wgSharedUploadPath = "$wgUploadPath/shared";
595 $wgHideInterlanguageLinks = !$this->interwiki;
596 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
597 $wgEnableParserCache = false;
598 $wgMathPath = "$wgScriptPath/math";
599 $wgEnableSidebarCache = false;
600 $wgGenerateThumbnailOnParse = true;
602 if ( !empty( $wgRightsText ) ) {
603 $wgRightsUrl = "$wgScriptPath/COPYING.html";
607 $wgUser->setOption( 'skin', $this->skin );
608 $wgUser->setOption( 'editsection', 0 );
610 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
611 if ( realpath( $this->destUploadDirectory ) == realpath( $wgUploadDirectory ) ) {
612 print "Disabling image snapshot because the destination is the same as the source\n";
613 $this->makeSnapshot = false;
615 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
617 $this->setupDone = true;
620 /** Reads the content of a title object, executes the skin and captures the result */
621 function getArticleHTML( &$title ) {
622 global $wgOut, $wgTitle, $wgArticle, $wgUser;
624 $linkCache =& LinkCache::singleton();
627 if ( is_null( $wgTitle ) ) {
631 $ns = $wgTitle->getNamespace();
632 if ( $ns == NS_SPECIAL ) {
633 $wgOut = new OutputPage;
634 $wgOut->setParserOptions( new ParserOptions );
635 SpecialPage::executePath( $wgTitle );
637 /** @todo merge with Wiki.php code */
638 if ( $ns == NS_IMAGE ) {
639 $wgArticle = new ImagePage( $wgTitle );
640 } elseif ( $ns == NS_CATEGORY ) {
641 $wgArticle = new CategoryPage( $wgTitle );
643 $wgArticle = new Article( $wgTitle );
645 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
647 return $this->getRedirect( $rt );
649 $wgOut = new OutputPage;
650 $wgOut->setParserOptions( new ParserOptions );
657 $sk =& $wgUser->getSkin();
659 $sk->outputPage( $wgOut );
660 $text = ob_get_contents();
666 function getRedirect( $rt ) {
667 $url = $rt->escapeLocalURL();
668 $text = $rt->getPrefixedText();
670 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
671 <html xmlns="http://www.w3.org/1999/xhtml">
673 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
674 <meta http-equiv="Refresh" content="0;url=$url" />
677 <p>Redirecting to <a href="$url">$text</a></p>
683 /** Returns image paths used in an XHTML document */
684 function findImages( $text ) {
685 global $wgOutputEncoding, $wgDumpImages;
686 $parser = xml_parser_create( $wgOutputEncoding );
687 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
689 $wgDumpImages = array();
690 xml_parse( $parser, $text );
691 xml_parser_free( $parser );
693 return $wgDumpImages;
697 * Copy a file specified by a URL to a given directory
699 * @param string $srcPath The source URL
700 * @param string $srcPathBase The base directory of the source URL
701 * @param string $srcDirBase The base filesystem directory of the source URL
702 * @param string $destDirBase The base filesystem directory of the destination URL
704 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
705 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
706 $sourceLoc = "$srcDirBase/$rel";
707 $destLoc = "$destDirBase/$rel";
708 #print "Copying $sourceLoc to $destLoc\n";
709 if ( !file_exists( $destLoc ) ) {
710 wfMkdirParents( dirname( $destLoc ), 0755 );
711 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
712 if ( !symlink( $sourceLoc, $destLoc ) ) {
713 print "Warning: unable to create symlink at $destLoc\n";
716 if ( !copy( $sourceLoc, $destLoc ) ) {
717 print "Warning: unable to copy $sourceLoc to $destLoc\n";
724 * Copy an image, and if it is a thumbnail, copy its parent image too
726 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
727 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
728 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
729 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
730 # The image was a thumbnail
731 # Copy the source image as well
732 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
733 $parts = explode( '/', $rel );
734 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
735 $newSrc = "$srcPathBase/$rel";
736 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
741 * Copy images (or create symlinks) from commons to a static directory.
742 * This is necessary even if you intend to distribute all of commons, because
743 * the directory contents is used to work out which image description pages
746 * Also copies math images, and full-sized images if the makeSnapshot option
750 function copyImages( $images ) {
751 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
752 $wgMathPath, $wgMathDirectory;
753 # Find shared uploads and copy them into the static directory
754 $sharedPathLength = strlen( $wgSharedUploadPath );
755 $mathPathLength = strlen( $wgMathPath );
756 $uploadPathLength = strlen( $wgUploadPath );
757 foreach ( $images as $escapedImage => $dummy ) {
758 $image = urldecode( $escapedImage );
760 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
761 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
762 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
763 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
764 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
765 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
770 function onGetFullURL( &$title, &$url, $query ) {
771 global $wgContLang, $wgArticlePath;
773 $iw = $title->getInterwiki();
774 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
775 if ( $title->getDBkey() == '' ) {
776 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
778 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
781 $url .= $this->compress ? ".gz" : "";
788 function onGetLocalURL( &$title, &$url, $query ) {
789 global $wgArticlePath;
791 if ( $title->isExternal() ) {
792 # Default is fine for interwiki
797 if ( $query != '' ) {
798 parse_str( $query, $params );
799 if ( isset($params['action']) && $params['action'] == 'raw' ) {
800 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
801 $file = 'gen.' . $params['gen'];
803 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
804 // Clean up Monobook.css etc.
805 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
806 $file = $matches[1] . '.' . $matches[2];
809 $this->rawPages[$file] = array( $file, $title, $params );
810 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
813 if ( $url === false ) {
814 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
816 $url .= $this->compress ? ".gz" : "";
820 function getHashedFilename( &$title ) {
821 if ( '' != $title->mInterwiki ) {
822 $dbkey = $title->getDBkey();
824 $dbkey = $title->getPrefixedDBkey();
827 $mainPage = Title::newMainPage();
828 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
832 return $this->getHashedDirectory( $title ) . '/' .
833 $this->getFriendlyName( $dbkey ) . '.html';
836 function getFriendlyName( $name ) {
838 # Replace illegal characters for Windows paths with underscores
839 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
841 # Work out lower case form. We assume we're on a system with case-insensitive
842 # filenames, so unless the case is of a special form, we have to disambiguate
843 if ( function_exists( 'mb_strtolower' ) ) {
844 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
846 $lowerCase = ucfirst( strtolower( $name ) );
849 # Make it mostly unique
850 if ( $lowerCase != $friendlyName ) {
851 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
853 # Handle colon specially by replacing it with tilde
854 # Thus we reduce the number of paths with hashes appended
855 $friendlyName = str_replace( ':', '~', $friendlyName );
857 return $friendlyName;
861 * Get a relative directory for putting a title into
863 function getHashedDirectory( &$title ) {
864 if ( '' != $title->getInterwiki() ) {
865 $pdbk = $title->getDBkey();
867 $pdbk = $title->getPrefixedDBkey();
870 # Find the first colon if there is one, use characters after it
871 $p = strpos( $pdbk, ':' );
872 if ( $p !== false ) {
873 $dbk = substr( $pdbk, $p + 1 );
874 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
879 # Split into characters
880 preg_match_all( '/./us', $dbk, $m );
883 $length = count( $chars );
886 for ( $i = 0; $i < $this->depth; $i++ ) {
890 if ( $i >= $length ) {
894 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
895 if ( function_exists( 'mb_strtolower' ) ) {
896 $dir .= mb_strtolower( $c );
898 $dir .= strtolower( $c );
901 $dir .= sprintf( "%02X", ord( $c ) );
909 * Calculate the start end end of a job based on the current slice
910 * @param integer $start
911 * @param integer $end
912 * @return array of integers
914 function sliceRange( $start, $end ) {
915 $count = $end - $start + 1;
916 $each = $count / $this->sliceDenominator;
917 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
918 if ( $this->sliceNumerator == $this->sliceDenominator ) {
921 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
923 return array( $sliceStart, $sliceEnd );
927 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
928 * @param integer $start
929 * @param integer $base The true start of the range; the minimum start
931 function modSliceStart( $start, $base = 1 ) {
932 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
936 * Determine whether a string belongs to the current slice, based on hash
938 function sliceFilter( $s ) {
939 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
945 function onSiteNoticeBefore( &$text ) {
949 function onSiteNoticeAfter( &$text ) {
954 function getMaxPageID() {
955 if ( $this->maxPageID === false ) {
956 $dbr =& wfGetDB( DB_SLAVE );
957 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
959 return $this->maxPageID;
964 /** XML parser callback */
965 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
966 global $wgDumpImages;
968 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
969 $wgDumpImages[$attribs['SRC']] = true;
973 /** XML parser callback */
974 function wfDumpEndTagHandler( $parser, $name ) {}