r23907 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23906‎ | r23907 | r23908 >
Date:16:00, 9 July 2007
Author:proes
Status:old
Tags:
Comment:
Added hook for Knewco to look up PubMed title.
Modified paths:
  • /trunk/extensions/Wikidata/OmegaWiki/Editor.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/OmegaWikiEditors.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/forms.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/suggest.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/OmegaWiki/forms.php
@@ -2,8 +2,13 @@
33
44 require_once('languages.php');
55
6 -function getTextBox($name, $value = "", $maximumLength = 255) {
7 - return '<input type="text" id="'. $name .'" name="'. $name .'" value="'. $value .'" maxlength="'. $maximumLength .'" style="width: 100%; padding: 0px; margin: 0px;"/>';
 6+function getTextBox($name, $value = "", $onChangeHandler = "", $maximumLength = 255) {
 7+ if ($onChangeHandler != "")
 8+ $onChangeAttribute = ' onchange="'. $onChangeHandler . '"';
 9+ else
 10+ $onChangeAttribute = '';
 11+
 12+ return '<input type="text" id="'. $name .'" name="'. $name .'" value="'. $value .'" maxlength="'. $maximumLength .'"' . $onChangeAttribute . ' style="width: 100%; padding: 0px; margin: 0px;"/>';
813 }
914
1015 function getTextArea($name, $text = "", $rows = 5, $columns = 80) {
Index: trunk/extensions/Wikidata/OmegaWiki/OmegaWikiEditors.php
@@ -373,7 +373,7 @@
374374 $linkEditor = new LinkEditor($linkAttribute, new SimplePermissionController(true), true);
375375 else {
376376 $linkEditor = new RecordTableCellEditor($linkAttribute);
377 - $linkEditor->addEditor(new ShortTextEditor($linkURLAttribute, new SimplePermissionController(true), true));
 377+ $linkEditor->addEditor(new ShortTextEditor($linkURLAttribute, new SimplePermissionController(true), true, "urlFieldChanged(this);"));
378378 $linkEditor->addEditor(new ShortTextEditor($linkLabelAttribute, new SimplePermissionController(true), true));
379379 }
380380
Index: trunk/extensions/Wikidata/OmegaWiki/suggest.js
@@ -667,3 +667,147 @@
668668
669669 elementsToSort.push(params);
670670 }
 671+
 672+function startsWith(value, prefix) {
 673+ return value.toLowerCase().substr(0, prefix.length) == prefix.toLowerCase();
 674+}
 675+
 676+function urlFieldChanged(urlField) {
 677+ var labelField = document.getElementById(stripSuffix(urlField.id, "url") + "label");
 678+ var url = urlField.value;
 679+
 680+// if (startsWith(url, "http://www.ncbi.nlm.nih.gov")) {
 681+// var pubMedId = 1000;
 682+// labelField.value = getPubMedTitle(pubMedId);
 683+// }
 684+}
 685+
 686+// Knewco specific Javascript
 687+
 688+function GetOffset( text, Pattern, inclusive ){
 689+ var extra = 0;
 690+
 691+ try{
 692+ offset = text.indexOf( Pattern );
 693+
 694+ if ( offset != -1 ){
 695+ if ( inclusive ){
 696+ offset = offset + Pattern.length;
 697+ }
 698+
 699+ return offset;
 700+ }
 701+ return -1;
 702+ }
 703+ catch(e){
 704+ return -1;
 705+ }
 706+
 707+}
 708+
 709+function ExtractText( text, startTag, startOffset, endTag, endOffset ){
 710+ var extra = 0;
 711+ var offset = 0;
 712+ var endPos = 0;
 713+
 714+ try{
 715+ /* search for a starting pattern in the text and return the remainder */
 716+ offset = GetOffset( text, startTag, true );
 717+ if ( offset == -1 ){
 718+ return null;
 719+ }
 720+
 721+ offset += startOffset;
 722+ text2 = text.slice( offset );
 723+
 724+ if ( endTag != '' ){
 725+ /* search for an ending pattern in the text and return the part before */
 726+ endPos = GetOffset( text2, endTag, false );
 727+
 728+ if ( endPos == -1 ){
 729+ return [offset,text2];
 730+ }
 731+ endPos += endOffset + offset;
 732+ text3 = text.slice( offset, endPos );
 733+ }
 734+ else{
 735+ return [offset,text2];
 736+ }
 737+ return [offset,text3];
 738+ }
 739+ catch(e){
 740+ return null;
 741+ }
 742+}
 743+
 744+function getPubMedTitle( pmid ){
 745+ try {
 746+ xmlhttp = GetXMLHttpRequest();
 747+ try{
 748+ xmlhttp.async = false;
 749+ } catch(e){
 750+ }
 751+
 752+ xmlhttp.open( "GET", "http://"+ HOST + "/knewco/get.py?http://www.ncbi.nlm.nih.gov/entrez/utils/pmfetch.fcgi?db=PubMed&id="+pmid+"&report=xml&mode=text", false );
 753+ xmlhttp.send(null);
 754+
 755+ if ( xmlhttp.status == 200){
 756+ AuthorsRec = ExtractText( xmlhttp.responseText, "<AuthorList>", 0, "</AuthorList>", 0 );
 757+ if ( AuthorsRec != null ) {
 758+ Authors = AuthorsRec[1];
 759+ }
 760+ else{
 761+ Authors = "";
 762+ }
 763+
 764+ var Offset = 0;
 765+ var AuthorText = "";
 766+ while (true) {
 767+ AuthorRec = ExtractText( Authors.slice( Offset ), "<Author>", 0, "</Author>", 0 );
 768+ if ( AuthorRec == null ){
 769+ break;
 770+ }
 771+ Offset += AuthorRec[0]
 772+ AuthorXml = AuthorRec[1]
 773+ LastNameRec = ExtractText( AuthorXml, "<LastName>", 0, "</LastName>", 0 );
 774+ if ( LastNameRec != null ){
 775+ LastName = LastNameRec[1];
 776+ } else{
 777+ LastName = "";
 778+ }
 779+ FirstNameRec = ExtractText( AuthorXml, "<FirstName>", 0, "</FirstName>", 0 );
 780+ if ( FirstNameRec == null ){
 781+ FirstNameRec = ExtractText( AuthorXml, "<ForeName>", 0, "</ForeName>", 0 );
 782+ }
 783+
 784+ if ( FirstNameRec != null ){
 785+ FirstName = FirstNameRec[1];
 786+ } else {
 787+ FirstName = "";
 788+ }
 789+
 790+ if ( LastName != "" ){
 791+ if ( AuthorText != "" ){
 792+ AuthorText += "; "
 793+ }
 794+
 795+ if ( FirstName != "" ){
 796+ AuthorText += LastName + ", " + FirstName;
 797+ }
 798+ else {
 799+ AuthorText += LastName;
 800+ }
 801+ }
 802+ }
 803+ TitleRec = ExtractText( xmlhttp.responseText, "<ArticleTitle>", 0, "</ArticleTitle>", 0 );
 804+ if ( TitleRec != null ){
 805+ return [AuthorText, TitleRec[1]];
 806+ }
 807+ else {
 808+ return [AuthorText, "unknown title"];
 809+ }
 810+ }
 811+ } catch(e){
 812+ }
 813+ return "";
 814+}
Index: trunk/extensions/Wikidata/OmegaWiki/Editor.php
@@ -993,17 +993,25 @@
994994 }
995995
996996 class ShortTextEditor extends ScalarEditor {
 997+ protected $onChangeHandler;
 998+
 999+ public function __construct(Attribute $attribute = null, PermissionController $permissionController, $isAddField, $onChangeHandler = "") {
 1000+ parent::__construct($attribute, $permissionController, $isAddField);
 1001+
 1002+ $this->onChangeHandler = $onChangeHandler;
 1003+ }
 1004+
9971005 public function getViewHTML(IdStack $idPath, $value) {
9981006 return htmlspecialchars($value);
9991007 }
10001008
10011009 public function getEditHTML(IdStack $idPath, $value) {
1002 - return getTextBox($this->updateId($idPath->getId()), $value);
 1010+ return getTextBox($this->updateId($idPath->getId()), $value, $this->onChangeHandler);
10031011 }
10041012
10051013 public function add(IdStack $idPath) {
10061014 if ($this->isAddField)
1007 - return getTextBox($this->addId($idPath->getId()), "");
 1015+ return getTextBox($this->addId($idPath->getId()), "", $this->onChangeHandler);
10081016 else
10091017 return "";
10101018 }

Status & tagging log