Index: trunk/extensions/UserContactLinks/UserSignature.php |
— | — | @@ -0,0 +1,88 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | +This program is free software; you can redistribute it and/or |
| 6 | +modify it under the terms of the GNU General Public License |
| 7 | +as published by the Free Software Foundation, version 2 |
| 8 | +of the License. |
| 9 | + |
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU General Public License for more details. |
| 14 | + |
| 15 | +You should have received a copy of the GNU General Public License |
| 16 | +along with this program; if not, write to the Free Software |
| 17 | +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | +*/ |
| 19 | + |
| 20 | +global $wgHooks, $wgOut; |
| 21 | +$wgHooks['ParserAfterStrip'][] = 'parseUserSignatures'; |
| 22 | +$wgExtensionCredits['parserhook'][] = array( |
| 23 | + 'version' => '0.3', |
| 24 | + 'name' => 'UserSignature', |
| 25 | + 'author' => 'Paul Grinberg', |
| 26 | + 'email' => 'gri6507 at yahoo dot com', |
| 27 | + 'description' => 'provides the ability to simply and consistantly add other user names using ^^^user^^^ syntax'); |
| 28 | + |
| 29 | +function parseUserSignatures(&$parser, &$text, &$strip_state) { |
| 30 | + while (preg_match('/\^\^\^(.+?)\^\^\^/', $text, $matches)) { |
| 31 | + $userid = getUserIDFromUserText($matches[1]); |
| 32 | + if ($userid != 0) { // successfully found the user based on first word |
| 33 | + $u = newFromId($userid); |
| 34 | + $text = str_replace("^^^$matches[1]^^^", "[[:user:" . $u->getName() . "|" . $u->getRealName() . "]]", $text); |
| 35 | + } else { |
| 36 | + $text = str_replace("^^^$matches[1]^^^", "'''{incorrect username}'''", $text); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +function newFromId( $id ) { |
| 44 | + $u = new User; |
| 45 | + $u->mId = $id; |
| 46 | + $u->mFrom = 'id'; |
| 47 | + return $u; |
| 48 | +} |
| 49 | + |
| 50 | +# only create the following function if it was not already installed with the Todo Tasks extension |
| 51 | +if (!function_exists('getUserIDFromUserText')) { |
| 52 | + function getUserIDFromUserText($user) { |
| 53 | + $dbr = wfGetDB( DB_SLAVE ); |
| 54 | + $userid = 0; |
| 55 | + |
| 56 | + if (preg_match('/^\s*(.*?)\s*$/', $user, $matches)) |
| 57 | + $user = $matches[1]; |
| 58 | + |
| 59 | + $u = User::newFromName($user); |
| 60 | + if ($u) { |
| 61 | + $userid = $u->idForName(); // valid userName |
| 62 | + } |
| 63 | + if (!$userid) { // if not a valid userName, try as a userRealName |
| 64 | + $userid = $dbr->selectField( 'user', 'user_id', array( 'user_real_name' => $user ), 'renderTodo' ); |
| 65 | + if (!$userid) { // if not valid userRealName, try case insensitive userRealName |
| 66 | + $sql = "SELECT user_id FROM ". $dbr->tableName('user') ." WHERE UPPER(user_real_name) LIKE '%" . strtoupper($user) . "%'"; |
| 67 | + $res = $dbr->query( $sql, __METHOD__ ); |
| 68 | + if ($dbr->numRows($res)) { |
| 69 | + $row = $dbr->fetchRow($res); |
| 70 | + $userid = $row[0]; |
| 71 | + } |
| 72 | + $dbr->freeResult($res); |
| 73 | + if (!$userid) { // if not case insensitive userRealName, try case insensitive lastname |
| 74 | + list ($first, $last) = preg_split('/\s+/', $user); |
| 75 | + if ($last != '') { |
| 76 | + $sql = "SELECT user_id FROM ". $dbr->tableName('user') ." WHERE UPPER(user_real_name) LIKE '%" . strtoupper($last) . "%'"; |
| 77 | + $res = $dbr->query( $sql, __METHOD__ ); |
| 78 | + if ($dbr->numRows($res)) { |
| 79 | + $row = $dbr->fetchRow($res); |
| 80 | + $userid = $row[0]; |
| 81 | + } |
| 82 | + $dbr->freeResult($res); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return $userid; |
| 88 | + } |
| 89 | +} |
\ No newline at end of file |