commit fcbbd70a5576d4ec62f597f1f1d007458b603ab9 Author: Ævar Arnfjörð Bjarmason Date: Sat Nov 26 23:22:24 2005 +0000 * A special page extension that adds a special page that generates citations for pages. diff --git a/SpecialCite.php b/SpecialCite.php new file mode 100644 index 00000000..2d6ba0bc --- /dev/null +++ b/SpecialCite.php @@ -0,0 +1,225 @@ + + * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later + */ + +$wgExtensionFunctions[] = 'wfSpecialCite'; +$wgExtensionCredits['specialpage'][] = array( + 'name' => 'Cite', + 'author' => 'Ævar Arnfjörð Bjarmason', + 'description' => 'adds a [[Special:Cite|citation]] special page & toolbox link', + 'url' => 'http://meta.wikimedia.org/wiki/Help:Cite' +); + +$wgHooks['SkinTemplateBuildNavUrlsNav_urlsAfterPermalink'][] = 'wfSpecialCiteNav'; +$wgHooks['MonoBookTemplateToolboxEnd'][] = 'wfSpecialCiteToolbox'; + +function wfSpecialCite() { + global $IP, $wgMessageCache, $wgHooks; + + $wgMessageCache->addMessages( + array( + 'cite' => 'Cite', + 'cite_page' => 'Page: ', + 'cite_submit' => 'Cite', + 'cite_page_link' => 'Cite this page', + 'cite_text' => + "* ''{{FULLPAGENAME}}'' (last modified {{CURRENTTIME}}," . + ' {{CURRENTDAY}} {{CURRENTMONTHNAME}} {{CURRENTYEAR}} UTC).' . + ' {{SITENAME}}, {{int:sitesubtitle}}. Retrived {{CURRENTTIME}},' . + ' {{CURRENTDAY}} {{CURRENTMONTHNAME}} {{CURRENTYEAR}} from' . + ' {{fullurl:{{FULLPAGENAME}}|oldid={{REVISIONID}}}}' + ) + ); + + require_once "$IP/includes/SpecialPage.php"; + class Cite extends SpecialPage { + function Cite() { + SpecialPage::SpecialPage( 'Cite' ); + } + + function execute( $par ) { + global $wgOut, $wgRequest; + + $this->setHeaders(); + + $page = isset( $par ) ? $par : $wgRequest->getText( 'page' ); + + $title = Title::newFromText( $page ); + $article = new Article( $title ); + + $cform = new CiteForm( &$title ); + + if ( is_null( $title ) || ! $article->exists() ) + $cform->execute(); + else { + $cform->execute(); + + $cout = new CiteOutput( &$title, &$article ); + $cout->execute(); + } + } + } + + class CiteForm { + var $mTitle; + + function CiteForm( &$title ) { + $this->mTitle =& $title; + } + + function execute() { + global $wgOut, $wgTitle; + + $wgOut->addHTML( + wfElement( 'form', + array( + 'id' => 'specialcite', + 'method' => 'get', + 'action' => $wgTitle->escapeLocalUrl() + ), + null + ) . + wfOpenElement( 'label' ) . + wfMsgHtml( 'cite_page' ) . + wfElement( 'input', + array( + 'type' => 'text', + 'size' => 20, + 'name' => 'page', + 'value' => is_object( $this->mTitle ) ? $this->mTitle->getPrefixedText() : '' + ), + '' + ) . + ' ' . + wfElement( 'input', + array( + 'type' => 'submit', + 'value' => wfMsgHtml( 'cite_submit' ) + ), + '' + ) . + wfCloseElement( 'label' ) . + wfCloseElement( 'form' ) + ); + } + + } + + class CiteOutput { + var $mTitle, $mArticle, $mMsg, $mParserOptions; + var $mParser; + + function CiteOutput( $title, $article ) { + global $wgHooks, $wgParser; + + $this->mTitle =& $title; + $this->mArticle =& $article; + + $wgHooks['ParserGetVariableValueRevid'][] = array( $this, 'revid' ); + $wgHooks['ParserGetVariableValueVarCache'][] = array( $this, 'varCache' ); + + $this->genParserOptions(); + $this->genParser(); + + $wgParser->setHook( 'cite', 'CiteParse', $this, &$mParser ); + } + + function execute() { + global $wgOut, $wgUser, $wgParser, $wgHooks; + + $wgHooks['ParserGetVariableValueTs'][] = array( $this, 'timestamp' ); + + $this->genMessage(); + $ret = $wgParser->parse( $this->mMsg, &$this->mTitle, $this->mParserOptions ); + $wgOut->addHtml( $ret->getText() ); + } + + function genMessage() { + global $wgMessageCache; + + $setting = $wgMessageCache->mDisableTransform; + $wgMessageCache->disableTransform(); + $this->mMsg = $wgMessageCache->get( 'cite_text', true, true ); + $wgMessageCache->mDisableTransform = $setting; + } + + function genParserOptions() { + $this->mParserOptions = ParserOptions::newFromUser( $wgUser ); + $this->mParserOptions->setDateFormat( MW_DATE_DEFAULT ); + $this->mParserOptions->setEditSection( false ); + } + + function genParser() { + $this->mParser = new Parser; + } + + function CiteParse( $in, $argv ) { + global $wgTitle; + + $ret = $this->mParser->parse( $in, $wgTitle, $this->mParserOptions, false ); + + return $ret->getText(); + } + + function varCache() { return false; } + + function timestamp( &$parser, &$ts ) { + if ( isset( $parser->mTagHooks['cite'] ) ) + $ts = wfTimestamp( TS_UNIX, $this->mArticle->getTimestamp() ); + + return true; + } + + function revid( &$parser, &$revid ) { + $this->mArticle->fetchContent(); + $revid = $this->mArticle->getRevIdFetched(); + + return false; + } + } + + SpecialPage::addPage( new Cite ); +} + +function wfSpecialCiteNav( &$skintemplate, &$nav_urls, &$oldid, &$revid ) { + if ( (int)$oldid ) + $nav_urls['cite'] = array( + 'text' => wfMsg( 'cite_page_link' ), + 'href' => '' + ); + else if ( $revid !== 0 ) + $nav_urls['cite'] = array( + 'text' => wfMsg( 'cite_page_link' ), + 'href' => $skintemplate->makeSpecialUrl( 'Cite/' . $skintemplate->thispage ) + ); + + return true; +} + +function wfSpecialCiteToolbox( &$monobook ) { + if ( isset( $monobook->data['nav_urls']['cite'] ) ) + if ( $monobook->data['nav_urls']['cite']['href'] == '' ) { + ?>
  • msg( 'cite_page_link' ); ?>
  • + + msg( 'cite_page_link' ); ?> + +
  • +