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 && $title->getPrefixedDBkey() != $mainPage ) {
189 $this->doArticle( $title );
193 $this->setCheckpoint( 'article', 'done' );
197 function doSpecials() {
200 $this->setupGlobals();
201 print "Special:Categories...";
202 $this->doArticle( SpecialPage::getTitleFor( 'Categories' ) );
206 /** Write the main page as index.html */
207 function doMainPage() {
209 print "Making index.html ";
211 // Set up globals with no ../../.. in the link URLs
212 $this->setupGlobals( 0 );
214 $title = Title::newMainPage();
215 $text = $this->getArticleHTML( $title );
217 # Parse the XHTML to find the images
218 $images = $this->findImages( $text );
219 $this->copyImages( $images );
221 $file = fopen( "{$this->dest}/index.html", "w" );
223 print "\nCan't open index.html for writing\n";
226 fwrite( $file, $text );
231 function doImageDescriptions() {
232 $this->doLocalImageDescriptions();
233 if ( !$this->noSharedDesc ) {
234 $this->doSharedImageDescriptions();
239 * Dump image description pages that don't have an associated article, but do
242 function doLocalImageDescriptions() {
243 global $wgSharedUploadDirectory;
246 $dbr =& wfGetDB( DB_SLAVE );
248 $cp = $this->getCheckpoint( 'local image' );
249 if ( $cp == 'done' ) {
250 print "Local image descriptions already done\n";
252 } elseif ( $cp !== false ) {
253 print "Writing image description pages starting from $cp\n";
254 $conds = array( 'img_name >= ' . $dbr->addQuotes( $cp ) );
256 print "Writing image description pages for local images\n";
260 $this->setupGlobals();
264 $res = $dbr->select( 'image', array( 'img_name' ), $conds, __METHOD__,
265 array( 'ORDER BY' => 'img_name', 'LIMIT' => $chunkSize ) );
266 $numRows = $dbr->numRows( $res );
268 while ( $row = $dbr->fetchObject( $res ) ) {
269 # Update conds for the next chunk query
270 $conds = array( 'img_name > ' . $dbr->addQuotes( $row->img_name ) );
272 // Slice the result set with a filter
273 if ( !$this->sliceFilter( $row->img_name ) ) {
277 wfWaitForSlaves( 10 );
278 if ( !( ++$i % REPORTING_INTERVAL ) ) {
279 print "{$row->img_name}\n";
280 if ( $row->img_name !== 'done' ) {
281 $this->setCheckpoint( 'local image', $row->img_name );
284 $title = Title::makeTitle( NS_IMAGE, $row->img_name );
285 if ( $title->getArticleID() ) {
286 // Already done by dumpHTML
289 $this->doArticle( $title );
291 $dbr->freeResult( $res );
292 } while ( $numRows );
294 $this->setCheckpoint( 'local image', 'done' );
299 * Dump images which only have a real description page on commons
301 function doSharedImageDescriptions() {
302 list( $start, $end ) = $this->sliceRange( 0, 255 );
304 $cp = $this->getCheckpoint( 'shared image' );
305 if ( $cp == 'done' ) {
306 print "Shared description pages already done\n";
308 } elseif ( $cp !== false ) {
309 print "Writing description pages for commons images starting from directory $cp/255\n";
312 print "Writing description pages for commons images\n";
315 $this->setupGlobals();
317 for ( $hash = $start; $hash <= $end; $hash++ ) {
318 $this->setCheckpoint( 'shared image', $hash );
320 $dir = sprintf( "%s/%01x/%02x", $this->sharedStaticDirectory,
321 intval( $hash / 16 ), $hash );
322 $handle = @opendir( $dir );
323 while ( $handle && $file = readdir( $handle ) ) {
324 if ( $file[0] == '.' ) {
327 if ( !(++$i % REPORTING_INTERVAL ) ) {
331 $title = Title::makeTitleSafe( NS_IMAGE, $file );
332 $this->doArticle( $title );
338 $this->setCheckpoint( 'shared image', 'done' );
342 function doCategories() {
345 $this->setupGlobals();
346 $dbr =& wfGetDB( DB_SLAVE );
348 $cp = $this->getCheckpoint( 'category' );
349 if ( $cp == 'done' ) {
350 print "Category pages already done\n";
352 } elseif ( $cp !== false ) {
353 print "Resuming category page dump from $cp\n";
354 $conds = array( 'cl_to >= ' . $dbr->addQuotes( $cp ) );
356 print "Starting category pages\n";
362 $res = $dbr->select( 'categorylinks', 'DISTINCT cl_to', $conds, __METHOD__,
363 array( 'ORDER BY' => 'cl_to', 'LIMIT' => $chunkSize ) );
364 $numRows = $dbr->numRows( $res );
366 while ( $row = $dbr->fetchObject( $res ) ) {
367 // Set conditions for next chunk
368 $conds = array( 'cl_to > ' . $dbr->addQuotes( $row->cl_to ) );
370 // Filter pages from other slices
371 if ( !$this->sliceFilter( $row->cl_to ) ) {
375 wfWaitForSlaves( 10 );
376 if ( !(++$i % REPORTING_INTERVAL ) ) {
377 print "{$row->cl_to}\n";
378 if ( $row->cl_to != 'done' ) {
379 $this->setCheckpoint( 'category', $row->cl_to );
382 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
383 $this->doArticle( $title );
385 $dbr->freeResult( $res );
386 } while ( $numRows );
388 $this->setCheckpoint( 'category', 'done' );
392 function doRedirects() {
393 print "Doing redirects...\n";
396 $end = $this->getMaxPageID();
397 $cp = $this->getCheckpoint( 'redirect' );
398 if ( $cp == 'done' ) {
399 print "Redirects already done\n";
401 } elseif ( $cp !== false ) {
402 print "Resuming redirect generation from page_id $cp\n";
403 $start = intval( $cp );
408 $this->setupGlobals();
409 $dbr =& wfGetDB( DB_SLAVE );
412 for ( $chunkStart = $start; $chunkStart <= $end; $chunkStart += $chunkSize ) {
413 $chunkEnd = min( $end, $chunkStart + $chunkSize - 1 );
415 'page_is_redirect' => 1,
416 "page_id BETWEEN $chunkStart AND $chunkEnd"
418 # Modulo slicing in SQL
419 if ( $this->sliceDenominator != 1 ) {
420 $n = intval( $this->sliceNumerator );
421 $m = intval( $this->sliceDenominator );
422 $conds[] = "page_id % $m = $n";
424 $res = $dbr->select( 'page', array( 'page_id', 'page_namespace', 'page_title' ),
425 $conds, __METHOD__ );
427 while ( $row = $dbr->fetchObject( $res ) ) {
428 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
429 if ( !(++$i % (REPORTING_INTERVAL*10) ) ) {
430 printf( "Done %d redirects (%2.3f%%)\n", $i, $row->page_id / $end * 100 );
431 $this->setCheckpoint( 'redirect', $row->page_id );
433 $this->doArticle( $title );
435 $dbr->freeResult( $res );
437 $this->setCheckpoint( 'redirect', 'done' );
440 /** Write an article specified by title */
441 function doArticle( $title ) {
442 global $wgTitle, $wgSharedUploadPath, $wgSharedUploadDirectory;
443 global $wgUploadDirectory;
445 if ( $this->noOverwrite ) {
446 $fileName = $this->dest.'/'.$this->getHashedFilename( $title );
447 if ( file_exists( $fileName ) ) {
452 $this->rawPages = array();
453 $text = $this->getArticleHTML( $title );
455 if ( $text === false ) {
459 # Parse the XHTML to find the images
460 $images = $this->findImages( $text );
461 $this->copyImages( $images );
464 $this->writeArticle( $title, $text );
467 wfMkdirParents( "{$this->dest}/raw", 0755 );
468 foreach( $this->rawPages as $record ) {
469 list( $file, $title, $params ) = $record;
471 $path = "{$this->dest}/raw/$file";
472 if ( !file_exists( $path ) ) {
473 $article = new Article( $title );
474 $request = new FauxRequest( $params );
475 $rp = new RawPage( $article, $request );
476 $text = $rp->getRawText();
478 print "Writing $file\n";
479 $file = fopen( $path, 'w' );
481 print("Can't open file $fullName for writing\n");
484 fwrite( $file, $text );
490 /** Write the given text to the file identified by the given title object */
491 function writeArticle( &$title, $text ) {
492 $filename = $this->getHashedFilename( $title );
493 $fullName = "{$this->dest}/$filename";
494 $fullDir = dirname( $fullName );
496 if ( $this->compress ) {
498 $text = gzencode( $text, 9 );
501 wfMkdirParents( $fullDir, 0755 );
503 wfSuppressWarnings();
504 $file = fopen( $fullName, 'w' );
508 die("Can't open file '$fullName' for writing.\nCheck permissions or use another destination (-d).\n");
512 fwrite( $file, $text );
516 /** Set up globals required for parsing */
517 function setupGlobals( $currentDepth = NULL ) {
518 global $wgUser, $wgTitle, $wgStylePath, $wgArticlePath, $wgMathPath;
519 global $wgUploadPath, $wgLogo, $wgMaxCredits, $wgSharedUploadPath;
520 global $wgHideInterlanguageLinks, $wgUploadDirectory, $wgThumbnailScriptPath;
521 global $wgSharedThumbnailScriptPath, $wgEnableParserCache, $wgHooks, $wgServer;
522 global $wgRightsUrl, $wgRightsText, $wgCopyrightIcon, $wgEnableSidebarCache;
523 global $wgGenerateThumbnailOnParse;
525 static $oldLogo = NULL;
527 if ( !$this->setupDone ) {
528 $wgHooks['GetLocalURL'][] =& $this;
529 $wgHooks['GetFullURL'][] =& $this;
530 $wgHooks['SiteNoticeBefore'][] =& $this;
531 $wgHooks['SiteNoticeAfter'][] =& $this;
532 $this->oldArticlePath = $wgServer . $wgArticlePath;
535 if ( is_null( $currentDepth ) ) {
536 $currentDepth = $this->depth;
539 if ( $this->alternateScriptPath ) {
540 if ( $currentDepth == 0 ) {
543 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth - 1 );
546 $wgScriptPath = '..' . str_repeat( '/..', $currentDepth );
549 $wgArticlePath = str_repeat( '../', $currentDepth ) . '$1';
552 # Allow for repeated setup
553 if ( !is_null( $oldLogo ) ) {
559 if ( strpos( $wgLogo, $wgUploadPath ) === 0 ) {
560 # If it's in the upload directory, rewrite it to the new upload directory
561 $wgLogo = "$wgScriptPath/{$this->imageRel}/" . substr( $wgLogo, strlen( $wgUploadPath ) + 1 );
562 } elseif ( $wgLogo{0} == '/' ) {
563 # This is basically heuristic
564 # Rewrite an absolute logo path to one relative to the the script path
565 $wgLogo = $wgScriptPath . $wgLogo;
569 if ( !$this->setupDone ) {
570 $this->oldCopyrightIcon = $wgCopyrightIcon;
572 $wgCopyrightIcon = str_replace( 'src="/images',
573 'src="' . htmlspecialchars( $wgScriptPath ) . '/images', $this->oldCopyrightIcon );
575 $wgStylePath = "$wgScriptPath/skins";
576 $wgUploadPath = "$wgScriptPath/{$this->imageRel}";
577 $wgSharedUploadPath = "$wgUploadPath/shared";
579 $wgHideInterlanguageLinks = !$this->interwiki;
580 $wgThumbnailScriptPath = $wgSharedThumbnailScriptPath = false;
581 $wgEnableParserCache = false;
582 $wgMathPath = "$wgScriptPath/math";
583 $wgEnableSidebarCache = false;
584 $wgGenerateThumbnailOnParse = true;
586 if ( !empty( $wgRightsText ) ) {
587 $wgRightsUrl = "$wgScriptPath/COPYING.html";
591 $wgUser->setOption( 'skin', $this->skin );
592 $wgUser->setOption( 'editsection', 0 );
594 if ( $this->makeSnapshot ) {
595 $this->destUploadDirectory = "{$this->dest}/{$this->imageRel}";
596 if ( realpath( $this->destUploadDirectory == $wgUploadDirectory ) ) {
597 $this->makeSnapshot = false;
601 $this->sharedStaticDirectory = "{$this->destUploadDirectory}/shared";
603 $this->setupDone = true;
606 /** Reads the content of a title object, executes the skin and captures the result */
607 function getArticleHTML( &$title ) {
608 global $wgOut, $wgTitle, $wgArticle, $wgUser;
610 $linkCache =& LinkCache::singleton();
613 if ( is_null( $wgTitle ) ) {
617 $ns = $wgTitle->getNamespace();
618 if ( $ns == NS_SPECIAL ) {
619 $wgOut = new OutputPage;
620 $wgOut->setParserOptions( new ParserOptions );
621 SpecialPage::executePath( $wgTitle );
623 /** @todo merge with Wiki.php code */
624 if ( $ns == NS_IMAGE ) {
625 $wgArticle = new ImagePage( $wgTitle );
626 } elseif ( $ns == NS_CATEGORY ) {
627 $wgArticle = new CategoryPage( $wgTitle );
629 $wgArticle = new Article( $wgTitle );
631 $rt = Title::newFromRedirect( $wgArticle->fetchContent() );
633 return $this->getRedirect( $rt );
635 $wgOut = new OutputPage;
636 $wgOut->setParserOptions( new ParserOptions );
643 $sk =& $wgUser->getSkin();
645 $sk->outputPage( $wgOut );
646 $text = ob_get_contents();
652 function getRedirect( $rt ) {
653 $url = $rt->escapeLocalURL();
654 $text = $rt->getPrefixedText();
656 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
657 <html xmlns="http://www.w3.org/1999/xhtml">
659 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
660 <meta http-equiv="Refresh" content="0;url=$url" />
663 <p>Redirecting to <a href="$url">$text</a></p>
669 /** Returns image paths used in an XHTML document */
670 function findImages( $text ) {
671 global $wgOutputEncoding, $wgDumpImages;
672 $parser = xml_parser_create( $wgOutputEncoding );
673 xml_set_element_handler( $parser, 'wfDumpStartTagHandler', 'wfDumpEndTagHandler' );
675 $wgDumpImages = array();
676 xml_parse( $parser, $text );
677 xml_parser_free( $parser );
679 return $wgDumpImages;
683 * Copy a file specified by a URL to a given directory
685 * @param string $srcPath The source URL
686 * @param string $srcPathBase The base directory of the source URL
687 * @param string $srcDirBase The base filesystem directory of the source URL
688 * @param string $destDirBase The base filesystem directory of the destination URL
690 function relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
691 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 ); // +1 for slash
692 $sourceLoc = "$srcDirBase/$rel";
693 $destLoc = "$destDirBase/$rel";
694 #print "Copying $sourceLoc to $destLoc\n";
695 if ( !file_exists( $destLoc ) ) {
696 wfMkdirParents( dirname( $destLoc ), 0755 );
697 if ( function_exists( 'symlink' ) && !$this->forceCopy ) {
698 symlink( $sourceLoc, $destLoc );
700 copy( $sourceLoc, $destLoc );
706 * Copy an image, and if it is a thumbnail, copy its parent image too
708 function copyImage( $srcPath, $srcPathBase, $srcDirBase, $destDirBase ) {
709 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath;
710 $this->relativeCopy( $srcPath, $srcPathBase, $srcDirBase, $destDirBase );
711 if ( substr( $srcPath, strlen( $srcPathBase ) + 1, 6 ) == 'thumb/' ) {
712 # The image was a thumbnail
713 # Copy the source image as well
714 $rel = substr( $srcPath, strlen( $srcPathBase ) + 1 );
715 $parts = explode( '/', $rel );
716 $rel = "{$parts[1]}/{$parts[2]}/{$parts[3]}";
717 $newSrc = "$srcPathBase/$rel";
718 $this->relativeCopy( $newSrc, $srcPathBase, $srcDirBase, $destDirBase );
723 * Copy images (or create symlinks) from commons to a static directory.
724 * This is necessary even if you intend to distribute all of commons, because
725 * the directory contents is used to work out which image description pages
728 * Also copies math images, and full-sized images if the makeSnapshot option
732 function copyImages( $images ) {
733 global $wgUploadPath, $wgUploadDirectory, $wgSharedUploadPath, $wgSharedUploadDirectory,
734 $wgMathPath, $wgMathDirectory;
735 # Find shared uploads and copy them into the static directory
736 $sharedPathLength = strlen( $wgSharedUploadPath );
737 $mathPathLength = strlen( $wgMathPath );
738 $uploadPathLength = strlen( $wgUploadPath );
739 foreach ( $images as $escapedImage => $dummy ) {
740 $image = urldecode( $escapedImage );
742 if ( substr( $image, 0, $sharedPathLength ) == $wgSharedUploadPath ) {
743 $this->copyImage( $image, $wgSharedUploadPath, $wgSharedUploadDirectory, $this->sharedStaticDirectory );
744 } elseif ( substr( $image, 0, $mathPathLength ) == $wgMathPath ) {
745 $this->relativeCopy( $image, $wgMathPath, $wgMathDirectory, "{$this->dest}/math" );
746 } elseif ( $this->makeSnapshot && substr( $image, 0, $uploadPathLength ) == $wgUploadPath ) {
747 $this->copyImage( $image, $wgUploadPath, $wgUploadDirectory, $this->destUploadDirectory );
752 function onGetFullURL( &$title, &$url, $query ) {
753 global $wgContLang, $wgArticlePath;
755 $iw = $title->getInterwiki();
756 if ( $title->isExternal() && $wgContLang->getLanguageName( $iw ) ) {
757 if ( $title->getDBkey() == '' ) {
758 $url = str_replace( '$1', "../$iw/index.html", $wgArticlePath );
760 $url = str_replace( '$1', "../$iw/" . wfUrlencode( $this->getHashedFilename( $title ) ),
763 $url .= $this->compress ? ".gz" : "";
770 function onGetLocalURL( &$title, &$url, $query ) {
771 global $wgArticlePath;
773 if ( $title->isExternal() ) {
774 # Default is fine for interwiki
779 if ( $query != '' ) {
780 parse_str( $query, $params );
781 if ( isset($params['action']) && $params['action'] == 'raw' ) {
782 if ( $params['gen'] == 'css' || $params['gen'] == 'js' ) {
783 $file = 'gen.' . $params['gen'];
785 $file = $this->getFriendlyName( $title->getPrefixedDBkey() );
786 // Clean up Monobook.css etc.
787 if ( preg_match( '/^(.*)\.(css|js)_[0-9a-f]{4}$/', $file, $matches ) ) {
788 $file = $matches[1] . '.' . $matches[2];
791 $this->rawPages[$file] = array( $file, $title, $params );
792 $url = str_replace( '$1', "raw/" . wfUrlencode( $file ), $wgArticlePath );
795 if ( $url === false ) {
796 $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename( $title ) ), $wgArticlePath );
798 $url .= $this->compress ? ".gz" : "";
802 function getHashedFilename( &$title ) {
803 if ( '' != $title->mInterwiki ) {
804 $dbkey = $title->getDBkey();
806 $dbkey = $title->getPrefixedDBkey();
809 $mainPage = Title::newMainPage();
810 if ( $mainPage->getPrefixedDBkey() == $dbkey ) {
814 return $this->getHashedDirectory( $title ) . '/' .
815 $this->getFriendlyName( $dbkey ) . '.html';
818 function getFriendlyName( $name ) {
820 # Replace illegal characters for Windows paths with underscores
821 $friendlyName = strtr( $name, '/\\*?"<>|~', '_________' );
823 # Work out lower case form. We assume we're on a system with case-insensitive
824 # filenames, so unless the case is of a special form, we have to disambiguate
825 if ( function_exists( 'mb_strtolower' ) ) {
826 $lowerCase = $wgLang->ucfirst( mb_strtolower( $name ) );
828 $lowerCase = ucfirst( strtolower( $name ) );
831 # Make it mostly unique
832 if ( $lowerCase != $friendlyName ) {
833 $friendlyName .= '_' . substr(md5( $name ), 0, 4);
835 # Handle colon specially by replacing it with tilde
836 # Thus we reduce the number of paths with hashes appended
837 $friendlyName = str_replace( ':', '~', $friendlyName );
839 return $friendlyName;
843 * Get a relative directory for putting a title into
845 function getHashedDirectory( &$title ) {
846 if ( '' != $title->getInterwiki() ) {
847 $pdbk = $title->getDBkey();
849 $pdbk = $title->getPrefixedDBkey();
852 # Find the first colon if there is one, use characters after it
853 $p = strpos( $pdbk, ':' );
854 if ( $p !== false ) {
855 $dbk = substr( $pdbk, $p + 1 );
856 $dbk = substr( $dbk, strspn( $dbk, '_' ) );
861 # Split into characters
862 preg_match_all( '/./us', $dbk, $m );
865 $length = count( $chars );
868 for ( $i = 0; $i < $this->depth; $i++ ) {
872 if ( $i >= $length ) {
876 if ( ord( $c ) >= 128 || preg_match( '/[a-zA-Z0-9!#$%&()+,[\]^_`{}-]/', $c ) ) {
877 if ( function_exists( 'mb_strtolower' ) ) {
878 $dir .= mb_strtolower( $c );
880 $dir .= strtolower( $c );
883 $dir .= sprintf( "%02X", ord( $c ) );
891 * Calculate the start end end of a job based on the current slice
892 * @param integer $start
893 * @param integer $end
894 * @return array of integers
896 function sliceRange( $start, $end ) {
897 $count = $end - $start + 1;
898 $each = $count / $this->sliceDenominator;
899 $sliceStart = $start + intval( $each * ( $this->sliceNumerator - 1 ) );
900 if ( $this->sliceNumerator == $this->sliceDenominator ) {
903 $sliceEnd = $start + intval( $each * $this->sliceNumerator ) - 1;
905 return array( $sliceStart, $sliceEnd );
909 * Adjust a start point so that it belongs to the current slice, where slices are defined by integer modulo
910 * @param integer $start
911 * @param integer $base The true start of the range; the minimum start
913 function modSliceStart( $start, $base = 1 ) {
914 return $start - ( $start % $this->sliceDenominator ) + $this->sliceNumerator - 1 + $base;
918 * Determine whether a string belongs to the current slice, based on hash
920 function sliceFilter( $s ) {
921 return crc32( $s ) % $this->sliceDenominator == $this->sliceNumerator - 1;
927 function onSiteNoticeBefore( &$text ) {
931 function onSiteNoticeAfter( &$text ) {
936 function getMaxPageID() {
937 if ( $this->maxPageID === false ) {
938 $dbr =& wfGetDB( DB_SLAVE );
939 $this->maxPageID = $dbr->selectField( 'page', 'max(page_id)', false, __METHOD__ );
941 return $this->maxPageID;
946 /** XML parser callback */
947 function wfDumpStartTagHandler( $parser, $name, $attribs ) {
948 global $wgDumpImages;
950 if ( $name == 'IMG' && isset( $attribs['SRC'] ) ) {
951 $wgDumpImages[$attribs['SRC']] = true;
955 /** XML parser callback */
956 function wfDumpEndTagHandler( $parser, $name ) {}