Skip to content

Commit 4833c9a

Browse files
committed
strip titles of html tags + update regex
Strip all html tags from title in getSortTitle function Update the regex in getSortTitle to not skip double brackets Fixes: zotero#159
1 parent 2cf66c3 commit 4833c9a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/php -d mysqlnd.net_read_timeout=86400
2+
<?php
3+
set_include_path("../../../include");
4+
require("header.inc.php");
5+
6+
Z_Core::$debug = true;
7+
8+
$startShard = !empty($argv[1]) ? $argv[1] : 1;
9+
$shardIDs = Zotero_DB::columnQuery("SELECT shardID FROM shards WHERE shardID >= ? ORDER BY shardID", $startShard);
10+
11+
foreach ($shardIDs as $shardID) {
12+
echo "Shard: $shardID\n";
13+
14+
# Fetch all title fields with special characters: [, ], (, ), {, }, <, >, ", ', ”, ’, “, ‘
15+
$rows = Zotero_DB::query("SELECT i.itemID, i.value, s.sortTitle FROM itemData as i INNER JOIN itemSortFields as s ON s.itemID = i.itemID WHERE fieldID = 110 AND value REGEXP '[{}><()\\\\[\\\\]\"\'”’“‘]' ", false, $shardID);
16+
Zotero_DB::beginTransaction();
17+
foreach($rows as $row) {
18+
# Find the desired sort title for that title field
19+
$sortTitle = Zotero_Items::getSortTitle($row['value']);
20+
# Do nothing if first characters of existing sort title are the same as of desired sort title
21+
if (isset($row['sortTitle']) && mb_substr($sortTitle ?? '', 0, 5) == mb_substr($row['sortTitle'] ?? '', 0, 5)) {
22+
continue;
23+
}
24+
# Do nothing if first characters of desired sort title are the same as of title
25+
if (mb_substr($sortTitle ?? '', 0, 5) == mb_substr($row['value'] ?? '', 0, 5)) {
26+
continue;
27+
}
28+
# Update item sort title
29+
Zotero_DB::query("UPDATE itemSortFields SET sortTitle = ? WHERE itemID = ?", [$sortTitle, $row['itemID']], $shardID);
30+
}
31+
Zotero_DB::commit();
32+
33+
}
34+
echo "Done";

model/Items.inc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2580,7 +2580,8 @@ public static function getSortTitle($title) {
25802580
if (!$title) {
25812581
return '';
25822582
}
2583-
return mb_strcut(preg_replace('/^[[({\-"\'“‘ ]+(.*)[\])}\-"\'”’ ]*?$/Uu', '$1', $title), 0, Zotero_Notes::$MAX_TITLE_LENGTH);
2583+
$cleaned_of_characters = preg_replace('/[\[({\\-"\'“‘\])}\-"\'”’]/u', '$1', $title);
2584+
return mb_strcut(strip_tags($cleaned_of_characters), 0, Zotero_Notes::$MAX_TITLE_LENGTH);
25842585
}
25852586
}
25862587

0 commit comments

Comments
 (0)