r62350 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62349‎ | r62350 | r62351 >
Date:05:00, 12 February 2010
Author:jonwilliford
Status:deferred
Tags:
Comment:
Modified the behavior of the edit page in the citation namespace. Instead of showing a text box, it shows a PubMed search of potentially matching articles that can be used to generate the citation.
Modified paths:
  • /trunk/extensions/RefHelper/RefHelper.create.php (modified) (history)
  • /trunk/extensions/RefHelper/RefHelper.hooks.php (modified) (history)
  • /trunk/extensions/RefHelper/RefHelper.i18n.php (modified) (history)
  • /trunk/extensions/RefHelper/RefHelper.php (modified) (history)
  • /trunk/extensions/RefHelper/RefHelper.search.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RefHelper/RefHelper.i18n.php
@@ -25,5 +25,9 @@
2626 'refhelper-search' => 'Search',
2727 'refhelper-toolbox_link_create' => 'Create reference',
2828 'refhelper-toolbox_link_search' => 'Create reference from search',
 29+ 'refhelper-newarticle_nocitation' => 'There is currently no citation with this name. ',
 30+ 'refhelper-newarticle_suggestions' => "If there is a matching citation from below, you can create this page by clicking on the corresponding \"Create\" button.\n<h2>Suggestions</h2>\n",
 31+ 'refhelper-newarticle_nosuggestions' => "Unfortunately, I was unable to suggest any references. You can [[Special:RefHelper|add the reference manually]] or [[Special:RefSearch|search for the citation to add]].\n",
 32+
2933 );
3034 ?>
Index: trunk/extensions/RefHelper/RefHelper.php
@@ -44,7 +44,7 @@
4545
4646 $wgHooks['SkinTemplateToolboxEnd'][] = 'RefHelperHooks::addRefHelperLink';
4747 $wgHooks['BeforePageDisplay'][] = 'RefHelperHooks::addRefHelperJavascript';
48 -
 48+$wgHooks['AlternateEdit'][] = 'RefSearch::newArticleHook';
4949
5050
5151 $dir = dirname(__FILE__) . '/';
Index: trunk/extensions/RefHelper/RefHelper.create.php
@@ -207,7 +207,7 @@
208208 $newcontent = '{{'."$wgRefHelperCiteTemplate\n$paramtext}}\n";
209209
210210 $citePage = new Article( $citeTitle );
211 - $citePage->doEdit( $newcontent, "Automated page creation." );
 211+ $citePage->doEdit( $newcontent, wfMsg(self::MSG.'refcreate_autocomment') );
212212 $rev_id = $citePage->insertOn( $db );
213213
214214 $wgOut->addWikiText( wfMsg(self::MSG.'refcreate_success', array("$wgRefHelperCiteNS:$refname") ) );
Index: trunk/extensions/RefHelper/RefHelper.hooks.php
@@ -5,17 +5,16 @@
66 }
77
88 class RefHelperHooks {
9 - function __construct() {
 9+ static function addRefHelperJavascript( $pageObj ) {
1010 wfLoadExtensionMessages('RefHelper');
11 - }
12 - function addRefHelperJavascript( $pageObj ) {
1311 global $wgRefHelperExtensionPath;
1412 $pageObj->addScript(
1513 Xml::element('script',array('src'=>"$wgRefHelperExtensionPath/refhelper.js", 'type'=>'text/javascript') ) );
16 - return TRUE;
 14+ return true;
1715 }
1816
19 - function addRefHelperLink( $tpl ) {
 17+ static function addRefHelperLink( $tpl ) {
 18+ wfLoadExtensionMessages('RefHelper');
2019 global $wgScript;
2120 echo Xml::openElement('li',array('class'=>'t-reflink')) .
2221 Xml::element('a',array('href'=>"$wgScript?title=Special:RefHelper"), wfMsg( RefHelper::MSG . 'toolbox_link_create' ) ) .
@@ -23,6 +22,6 @@
2423 echo Xml::openElement('li',array('class'=>'t-reflink')) .
2524 Xml::element('a',array('href'=>"$wgScript?title=Special:RefSearch"), wfMsg( RefHelper::MSG . 'toolbox_link_search' ) ) .
2625 Xml::closeElement('li');
27 - return TRUE;
 26+ return true;
2827 }
2928 }
Index: trunk/extensions/RefHelper/RefHelper.search.php
@@ -15,8 +15,6 @@
1616 $action = $wgRequest->getText('action');
1717
1818 $query = htmlentities($wgRequest->getText('query'));
19 - $query = str_replace(" ","+",$query);
20 - $reqfilled = strlen($query);
2119
2220 # Output
2321 $wgOut->addHTML(
@@ -42,44 +40,80 @@
4341 Xml::closeElement('table') .
4442 Xml::closeElement('form') );
4543
 44+ $reqfilled = strlen($query);
4645 if( $action=="submit" || $reqfilled ) {
47 - $ch = curl_init("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?term=$query&tool=mediawiki_refhelper");
48 - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
49 - $result = curl_exec($ch);
50 - curl_close($ch);
 46+ $wgOut->addHTML( self::perform_search( $query ) );
 47+ }
 48+ $wgOut->addHTML( Xml::closeElement('fieldset') );
 49+ }
 50+ static function newArticleHook(&$editpage ) {
 51+ wfLoadExtensionMessages('RefHelper');
 52+ global $wgOut, $wgRefHelperCiteNS;
5153
52 - $num = preg_match_all("|<Id>(\d+)</Id>|", $result, $matches );
53 -
54 - $wgOut->addHTML(Xml::openElement('table'));
55 - for( $i = 0; $i < $num; $i++ ) {
56 - $pmid = $matches[1][$i];
57 - $result = self::query_pmid($pmid);
 54+ // don't display if page already exists
 55+ $title = $editpage->getArticle()->getTitle();
 56+ if( $title->exists() ) return true;
 57+ if( $title->getNsText()!==$wgRefHelperCiteNS) return true;
5858
59 - $author = array_shift($result["authors"]);
60 - if( isset($author) ) $author = $author[1];
61 - $title = $result["title"];
62 - $query = $result["query_url"];
63 - $year = $result["year"];
64 - if( count($result["AU"]) > 1 ) $etal = " et al.";
65 - else $etal = "";
 59+ $editpage->setHeaders();
6660
67 - $wgOut->addHTML(
68 - Xml::openElement('tr') .
69 - Xml::element('td',null,"$author $etal ($year) \"$title\"") .
70 - Xml::openElement('td') .
71 - Xml::openElement('form',array('method'=>'get', 'action'=>$wgScript, 'id'=>"mw-create-ref-form$i")) .
72 - Xml::hidden( 'title', 'Special:RefHelper' ) .
73 - Xml::hidden( 'pmid', $pmid ) .
74 - Xml::element('input',array('value'=>wfMsg(RefHelper::MSG.'create'), 'type'=>'submit')) .
75 - Xml::closeElement('form') .
76 - Xml::closeElement('td') .
77 - Xml::closeElement('tr') );
78 - }
79 - $wgOut->addHTML( Xml::closeElement('table') );
 61+ $query = $title->getText();
 62+ $query = preg_replace('/ ((?>19|20)[0-9]{2})$/',' $1[PDAT]',$query);
 63+ $query = preg_replace('/(?> |^)([^0-9]+) /',' $1[AUTH] ',$query);
 64+ /*$query = preg_replace('/ ([0-9]{4})$/',' $1[PDAT]',$query);*/
 65+
 66+ $wgOut->addWikiMsg( RefHelper::MSG . 'newarticle_nocitation');
 67+
 68+ $result = self::perform_search( $query );
 69+ if( $result ) {
 70+ $wgOut->addWikiMsg( RefHelper::MSG . 'newarticle_suggestions');
 71+ $wgOut->addHTML( $result );
8072 }
81 - $wgOut->addHTML( Xml::closeElement('fieldset') );
 73+ else {
 74+ $wgOut->addWikiMsg( RefHelper::MSG . 'newarticle_nosuggestions');
 75+ }
 76+ return false;
8277 }
83 - static function parse_medline( $text, $field ) {
 78+ static function perform_search( $query ) {
 79+ global $wgScript;
 80+ $query = str_replace(" ","+",$query);
 81+ $ch = curl_init("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?term=$query&tool=mediawiki_refhelper");
 82+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 83+ $result = curl_exec($ch);
 84+ curl_close($ch);
 85+
 86+ $num = preg_match_all("|<Id>(\d+)</Id>|", $result, $matches );
 87+ if( $num === 0 || $num === false ) return false;
 88+
 89+ $ret = Xml::openElement('table');
 90+ for( $i = 0; $i < $num; $i++ ) {
 91+ $pmid = $matches[1][$i];
 92+ $result = self::query_pmid($pmid);
 93+
 94+ $author = array_shift($result["authors"]);
 95+ if( isset($author) ) $author = $author[1];
 96+ $title = $result["title"];
 97+ $query = $result["query_url"];
 98+ $year = $result["year"];
 99+ if( count($result["AU"]) > 1 ) $etal = " et al.";
 100+ else $etal = "";
 101+
 102+ $ret .=
 103+ Xml::openElement('tr') .
 104+ Xml::element('td',null,"$author $etal ($year) \"$title\"") .
 105+ Xml::openElement('td') .
 106+ Xml::openElement('form',array('method'=>'get', 'action'=>$wgScript, 'id'=>"mw-create-ref-form$i")) .
 107+ Xml::hidden( 'title', 'Special:RefHelper' ) .
 108+ Xml::hidden( 'pmid', $pmid ) .
 109+ Xml::element('input',array('value'=>wfMsg(RefHelper::MSG.'create'), 'type'=>'submit')) .
 110+ Xml::closeElement('form') .
 111+ Xml::closeElement('td') .
 112+ Xml::closeElement('tr');
 113+ }
 114+ $ret .= Xml::closeElement('table');
 115+ return $ret;
 116+ }
 117+ private static function parse_medline( $text, $field ) {
84118 $field = strtoupper($field);
85119 $num = preg_match_all("|\n$field\s*- (.*)(?>\n (.*))*|", $text, $matches, PREG_SET_ORDER );
86120 $ret = array();

Follow-up revisions

RevisionCommit summaryAuthorDate
r62352Made small improvement over r62350 (and r62292). The refname is automaticall...jonwilliford05:26, 12 February 2010

Status & tagging log