User:Nigelk/ConcatPages

 
A proposal to move this page to MediaWiki.org was rejected.

See the [demonstration]. this version works with navmaps, and has only been tested very lightly with mw 1.5.5 on php 5.0.4.

to use this, you must have the NavMap Extension 0.6 or later.

<?php

$wgExtensionFunctions[] = "wfExtensionConcatPages";

$wgExtensionCredits['other'][] = array(
				       'name' => 'SpecialConcatPages',
				       'description' => 'Concatenates one or more pages, either from a sequence of a NavMap, or from an list of unrelated pages',
				       'url' => 'http://meta.wikimedia.org/wiki/User:Nigelk/ConcatPages',
				       'author' => 'Nigel Kerr',
				       'version'=>'0.1');

require_once("$IP/includes/SpecialPage.php");

class SpecialConcatPages extends SpecialPage {
  function SpecialConcatPages() {
    SpecialPage::SpecialPage('ConcatPages');
  }

  function removeNavMapLinks( $text, $map = '[^<>]+') {
    $pattern = '/<nav(map)?( [^>]*)?>\\s*' . preg_quote($map) . '\\s*<\\/nav(map)?>/s';
    $repl = '';
    return preg_replace( $pattern, $repl, $text );
  }

  function getRawTextOfPage( $page ) {
    global $wgTitle;
    global $wgArticle;

    /* remove these when we go non-globals... */
    $oldTitle = $wgTitle;
    $oldArticle = $wgArticle;

    $title = trim($page);
    $article = new Article( Title::newFromText( $title ) );
    $content = $article->getContent(false);
    
    $wgTitle = $oldTitle;
    $wgArticle = $oldArticle;
    /* end global shuffling */

    return $content;
  }

  function outputConcatPagesForm( $t ) {
    global $wgOut;
    $action = $t->escapeLocalURL( 'action=submit' );
    $wgOut->addHTML( "<form method='post' action=\"$action\"><input type='hidden' name='action' value='submit' />");
    $wgOut->addHTML( wfMsg('concatpagesmappagelabel') );
    $wgOut->addHTML( "<input name='map'/><br />" );
    $wgOut->addHTML( wfMsg('concatpagesstartpagelabel') );
    $wgOut->addHTML("<input name='start'/><br />" );
    $wgOut->addHTML( wfMsg('concatpagesendpagelabel') );
    $wgOut->addHTML("<input name='end'/><br /><input type='submit' /></form>" );
  }

  function outputBadNavMapMessage( $p ) {
    return wfMsg( 'badnavmapmessage', $p['map'], $p['start'], $p['end']);
  }

  function getPageParams() {
    global  $wgRequest;
    $p = array();
    if( $wgRequest->getVal( 'action' ) == 'submit') {
      $p['map'] = trim($wgRequest->getText( 'map' ));
      $p['start'] = trim($wgRequest->getText( 'start' ));
      $p['end'] = trim($wgRequest->getText( 'end' ));
    }
    return $p;
  }

  function outputPagesFromMap($p = array()) {
    global $wgOut;
    
    $nav = new NavMap( $p['map'] );
    $somethingWrong = true;

    if ( $nav->isValid()) {
      $pageseq = $nav->getPageSequence( $p['start'], $p['end'] );

      if ( is_array($pageseq) && count($pageseq) > 0 ) {
	$somethingWrong = false;

	$wgOut->addHTML("<style type='text/css'>" . wfMsg('concatpagesdivstyle') . "</style>\n");

	foreach ( $pageseq as $key => $page ) {
	  $wgOut->addHTML("<div class='concat-page'>");
	  $c = $this->removeNavMapLinks($this->getRawTextOfPage($page), $p['map']);
	  if ( ! is_null($c) && trim($c) != '' ) {
	    $wgOut->addWikiText("= [[$page]] =\n\n");
	    $wgOut->addWikiText($c);
	  } else {
	    $wgOut->addWikiText("=== [[$page]] ===\n\n");
	    $wgOut->addWikiText("[[$page]] does not have substantive content yet.\n\n");
	  }
	  $wgOut->addHTML("</div>");
	}
      }
    }
    if ($somethingWrong) {
      $wgOut->addWikiText( $this->outputBadNavMapMessage($p) );
    }
  }

  function execute() {
    global $wgOut;
    $p = $this->getPageParams();

    $titleObj = Title::makeTitle( NS_SPECIAL, "ConcatPages" );

    if ( $p['map'] != '' || 
	 $p['start'] != '' || 
	 $p['end'] != '' ) {
      $this->outputPagesFromMap($p);
    } else {
      $this->outputConcatPagesForm($titleObj, $wgOut);
    }
    
  }

}

function wfExtensionConcatPages() {
  global $wgMessageCache;

  $wgMessageCache->addMessages(
	    array(
		  'concatpages' => 'Concatenation of Pages',
		  'concatpagesdivstyle' => '.concat-page { padding: .5em; margin: .5em; border: thin grey solid; }',
		  'concatpagesmappagelabel' => 'Map Page:',
		  'concatpagesstartpagelabel' => 'Start Page:',
		  'concatpagesendpagelabel' => 'End Page:',
		  'badnavmapmessage' => '<p><strong>ERROR</strong> problems with making a map of [[$1]] to assemble pages from [[$2]] to [[$3]].  Inspect the map for structural problems or if you have the right names.</p>',
		  )
	    );
  
  SpecialPage::addPage( new SpecialConcatPages() );
}

?>