Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace shorten_subject #503

Merged
merged 9 commits into from
Jun 8, 2013
6 changes: 3 additions & 3 deletions SSI.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,8 @@ function ssi_queryPosts($query_where = '', $query_where_params = array(), $query
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'short_subject' => shorten_subject($row['subject'], 25),
'preview' => Util::strlen($preview) > 128 ? Util::substr($preview, 0, 128) . '...' : $preview,
'short_subject' => shorten_text($row['subject'], !empty($modSettings['ssi_subject_length']) ? $modSettings['ssi_subject_length'] : 24),
'preview' => shorten_text($preview, !empty($modSettings['ssi_preview_length']) ? $modSettings['ssi_preview_length'] : 128),
'body' => $row['body'],
'time' => standardTime($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
Expand Down Expand Up @@ -601,7 +601,7 @@ function ssi_recentTopics($num_recent = 8, $exclude_boards = null, $include_boar
'subject' => $row['subject'],
'replies' => $row['num_replies'],
'views' => $row['num_views'],
'short_subject' => shorten_subject($row['subject'], 25),
'short_subject' => shorten_text($row['subject'], 25),
'preview' => $row['body'],
'time' => standardTime($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
Expand Down
6 changes: 3 additions & 3 deletions sources/ScheduledTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ function scheduled_daily_digest()

require_once(SUBSDIR . '/Boards.subs.php');
// Just get the board names.

$boards = fetchBoardsInfo(array('boards' => $boards));

if (empty($boards))
Expand Down Expand Up @@ -673,7 +673,7 @@ function scheduled_daily_digest()
{
// Convert to markdown markup e.g. text ;)
pbe_prepare_text($row['body']);
$row['body'] = shorten_text($row['body']);
$row['body'] = shorten_text($row['body'], !empty($modSettings['digest_preview_length']) ? $modSettings['digest_preview_length'] : 375, true);
$row['body'] = preg_replace("~\n~s","\n ", $row['body']);
}

Expand Down Expand Up @@ -719,7 +719,7 @@ function scheduled_daily_digest()
// Replace the body array with the appropriate preview message
$body = $types['reply'][$id]['lines'][$topic['id']]['body_text'];
pbe_prepare_text($body);
$body = shorten_text($body);
$body = shorten_text($body, !empty($modSettings['digest_preview_length']) ? $modSettings['digest_preview_length'] : 375, true);
$body = preg_replace("~\n~s","\n ", $body);
$types['reply'][$id]['lines'][$topic['id']]['body'] = $body;

Expand Down
59 changes: 21 additions & 38 deletions sources/Subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,58 +804,41 @@ function un_htmlspecialchars($string)

}

/**
* Shorten a subject + internationalization concerns.
*
* - shortens a subject so that it is either shorter than length, or that length plus an ellipsis.
* - respects internationalization characters and entities as one character.
* - avoids trailing entities.
* - returns the shortened string.
*
* @param string $subject
* @param int $len
*/
function shorten_subject($subject, $len)
{
// It was already short enough!
if (Util::strlen($subject) <= $len)
return $subject;

// Shorten it by the length it was too long, and strip off junk from the end.
return Util::substr($subject, 0, $len) . '...';
}

/**
* Shorten a string of text
*
* - shortens a text string so that it is approximately a certain length or under
* - attempts to break the string on the first word boundary after the allowed length
* - if resulting length is > len plus buffer then it is truncated to length plus an ellipsis.
* - shortens a text string so that it is either shorter than length, or that length plus an ellipsis.
* - optionally attempts to break the string on a word boundary approximately at the allowed length
* - if using cutword and the resulting length is > len plus buffer then it is truncated to length plus an ellipsis.
* - respects internationalization characters and entities as one character.
* - returns the shortened string.
*
* @param string $text
* @param int $len
* @param int $buffer maximum length overflow to allow cutting on a word boundary
* @param bool $cutword try to cut at a word boundary
* @param int $buffer maximum length overflow to allow when cutting on a word boundary
*/
function shorten_text($text, $len = 384, $buffer = 12)
function shorten_text($text, $len = 384, $cutword = false, $buffer = 12)
{
$current = Util::strlen($text);

// Its to long so lets cut it down to size
if ($current > $len)
// If its to long, cut it down to size
if (Util::strlen($text) > $len)
{
// Look for len characters and cut on first word boundary after
preg_match('~(.{' . $len . '}.*?)\b~s', $text, $matches);
if ($cutword)
{
// Look for len - buffer characters and cut on first word boundary after
preg_match('~(.{' . ($len - $buffer) . '}.*?)\b~s', $text, $matches);

// Always one clown in the audience who likes long words or not using the spacebar
if (Util::strlen($matches[1]) > $len + $buffer)
$matches[1] = substr($matches[1], 0, $len);
// Always one clown in the audience who likes long words or not using the spacebar
if (Util::strlen($matches[1]) > $len + $buffer)
$matches[1] = Util::substr($matches[1], 0, $len);

return rtrim($matches[1]) . '...';
$text = rtrim($matches[1]) . ' ...';
}
else
$text = Util::substr($subject, 0, $len) . '...';
}
else
return $text;

return $text;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion sources/admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ public function action_search_internal()
'url' => (substr($item[1], 0, 4) == 'area' ? $scripturl . '?action=admin;' . $item[1] : $item[1]) . ';' . $context['session_var'] . '=' . $context['session_id'] . ((substr($item[1], 0, 4) == 'area' && $section == 'settings' ? '#' . $item[0][0] : '')),
'name' => $name,
'type' => $section,
'help' => shorten_subject(isset($item[2]) ? strip_tags($helptxt[$item[2]]) : (isset($helptxt[$found]) ? strip_tags($helptxt[$found]) : ''), 255),
'help' => shorten_text(isset($item[2]) ? strip_tags($helptxt[$item[2]]) : (isset($helptxt[$found]) ? strip_tags($helptxt[$found]) : ''), 255),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions sources/controllers/Display.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function action_index()
// Create a previous next string if the selected theme has it as a selected option.
$context['previous_next'] = $modSettings['enablePreviousNext'] ? '<a href="' . $scripturl . '?topic=' . $topic . '.0;prev_next=prev#new">' . $txt['previous_next_back'] . '</a> - <a href="' . $scripturl . '?topic=' . $topic . '.0;prev_next=next#new">' . $txt['previous_next_forward'] . '</a>' : '';
if (!empty($context['topic_derived_from']))
$context['previous_next'] .= ' - <a href="' . $scripturl . '?msg=' . $context['topic_derived_from']['derived_from'] . '">' . sprintf($txt['topic_derived_from'], '<em>' . shorten_subject($context['topic_derived_from']['subject'], 25)) . '</em></a>';
$context['previous_next'] .= ' - <a href="' . $scripturl . '?msg=' . $context['topic_derived_from']['derived_from'] . '">' . sprintf($txt['topic_derived_from'], '<em>' . shorten_text($context['topic_derived_from']['subject'], !empty($modSettings['subject_length']) ? $modSettings['subject_length'] : 24)) . '</em></a>';

// Check if spellchecking is both enabled and actually working. (for quick reply.)
$context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
Expand Down Expand Up @@ -703,7 +703,7 @@ function action_index()
require_once(SUBSDIR . '/Likes.subs.php');
$context['likes'] = loadLikes($messages, true);
}

$messages_request = $db->query('', '
SELECT
m.id_msg, m.icon, m.subject, m.poster_time, m.poster_ip, m.id_member, m.modified_time, m.modified_name, m.body,
Expand Down
4 changes: 2 additions & 2 deletions sources/controllers/Draft.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ function action_showDrafts($member_id, $topic = false, $draft_type = 0)
// Post drafts
if ($draft_type === 0)
$context['drafts'][] = array(
'subject' => empty($draft['subject']) ? $txt['drafts_none'] : censorText(shorten_subject(stripslashes($draft['subject']), 24)),
'subject' => empty($draft['subject']) ? $txt['drafts_none'] : censorText(shorten_text(stripslashes($draft['subject']), !empty($modSettings['draft_subject_length']) ? $modSettings['draft_subject_length'] : 24)),
'poster_time' => relativeTime($draft['poster_time']),
'link' => '<a href="' . $scripturl . '?action=post;board=' . $draft['id_board'] . ';' . (!empty($draft['id_topic']) ? 'topic='. $draft['id_topic'] .'.0;' : '') . 'id_draft=' . $draft['id_draft'] . '">' . (!empty($draft['subject']) ? $draft['subject'] : $txt['drafts_none']) . '</a>',
);
// PM drafts
elseif ($draft_type === 1)
$context['drafts'][] = array(
'subject' => empty($draft['subject']) ? $txt['drafts_none'] : censorText(shorten_subject(stripslashes($draft['subject']), 24)),
'subject' => empty($draft['subject']) ? $txt['drafts_none'] : censorText(shorten_text(stripslashes($draft['subject']), !empty($modSettings['draft_subject_length']) ? $modSettings['draft_subject_length'] : 24)),
'poster_time' => relativeTime($draft['poster_time']),
'link' => '<a href="' . $scripturl . '?action=pm;sa=send;id_draft=' . $draft['id_draft'] . '">' . (!empty($draft['subject']) ? $draft['subject'] : $txt['drafts_none']) . '</a>',
);
Expand Down
6 changes: 2 additions & 4 deletions sources/controllers/MessageIndex.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,10 @@ function action_messageindex()
{
// Limit them to $modSettings['preview_characters'] characters
$row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
if (Util::strlen($row['first_body']) > $modSettings['preview_characters'])
$row['first_body'] = Util::substr($row['first_body'], 0, $modSettings['preview_characters']) . '...';
$row['first_body'] = shorten_text($row['first_body'], !empty($modSettings['preview_characters']) ? $modSettings['preview_characters'] : 128, true);

$row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
if (Util::strlen($row['last_body']) > $modSettings['preview_characters'])
$row['last_body'] = Util::substr($row['last_body'], 0, $modSettings['preview_characters']) . '...';
$row['last_body'] = shorten_text($row['last_body'], !empty($modSettings['preview_characters']) ? $modSettings['preview_characters'] : 128, true);

// Censor the subject and message preview.
censorText($row['first_subject']);
Expand Down
3 changes: 2 additions & 1 deletion sources/controllers/PostModeration.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@ function action_unapproved_attachments()
),
'data' => array(
'function' => create_function('$data', '
return \'<a href="\' . $data[\'message\'][\'href\'] . \'">\' . shorten_subject($data[\'message\'][\'subject\'], 20) . \'</a>\';'
global $modSettings;
return \'<a href="\' . $data[\'message\'][\'href\'] . \'">\' . shorten_text($data[\'message\'][\'subject\'], !empty($modSettings[\'subject_length\']) ? $modSettings[\'subject_length\'] : 24) . \'</a>\';'
),
'class' => 'smalltext',
'style' => 'width:15em;',
Expand Down
7 changes: 3 additions & 4 deletions sources/controllers/Recent.controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,10 @@ function action_unread()
{
// Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
$row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['id_first_msg']), array('<br />' => '&#10;')));
if (Util::strlen($row['first_body']) > 128)
$row['first_body'] = Util::substr($row['first_body'], 0, 128) . '...';
$row['first_body'] = shorten_text($row['first_body'], !empty($modSettings['preview_characters']) ? $modSettings['preview_characters'] : 128, true);

$row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['id_last_msg']), array('<br />' => '&#10;')));
if (Util::strlen($row['last_body']) > 128)
$row['last_body'] = Util::substr($row['last_body'], 0, 128) . '...';
$row['last_body'] = shorten_text($row['last_body'], !empty($modSettings['preview_characters']) ? $modSettings['preview_characters'] : 128, true);

// Censor the subject and message preview.
censorText($row['first_subject']);
Expand Down
2 changes: 1 addition & 1 deletion sources/subs/BoardIndex.subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function getBoardIndex($boardIndexOptions)

// Prepare the subject, and make sure it's not too long.
censorText($row_board['subject']);
$row_board['short_subject'] = shorten_subject($row_board['subject'], 24);
$row_board['short_subject'] = shorten_text($row_board['subject'], !empty($modSettings['subject_length']) ? $modSettings['subject_length'] : 24);
$this_last_post = array(
'id' => $row_board['id_msg'],
'time' => $row_board['poster_time'] > 0 ? relativeTime($row_board['poster_time']) : $txt['not_applicable'],
Expand Down
5 changes: 2 additions & 3 deletions sources/subs/Post.subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2349,14 +2349,13 @@ function lastPost()
censorText($row['body']);

$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled']), array('<br />' => '&#10;')));
if (Util::strlen($row['body']) > 128)
$row['body'] = Util::substr($row['body'], 0, 128) . '...';
$row['body'] = shorten_text($row['body'], !empty($modSettings['lastpost_preview_characters']) ? $modSettings['lastpost_preview_characters'] : 128, true);

// Send the data.
return array(
'topic' => $row['id_topic'],
'subject' => $row['subject'],
'short_subject' => shorten_subject($row['subject'], 24),
'short_subject' => shorten_text($row['subject'], !empty($modSettings['subject_length']) ? $modSettings['subject_length'] : 24),
'preview' => $row['body'],
'time' => standardTime($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
Expand Down
5 changes: 2 additions & 3 deletions sources/subs/Recent.subs.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ function getLastPosts($latestPostOptions)
censorText($row['body']);

$row['body'] = strip_tags(strtr(parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']), array('<br />' => '&#10;')));
if (Util::strlen($row['body']) > 128)
$row['body'] = Util::substr($row['body'], 0, 128) . '...';
$row['body'] = shorten_text($row['body'], !empty($modSettings['lastpost_preview_characters']) ? $modSettings['lastpost_preview_characters'] : 128, true);

// Build the array.
$posts[] = array(
Expand All @@ -84,7 +83,7 @@ function getLastPosts($latestPostOptions)
'link' => empty($row['id_member']) ? $row['poster_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>'
),
'subject' => $row['subject'],
'short_subject' => shorten_subject($row['subject'], 24),
'short_subject' => shorten_text($row['subject'], !empty($modSettings['subject_length']) ? $modSettings['subject_length'] : 24),
'preview' => $row['body'],
'time' => relativeTime($row['poster_time']),
'timestamp' => forum_time(true, $row['poster_time']),
Expand Down