+/**
+ * Add author plugin text for wiki text body
+ *
+ * @param string $wikitext
+ * @param integer $timestamp_to_keep Set null when not to keep timestamp
+ */
+function add_author_info($wikitext, $timestamp_to_keep)
+{
+ global $auth_user, $auth_user_fullname;
+ $author = preg_replace('/"/', '', $auth_user);
+ $fullname = $auth_user_fullname;
+ if (!$fullname && $author) {
+ // Fullname is empty, use $author as its fullname
+ $fullname = preg_replace('/^[^:]*:/', '', $author);
+ }
+ $datetime_to_keep = '';
+ if (!is_null($timestamp_to_keep)) {
+ $datetime_to_keep .= ';' . get_date_atom($timestamp_to_keep + LOCALZONE);
+ }
+ $displayname = preg_replace('/"/', '', $fullname);
+ $user_prefix = get_auth_user_prefix();
+ $author_text = sprintf('#author("%s","%s","%s")',
+ get_date_atom(UTIME + LOCALZONE) . $datetime_to_keep,
+ ($author ? $user_prefix . $author : ''),
+ $displayname) . "\n";
+ return $author_text . $wikitext;
+}
+
+function remove_author_info($wikitext)
+{
+ return preg_replace('/^\s*#author\([^\n]*(\n|$)/m', '', $wikitext);
+}
+
+/**
+ * Remove author line from wikitext
+ */
+function remove_author_header($wikitext)
+{
+ $start = 0;
+ while (($pos = strpos($wikitext, "\n", $start)) != false) {
+ $line = substr($wikitext, $start, $pos);
+ $m = null;
+ if (preg_match('/^#author\(/', $line, $m)) {
+ // fond #author line, Remove this line only
+ if ($start === 0) {
+ return substr($wikitext, $pos + 1);
+ } else {
+ return substr($wikitext, 0, $start - 1) .
+ substr($wikitext, $pos + 1);
+ }
+ } else if (preg_match('/^#freeze(\W|$)/', $line, $m)) {
+ // Found #freeze still in header
+ } else {
+ // other line, #author not found
+ return $wikitext;
+ }
+ $start = $pos + 1;
+ }
+ return $wikitext;
+}
+
+/**
+ * Get author info from wikitext
+ */
+function get_author_info($wikitext)
+{
+ $start = 0;
+ while (($pos = strpos($wikitext, "\n", $start)) != false) {
+ $line = substr($wikitext, $start, $pos);
+ $m = null;
+ if (preg_match('/^#author\(/', $line, $m)) {
+ return $line;
+ } else if (preg_match('/^#freeze(\W|$)/', $line, $m)) {
+ // Found #freeze still in header
+ } else {
+ // other line, #author not found
+ return null;
+ }
+ $start = $pos + 1;
+ }
+ return null;
+}
+
+/**
+ * Get updated datetime from author
+ */
+function get_update_datetime_from_author($author_line) {
+ $m = null;
+ if (preg_match('/^#author\(\"([^\";]+)(?:;([^\";]+))?/', $author_line, $m)) {
+ if ($m[2]) {
+ return $m[2];
+ } else if ($m[1]) {
+ return $m[1];
+ }
+ }
+ return null;
+}
+
+function get_date_atom($timestamp)
+{
+ // Compatible with DATE_ATOM format
+ // return date(DATE_ATOM, $timestamp);
+ $zmin = abs(LOCALZONE / 60);
+ return date('Y-m-d\TH:i:s', $timestamp) . sprintf('%s%02d:%02d',
+ (LOCALZONE < 0 ? '-' : '+') , $zmin / 60, $zmin % 60);
+}
+