r62205 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62204‎ | r62205 | r62206 >
Date:23:19, 9 February 2010
Author:jonwilliford
Status:deferred (Comments)
Tags:
Comment:
Adding the RefHelper extension.
Modified paths:
  • /trunk/extensions/RefHelper (added) (history)
  • /trunk/extensions/RefHelper/RefHelper.alias.php (added) (history)
  • /trunk/extensions/RefHelper/RefHelper.create.php (added) (history)
  • /trunk/extensions/RefHelper/RefHelper.hooks.php (added) (history)
  • /trunk/extensions/RefHelper/RefHelper.i18n.php (added) (history)
  • /trunk/extensions/RefHelper/RefHelper.php (added) (history)
  • /trunk/extensions/RefHelper/RefHelper.search.php (added) (history)
  • /trunk/extensions/RefHelper/refhelper.js (added) (history)

Diff [purge]

Index: trunk/extensions/RefHelper/RefHelper.i18n.php
@@ -0,0 +1,19 @@
 2+<?php
 3+$messages = array();
 4+
 5+$messages['en'] = array(
 6+ 'refhelper' => 'Reference Helper',
 7+ 'refsearch' => 'Reference Search',
 8+ 'refsearch_pmid_instructions' => 'Search Pubmed for References',
 9+ 'label_workspace' => 'Workspace (copy and paste area)',
 10+ 'label_authorforename' => 'Author $1\'s first name',
 11+ 'label_authorsurname' => 'Surname',
 12+ 'title' => 'Title',
 13+ 'journal' => 'Journal',
 14+ 'volume' => 'Volume',
 15+ 'pages' => 'Pages',
 16+ 'year' => 'Year',
 17+ 'refname' => 'Ref Name',
 18+ 'category' => 'Category $1',
 19+);
 20+?>
Index: trunk/extensions/RefHelper/RefHelper.php
@@ -0,0 +1,44 @@
 2+<?php
 3+/*
 4+ The RefHelper extension is free software: you can redistribute it
 5+ and/or modify it under the terms of the GNU General Public License
 6+ as published by the Free Software Foundation, either version 3 of
 7+ the License, or (at your option) any later version.
 8+
 9+ This program is distributed WITHOUT ANY WARRANTY. See
 10+ http://www.gnu.org/licenses/#GPL for more details.
 11+*/
 12+
 13+if (!defined('MEDIAWIKI')) {
 14+ echo <<<EOT
 15+To install my extension, put the following line in LocalSettings.php:
 16+require_once( "\$IP/extensions/RefHelper/RefHelper.php" );
 17+EOT;
 18+ exit( 1 );
 19+}
 20+
 21+$wgExtensionCredits['specialpage'][] = array(
 22+ 'name' => 'RefHelper',
 23+ 'author' => 'Jonathan Williford',
 24+ 'url' => 'http://neurov.is/on',
 25+ 'description' => 'This extension helps create pages for references.',
 26+ 'descriptionmsg' => 'This extension helps create pages for references.',
 27+ 'version' => '0.0.1',
 28+);
 29+
 30+global $wgHooks;
 31+
 32+$wgHooks['SkinTemplateToolboxEnd'][] = 'RefHelperHooks::addRefHelperLink';
 33+$wgHooks['BeforePageDisplay'][] = 'RefHelperHooks::addRefHelperJavascript';
 34+
 35+
 36+
 37+$dir = dirname(__FILE__) . '/';
 38+
 39+$wgAutoloadClasses['RefHelperHooks'] = $dir . 'RefHelper.hooks.php';
 40+$wgAutoloadClasses['RefHelper'] = $dir . 'RefHelper.create.php';
 41+$wgAutoloadClasses['RefSearch'] = $dir . 'RefHelper.search.php';
 42+$wgExtensionMessagesFiles['RefHelper'] = $dir . 'RefHelper.i18n.php';
 43+$wgExtensionAliasesFiles['RefHelper'] = $dir . 'RefHelper.alias.php';
 44+$wgSpecialPages['RefHelper'] = 'RefHelper';
 45+$wgSpecialPages['RefSearch'] = 'RefSearch';
Index: trunk/extensions/RefHelper/RefHelper.create.php
@@ -0,0 +1,211 @@
 2+<?php
 3+class RefHelper extends SpecialPage {
 4+ function __construct() {
 5+ parent::__construct( 'RefHelper','edit',true,false,'default',false );
 6+ wfLoadExtensionMessages('RefHelper');
 7+ }
 8+ /** A simple helper function to output the html of a table row with an input box.
 9+ @param $out $wgOut should be passed
 10+ @param $varname the string of the GET variable name
 11+ @param $varval the value of the GET variable name
 12+ @param $label the text describing the variable
 13+ */
 14+ private function addTableRow( &$out, $varname, $varval, $label ) {
 15+ $out->addHTML( "<tr>\n\t<td class='mw-label'><label for='$varname'>$label:</label></td>\n");
 16+ $out->addHTML( "\t<td class='mw-input'><input name='$varname' size='50' value='$varval' type='text' id='inp_$varname'/></td></tr>\n");
 17+ }
 18+ /** Another simple helper function to output the html of a table row, but with two input boxes.
 19+ See addTableRow for parameter details
 20+ */
 21+ private function add2ColTableRow( &$out, $varname1, $varname2, $varval1, $varval2, $label1, $label2 ) {
 22+ $out->addHTML( "<tr>\n\t<td class='mw-label'><label for='author2'>$label1:</label></td>\n");
 23+ $out->addHTML( "\t<td class='mw-input'>");
 24+ $out->addHTML( "<input name='$varname1' size='15' value='$varval1' type='text' id='inp_$varname1' oninput='updateFirstName(event)'>");
 25+ $out->addHTML( " $label2: ");
 26+ $out->addHTML("<input name='$varname2' size='20' value='$varval2' type='text' id='inp_$varname2' oninput='updateSurname(event)'>");
 27+ $out->addHTML( "</td></tr>\n");
 28+ }
 29+
 30+ /** Create the html body and (depending on the GET variables) creates the page.
 31+ */
 32+ function execute( $par ) {
 33+ global $wgRequest, $wgOut;
 34+
 35+ $this->setHeaders();
 36+
 37+ # Get request data from, e.g.
 38+ $action = $wgRequest->getText('action');
 39+ $refname = htmlentities($wgRequest->getText('refname'));
 40+ $author1 = htmlentities($wgRequest->getText('author1'), ENT_COMPAT, "UTF-8");
 41+ $author2 = htmlentities($wgRequest->getText('author2'), ENT_COMPAT, "UTF-8");
 42+ $author3 = htmlentities($wgRequest->getText('author3'), ENT_COMPAT, "UTF-8");
 43+ $author4 = htmlentities($wgRequest->getText('author4'), ENT_COMPAT, "UTF-8");
 44+ $author5 = htmlentities($wgRequest->getText('author5'), ENT_COMPAT, "UTF-8");
 45+
 46+ $surname1 = htmlentities($wgRequest->getText('surname1'), ENT_COMPAT, "UTF-8");
 47+ $surname2 = htmlentities($wgRequest->getText('surname2'), ENT_COMPAT, "UTF-8");
 48+ $surname3 = htmlentities($wgRequest->getText('surname3'), ENT_COMPAT, "UTF-8");
 49+ $surname4 = htmlentities($wgRequest->getText('surname4'), ENT_COMPAT, "UTF-8");
 50+ $surname5 = htmlentities($wgRequest->getText('surname5'), ENT_COMPAT, "UTF-8");
 51+
 52+ $pmid = htmlentities($wgRequest->getText('pmid'), ENT_COMPAT, "UTF-8");
 53+
 54+ $articletitle = htmlentities($wgRequest->getText('articletitle'));
 55+ $journal = htmlentities($wgRequest->getText('journal'));
 56+ $volume = htmlentities($wgRequest->getText('volume'));
 57+ $pages = htmlentities($wgRequest->getText('pages'));
 58+ $year = htmlentities($wgRequest->getText('year'));
 59+
 60+ $cat1 = htmlentities($wgRequest->getText('cat1'));
 61+ $cat2 = htmlentities($wgRequest->getText('cat2'));
 62+ $cat3 = htmlentities($wgRequest->getText('cat3'));
 63+ $cat4 = htmlentities($wgRequest->getText('cat4'));
 64+
 65+ $reqfilled = strlen($author1) && strlen($articletitle) && strlen($journal) && strlen($year) && strlen($refname);
 66+ if( $action!="submit" || !$reqfilled ) {
 67+ if( strlen($pmid) ) {
 68+ $result = RefSearch::query_pmid($pmid);
 69+ $articletitle = $result["title"];
 70+ $journal = $result["journal"];
 71+ $volume = $result["volume"];
 72+ $pages = $result["pages"];
 73+ $year = $result["year"];
 74+ $auths = $result["authors"];
 75+ if( isset( $auths[0] ) ) {
 76+ $author1 = $auths[0][0];
 77+ $surname1 = $auths[0][1];
 78+ }
 79+ if( isset( $auths[1] ) ) {
 80+ $author2 = $auths[1][0];
 81+ $surname2 = $auths[1][1];
 82+ }
 83+ if( isset( $auths[2] ) ) {
 84+ $author3 = $auths[2][0];
 85+ $surname3 = $auths[2][1];
 86+ }
 87+ if( isset( $auths[3] ) ) {
 88+ $author4 = $auths[3][0];
 89+ $surname4 = $auths[3][1];
 90+ }
 91+ if( isset( $auths[4] ) ) {
 92+ $author5 = $auths[4][0];
 93+ $surname5 = $auths[4][1];
 94+ }
 95+ }
 96+
 97+ # Output
 98+ $wgOut->addHTML( "<fieldset>\n" );
 99+ $wgOut->addHTML( "<legend>Create New Reference</legend>\n");
 100+ $wgOut->addHTML( '<form enctype="multipart/form-data" method="post" action="/w/index.php?title=Special:RefHelper&amp;action=submit" id="mw-create-ref-form"><input name="action" value="submit" type="hidden"/><table id="mw-import-table"><tbody>'."\n");
 101+
 102+ $wgOut->addHTML( "<tr>\n\t<td class='mw-label'>Workspace (copy and paste area):</td>\n");
 103+ $wgOut->addHTML( "\t<td class='mw-input'><textarea id='inp_pastearea' rows='5' cols='20' size='50' oninput='autoPopulateRefFields()'></textarea></td></tr>\n");
 104+
 105+ $wgOut->addHTML( "<tr>\n\t<td class='mw-label'><label for='pmid'>PMID:</label></td>\n");
 106+ $wgOut->addHTML( "\t<td class='mw-input'>");
 107+ $wgOut->addHTML( "<input name='pmid' size='15' value='$pmid' type='text' id='inp_pmid' oninput='updateFirstName(event)'>");
 108+ $wgOut->addHTML( "");
 109+ $wgOut->addHTML( "</td></tr>\n");
 110+ self::addTableRow( $wgOut, "articletitle", $articletitle, wfMsg('title') );
 111+
 112+ self::add2ColTableRow( $wgOut, 'author1', 'surname1', $author1, $surname1,
 113+ wfMsg('label_authorforename','1'),wfMsg('label_authorsurname','1'));
 114+ self::add2ColTableRow( $wgOut, 'author2', 'surname2', $author1, $surname2,
 115+ wfMsg('label_authorforename','2'),wfMsg('label_authorsurname','2'));
 116+ self::add2ColTableRow( $wgOut, 'author3', 'surname3', $author1, $surname3,
 117+ wfMsg('label_authorforename','3'),wfMsg('label_authorsurname','3'));
 118+ self::add2ColTableRow( $wgOut, 'author4', 'surname4', $author1, $surname4,
 119+ wfMsg('label_authorforename','4'),wfMsg('label_authorsurname','4'));
 120+ self::add2ColTableRow( $wgOut, 'author5', 'surname5', $author1, $surname5,
 121+ wfMsg('label_authorforename','5'),wfMsg('label_authorsurname','5'));
 122+
 123+ self::addTableRow( $wgOut, "articletitle", $articletitle, wfMsg('title') );
 124+ self::addTableRow( $wgOut, "journal", $journal, wfMsg('journal') );
 125+ self::addTableRow( $wgOut, "pages", $pages, wfMsg('pages') );
 126+ self::addTableRow( $wgOut, "year", $year, wfMsg('year') );
 127+ self::addTableRow( $wgOut, "refname", $refname, wfMsg('refname') );
 128+ self::addTableRow( $wgOut, "cat1", $cat1, wfMsg('category','1') );
 129+ self::addTableRow( $wgOut, "cat2", $cat2, wfMsg('category','2') );
 130+ self::addTableRow( $wgOut, "cat3", $cat3, wfMsg('category','3') );
 131+ self::addTableRow( $wgOut, "cat4", $cat4, wfMsg('category','4') );
 132+
 133+
 134+ $wgOut->addHTML( "<tr><td class=\"mw-submit\"><input value=\"Create\" type=\"submit\"></td>\n");
 135+ $wgOut->addHTML( ' </tr></tbody></table></form></fieldset>'."\n" );
 136+ }
 137+ else
 138+ {
 139+ $db = wfGetDB(DB_MASTER);
 140+
 141+ $citeTitle = Title::newFromText($refname, NS_CITE );
 142+ $pageTitle = Title::newFromText($refname);
 143+
 144+ $citeExists = $citeTitle->exists();
 145+ $pageExists = $pageTitle->exists();
 146+
 147+ if( $citeExists==FALSE )
 148+ {
 149+ $newcontent = "";
 150+ $newcontent .= ":<onlyinclude>{{cite journal\n";
 151+ $newcontent .= "| first = $author1\n";
 152+ $newcontent .= "| last = $surname1\n";
 153+
 154+ if( strlen( $author2 ) || strlen( $surname2 )) {
 155+ $newcontent .= "| first2 = $author2\n";
 156+ $newcontent .= "| last2 = $surname2\n";
 157+ }
 158+ if( strlen( $author3 ) || strlen( $surname3 )) {
 159+ $newcontent .= "| first3 = $author3\n";
 160+ $newcontent .= "| last3 = $surname3\n";
 161+ }
 162+ if( strlen( $author4 ) || strlen( $surname4 )) {
 163+ $newcontent .= "| first4 = $author4\n";
 164+ $newcontent .= "| last4 = $surname4\n";
 165+ }
 166+ if( strlen( $author5 ) || strlen( $surname5 )) {
 167+ $newcontent .= "| first5 = $author5\n";
 168+ $newcontent .= "| last5 = $surname5\n";
 169+ }
 170+
 171+ $newcontent .= "| title = [[$refname|$articletitle]]\n";
 172+ $newcontent .= "| journal = $journal\n";
 173+ $newcontent .= "| volume = $volume\n";
 174+ $newcontent .= "| pages = $pages\n";
 175+ $newcontent .= "| pmid = $pmid\n";
 176+ $newcontent .= "| date = $year}}</onlyinclude>\n";
 177+ $newcontent .= "{{Publication citation}}\n";
 178+
 179+ $citePage = new Article( $citeTitle );
 180+ $citePage->doEdit( $newcontent, "Automated page creation." );
 181+ $rev_id = $citePage->insertOn( $db );
 182+
 183+ $wgOut->addWikiText("Success! The page [[Cite:$refname]] didn't exist yet.\n");
 184+ }
 185+ else {
 186+ $wgOut->addWikiText("The page [[Cite:$refname]] already exists!\n");
 187+ }
 188+ if( $pageExists==FALSE ) {
 189+ $newcontent = "";
 190+ $newcontent .= "==Citation==\n";
 191+ $newcontent .= "{{ToCite}}\n";
 192+ $newcontent .= "[[Category:References]]\n";
 193+ if( strlen( $cat1 ) ) $newcontent .= "[[Category:$cat1]]\n";
 194+ if( strlen( $cat2 ) ) $newcontent .= "[[Category:$cat2]]\n";
 195+ if( strlen( $cat3 ) ) $newcontent .= "[[Category:$cat3]]\n";
 196+ if( strlen( $cat4 ) ) $newcontent .= "[[Category:$cat4]]\n";
 197+
 198+ $newPage = new Article( $pageTitle );
 199+ $newPage->doEdit( $newcontent, "Automated page creation." );
 200+ $rev_id = $newPage->insertOn( $db );
 201+
 202+ $wgOut->addWikiText("Success! The page [[$refname]] didn't exist yet.");
 203+ }
 204+ else {
 205+ $wgOut->addWikiText("The page [[$refname]] already exists!");
 206+ }
 207+
 208+ $wgOut->addWikiText("[[Special:RefHelper|Create New Reference]]");
 209+ }
 210+ }
 211+}
 212+
Index: trunk/extensions/RefHelper/RefHelper.hooks.php
@@ -0,0 +1,23 @@
 2+<?php
 3+
 4+if (!defined('MEDIAWIKI')) {
 5+ exit( 1 );
 6+}
 7+
 8+class RefHelperHooks {
 9+ function addRefHelperJavascript( $pageObj ) {
 10+ global $wgRefHelperExtensionPath;
 11+ $pageObj->addScript( "<script src='$wgRefHelperExtensionPath/refhelper.js' type='text/javascript'></script>" );
 12+ return TRUE;
 13+ }
 14+
 15+ function addRefHelperLink( $tpl ) {
 16+ ?><li id="t-reflink"><?php
 17+ ?><a href="https://www.mediawiki.org/on/Special:RefHelper">Create Reference</a><?php
 18+ ?></li><?php
 19+ ?><li id="t-reflink"><?php
 20+ ?><a href="https://www.mediawiki.org/on/Special:RefSearch">Create Reference from Search</a><?php
 21+ ?></li><?php
 22+ return TRUE;
 23+ }
 24+}
Property changes on: trunk/extensions/RefHelper/RefHelper.hooks.php
___________________________________________________________________
Name: svn:executable
125 + *
Index: trunk/extensions/RefHelper/RefHelper.search.php
@@ -0,0 +1,134 @@
 2+<?php
 3+class RefSearch extends SpecialPage {
 4+ function __construct() {
 5+ parent::__construct( 'RefSearch','edit',true,false,'default',false );
 6+ wfLoadExtensionMessages('RefHelper');
 7+ }
 8+
 9+ function execute( $par ) {
 10+ global $wgRequest, $wgOut;
 11+
 12+ $this->setHeaders();
 13+
 14+ # Get request data from, e.g.
 15+ $action = $wgRequest->getText('action');
 16+
 17+ $query = htmlentities($wgRequest->getText('query'));
 18+ $query = str_replace(" ","+",$query);
 19+ $reqfilled = strlen($query);
 20+
 21+ # Output
 22+ $wgOut->addHTML( "<fieldset>\n" );
 23+ $wgOut->addHTML( "<legend>Search PubMed for References</legend>\n");
 24+ $wgOut->addHTML( '<form enctype="multipart/form-data" method="post" action="/w/index.php?title=Special:RefSearch&amp;action=submit" id="mw-create-ref-form"><input name="action" value="submit" type="hidden"/><table id="mw-import-table"><tbody>'."\n");
 25+
 26+ $wgOut->addHTML( "<tr>\n");
 27+ $wgOut->addHTML( "\t<td class='mw-input'><input name='query' size='50' value='$query' type='text' id='inp_articletitle'/></td>\n");
 28+ $wgOut->addHTML( "<td class=\"mw-submit\"><input value=\"Search\" type=\"submit\"></td>\n");
 29+ $wgOut->addHTML( "\n");
 30+
 31+ $wgOut->addHTML( ' </tr></tbody></table><input name="editToken" value="014149335353561d3d9dc6e1273e037a+\" type="hidden"></form>'."\n" );
 32+
 33+ if( $action=="submit" || $reqfilled ) {
 34+ $ch = curl_init("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?term=$query&tool=mediawiki_refhelper");
 35+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 36+ $result = curl_exec($ch);
 37+ curl_close($ch);
 38+
 39+ $num = preg_match_all("|<Id>(\d+)</Id>|", $result, $matches );
 40+ $wgOut->addHTML("<table>\n");
 41+ for( $i = 0; $i < $num; $i++ ) {
 42+ $pmid = $matches[1][$i];
 43+ $result = self::query_pmid($pmid);
 44+
 45+ $author = array_shift($result["authors"]);
 46+ if( isset($author) ) $author = $author[1];
 47+ $title = $result["title"];
 48+ $query = $result["query_url"];
 49+ $year = $result["year"];
 50+ if( count($result["AU"]) > 1 ) $etal = " et al.";
 51+ else $etal = "";
 52+
 53+
 54+ $wgOut->addHTML("<tr>\n");
 55+ $wgOut->addHTML("\t<td>\n");
 56+ $wgOut->addHTML("$author $etal ($year) \"$title\"");
 57+ $wgOut->addHTML("\t</td>\n");
 58+ $wgOut->addHTML("\t<td>\n");
 59+ $wgOut->addHTML( "<form enctype='multipart/form-data' method='post' action='/w/index.php?title=Special:RefHelper&amp;action=submit&amp;pmid=$pmid' id='mw-create-ref-form$i'><input value=\"Create\" type=\"submit\"/></form> <a href='$query'>(debug)</a>");
 60+ $wgOut->addHTML("\t</td>\n");
 61+ $wgOut->addHTML("</tr>\n");
 62+ }
 63+ $wgOut->addHTML("</table>\n");
 64+ }
 65+ $wgOut->addHTML( "</fieldset>\n");
 66+ }
 67+ static function parse_medline( $text, $field ) {
 68+ $field = strtoupper($field);
 69+ $num = preg_match_all("|\n$field\s*- (.*)(?>\n (.*))*|", $text, $matches, PREG_SET_ORDER );
 70+ $ret = array();
 71+ for( $i = 0; $i < $num; $i++ )
 72+ {
 73+ array_shift($matches[$i]);
 74+ $ret[] = implode( " ", $matches[$i] );
 75+ }
 76+ return $ret;
 77+ }
 78+ static function query_pmid( $pmid ) {
 79+ $ret = array();
 80+ $ret["query_url"] = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&report=medline&mode=text&id=$pmid&email=jonwilliford@gmail.com&tool=mediawiki_refhelper";
 81+ $ch = curl_init( $ret["query_url"] );
 82+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 83+ $result = curl_exec($ch);
 84+ curl_close($ch);
 85+
 86+ $ret["title"] = array_shift( self::parse_medline( $result, "TI" ));
 87+ $ret["journal"] = array_shift( self::parse_medline( $result, "TA" ));
 88+ $ret["year"] = substr( array_shift( self::parse_medline( $result, "DP" )), 0, 4 );
 89+ $ret["volume"] = array_shift( self::parse_medline( $result, "VI" ));
 90+ $ret["issue"] = array_shift( self::parse_medline( $result, "IP" ));
 91+ $ret["pages"] = array_shift( self::parse_medline( $result, "PG" ));
 92+
 93+ $ret["firstlasts"] = self::parse_medline( $result, "FAU" );
 94+ $ret["AU"] = self::parse_medline( $result, "AU" );
 95+
 96+ $ret["authors"] = array();
 97+ /*if( isset( $ret["firstlasts"] ) )
 98+ {
 99+ for( $i = 0; $i < count( $ret["firstlasts"] ); $i++ ) {
 100+
 101+ $auth = $ret["firstlasts"][$i];
 102+
 103+ if( preg_match("|(.+), (.+)|", $auth, $matches ) ) {
 104+
 105+ // index 0 for first name, index 1 for surname
 106+ $ret["authors"][$i][1] = $matches[1];
 107+ $ret["authors"][$i][0] = $matches[2];
 108+ }
 109+ else {
 110+ $ret["authors"][$i] = array(0=>"",1=>$auth);
 111+ }
 112+ }
 113+ }
 114+ else*/
 115+ {
 116+ for( $i = 0; $i < count( $ret["AU"] ); $i++ ) {
 117+
 118+ $auth = $ret["AU"][$i];
 119+
 120+ if( preg_match("|^(.+) (.+)$|", $auth, $matches ) ) {
 121+
 122+ // index 0 for first name, index 1 for surname
 123+ $ret["authors"][$i][1] = $matches[1];
 124+ $ret["authors"][$i][0] = $matches[2];
 125+ }
 126+ else {
 127+ $ret["authors"][$i] = array(0=>"",1=>$auth);
 128+ }
 129+ }
 130+ }
 131+
 132+ return $ret;
 133+ }
 134+}
 135+
Index: trunk/extensions/RefHelper/refhelper.js
@@ -0,0 +1,107 @@
 2+// http://www.intridea.com/2007/12/16/faking-onpaste-in-firefox
 3+
 4+function populateRefName() {
 5+ inpYear = document.getElementById("inp_year");
 6+ inpRefName = document.getElementById("inp_refname");
 7+ var inpSurname = new Array();
 8+ var i = 1;
 9+ for( ; i <= 5; i++ ) {
 10+ var str = "inp_surname"+i;
 11+ inpSurname[i] = document.getElementById(str);
 12+ if( inpSurname[i].value.length == 0 ) break;
 13+ }
 14+ var numAuthors = i-1;
 15+
 16+ inpRefName.value = "";
 17+ for( i=1; i <= 2; i++ ) {
 18+ inpRefName.value += trim(inpSurname[i].value) + " ";
 19+ }
 20+ inpRefName.value += inpYear.value;
 21+}
 22+
 23+function wasPasted( e ) {
 24+ var ret =(e.previousValue && e.value.length > e.previousValue.length + 1) ||
 25+ (!e.previousValue && e.value.length > 1) ;
 26+
 27+ e.previousValue = e.value;
 28+ return ret;
 29+}
 30+
 31+
 32+function autoPopulateRefFields() {
 33+ inpPasteArea = document.getElementById("inp_pastearea");
 34+
 35+ if( !wasPasted(inpPasteArea) ) return;
 36+
 37+ inpPages = document.getElementById("inp_pages");
 38+ inpYear = document.getElementById("inp_year");
 39+ inpTitle = document.getElementById("inp_articletitle");
 40+
 41+ var str = inpPasteArea.value;
 42+ //alert( "Autopopulate called! "+str );
 43+
 44+ // find the page numbers
 45+ var match = /([0-9]+)\s*[-–]\s*([0-9]+)/.exec( str );
 46+ var pages_index = match.index;
 47+ var firstpage = ""+match[1];
 48+ var lastpage = ""+match[2];
 49+ if( firstpage.length > lastpage.length )
 50+ {
 51+ lastpage =
 52+ firstpage.substring(0, firstpage.length-lastpage.length)
 53+ + lastpage;
 54+ }
 55+ inpPages.value = firstpage + "-" + lastpage;
 56+
 57+ // find the year
 58+ match = /[^0-9](19[0-9]{2}|20[0-9]{2})[^0-9]/.exec( str );
 59+ var year_index = match.index;
 60+ inpYear.value = match[1];
 61+
 62+ // find the title
 63+ re = /[-\w\s:]{15,}\./
 64+ match = re.exec(str);
 65+ var title_index = match.index;
 66+ inpTitle.value = trim(match[0]);
 67+
 68+ // update the reference name
 69+ populateRefName();
 70+}
 71+function setAuthorName( authorNum, second, first ) {
 72+ var firstElem = document.getElementById("inp_author" + authorNum);
 73+ var lastElem = document.getElementById("inp_surname" + authorNum);
 74+
 75+ if( firstElem.value.length == 0 || lastElem.value.length == 0 )
 76+ {
 77+ firstElem.value = first;
 78+ lastElem.value = second;
 79+ }
 80+}
 81+
 82+function updateFirstName( evnt ) {
 83+ var e = evnt.target;
 84+
 85+ if( !wasPasted(e) ) return;
 86+
 87+ var match = /^inp_author([0-9]+)$/.exec( e.id );
 88+ var authorNum = match[1];
 89+
 90+ re = /^\s*([^,]+)\s*,\s*([^,]+)\s*$/
 91+ if( re.test(e.value) )
 92+ {
 93+ match = re.exec(e.value);
 94+ setAuthorName( authorNum, match[1], match[2] );
 95+ return;
 96+ }
 97+
 98+ re = /^\s*([a-zA-Z]{2,})[\s]*[,]?[\s]*((?:[\s.]+[A-Z]{1,2})+[.]?)$/
 99+ if( re.test(e.value) )
 100+ {
 101+ match = re.exec(e.value);
 102+ setAuthorName( authorNum, match[1], match.slice(2) );
 103+ return;
 104+ }
 105+}
 106+function updateSurname( evnt ) {
 107+ populateRefName();
 108+}
Property changes on: trunk/extensions/RefHelper/refhelper.js
___________________________________________________________________
Name: svn:executable
1109 + *
Index: trunk/extensions/RefHelper/RefHelper.alias.php
@@ -0,0 +1,8 @@
 2+<?php
 3+$aliases = array();
 4+
 5+/** English */
 6+$aliases['en'] = array(
 7+ 'RefHelper' => array( 'RefHelper' ),
 8+);
 9+?>

Follow-up revisions

RevisionCommit summaryAuthorDate
r62292This commit should address all of the code review comments from r62205.jonwilliford05:55, 11 February 2010

Comments

#Comment by Siebrand (talk | contribs)   00:00, 10 February 2010

Some remarks:

  • please use Xml:: methods instead of hard coded (X)HTML
  • do not use Camel Case in user interface texts ('Search Pubmed for References' -> 'Search Pubmed for references'
  • internationalise *everything*. There is hard coded English UI text in the code that shouldn't be there. ("==Citation==", "Category:" and a lot of others)
  • Do not hard code template names; make them configurable with the default in a message ({{cite journal}}, {{ToCite}}, possibly others)
  • don't tab except for indentation

See Manual:Coding_conventions and Localisation for more information.

Please also address the open comments on r61323 and 61751 before adding more extensions.

#Comment by Raymond (talk | contribs)   05:40, 10 February 2010

Another remark:

  • Please prefix all message keys with "refhelper-" to avoid conflicts with message keys from core and probably older extensions.
#Comment by JonathanWilliford (talk | contribs)   18:56, 11 February 2010

Thank you both for taking the time to review my code. All of these issues have been addressed in r62205.

Status & tagging log