r11979 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r11978‎ | r11979 | r11980 >
Date:15:13, 5 December 2005
Author:eloquence
Status:old
Tags:
Comment:
1) Move namespace initialization into namespace class, where it belongs.
2) Namespace manager now successfully performs all operations. Still needs
some testing for borderline cases.
Modified paths:
  • /branches/wikidata/phase3/includes/Namespace.php (modified) (history)
  • /branches/wikidata/phase3/includes/Setup.php (modified) (history)
  • /branches/wikidata/phase3/includes/SpecialNamespaces.php (modified) (history)
  • /branches/wikidata/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: branches/wikidata/phase3/includes/Setup.php
@@ -191,50 +191,11 @@
192192
193193 wfProfileOut( $fname.'-language1' );
194194
195 -# Initialize namespaces
196 -# Default namespaces are those to which any synonyms should *redirect*
197 -#
198 -# select namespace.ns_number,namespace_names.ns_name,namespace_names.ns_default fromnamespace, namespace_names where namespace.ns_id=namespace_names.ns_id;
199 -global $wgNamespaces; // redundant?
200 -$wgNamespaces = array();
201 -
202195 if ( ! defined( 'MEDIAWIKI_INSTALL' ) ) {
203 - $dbr =& wfGetDB( DB_SLAVE );
204 - $res = $dbr->select( 'namespace',
205 - array('ns_id','ns_search_default','ns_subpages', 'ns_parent', 'ns_target', 'ns_system', 'ns_hidden'),
206 - array(),
207 - 'Setup',
208 - array('ORDER BY'=>'ns_id ASC')
209 - );
210 -
211 - while( $row = $dbr->fetchObject( $res ) ){
212 - # See Namespace.php for documentation on all namespace
213 - # properties which are accessed below.
214 - $id=$row->ns_id;
215 - $wgNamespaces[$id]=new Namespace();
216 - $wgNamespaces[$id]->setIndex($id);
217 - $wgNamespaces[$id]->setSystemType($row->ns_system);
218 - $wgNamespaces[$id]->setSearchedByDefault($row->ns_search_default);
219 - $wgNamespaces[$id]->setSubpages($row->ns_subpages);
220 - $wgNamespaces[$id]->setHidden($row->ns_hidden);
221 - $wgNamespaces[$id]->setTarget($row->ns_target);
222 - $wgNamespaces[$id]->setParentIndex($row->ns_parent);
223 - $res2 = $dbr->select( 'namespace_names', array('ns_name','ns_default,ns_canonical'),
224 - array('ns_id = '. $row->ns_id),
225 - 'Setup', array('order by'=>'ns_default desc,ns_canonical desc,ns_id asc'));
226 -
227 - # Add the list of valid names
228 - while($row2 = $dbr->fetchObject($res2) ) {
229 - $nsi=$wgNamespaces[$id]->addName($row2->ns_name);
230 - if($row2->ns_default) {
231 - $wgNamespaces[$id]->setDefaultNameIndex($nsi);
232 - }
233 - if($row2->ns_canonical) {
234 - $wgNamespaces[$id]->setCanonicalNameIndex($nsi);
235 - }
236 - }
237 - }
238 - $dbr->freeResult( $res );
 196+ # Load namespace definitions from the database
 197+ Namespace::load();
 198+} else {
 199+ $wgNamespaces=array();
239200 }
240201
241202 wfProfileOut( $fname.'-database' );
Index: branches/wikidata/phase3/includes/Namespace.php
@@ -231,6 +231,20 @@
232232 return null;
233233 }
234234 }
 235+
 236+ /**
 237+ Used when a default name is deleted, to assign a new one
 238+ @return int - index to the first non-empty name of this namespace
 239+ null if there are no non-empty names.
 240+ */
 241+ function getNewDefaultNameIndex() {
 242+ foreach($this->names as $nsi=>$name) {
 243+ if(!empty($name)) {
 244+ return $nsi;
 245+ }
 246+ }
 247+ return null;
 248+ }
235249
236250 function setDefaultNameIndex($index) {
237251 $this->defaultNameIndex=$index;
@@ -269,10 +283,11 @@
270284 return null;
271285 }
272286
273 - function setName($oldname,$newname) {
274 - if(!$this->isValidName($newname)) {
 287+ function setName($oldname,$newname,$checkvalid=true) {
 288+ if($checkvalid && !$this->isValidName($newname)) {
275289 return NULL;
276290 }
 291+ $newname=strtr($newname, ' ','_');
277292 $nsi=$this->getNameIndexForName($oldname);
278293 if(!is_null($nsi)) {
279294 $this->names[$nsi]=$newname;
@@ -573,17 +588,6 @@
574589 }
575590 }
576591
577 - # Check if we're not creating duplicate names
578 - # by modifying an existing namespace
579 - if($operation==NS_NAME_MODIFY) {
580 - foreach($this->names as $exname) {
581 - if($exname == $name) {
582 - $rv[NS_RESULT] = NS_NAME_ISSUES;
583 - $rv[NS_DUPLICATE_NAMES][]=$name;
584 - }
585 - }
586 - }
587 -
588592 # Interwiki
589593 if(Title::getInterwikiLink( $name)) {
590594 $rv[NS_RESULT]=NS_NAME_ISSUES;
@@ -664,7 +668,57 @@
665669 array()
666670 );
667671 }
 672+ }
 673+ foreach($nameOperations as $name=>$operation) {
 674+ if($operation==NS_NAME_ADD) {
 675+ $isDefault = ($name==$this->getDefaultName());
 676+ $isCanonical = ($name==$this->getCanonicalName());
 677+ if(!$testSave) {
 678+ $dbm->insert(
 679+ 'namespace_names',
 680+ array(
 681+ 'ns_id'=>$this->getIndex(),
 682+ 'ns_name'=>$name,
 683+ 'ns_default'=>$isDefault,
 684+ 'ns_canonical'=>$isCanonical
 685+ ),
 686+ $fname,
 687+ array()
 688+ );
 689+ }
 690+ } elseif($operation==NS_NAME_MODIFY) { $oldname = $wgNamespaces[$index]->names[$this->getNameIndexForName($name)];
 691+ if(!$testSave) {
 692+ $dbm->update(
 693+ 'namespace_names',
 694+ array( /* SET */
 695+ 'ns_name'=>$name,
 696+ ),
 697+ array(
 698+ 'ns_name'=>$oldname),
 699+ $fname);
 700+ }
 701+ } elseif($operation==NS_NAME_DELETE) {
 702+ $dbm->delete(
 703+ 'namespace_names',
 704+ array('ns_name'=>$name),
 705+ '*');
 706+ }
 707+ }
 708+ if($create) {
 709+ $rv[NS_RESULT]=NS_CREATED;
 710+
 711+ # If this was just a test for a new
 712+ # namespace, reset the index to NULL so
 713+ # it will be created for real
 714+ # if save() is called on the same object.
 715+ if($testSave) {
 716+ $this->setIndex(NULL);
 717+ }
668718 } else {
 719+ # Set canonical and default names.
 720+ # This needs to happen after other name operations
 721+ # because we can't operate on the new names until
 722+ # they exist. :-)
669723 $oldDefaultName=$wgNamespaces[$index]->getDefaultName();
670724 $newDefaultName=$this->getDefaultName();
671725
@@ -709,56 +763,14 @@
710764 );
711765 }
712766 }
713 - }
714 - foreach($nameOperations as $name=>$operation) {
715 - if($operation==NS_NAME_ADD) {
716 - $isDefault = ($name==$this->getDefaultName());
717 - $isCanonical = ($name==$this->getCanonicalName());
718 - if(!$testSave) {
719 - $dbm->insert(
720 - 'namespace_names',
721 - array(
722 - 'ns_id'=>$this->getIndex(),
723 - 'ns_name'=>$name,
724 - 'ns_default'=>$isDefault,
725 - 'ns_canonical'=>$isCanonical
726 - ),
727 - $fname,
728 - array()
729 - );
730 - }
731 - } elseif($operation==NS_NAME_MODIFY) { $oldname=$wgNamespaces[$this->getNameIndexForName($name)];
732 - if(!$testSave) {
733 - $dbm->update(
734 - 'namespace_name',
735 - array( /* SET */
736 - 'ns_name'=>$name,
737 - ),
738 - array(
739 - 'ns_name'=>$oldname),
740 - $fname);
741 - }
742 - } elseif($operation==NS_NAME_DELETE) {
743 - $dbm->delete(
744 - 'namespace_names',
745 - array('ns_name'=>$name),
746 - '*');
747 - }
748 - }
749 - if($create) {
750 - $rv[NS_RESULT]=NS_CREATED;
751 -
752 - # If this was just a test for a new
753 - # namespace, reset the index to NULL so
754 - # it will be created for real
755 - # if save() is called on the same object.
756 - if($testSave) {
757 - $this->setIndex(NULL);
758 - }
759 - } else {
 767+
760768 $rv[NS_RESULT]=NS_MODIFIED;
761769 }
762770 $rv[NS_SAVE_ID]=$index;
 771+
 772+ # Note that it may be desirable to call Namespace::load()
 773+ # in addition to this since the name (not namespace) indexes in
 774+ # the database can be different from the one in the array.
763775 if(!$testSave) {
764776 $wgNamespaces[$index]=$this;
765777 }
@@ -829,6 +841,55 @@
830842 return true;
831843 }
832844
 845+ /**
 846+ * Load or reload namespace definitions from the database
 847+ * into a global array.
 848+ *
 849+ * @static
 850+ */
 851+ function load() {
 852+
 853+ global $wgNamespaces;
 854+ $wgNamespaces = array();
 855+ $dbr =& wfGetDB( DB_SLAVE );
 856+ $res = $dbr->select( 'namespace',
 857+ array('ns_id','ns_search_default','ns_subpages', 'ns_parent', 'ns_target', 'ns_system', 'ns_hidden'),
 858+ array(),
 859+ 'Setup',
 860+ array('ORDER BY'=>'ns_id ASC')
 861+ );
 862+ while( $row = $dbr->fetchObject( $res ) ){
 863+ # See Namespace.php for documentation on all namespace
 864+ # properties which are accessed below.
 865+ $id=$row->ns_id;
 866+ $wgNamespaces[$id]=new Namespace();
 867+ $wgNamespaces[$id]->setIndex($id);
 868+ $wgNamespaces[$id]->setSystemType($row->ns_system);
 869+ $wgNamespaces[$id]->setSearchedByDefault($row->ns_search_default);
 870+ $wgNamespaces[$id]->setSubpages($row->ns_subpages);
 871+ $wgNamespaces[$id]->setHidden($row->ns_hidden);
 872+ $wgNamespaces[$id]->setTarget($row->ns_target);
 873+ $wgNamespaces[$id]->setParentIndex($row->ns_parent);
 874+ $res2 = $dbr->select( 'namespace_names', array('ns_name','ns_default,ns_canonical'),
 875+ array('ns_id = '. $row->ns_id),
 876+ 'Setup', array('order by'=>'ns_default desc,ns_canonical desc,ns_id asc'));
 877+
 878+ # Add the list of valid names
 879+ while($row2 = $dbr->fetchObject($res2) ) {
 880+ $nsi=$wgNamespaces[$id]->addName($row2->ns_name);
 881+ if($row2->ns_default) {
 882+ $wgNamespaces[$id]->setDefaultNameIndex($nsi);
 883+ }
 884+ if($row2->ns_canonical) {
 885+ $wgNamespaces[$id]->setCanonicalNameIndex($nsi);
 886+ }
 887+ }
 888+ }
 889+ $dbr->freeResult( $res );
 890+ }
 891+
 892+
 893+
833894 }
834895
835896 }
Index: branches/wikidata/phase3/includes/SpecialNamespaces.php
@@ -418,8 +418,9 @@
419419 foreach($wgNamespaces as $ns) {
420420 $nsindex=$ns->getIndex();
421421 $newns[$nsindex]=new Namespace();
422 - $newns[$nsindex]->setIndex($nsindex);
423 -
 422+ $newns[$nsindex]->setIndex($nsindex);
 423+ $newns[$nsindex]->setSystemType($ns->getSystemType());
 424+
424425 if(!$ns->isSpecial()) {
425426 $subvar="ns{$nsindex}Subpages";
426427 $searchvar="ns{$nsindex}Search";
@@ -429,7 +430,7 @@
430431 $subpages=$wgRequest->getBool($subvar);
431432 $searchdefault=$wgRequest->getBool($searchvar);
432433 $hidden=$wgRequest->getBool($hiddenvar);
433 - $prefix=$wgRequest->getBool($prefixvar);
 434+ $prefix=$wgRequest->getText($prefixvar);
434435 $parent=$wgRequest->getIntOrNull($parentvar);
435436 $newns[$nsindex]->setSubpages($subpages);
436437 $newns[$nsindex]->setSearchedByDefault($searchdefault);
@@ -439,50 +440,65 @@
440441 $newns[$nsindex]->setParentIndex($parent);
441442 }
442443 }
 444+ $newns[$nsindex]->names=$ns->names;
443445
 446+ # This can never be changed by the user.
 447+ $newns[$nsindex]->setCanonicalNameIndex($ns->getCanonicalNameIndex());
 448+
 449+ # New names, appended to end
 450+ for($i=1;$i<=3;$i++) {
 451+ $nvar="ns{$nsindex}NewName{$i}";
 452+ if($nname=$wgRequest->getText($nvar)) {
 453+ $newns[$nsindex]->addName($nname);
 454+ }
 455+ }
 456+
 457+ # Changes and deletions. Do them last since they can
 458+ # affect index slots of existing names.
444459 foreach($ns->names as $nameindex=>$name) {
445460 $var="ns{$nsindex}Name{$nameindex}";
446461 if($req=$wgRequest->getText($var)) {
447 - wfDebug("Name var $var contains $req\n");
448 - $newns[$nsindex]->names[$nameindex]=$req;
 462+ #wfDebug("Name var $var contains $req\n");
 463+
 464+ # Alter name if necessary.
 465+ if($req!=$name) {
 466+
 467+ # The last parameter means
 468+ # that we do not check if the
 469+ # name is valid - this
 470+ # is done later for all names.
 471+ $newns[$nsindex]->setName(
 472+ $name,$req,false
 473+ );
 474+
 475+ #wfDebug("Setting name $nameindex of namespace $nsindex to $req. Old name is $name.\n");
 476+ }
449477 }
450478 $delvar="ns{$nsindex}Delete{$nameindex}";
451479 if($wgRequest->getInt($delvar)) {
452 - wfDebug("$delvar should be deleted.\n");
 480+ #wfDebug("$delvar should be deleted.\n");
453481 $newns[$nsindex]->removeNameByIndex($nameindex);
454482 }
455483 }
456484
457 - # Canonical names cannot be changed through the UI
458 - if(!is_null($ns->getCanonicalNameIndex())) {
459 - $cindex=$newns[$nsindex]->addName($ns->getCanonicalName());
460 - $newns[$nsindex]->setCanonicalNameIndex($cindex);
461 - }
462 -
463 - # New names, appended to end
464 - for($i=1;$i<=3;$i++) {
465 - $nvar="ns{$nsindex}NewName{$i}";
466 - if($nname=$wgRequest->getText($nvar)) {
467 - $newns[$nsindex]->addName($nname);
468 - }
469 - }
470 -
471 - # Which default name? (deleted names?)
472485 $dvar="ns{$nsindex}Default";
473 - $dreq=$wgRequest->getIntOrNull($dvar);
474 - if(!is_null($dreq)) {
475 - wfDebug("Default name: $dreq\n");
476 - $newns[$nsindex]->setDefaultNameIndex($dreq);
 486+ # Did the user select a default name?
 487+ $dindex=$wgRequest->getIntOrNull($dvar);
 488+ # If not, get the old one.
 489+ if(is_null($dindex)) { $dindex=$ns->getDefaultNameIndex();}
 490+ # Does the name exist and is it non-empty?
 491+ if(!is_null($dindex) && array_key_exists($dindex, $newns[$nsindex]->names) && !empty($newns[$nsindex]->names[$dindex]) ) {
 492+ # Use this default name.
 493+ $newns[$nsindex]->setDefaultNameIndex($dindex);
 494+ #wfDebug("Setting index for $nsindex to $dindex!\n");
477495 } else {
478 - $newns[$nsindex]->setDefaultNameIndex(
479 - $ns->getDefaultNameIndex()
480 - );
481 - wfDebug("No default name submitted, using previous one.\n");
 496+ # We have lost our default name, perhaps
 497+ # it was deleted. Get a new one if
 498+ # possible.
 499+ $newns[$nsindex]->setDefaultNameIndex($newns[$nsindex]->getNewDefaultNameIndex());
 500+
482501 }
483 -
484 -
485502 }
486 -
487503 foreach($newns as $nns) {
488504 $nrv=$nns->testSave();
489505 if($nrv[NS_RESULT]==NS_NAME_ISSUES) {
@@ -491,6 +507,14 @@
492508 }
493509 $nns->save();
494510 }
 511+
 512+ # IMPORTANT: The namespace name indexes are unpredictable when
 513+ # serialized, so we have to reload the definitions from the
 514+ # database at this point; otherwise, there could be index
 515+ # mismatches.
 516+ Namespace::load();
 517+
 518+ # Return to the namespace manager with the changes made.
495519 $wgOut->addWikiText(wfMsg("namespace_changes_saved"));
496520 $this->showForm();
497521 return true;
Index: branches/wikidata/phase3/languages/Language.php
@@ -2198,7 +2198,7 @@
21992199 'namespace_name_prefix'=>'There are pages which contain this name as a "pseudonamespace" in the title. These pages would become invisible if the namespace was created.',
22002200 'namespace_name_interwiki'=>'The name is already used as an Interwiki prefix to link to another wiki site. This Interwiki prefix would become unusable if this name was used.',
22012201 'namespace_name_linked'=>'The namespace name is still linked to from some pages. If it was deleted, these links would be broken.',
2202 -'namespace_error'=>'The namespace "$1" cannot be created.',
 2202+'namespace_error'=>'The namespace "$1" cannot be created or modified.',
22032203 'namespace_delete_error'=>'The namespace "$1" cannot be deleted.',
22042204 'namespace_deleted'=>'The namespace "$1" has been deleted successfully.',
22052205 'namespace_changes_saved'=>'The namespace changes have been saved.',

Status & tagging log