Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions misc/db-updates/2023-07-12/sortTitleClean
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/php -d mysqlnd.net_read_timeout=86400
<?php
set_include_path("../../../include");
require("header.inc.php");

Z_Core::$debug = true;

$startShard = !empty($argv[1]) ? $argv[1] : 1;
$shardIDs = Zotero_DB::columnQuery("SELECT shardID FROM shards WHERE shardID >= ? ORDER BY shardID", $startShard);

foreach ($shardIDs as $shardID) {
echo "Shard: $shardID\n";

# Fetch all title fields with special characters: [, ], (, ), {, }, <, >, ", ', ”, ’, “, ‘
$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);
Zotero_DB::beginTransaction();
foreach($rows as $row) {
# Find the desired sort title for that title field
$sortTitle = Zotero_Items::getSortTitle($row['value']);
# Do nothing if first characters of existing sort title are the same as of desired sort title
if (isset($row['sortTitle']) && mb_substr($sortTitle ?? '', 0, 5) == mb_substr($row['sortTitle'] ?? '', 0, 5)) {
continue;
}
# Do nothing if first characters of desired sort title are the same as of title
if (mb_substr($sortTitle ?? '', 0, 5) == mb_substr($row['value'] ?? '', 0, 5)) {
continue;
}
# Update item sort title
Zotero_DB::query("UPDATE itemSortFields SET sortTitle = ? WHERE itemID = ?", [$sortTitle, $row['itemID']], $shardID);
}
Zotero_DB::commit();

}
echo "Done";
3 changes: 2 additions & 1 deletion model/Items.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,8 @@ public static function getSortTitle($title) {
if (!$title) {
return '';
}
return mb_strcut(preg_replace('/^[[({\-"\'“‘ ]+(.*)[\])}\-"\'”’ ]*?$/Uu', '$1', $title), 0, Zotero_Notes::$MAX_TITLE_LENGTH);
$cleaned_of_characters = preg_replace('/[\[({\\-"\'“‘\])}\-"\'”’]/u', '$1', $title);
return mb_strcut(strip_tags($cleaned_of_characters), 0, Zotero_Notes::$MAX_TITLE_LENGTH);
}
}

Expand Down