Advertisement
drorsnir

MultiBoilerplate.php

Mar 3rd, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.25 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Allows a boilerplate to be selected from a drop down box located above the
  5.  * edit form when editing non-exstant pages or, optionally (based upon
  6.  * configuration variable $wgMultiBoilerplateOverwrite), load the template
  7.  * over the current contents.
  8.  *
  9.  * @file
  10.  * @ingroup Extensions
  11.  *
  12.  * @link http://www.mediawiki.org/wiki/Extension:MultiBoilerplate
  13.  *
  14.  * @author Robert Leverington <robert@rhl.me.uk>
  15.  * @copyright Copyright © 2007 - 2009 Robert Leverington.
  16.  * @copyright Copyright © 2009 Al Maghi.
  17.  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  18.  */
  19.  
  20. // If this is run directly from the web die as this is not a valid entry point.
  21. if( !defined( 'MEDIAWIKI' ) ) die( 'Invalid entry point.' );
  22.  
  23. // Extension credits.
  24. $wgExtensionCredits[ 'other' ][] = array(
  25.     'path'           => __FILE__,
  26.     'name'           => 'MultiBoilerplate',
  27.     'descriptionmsg' => 'multiboilerplate-desc',
  28.     'author'         => array( 'Robert Leverington', 'Al Maghi' ),
  29.     'url'            => 'https://www.mediawiki.org/wiki/Extension:MultiBoilerplate',
  30.     'version'        => '1.8.0',
  31. );
  32.  
  33. // Hook into EditPage::showEditForm:initial to modify the edit page header.
  34. $wgHooks[ 'EditPage::showEditForm:initial' ][] = 'efMultiBoilerplate';
  35.  
  36. // Set extension messages file.
  37. $dir = dirname( __FILE__ ) . '/';
  38. $wgExtensionMessagesFiles[ 'MultiBoilerplate' ] = $dir . 'MultiBoilerplate.i18n.php';
  39. $wgExtensionMessagesFiles[ 'MultiBoilerplate' ] = $dir . 'MultiBoilerplate.i18n.php';
  40. $wgAutoloadClasses['SpecialBoilerplates'] = $dir . 'SpecialBoilerplates_body.php';
  41. $wgSpecialPages['Boilerplates'] = 'SpecialBoilerplates';
  42. $wgSpecialPageGroups['Boilerplates'] = 'wiki'; //section of [[Special:SpecialPages]]
  43.  
  44. // Default configuration variables.
  45. /* Array of boilerplate names to boilerplate pages to load, for example:
  46.  * e.g. $wgMultiBoilerplateOptions[ 'My Boilerplate' ] = 'Template:My Boilerplate';
  47.  * If set to false then the MediaWiki:multiboilerplate message is used to configure
  48.  * boilerplates in the format of:
  49.  * "* Boilerplate Name|Template:Boilerplate Template"
  50.  */
  51. $wgMultiBoilerplateOptions = array();
  52. /* Whether or not to show the form when editing pre-existing pages. */
  53. $wgMultiBoilerplateOverwrite = false;
  54. /* Whether or not to display a special page listing boilerplates.
  55.  * If set to true then the special page exists. */
  56. $wgMultiBoilerplateDiplaySpecialPage = false;
  57.  
  58. $wgHooks['SpecialPage_initList'][]='efBoilerplateDisplaySpecialPage';
  59. function efBoilerplateDisplaySpecialPage( &$aSpecialPages ) {
  60.     global $wgMultiBoilerplateDiplaySpecialPage;
  61.     if ( !$wgMultiBoilerplateDiplaySpecialPage ) {
  62.         unset( $aSpecialPages['Boilerplates'] );
  63.     }
  64.     return true;
  65. }
  66.  
  67.  
  68. /**
  69.  * Generate the form to be displayed at the top of the edit page and insert it.
  70.  * @param $form EditPage object.
  71.  * @return true
  72.  */
  73. function efMultiBoilerplate( $form ) {
  74.  
  75.     // Get various variables needed for this extension.
  76.     global $wgMultiBoilerplateOptions, $wgMultiBoilerplateOverwrite, $wgTitle, $wgRequest;
  77.  
  78.     // If $wgMultiBoilerplateOverwrite is true then detect whether
  79.     // the current page exists or not and if it does return true
  80.     // to end execution of this function.
  81.     if( !$wgMultiBoilerplateOverwrite && $wgTitle->exists( $wgTitle->getArticleID() ) ) return true;
  82.  
  83.     // Generate the options list used inside the boilerplate selection box.
  84.     // If $wgMultiBoilerplateOptions is an array then use that, else fall back
  85.     // to the MediaWiki:Multiboilerplate message.
  86.     if( is_array( $wgMultiBoilerplateOptions ) ) {
  87.         $options = '';
  88.         foreach( $wgMultiBoilerplateOptions as $name => $template ) {
  89.             $selected = false;
  90.             if( $wgRequest->getVal( 'boilerplate' ) == $template ) $selected = true;
  91.             $options .= Xml::option( $name, $template, $selected );
  92.         }
  93.     } else {
  94.         $things = wfMessage( 'multiboilerplate' )->inContentLanguage()->text();
  95.         $options = '';
  96.         $things = explode( "\n", str_replace( "\r", "\n", str_replace( "\r\n", "\n", $things ) ) ); // Ensure line-endings are \n
  97.         foreach( $things as $row ) {
  98.             if ( substr( ltrim( $row ), 0, 1)==="*" ) {
  99.                 $row = ltrim( $row, '* ' ); // Remove the asterix (and a space if found) from the start of the line.
  100.                 $row = explode( '|', $row );
  101.                 if( !isset( $row[ 1 ] ) ) return true; // Invalid syntax, abort.
  102.                 $selected = false;
  103.                 if( $wgRequest->getVal( 'boilerplate' ) == $row[ 1 ] ) $selected = true;
  104.                 $options .= Xml::option( $row[ 0 ], $row[ 1 ], $selected );
  105.             }
  106.         }
  107.     }
  108.  
  109.     // No options found in either configuration file, abort.
  110.     if( $options == '' ) return true;
  111.  
  112.     // Append the selection form to the top of the edit page.
  113.     $form->editFormPageTop .=
  114.         Xml::openElement( 'form', array( 'id' => 'multiboilerplateform', 'name' => 'multiboilerplateform', 'method' => 'get', 'action' => $wgTitle->getEditURL() ) ) .
  115.             Xml::openElement( 'fieldset' ) .
  116.                 Xml::element( 'legend', null, wfMessage( 'multiboilerplate-legend' ) ) .
  117.                 Xml::openElement( 'label' ) .
  118.                     wfMessage( 'multiboilerplate-label' ) .
  119.                     Xml::openElement( 'select', array( 'name' => 'boilerplate' ) ) .
  120.                         $options .
  121.                     Xml::closeElement( 'select' ) .
  122.                 Xml::closeElement( 'label' ) .
  123.                 ' ' .
  124.                 Html::Hidden( 'action', 'edit' ) .
  125.                 Html::Hidden( 'title', $wgRequest->getText( 'title' ) ) .
  126.                 Xml::submitButton( wfMessage( 'multiboilerplate-submit' ) ) .
  127.             Xml::closeElement( 'fieldset' ) .
  128.         Xml::closeElement( 'form' );
  129.  
  130.     // If the Load button has been pushed replace the article text with the boilerplate.
  131.     if( $wgRequest->getText( 'boilerplate', false ) ) {
  132.         $plate = new Article( Title::newFromURL( $wgRequest->getVal( 'boilerplate' ) ) );
  133.         $content = $plate->fetchContent();
  134.         /* Strip out noinclude tags and contained data, and strip includeonly
  135.          * tags (but retain contained data). If a function exists in the
  136.          * parser exists to do this it would be nice to replace this with it (I
  137.          * found one with a name as if it would do this, but it didn't seam to
  138.          * work).
  139.          */
  140.         $content = preg_replace( '#<noinclude>(.*?)</noinclude>#ims', '', $content );
  141.         $content = preg_replace( '#<includeonly>(.*?)</includeonly>#ims', '$1', $content );
  142.         // TODO: Handle <onlyinclude> tags.
  143.         $form->textbox1 = $content;
  144.     }
  145.  
  146.     // Return true so things don't break.
  147.     return true;
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement