r22425 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22424‎ | r22425 | r22426 >
Date:14:05, 25 May 2007
Author:erik
Status:old
Tags:
Comment:
support for wikidata prefs panel & dataset pref using austin che's pref extension
Modified paths:
  • /trunk/extensions/Wikidata/AddPrefs.php (added) (history)
  • /trunk/extensions/Wikidata/App.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/SpecialConceptMapping.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/Wikidata.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/App.php
@@ -4,6 +4,8 @@
55 $wgHooks['BeforePageDisplay'][]='addWikidataHeader';
66 $wgHooks['GetEditLinkTrail'][]='addWikidataEditLinkTrail';
77 $wgHooks['GetHistoryLinkTrail'][]='addHistoryLinkTrail';
 8+$wgExtensionFunctions[]='initializeWikidataMessages';
 9+
810 $wgCustomHandlerPath = array('*'=>"{$IP}/extensions/Wikidata/OmegaWiki/");
911 $wgDefaultClassMids = array(402295);
1012
@@ -31,6 +33,7 @@
3234 $wgGroupPermissions['wikidata-omega']['editwikidata-tt']=false;
3335 $wgGroupPermissions['wikidata-test']['editwikidata-tt']=true;
3436
 37+require_once("{$IP}/extensions/Wikidata/AddPrefs.php");
3538 require_once("{$IP}/extensions/Wikidata/SpecialLanguages.php");
3639 require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialSuggest.php");
3740 require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialSelect.php");
@@ -39,6 +42,13 @@
4043 require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialNeedsTranslation.php");
4144 require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialImportLangNames.php");
4245 require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialAddCollection.php");
 46+
 47+$wgExtensionPreferences[]=array(
 48+ 'name'=>'ow_uipref_context',
 49+ 'section'=>'ow_uiprefs',
 50+ 'type'=>PREF_TEXT_T,
 51+ 'size'=>10);
 52+
4353 require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialConceptMapping.php");
4454 function addWikidataHeader() {
4555 global $wgOut,$wgScriptPath;
@@ -65,4 +75,24 @@
6676 }
6777 }
6878
 79+function initializeWikidataMessages() {
 80+ global
 81+ $wgMessageCache;
 82+
 83+ $wgMessageCache->addMessages(
 84+ array(
 85+ 'ow_uilang'=>'Your user interface language: $1',
 86+ 'ow_uilang_set'=>'Set your preferences',
 87+ 'ow_save' => 'Save',
 88+ 'ow_history' => 'History',
 89+ 'ow_datasets' => 'Data-set selection',
 90+ 'ow_noedit' => 'You are not permitted to edit pages in the dataset "$1". Please see [[Project:Permission policy|our editing policy]].',
 91+ 'ow_noedit_title' => 'No permission to edit',
 92+ 'ow_uipref_context' => 'Default dataset prefix (without underscore)',
 93+ 'ow_uiprefs' => 'Wikidata',
 94+ )
 95+ );
 96+
 97+}
 98+
6999 ?>
Index: trunk/extensions/Wikidata/OmegaWiki/Wikidata.php
@@ -25,22 +25,7 @@
2626
2727
2828 public function __construct() {
29 - global
30 - $wgMessageCache;
3129
32 - $wgMessageCache->addMessages(
33 - array(
34 - 'ow_uilang'=>'Your user interface language: $1',
35 - 'ow_uilang_set'=>'Set your preferences',
36 - 'ow_save' => 'Save',
37 - 'ow_history' => 'History',
38 - 'ow_datasets' => 'Data-set selection',
39 - 'ow_noedit' => 'You are not permitted to edit pages in the dataset "$1". Please see [[Project:Permission policy|our editing policy]].',
40 - 'ow_noedit_title' => 'No permission to edit',
41 -
42 - )
43 - );
44 -
4530 global
4631 $wgAvailableAuthorities, $wgFilterLanguageId, $wgShowLanguageSelector,
4732 $wgShowClassicPageTitles, $wgPossiblySynonymousRelationTypeId;
@@ -155,7 +140,6 @@
156141 global $wgTitle, $wgUser;
157142 $dc=wdGetDataSetContext();
158143 $ow_datasets=wfMsg('ow_datasets');
159 -
160144 $html="<div class=\"dataset-panel\">";;
161145 $html.="<table border=\"0\"><tr><th class=\"dataset-panel-heading\">$ow_datasets</th></tr>";
162146 $dataSets=wdGetDataSets();
@@ -324,6 +308,7 @@
325309 $datasets=wdGetDataSets();
326310 $groups=$wgUser->getGroups();
327311 $dbs=wfGetDB(DB_SLAVE);
 312+ $pref=$wgUser->getOption('ow_uipref_context');
328313
329314 $trydefault='';
330315 foreach($groups as $group) {
@@ -332,9 +317,16 @@
333318 $trydefault=$wdGroupDefaultView[$group];
334319 }
335320 }
 321+
 322+ # URL parameter takes precedence over all else
336323 if( ($ds=$wgRequest->getText('dataset')) && array_key_exists($ds,$datasets) && $dbs->tableExists($ds."_transactions") ) {
337324 return $datasets[$ds];
338 - } elseif(!empty($trydefault) && array_key_exists($trydefault,$datasets)) {
 325+ # User preference
 326+ } elseif(!empty($pref) && array_key_exists($pref,$datasets)) {
 327+ return $datasets[$pref];
 328+ }
 329+ # Group preference
 330+ elseif(!empty($trydefault) && array_key_exists($trydefault,$datasets)) {
339331 return $datasets[$trydefault];
340332 } else {
341333 return $datasets[$wdDefaultViewDataSet];
Index: trunk/extensions/Wikidata/OmegaWiki/SpecialConceptMapping.php
@@ -16,7 +16,7 @@
1717 'author' => 'Kim Bruning',
1818 );
1919
20 -define(help_text,
 20+define('help_text',
2121 "<p>possible actions: <ul>
2222 <li>&action=insert&<data_context_prefix>=<defined_id>&... insert a mapping</li>
2323 <li>&action=get&concept=<concept_id> read a mapping back</li>
Index: trunk/extensions/Wikidata/AddPrefs.php
@@ -0,0 +1,206 @@
 2+<?php
 3+
 4+/**
 5+ * This allows other extensions to add their own preferences to the default preferences display
 6+ *
 7+ * Author: Austin Che <http://openwetware.org/wiki/User:Austin>
 8+ */
 9+
 10+$wgExtensionCredits['specialpage'][] = array(
 11+ 'name' => 'PreferencesExtension',
 12+ 'version' => '2006/11/16',
 13+ 'author' => 'Austin Che',
 14+ 'url' => 'http://openwetware.org/wiki/User:Austin/Extensions/PreferencesExtension',
 15+ 'description' => 'Enables extending user preferences',
 16+);
 17+$wgHooks['SpecialPage_initList'][] = 'wfOverridePreferences';
 18+
 19+// constants for pref types
 20+define('PREF_USER_T', 1);
 21+define('PREF_TOGGLE_T', 2);
 22+define('PREF_TEXT_T', 3);
 23+define('PREF_PASSWORD_T', 4);
 24+define('PREF_INT_T', 5);
 25+
 26+// each element of the following should be an array that can have keys:
 27+// name, section, type, size, validate, load, save, html, min, max, default
 28+if (!isset($wgExtensionPreferences))
 29+ $wgExtensionPreferences = array();
 30+
 31+/**
 32+ * Adds an array of prefs to be displayed in the user preferences
 33+ *
 34+ * @param array $prefs
 35+ */
 36+function wfAddPreferences($prefs)
 37+{
 38+ global $wgExtensionPreferences;
 39+
 40+ foreach ($prefs as $pref)
 41+ {
 42+ $wgExtensionPreferences[] = $pref;
 43+ }
 44+}
 45+
 46+function wfOverridePreferences(&$list)
 47+{
 48+ // we 'override' the default preferences special page with our own
 49+ $list["Preferences"] = array("SpecialPage", "Preferences", "", true, "wfSpecialPreferencesExtension");
 50+ return true;
 51+}
 52+
 53+function wfSpecialPreferencesExtension()
 54+{
 55+ require_once('SpecialPreferences.php');
 56+
 57+ // override the default preferences form
 58+ class SpecialPreferencesExtension extends PreferencesForm
 59+ {
 60+ // unlike parent, we don't load in posted form values in constructor
 61+ // until savePreferences when we need it
 62+ // we also don't need resetPrefs, instead loading the newest values when displaying the form
 63+ // finally parent's execute function doesn't need overriding
 64+ // this leaves only two functions to override
 65+ // one for displaying the form and one for saving the values
 66+
 67+ function savePreferences()
 68+ {
 69+ // handle extension prefs first
 70+ global $wgUser, $wgRequest;
 71+ global $wgExtensionPreferences;
 72+
 73+ foreach ($wgExtensionPreferences as $p)
 74+ {
 75+ $name = isset($p['name']) ? $p['name'] : "";
 76+ if (! $name)
 77+ continue;
 78+
 79+ $value = $wgRequest->getVal($name);
 80+ $type = isset($p['type']) ? $p['type'] : PREF_USER_T;
 81+ switch ($type)
 82+ {
 83+ case PREF_TOGGLE_T:
 84+ if (isset($p['save']))
 85+ $p['save']($name, $value);
 86+ else
 87+ $wgUser->setOption($name, $wgRequest->getCheck("wpOp{$name}"));
 88+ break;
 89+
 90+ case PREF_INT_T:
 91+ $min = isset($p['min']) ? $p['min'] : 0;
 92+ $max = isset($p['max']) ? $p['max'] : 0x7fffffff;
 93+ if (isset($p['save']))
 94+ $p['save']($name, $value);
 95+ else
 96+ $wgUser->setOption($name, $this->validateIntOrNull($value, $min, $max));
 97+ break;
 98+
 99+ case PREF_PASSWORD_T:
 100+ case PREF_TEXT_T:
 101+ case PREF_USER_T:
 102+ default:
 103+ if (isset($p['validate']))
 104+ $value = $p['validate']($value);
 105+ if (isset($p['save']))
 106+ $p['save']($name, $value);
 107+ else
 108+ $wgUser->setOption($name, $value);
 109+ break;
 110+ }
 111+ }
 112+
 113+ // call parent's function which saves the normal prefs and writes to the db
 114+ parent::savePreferences();
 115+ }
 116+
 117+ function mainPrefsForm( $status , $message = '' )
 118+ {
 119+ global $wgOut, $wgRequest, $wgUser;
 120+ global $wgExtensionPreferences;
 121+
 122+ // first get original form, then hack into it new options
 123+ parent::mainPrefsForm($status, $message);
 124+ $html = $wgOut->getHTML();
 125+ $wgOut->clearHTML();
 126+
 127+ $sections = array();
 128+ foreach ($wgExtensionPreferences as $p)
 129+ {
 130+ if (! isset($p['section']) || ! $p['section'])
 131+ continue;
 132+ $section = $p['section'];
 133+
 134+ $name = isset($p['name']) ? $p['name'] : "";
 135+ $value = "";
 136+ if ($name)
 137+ {
 138+ if (isset($p['load']))
 139+ $value = $p['load']($name);
 140+ else
 141+ $value = $wgUser->getOption($name);
 142+ }
 143+ if ($value === '' && isset($p['default']))
 144+ $value = $p['default'];
 145+
 146+ $sectext = htmlspecialchars(wfMsg($section));
 147+ $regex = "/(<fieldset>\s*<legend>\s*" . preg_quote($sectext) .
 148+ "\s*<\/legend>.*?)(<\/fieldset>)/s";
 149+
 150+ // check if $section exists in prefs yet
 151+ // cache the existence of sections
 152+ if (!isset($sections[$section]))
 153+ {
 154+ $sections[$section] = true;
 155+
 156+ if (! preg_match($regex, $html, $m))
 157+ {
 158+ // doesn't exist so add an empty section to end
 159+ $addhtml = "<fieldset><legend>$sectext</legend></fieldset>";
 160+ $html = preg_replace("/(<div id='prefsubmit'.*)/s", "$addhtml $1", $html);
 161+ }
 162+
 163+ }
 164+
 165+ $type = isset($p['type']) ? $p['type'] : PREF_USER_T;
 166+ switch ($type)
 167+ {
 168+ case PREF_TOGGLE_T:
 169+ $addhtml = $this->getToggle($name);
 170+ break;
 171+
 172+ case PREF_INT_T:
 173+ case PREF_TEXT_T:
 174+ case PREF_PASSWORD_T:
 175+ $size = isset($p['size']) && $p['size'] ? "size=\"{$p['size']}\"" : "";
 176+ $caption = isset($p['caption']) && $p['caption'] ? $p['caption'] : wfMsg($name);
 177+ if ($type == PREF_PASSWORD_T)
 178+ $type = "password";
 179+ else
 180+ $type = "text";
 181+ $addhtml = "<table>" .
 182+ $this->addRow("<label for=\"{$name}\">$caption</label>",
 183+ "<input type=\"$type\" name=\"{$name}\" value=\"{$value}\" $size />") . "</table>" ;
 184+ break;
 185+
 186+ case PREF_USER_T:
 187+ default:
 188+ $addhtml = preg_replace("/@VALUE@/", $value, isset($p['html']) ? $p['html'] : "");
 189+ break;
 190+ }
 191+
 192+ // the section exists
 193+ $html = preg_replace($regex, "$1 $addhtml $2", $html);
 194+ }
 195+
 196+ $wgOut->addHTML($html);
 197+
 198+ // debugging
 199+ //$wgOut->addHTML($wgUser->encodeOptions());
 200+ }
 201+ }
 202+
 203+ global $wgRequest;
 204+ $prefs = new SpecialPreferencesExtension($wgRequest);
 205+ $prefs->execute();
 206+}
 207+?>
\ No newline at end of file