Skip to content

Commit

Permalink
replace str compare (#1685)
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Jan 6, 2024
1 parent 9635a7a commit a9fa990
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 32 deletions.
12 changes: 12 additions & 0 deletions var/Typecho/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,18 @@ public static function subStr(string $str, int $start, int $length, string $trim
return $length < $iLength ? ($str . $trim) : $str;
}

/**
* 判断两个字符串是否为空并依次返回
*
* @param string|null $a
* @param string|null $b
* @return string|null
*/
public static function strBy(?string $a, ?string $b = null): ?string
{
return isset($a) && $a !== '' ? $a : $b;
}

/**
* 获取宽字符串长度函数
*
Expand Down
27 changes: 13 additions & 14 deletions var/Widget/Base/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ public function insert(array $rows): int
$insertStruct = [
'cid' => $rows['cid'],
'created' => empty($rows['created']) ? $this->options->time : $rows['created'],
'author' => !isset($rows['author']) || strlen($rows['author']) === 0 ? null : $rows['author'],
'author' => Common::strBy($rows['author']),
'authorId' => empty($rows['authorId']) ? 0 : $rows['authorId'],
'ownerId' => empty($rows['ownerId']) ? 0 : $rows['ownerId'],
'mail' => !isset($rows['mail']) || strlen($rows['mail']) === 0 ? null : $rows['mail'],
'url' => !isset($rows['url']) || strlen($rows['url']) === 0 ? null : $rows['url'],
'ip' => !isset($rows['ip']) || strlen($rows['ip']) === 0 ? $this->request->getIp() : $rows['ip'],
'agent' => !isset($rows['agent']) || strlen($rows['agent']) === 0
? $this->request->getAgent() : $rows['agent'],
'text' => !isset($rows['text']) || strlen($rows['text']) === 0 ? null : $rows['text'],
'type' => empty($rows['type']) ? 'comment' : $rows['type'],
'status' => empty($rows['status']) ? 'approved' : $rows['status'],
'mail' => Common::strBy($rows['mail']),
'url' => Common::strBy($rows['url']),
'ip' => Common::strBy($rows['ip'], $this->request->getIp()),
'agent' => Common::strBy($rows['agent'], $this->request->getAgent()),
'text' => Common::strBy($rows['text']),
'type' => Common::strBy($rows['type'], 'comment'),
'status' => Common::strBy($rows['status'], 'approved'),
'parent' => empty($rows['parent']) ? 0 : $rows['parent'],
];

Expand Down Expand Up @@ -137,11 +136,11 @@ public function update(array $rows, Query $condition): int

/** 构建插入结构 */
$preUpdateStruct = [
'author' => !isset($rows['author']) || strlen($rows['author']) === 0 ? null : $rows['author'],
'mail' => !isset($rows['mail']) || strlen($rows['mail']) === 0 ? null : $rows['mail'],
'url' => !isset($rows['url']) || strlen($rows['url']) === 0 ? null : $rows['url'],
'text' => !isset($rows['text']) || strlen($rows['text']) === 0 ? null : $rows['text'],
'status' => empty($rows['status']) ? 'approved' : $rows['status'],
'author' => Common::strBy($rows['author']),
'mail' => Common::strBy($rows['mail']),
'url' => Common::strBy($rows['url']),
'text' => Common::strBy($rows['text']),
'status' => Common::strBy($rows['status'], 'approved'),
];

$updateStruct = [];
Expand Down
22 changes: 11 additions & 11 deletions var/Widget/Base/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ public function insert(array $rows): int
$insertStruct = [
'title' => !isset($rows['title']) || strlen($rows['title']) === 0
? null : htmlspecialchars($rows['title']),
'created' => !isset($rows['created']) ? $this->options->time : $rows['created'],
'created' => empty($rows['created']) ? $this->options->time : $rows['created'],
'modified' => $this->options->time,
'text' => !isset($rows['text']) || strlen($rows['text']) === 0 ? null : $rows['text'],
'text' => Common::strBy($rows['text']),
'order' => empty($rows['order']) ? 0 : intval($rows['order']),
'authorId' => $rows['authorId'] ?? $this->user->uid,
'template' => empty($rows['template']) ? null : $rows['template'],
'type' => empty($rows['type']) ? 'post' : $rows['type'],
'status' => empty($rows['status']) ? 'publish' : $rows['status'],
'password' => !isset($rows['password']) || strlen($rows['password']) === 0 ? null : $rows['password'],
'template' => Common::strBy($rows['template']),
'type' => Common::strBy($rows['type'], 'post'),
'status' => Common::strBy($rows['status'], 'publish'),
'password' => Common::strBy($rows['password']),
'commentsNum' => empty($rows['commentsNum']) ? 0 : $rows['commentsNum'],
'allowComment' => !empty($rows['allowComment']) && 1 == $rows['allowComment'] ? 1 : 0,
'allowPing' => !empty($rows['allowPing']) && 1 == $rows['allowPing'] ? 1 : 0,
Expand Down Expand Up @@ -230,11 +230,11 @@ public function update(array $rows, Query $condition): int
'title' => !isset($rows['title']) || strlen($rows['title']) === 0
? null : htmlspecialchars($rows['title']),
'order' => empty($rows['order']) ? 0 : intval($rows['order']),
'text' => !isset($rows['text']) || strlen($rows['text']) === 0 ? null : $rows['text'],
'template' => empty($rows['template']) ? null : $rows['template'],
'type' => empty($rows['type']) ? 'post' : $rows['type'],
'status' => empty($rows['status']) ? 'publish' : $rows['status'],
'password' => empty($rows['password']) ? null : $rows['password'],
'text' => Common::strBy($rows['text']),
'template' => Common::strBy($rows['template']),
'type' => Common::strBy($rows['type'], 'post'),
'status' => Common::strBy($rows['status'], 'publish'),
'password' => Common::strBy($rows['password']),
'allowComment' => !empty($rows['allowComment']) && 1 == $rows['allowComment'] ? 1 : 0,
'allowPing' => !empty($rows['allowPing']) && 1 == $rows['allowPing'] ? 1 : 0,
'allowFeed' => !empty($rows['allowFeed']) && 1 == $rows['allowFeed'] ? 1 : 0,
Expand Down
2 changes: 1 addition & 1 deletion var/Widget/Contents/Attachment/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function updateAttachment()

/** 取出数据 */
$input = $this->request->from('name', 'slug', 'description');
$input['slug'] = Common::slugName(empty($input['slug']) ? $input['name'] : $input['slug']);
$input['slug'] = Common::slugName(Common::strBy($input['slug'], $input['name']));

$attachment['title'] = $input['name'];
$attachment['slug'] = $input['slug'];
Expand Down
4 changes: 2 additions & 2 deletions var/Widget/Metas/Category/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function insertCategory()
/** 取出数据 */
$category = $this->request->from('name', 'slug', 'description', 'parent');

$category['slug'] = Common::slugName(empty($category['slug']) ? $category['name'] : $category['slug']);
$category['slug'] = Common::slugName(Common::strBy($category['slug'], $category['name']));
$category['type'] = 'category';
$category['order'] = $this->getMaxOrder('category', $category['parent']) + 1;

Expand Down Expand Up @@ -284,7 +284,7 @@ public function updateCategory()
/** 取出数据 */
$category = $this->request->from('name', 'slug', 'description', 'parent');
$category['mid'] = $this->request->get('mid');
$category['slug'] = Common::slugName(empty($category['slug']) ? $category['name'] : $category['slug']);
$category['slug'] = Common::slugName(Common::strBy($category['slug'], $category['name']));
$category['type'] = 'category';
$current = $this->db->fetchRow($this->select()->where('mid = ?', $category['mid']));

Expand Down
4 changes: 2 additions & 2 deletions var/Widget/Metas/Tag/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function insertTag()
/** 取出数据 */
$tag = $this->request->from('name', 'slug');
$tag['type'] = 'tag';
$tag['slug'] = Common::slugName(empty($tag['slug']) ? $tag['name'] : $tag['slug']);
$tag['slug'] = Common::slugName(Common::strBy($tag['slug'], $tag['name']));

/** 插入数据 */
$tag['mid'] = $this->insert($tag);
Expand Down Expand Up @@ -254,7 +254,7 @@ public function updateTag()
/** 取出数据 */
$tag = $this->request->from('name', 'slug', 'mid');
$tag['type'] = 'tag';
$tag['slug'] = Common::slugName(empty($tag['slug']) ? $tag['name'] : $tag['slug']);
$tag['slug'] = Common::slugName(Common::strBy($tag['slug'], $tag['name']));

/** 更新数据 */
$this->update($tag, $this->db->sql()->where('mid = ?', $this->request->filter('int')->get('mid')));
Expand Down
4 changes: 2 additions & 2 deletions var/Widget/XmlRpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ public function wpNewCategory(int $blogId, string $userName, string $password, a
{
/** 开始接受数据 */
$input['name'] = $category['name'];
$input['slug'] = Common::slugName(empty($category['slug']) ? $category['name'] : $category['slug']);
$input['slug'] = Common::slugName(Common::strBy($category['slug'], $category['name']));
$input['parent'] = $category['parent_id'] ?? ($category['parent'] ?? 0);
$input['description'] = $category['description'] ?? $category['name'];
$input['description'] = Common::strBy($category['description'], $category['name']);

/** 调用已有组件 */
$categoryWidget = CategoryEdit::alloc(null, $input, function (CategoryEdit $category) {
Expand Down

0 comments on commit a9fa990

Please sign in to comment.