@@ -3478,7 +3478,7 @@ function pginfo_rename_db_write ($fromname, $toname) {
34783478 }
34793479
34803480 // リンク情報更新準備
3481- $ this ->plain_db_write ($ fromname ," delete " );
3481+ $ this ->plain_db_write ($ fromname , ' delete ' );
34823482
34833483 $ _toname = addslashes ($ toname );
34843484 $ reading = addslashes ($ this ->get_page_reading ($ toname , TRUE ));
@@ -3711,14 +3711,21 @@ function plain_db_write($page, $action, $init = FALSE, $notimestamp = FALSE)
37113711 // ページ削除
37123712 elseif ($ action === 'delete ' )
37133713 {
3714- $ query = "DELETE FROM " .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname ."_plain " )." WHERE pgid = $ pgid; " ;
3714+ $ plugin = end ($ this ->root ->plugin_stack );
3715+
3716+ $ query = "DELETE FROM " .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname ."_plain " )." WHERE pgid = $ pgid " ;
37153717 $ result =$ this ->xpwiki ->db ->queryF ($ query );
3716- //if (!$result) echo $query."<hr>";
37173718
37183719 //リンクページ
3719- $ query = "DELETE FROM " .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname ."_rel " )." WHERE pgid = " .$ pgid ." OR relid = " .$ pgid. " ; " ;
3720+ $ query = "DELETE FROM " .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname ."_rel " )." WHERE pgid = " .$ pgid ." OR relid = " .$ pgid ;
37203721 $ result =$ this ->xpwiki ->db ->queryF ($ query );
37213722
3723+ if ($ plugin !== 'rename ' ) {
3724+ // alias
3725+ $ query = "DELETE FROM " .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname ."_alias " )." WHERE pgid = $ pgid " ;
3726+ $ result =$ this ->xpwiki ->db ->queryF ($ query );
3727+ }
3728+
37223729 // Optimaize DB tables
37233730 $ tables = array ();
37243731 foreach (array ('attach ' , 'cache ' , 'count ' , 'pginfo ' , 'plain ' , 'rel ' , 'tb ' ) as $ table ) {
@@ -4327,32 +4334,34 @@ function get_page_reading ($page, $init = FALSE) {
43274334
43284335 // ページ別名を取得
43294336 function get_page_alias ($ page , $ as_array = false , $ clr = false , $ path = 'real ' ) {
4330- static $ pg_ary ;
4331-
4337+ static $ pg_ary = array ();
4338+
4339+ if (! isset ($ pg_ary [$ this ->xpwiki ->pid ])) {
4340+ $ pg_ary [$ this ->xpwiki ->pid ] = array ();
4341+ }
4342+
43324343 if ($ path !== 'real ' ) $ path = 'relative ' ;
43334344
4334- if ($ clr || !isset ($ pg_ary [$ this ->xpwiki ->pid ])) {
4335- $ _tmp = $ pg_ary [$ this ->xpwiki ->pid ] = array ();
4345+ if ($ clr || ! isset ($ pg_ary [$ this ->xpwiki ->pid ][ $ page ])) {
4346+ $ pg_ary [$ this ->xpwiki ->pid ][ $ page ] = array ();
43364347 $ dirreg = '#^ ' . preg_quote ($ this ->page_dirname ($ page ), '# ' ) . '/# ' ;
4337- foreach ($ this ->root ->page_aliases as $ _alias => $ _page ) {
4338- $ pg_ary [$ this ->xpwiki ->pid ][$ _page ]['real ' ][] = $ _alias ;
4339- $ pg_ary [$ this ->xpwiki ->pid ][$ _page ]['relative ' ][] = preg_replace ($ dirreg , '../ ' , $ _alias );
4340- }
4341- foreach ($ pg_ary [$ this ->xpwiki ->pid ] as $ _page => $ _ary ) {
4342- natcasesort ($ pg_ary [$ this ->xpwiki ->pid ][$ _page ]['real ' ]);
4343- natcasesort ($ pg_ary [$ this ->xpwiki ->pid ][$ _page ]['relative ' ]);
4344- }
4348+ $ _alias = $ this ->get_pagealiases (array ($ page ));
4349+ $ pg_ary [$ this ->xpwiki ->pid ][$ page ]['real ' ] = $ _alias ;
4350+ $ pg_ary [$ this ->xpwiki ->pid ][$ page ]['relative ' ] = preg_replace ($ dirreg , '../ ' , $ _alias );
4351+ natcasesort ($ pg_ary [$ this ->xpwiki ->pid ][$ page ]['real ' ]);
4352+ natcasesort ($ pg_ary [$ this ->xpwiki ->pid ][$ page ]['relative ' ]);
43454353 }
4346-
4347- $ ret = (isset ($ pg_ary [$ this ->xpwiki ->pid ][$ page ]))? $ pg_ary [$ this ->xpwiki ->pid ][$ page ][$ path ] : array ();
4354+
4355+ $ ret = (! empty ($ pg_ary [$ this ->xpwiki ->pid ][$ page ]))? $ pg_ary [$ this ->xpwiki ->pid ][$ page ][$ path ] : array ();
4356+
43484357 if ($ as_array ) return $ ret ;
43494358 return join (': ' , $ ret );
43504359 }
43514360
43524361 // ページ別名を保存
43534362 function put_page_alias ($ page , $ alias ) {
4354- if (!$ alias && in_array ( $ page , $ this ->root -> page_aliases ) === false ) return false ;
4355-
4363+ if (!$ alias && ! $ aliases_old = $ this ->get_page_alias ( $ page , true ) ) return false ;
4364+
43564365 $ alias = trim ($ alias );
43574366 $ alias = preg_replace ('/[\r\n]+/ ' , ': ' , $ alias );
43584367 $ aliases = explode (': ' , $ alias );
@@ -4364,74 +4373,94 @@ function put_page_alias ($page, $alias) {
43644373 natcasesort ($ aliases );
43654374 $ aliases = array_slice ($ aliases , 0 );
43664375 }
4367-
4376+
43684377 $ aliases_old = $ this ->get_page_alias ($ page , true );
43694378 $ aliases_old = array_slice ($ aliases_old , 0 );
4370-
4379+
43714380 if ($ aliases_old === $ aliases ) return false ;
4372-
4373- $ this ->root ->page_aliases = array_diff ($ this ->root ->page_aliases , array ($ page ));
4374-
4381+
43754382 if ($ alias ) {
4383+ $ pgid = $ this ->get_pgid_by_name ($ page );
4384+ $ query = 'DELETE FROM ` ' .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname .'_alias ' ).'` WHERE `pgid`= ' .$ pgid ;
4385+ $ this ->xpwiki ->db ->query ($ query );
4386+ $ query = array ();
43764387 foreach ($ aliases as $ _alias ) {
43774388 $ _check = ($ this ->root ->page_case_insensitive )? $ this ->get_pagename_realcase ($ _alias ) : $ _alias ;
4378- if (!isset ( $ this -> root -> page_aliases [ $ _alias ]) && ! $ this ->is_page ($ _check )) {
4379- $ this ->root -> page_aliases [ $ _alias] = $ page ;
4389+ if (!$ this ->is_page ($ _check )) {
4390+ $ query [] = ' ( ' . $ this ->xpwiki -> db -> quoteString ( $ _alias). ' , ' . $ this -> get_pgid_by_name ( $ page). ' ) ' ;
43804391 }
43814392 }
4393+ if ($ query ) {
4394+ $ query = 'INSERT INTO ` ' .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname .'_alias ' ).'` (`name`,`pgid`) VALUES ' .join (', ' , $ query );
4395+ $ this ->xpwiki ->db ->query ($ query );
4396+ }
43824397 }
4383-
4384- // save
4385- $ this ->save_page_alias ();
4386-
4398+
43874399 // Cache remake of get_page_alias()
4388- $ this ->get_page_alias ('' , true , true );
4389-
4400+ $ this ->get_page_alias ($ page , true , true );
4401+
43904402 return true ;
43914403 }
43924404
43934405 function save_page_alias () {
4394- natcasesort ($ this ->root ->page_aliases );
4395- $ page_aliases_i = array_change_key_case ($ this ->root ->page_aliases , CASE_LOWER );
4396-
4397- $ quote ['from ' ] = array ('\\' , "' " ,);
4398- $ quote ['to ' ] = array ('\\\\' , "\\' " );
4399-
4400- $ dat = "\$root->page_aliases = array( \n" ;
4401- foreach ($ this ->root ->page_aliases as $ _alias => $ _page ) {
4402- $ _alias = str_replace ($ quote ['from ' ], $ quote ['to ' ], $ _alias );
4403- $ _page = str_replace ($ quote ['from ' ], $ quote ['to ' ], $ _page );
4404- $ dat .= "\t' {$ _alias }' => ' {$ _page }', \n" ;
4405- }
4406- $ dat .= "); \n" ;
4407-
4408- //$this->save_config('pukiwiki.ini.php', 'page_aliases', $dat);
4409-
4410- $ dat .= "\$root->page_aliases_i = array( \n" ;
4411- foreach ($ page_aliases_i as $ _alias => $ _page ) {
4412- $ _alias = str_replace ($ quote ['from ' ], $ quote ['to ' ], $ _alias );
4413- $ _page = str_replace ($ quote ['from ' ], $ quote ['to ' ], $ _page );
4414- $ dat .= "\t' {$ _alias }' => ' {$ _page }', \n" ;
4415- }
4416- $ dat .= "); " ;
4417-
4418- $ this ->save_config ('pukiwiki.ini.php ' , 'page_aliases ' , $ dat );
4419-
44204406 // Clear cache *.api
44214407 $ GLOBALS ['xpwiki_cache_deletes ' ][$ this ->cont ['CACHE_DIR ' ]]['api ' ] = '*.autolink.api ' ;
44224408 }
44234409
44244410 // ページ別名が設定されているか
44254411 function is_alias ($ name ) {
4412+ $ page = $ this ->get_name_by_alias ($ name );
4413+ return ($ page === '' )? false : $ page ;
4414+ }
4415+
4416+ // db からページ別名一覧を取得
4417+ function get_pagealiases ($ pages = array (), $ asKey = false ) {
4418+ $ aliases = array ();
4419+ $ in = array ();
4420+ if ($ pages ) {
4421+ foreach ($ pages as $ page ) {
4422+ if ($ pgid = $ this ->get_pgid_by_name ($ page )) {
4423+ $ in [] = $ pgid ;
4424+ }
4425+ }
4426+ }
4427+ if (! $ pages || $ in ) {
4428+ $ where = '' ;
4429+ if ($ in ) {
4430+ $ where = ' WHERE `pgid` IN ( ' .join (', ' , $ in ).') ' ;
4431+ }
4432+ $ query = 'SELECT `name` ' .($ asKey ? ', `pgid` ' : '' ).' FROM ` ' .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname .'_alias ' ).'` ' .$ where ;
4433+ if ($ result = $ this ->xpwiki ->db ->query ($ query )) {
4434+ while ($ alias = $ this ->xpwiki ->db ->fetchRow ($ result )) {
4435+ if (! $ asKey ) {
4436+ $ aliases [] = $ alias [0 ];
4437+ } else {
4438+ $ aliases [$ alias [0 ]] = $ alias [1 ];
4439+ }
4440+ }
4441+ }
4442+ }
4443+ return $ aliases ;
4444+ }
4445+
4446+ // db からページ別名に対応する実ページ名を取得
4447+ function get_name_by_alias ($ alias ) {
4448+ $ page = '' ;
44264449 if ($ this ->root ->page_case_insensitive ) {
4427- $ page_aliases = $ this ->root ->page_aliases_i ;
4428- $ name = strtolower ($ name );
4450+ $ where = ' WHERE LOWER(`name`)= ' .$ this ->xpwiki ->db ->quoteString (strtolower ($ alias ));
44294451 } else {
4430- $ page_aliases = $ this ->root ->page_aliases ;
4452+ $ where = ' WHERE `name`= ' .$ this ->xpwiki ->db ->quoteString ($ alias );
4453+ }
4454+ $ query = 'SELECT `pgid` FROM ` ' .$ this ->xpwiki ->db ->prefix ($ this ->root ->mydirname .'_alias ' ).'` ' .$ where .' LIMIT 1 ' ;
4455+ //exit($query);
4456+ if ($ result = $ this ->xpwiki ->db ->query ($ query )) {
4457+ list ($ pgid ) = $ this ->xpwiki ->db ->fetchRow ($ result );
4458+ $ page = $ this ->get_name_by_pgid ($ pgid );
44314459 }
4432- return isset ( $ page_aliases [ $ name ])? $ page_aliases [ $ name ] : FALSE ;
4460+ return $ page ;
44334461 }
4434-
4462+
4463+
44354464 // ページオーダーを取得
44364465 function get_page_order ($ page ) {
44374466 if (! isset ($ this ->root ->pgorders [$ page ])) {
0 commit comments