Skip to content

Commit db28a06

Browse files
committed
use MySQL DB for page-name aliases
ページ別名のデータを MySQL データベース保存し、利用するようにした。
1 parent c95a881 commit db28a06

File tree

12 files changed

+190
-112
lines changed

12 files changed

+190
-112
lines changed

Diff for: xoops_trust_path/modules/xpwiki/class/func/pukiwiki_func.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ function get_autolink_pattern(& $pages, $min_len = -1, $make_a = true, $aliases
19781978
$min_len = $this->root->autolink; // set $this->root->autolink, when omitted.
19791979
}
19801980

1981-
$_aliases = array_keys(array_intersect($this->root->page_aliases, $pages));
1981+
$_aliases = $this->get_pagealiases($pages);
19821982
foreach (array_merge($pages, $_aliases) as $page) {
19831983
if (strlen($page) >= $min_len) {
19841984
$auto_pages[] = $page;

Diff for: xoops_trust_path/modules/xpwiki/class/func/xpwiki_func.php

+93-64
Original file line numberDiff line numberDiff line change
@@ -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])) {

Diff for: xoops_trust_path/modules/xpwiki/class/include/init.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@
230230
if ($root->userinfo['admin']) {
231231
// Database check
232232
$query = 'SELECT `key` FROM ' . $this->xpwiki->db->prefix($root->mydirname.'_cache') . ' LIMIT 1' ;
233-
if(! $this->xpwiki->db->query($query)) {
233+
$_dbChkRes = $this->xpwiki->db->query($query);
234+
$query = "SELECT count(*) FROM ".$this->xpwiki->db->prefix($root->mydirname.'_alias');
235+
$_dbChkRes = ($_dbChkRes && $this->xpwiki->db->query($query));
236+
if(! $_dbChkRes) {
234237
$title = 'Please update this module on admin panel.';
235238
if (defined('XOOPS_CUBE_LEGACY')) {
236239
$this->redirect_header(XOOPS_URL . '/modules/legacy/admin/index.php?action=ModuleUpdate&dirname=' . $root->mydirname, 1, $title);
@@ -277,11 +280,6 @@
277280
$die .= 'Define(s) not found: (Maybe the old *.ini.php?)' . "\n" . $temp;
278281
}
279282

280-
// page aliases (case-insensitive data)
281-
if ($root->page_aliases && ! $root->page_aliases_i) {
282-
$this->save_page_alias();
283-
}
284-
285283
if($die) $this->die_message(nl2br("\n\n" . $die));
286284
unset($die, $temp);
287285

Diff for: xoops_trust_path/modules/xpwiki/ini/pukiwiki.ini.php

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
/////////////////////////////////////////////////
1212
// Variable initialize
1313
$root->ext_autolinks = array(); // External AutoLink
14-
$root->page_aliases = array(); // Pagename aliases
15-
$root->page_aliases_i = array(); // Pagename aliases (case-insensitive)
1614

1715
/////////////////////////////////////////////////
1816
// Functionality settings

Diff for: xoops_trust_path/modules/xpwiki/onupdate.php

+35-1
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,44 @@ function xpwiki_onupdate_base( $module , $mydirname )
9595
`ttl` int(11) NOT NULL default \'0\',
9696
KEY `key` (`key`),
9797
KEY `plugin` (`plugin`)
98-
)'
98+
) ENGINE=MyISAM'
9999
);
100100
}
101101

102+
$query = "SELECT count(*) FROM ".$db->prefix($mydirname."_alias") ;
103+
if (! $db->query($query)) {
104+
$db->query(
105+
'CREATE TABLE `'.$db->prefix($mydirname.'_alias').'` (
106+
`name` varchar(255) binary NOT NULL DEFAULT \'\',
107+
`pgid` int(11) NOT NULL DEFAULT \'0\',
108+
PRIMARY KEY (`name`),
109+
KEY `pgid` (`pgid`)
110+
) ENGINE=MyISAM'
111+
);
112+
include_once XOOPS_TRUST_PATH."/modules/xpwiki/include.php";
113+
$xpwiki = XpWiki::getInitedSingleton($mydirname);
114+
$xpwiki->init();
115+
if ($xpwiki->root->page_aliases) {
116+
$query = array();
117+
foreach($xpwiki->root->page_aliases as $alias => $page) {
118+
if ($pgid = $xpwiki->func->get_pgid_by_name($page)) {
119+
$query[] = '('.$db->quoteString($alias).','.$pgid.')';
120+
}
121+
}
122+
if ($query) {
123+
$query = 'INSERT INTO `'.$db->prefix($mydirname."_alias").'` (`name`,`pgid`) VALUES '.join(',', $query);
124+
if ($db->query($query)) {
125+
$inifile = $xpwiki->cont['CACHE_DIR'] . 'pukiwiki.ini.php';
126+
$ini = file_get_contents($inifile);
127+
file_put_contents($inifile.'.alias.bak', $ini);
128+
$ini = preg_replace('#//<page_aliases>.+//<page_aliases/>\s+#s', '', $ini);
129+
file_put_contents($inifile, $ini);
130+
}
131+
}
132+
}
133+
unset($xpwiki);
134+
}
135+
102136
// ADD Keys
103137
$table = $db->prefix($mydirname.'_attach');
104138
if ($result = $db->query('SHOW INDEX FROM `' . $table . '`')) {

Diff for: xoops_trust_path/modules/xpwiki/plugin/api.inc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function autolink ($need_ret = false, $base = null, $options = array()) {
4646

4747
// Get all aliases
4848
if (empty($options['noaliases'])) {
49-
$all_aliases = array_keys(array_intersect($this->root->page_aliases, $this->func->get_existpages()));
49+
$all_aliases = $this->func->get_pagealiases($this->func->get_existpages());
5050
} else {
5151
$all_aliases = array();
5252
}

0 commit comments

Comments
 (0)