Solr Admin Helper

Options

  1. // ==UserScript==
  2. // @name Solr Admin Helper
  3. // @author Hang Yuan
  4. // @namespace hyuan.solr
  5. // @description Options
  6. // @include //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js
  7. // @version 1.1.9
  8. // @match */solr/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. require.config({
  13. packages: [
  14. { name: 'jquery', location: '//code.jquery.com/jquery-2.1.1.min', main: 'jquery-2.1.1.min' }
  15. , { name: 'jqueryui', location: '//code.jquery.com/ui/1.11.0', main: 'jquery-ui' }
  16. , { name: 'css', location: '//cdnjs.cloudflare.com/ajax/libs/require-css/0.1.1', main: 'css' }
  17. , { name: 'domReady', location: '//cdnjs.cloudflare.com/ajax/libs/require-domReady/2.0.1', main: 'domReady.min' }
  18. ]
  19. , shim: {
  20. 'jquery': { exports: 'jquery' }
  21. , 'jqueryui': { exports: 'jqueryui', deps: ['jquery', 'css!jqueryui/themes/smoothness/jquery-ui'] }
  22. }
  23. , map: {
  24. '*': {
  25. 'css': 'css' // or whatever the path to require-css is
  26. }
  27. }
  28. });
  29.  
  30. require(['jquery', 'jqueryui', 'css!jqueryui/themes/smoothness/jquery-ui', 'domReady'],
  31. function($) {
  32. $(document).ready(function() {
  33. console.log('initializing ...');
  34.  
  35. function addCss(cssString) {
  36. var head = document.getElementsByTagName('head')[0];
  37. //text-alignreturn unless head;
  38. var newCss = document.createElement('style');
  39. newCss.type = "text/css";
  40. newCss.innerHTML = cssString;
  41. head.appendChild(newCss);
  42. }
  43.  
  44. function getSelection() {
  45. if (window.getSelection) {
  46. return window.getSelection().toString();
  47. } else if (document.selection && document.selection.type != "Control") {
  48. return document.selection.createRange().text;
  49. }
  50. }
  51. var ID_REGEXP = /<str name="id">(.*?)<\/str>/gi;
  52. function extractId(val) {
  53. var match = ID_REGEXP.exec($.trim(val));
  54. ID_REGEXP.lastIndex = 0; // XXX need to reset the lastIndex to make the RegExp object reusable
  55. return (match && match[1] || '').replace(/([\/:])/g, '\\$1');
  56. }
  57. function getRecordIds(q) {
  58. var command = {
  59. 'q': q
  60. ,'fl': 'id'
  61. };
  62. return $.ajax({
  63. url: location.protocol + '//' + location.host + '/solr/feedback/select/?wt=json&' + $.param(command)
  64. , type : 'GET'
  65. , contentType : 'application/json'
  66. , dataType : 'json'
  67. });
  68. }
  69. function deleteRecord(id) {
  70. var command = {
  71. "delete": { "id": id }
  72. };
  73. return $.ajax({
  74. url: location.protocol + '//' + location.host + '/solr/feedback/update/?commit=true'
  75. , type : 'POST'
  76. , contentType : 'application/json'
  77. , dataType : 'json'
  78. , data: JSON.stringify(command)
  79. });
  80. }
  81. function modifyRecord(id, field, value) {
  82. var record = {};
  83. record.id = id;
  84. if (value == 'NULL') {
  85. record[field] = { "set": null };
  86. } else {
  87. record[field] = { "set": value };
  88. }
  89.  
  90. return $.ajax({
  91. url: location.protocol + '//' + location.host + '/solr/feedback/update/?commit=true'
  92. , type : 'POST'
  93. , contentType : 'application/json'
  94. , dataType : 'json'
  95. , data: JSON.stringify([record])
  96. });
  97. }
  98. function setUpModifyButton() {
  99. var $modifyRecordDialog = $('<div title="Modify Record">'
  100. + '<form><fieldset>'
  101. + '<label for="solrRecordModifier_id">ID</label>'
  102. + '<input type="text" name="solrRecordModifier_id" id="solrRecordModifier_id" value="" class="text ui-widget-content ui-corner-all" size="70">'
  103. + '<label for="solrRecordModifier_field">Field</label>'
  104. + '<input type="text" name="solrRecordModifier_field" id="solrRecordModifier_field" value="" class="text ui-widget-content ui-corner-all" size="70">'
  105. + '<label for="solrRecordModifier_value">New Value</label>'
  106. + '<input type="text" name="solrRecordModifier_value" id="solrRecordModifier_value" value="" class="text ui-widget-content ui-corner-all" size="70">'
  107. + '<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">'
  108. + '</fieldset></from>'
  109. + '</div>');
  110. $modifyRecordDialog.dialog({
  111. autoOpen: false,
  112. resizable: true,
  113. width:530,
  114. modal: true,
  115. buttons: {
  116. "Submit": function() {
  117. var dialog = this;
  118. modifyRecord($idInput.val(), $fieldInput.val(), $valueInput.val())
  119. .done(function() {
  120. $( dialog ).dialog( "close" );
  121. })
  122. .fail(function(jqXHR, textStatus) {
  123. alert('Failed to modify the specified record. \n\n' + jqXHR.responseText);
  124. });
  125. },
  126. Cancel: function() {
  127. $( this ).dialog( "close" );
  128. }
  129. }
  130. })
  131. var $idInput = $modifyRecordDialog.find('#solrRecordModifier_id');
  132. var $fieldInput = $modifyRecordDialog.find('#solrRecordModifier_field');
  133. var $valueInput = $modifyRecordDialog.find('#solrRecordModifier_value');
  134. var modifyBtn = $('<button id="solrRecordModifier">Modify</button>');
  135. modifyBtn.insertAfter($('#form button:last'));
  136. modifyBtn.click(function(event) {
  137. event.preventDefault();
  138. $idInput.val(extractId(getSelection()));
  139. $modifyRecordDialog.dialog('open');
  140. });
  141. }
  142. function setUpDeleteButton() {
  143. var $deleteRecordDialog = $('<div title="Delete Record">'
  144. + '<p>Are sure to delete the record</p>'
  145. + '<div class="form"><form><fieldset><div class="fieldset">'
  146. + '<label for="solrRecordDeleter_id">ID</label>'
  147. + '<input type="text" name="solrRecordDeleter_id" id="solrRecordDeleter_id" value="" class="text ui-widget-content ui-corner-all" size="70">'
  148. + '</div></fieldset></from></div>'
  149. + '</div>');
  150. $deleteRecordDialog.dialog({
  151. autoOpen: false,
  152. resizable: true,
  153. width:530,
  154. modal: true,
  155. buttons: {
  156. "Delete": function() {
  157. var dialog = this;
  158. if ($idInput.val()) {
  159. deleteRecord($idInput.val())
  160. .done(function() {
  161. $( dialog ).dialog( "close" );
  162. })
  163. .fail(function(jqXHR, textStatus) {
  164. alert('Failed to delete the specified record. \n\n' + jqXHR.responseText);
  165. });
  166. }
  167. },
  168. Cancel: function(jqXHR, textStatus, errorThrown ) {
  169. $( this ).dialog( "close" );
  170. }
  171. }
  172. });
  173. var $idInput = $deleteRecordDialog.find('#solrRecordDeleter_id');
  174. var deleteBtn = $('<button id="solrRecordDeleter">Delete</button>');
  175. deleteBtn.insertAfter($('#form button:last'));
  176. deleteBtn.click(function(event) {
  177. event.preventDefault();
  178. $idInput.val(extractId(getSelection()));
  179. $deleteRecordDialog.dialog('open');
  180. });
  181. }
  182. $(document).one('click', function() {
  183. addCss (
  184. '.ui-dialog-content fieldset { border: 0; text-align: left ! important; }'
  185. + '.ui-dialog-content label, .ui-dialog-content input { display: block; }'
  186. + '.ui-dialog-content input.text { margin-bottom: 12px; padding: 0.4em; width: 95%; }'
  187. );
  188. setUpModifyButton();
  189. setUpDeleteButton();
  190. });
  191. });
  192.  
  193. });