+ /* Generates a HTML-formatted table of contents which links to individual sections
+ from the wikisource. Used for collapsing long pages.
+ */
+ /* static */ function getTocFromSource( $text ) {
+ #$numMatches = preg_match_all( "/<H([1-6])(.*?" . ">)(.*?)<\/H[1-6]>/i", $text, $matches );
+
+ global $wgUser,$wgInputEncoding,$wgTitle,$wgOut,$wgParser;
+ $sk = $wgUser->getSkin();
+
+ $striparray=array();
+ $oldtype=$wgParser->mOutputType;
+ $wgParser->mOutputType=OT_WIKI;
+ $text=$wgParser->strip($text, $striparray, true);
+ $wgParser->mOutputType=$oldtype;
+
+ $numMatches = preg_match_all( "/^(=+)(.*?)=+|^<h([1-6]).*?>(.*?)<\/h[1-6].*?>/mi",$text,$matches);
+
+ # no headings: text cannot be collapsed
+ if( $numMatches = 0 ) {
+ return "";
+ }
+
+ # We combine the headlines into a bundle and convert them to HTML
+ # in order to make stripping out the wikicrap easier.
+ $combined=implode("!@@@!",$matches[2]);
+ $myout=$wgParser->parse($combined,$wgTitle,$wgOut->mParserOptions);
+ $combined_html=$myout->getText();
+ $headlines=array();
+ $headlines=explode("!@@@!",$combined_html);
+ # headline counter
+ $headlineCount = 0;
+ $toclevel = 0;
+ $toc = "";
+ $full = "";
+ $head = array();
+ $sublevelCount = array();
+ $level = 0;
+ $prevlevel = 0;
+ foreach( $headlines as $headline ) {
+ $numbering = "";
+ if( $level ) {
+ $prevlevel = $level;
+ }
+ $level = $matches[1][$headlineCount];
+
+ # wikisource headings need to be converted into numbers
+ # =foo= equals <h1>foo</h1>, ==foo== equals <h2>foo</h2> etc.
+ if(strpos($level,"=")!==false) {
+ $level=strlen($level);
+ }
+
+ if( $prevlevel && $level > $prevlevel ) {
+ # reset when we enter a new level
+ $sublevelCount[$level] = 0;
+ $toc .= $sk->tocIndent( $level - $prevlevel );
+ $toclevel += $level - $prevlevel;
+ }
+ if( $level < $prevlevel ) {
+ # reset when we step back a level
+ $sublevelCount[$level+1]=0;
+ $toc .= $sk->tocUnindent( $prevlevel - $level );
+ $toclevel -= $prevlevel - $level;
+ }
+ # count number of headlines for each level
+ @$sublevelCount[$level]++;
+ $dot = 0;
+ for( $i = 1; $i <= $level; $i++ ) {
+ if( !empty( $sublevelCount[$i] ) ) {
+ if( $dot ) {
+ $numbering .= ".";
+ }
+ $numbering .= $sublevelCount[$i];
+ $dot = 1;
+ }
+ }
+
+
+ # The canonized header is a version of the header text safe to use for links
+ # Avoid insertion of weird stuff like <math> by expanding the relevant sections
+ $state=array();
+ $canonized_headline = Parser::unstrip( $headline, $state);
+
+ # strip out HTML
+ $canonized_headline = preg_replace( "/<.*?" . ">/","",$canonized_headline );
+ $tocline = trim( $canonized_headline );
+ $canonized_headline = preg_replace("/[ \\?&\\/<>\\(\\)\\[\\]=,+']+/", '_', urlencode( do_html_entity_decode( $tocline, ENT_COMPAT, $wgInputEncoding ) ) );
+ $refer[$headlineCount] = $canonized_headline;
+
+ # count how many in assoc. array so we can track dupes in anchors
+ @$refers[$canonized_headline]++;
+ $refcount[$headlineCount]=$refers[$canonized_headline];
+ $tocline = $numbering . " " . $tocline;
+
+ # Create the anchor for linking from the TOC to the section
+ $anchor = $canonized_headline;
+
+ if($refcount[$headlineCount] > 1 ) {
+ $anchor .= "_" . $refcount[$headlineCount];
+ }
+ $headlineCount++;
+ $toc .= $sk->tocLine($anchor,$tocline,$toclevel,$headlineCount);
+ }
+ $toclines = $headlineCount;
+ $toc .= $sk->tocUnindent( $toclevel );
+ $toc = $sk->tocTable( $toc );
+ return $toc;
+
+ }