Display "Printable version" links in toolbox on special pages
[lhc/web/wiklou.git] / includes / search / SearchUpdate.php
1 <?php
2 /**
3 * Search index updater
4 *
5 * See deferred.txt
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Search
24 */
25
26 /**
27 * Database independant search index updater
28 *
29 * @ingroup Search
30 */
31 class SearchUpdate implements DeferrableUpdate {
32
33 private $mId = 0, $mNamespace, $mTitle, $mText;
34 private $mTitleWords;
35
36 function __construct( $id, $title, $text = false ) {
37 if ( is_string( $title ) ) {
38 $nt = Title::newFromText( $title );
39 } else {
40 $nt = $title;
41 }
42
43 if ( $nt ) {
44 $this->mId = $id;
45 $this->mText = $text;
46
47 $this->mNamespace = $nt->getNamespace();
48 $this->mTitle = $nt->getText(); # Discard namespace
49
50 $this->mTitleWords = $this->mTextWords = array();
51 } else {
52 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
53 }
54 }
55
56 function doUpdate() {
57 global $wgContLang, $wgDisableSearchUpdate;
58
59 if ( $wgDisableSearchUpdate || !$this->mId ) {
60 return;
61 }
62
63 wfProfileIn( __METHOD__ );
64
65 $search = SearchEngine::create();
66 $lc = SearchEngine::legalSearchChars() . '&#;';
67
68 if ( $this->mText === false ) {
69 $search->updateTitle( $this->mId,
70 $search->normalizeText( Title::indexTitle( $this->mNamespace, $this->mTitle ) ) );
71 wfProfileOut( __METHOD__ );
72 return;
73 }
74
75 # Language-specific strip/conversion
76 $text = $wgContLang->normalizeForSearch( $this->mText );
77
78 wfProfileIn( __METHOD__ . '-regexps' );
79 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
80 ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
81 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
82 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
83
84 # Strip external URLs
85 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
86 $protos = "http|https|ftp|mailto|news|gopher";
87 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
88 $text = preg_replace( $pat, "\\1 \\3", $text );
89
90 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
91 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
92 $text = preg_replace( $p1, "\\1 ", $text );
93 $text = preg_replace( $p2, "\\1 \\3 ", $text );
94
95 # Internal image links
96 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
97 $text = preg_replace( $pat2, " \\1 \\3", $text );
98
99 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
100 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
101
102 # Strip all remaining non-search characters
103 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
104
105 # Handle 's, s'
106 #
107 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
108 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
109 #
110 # These tail-anchored regexps are insanely slow. The worst case comes
111 # when Japanese or Chinese text (ie, no word spacing) is written on
112 # a wiki configured for Western UTF-8 mode. The Unicode characters are
113 # expanded to hex codes and the "words" are very long paragraph-length
114 # monstrosities. On a large page the above regexps may take over 20
115 # seconds *each* on a 1GHz-level processor.
116 #
117 # Following are reversed versions which are consistently fast
118 # (about 3 milliseconds on 1GHz-level processor).
119 #
120 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
121 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
122
123 # Strip wiki '' and '''
124 $text = preg_replace( "/''[']*/", " ", $text );
125 wfProfileOut( __METHOD__ . '-regexps' );
126
127 wfRunHooks( 'SearchUpdate', array( $this->mId, $this->mNamespace, $this->mTitle, &$text ) );
128
129 # Perform the actual update
130 $search->update( $this->mId, $search->normalizeText( Title::indexTitle( $this->mNamespace, $this->mTitle ) ),
131 $search->normalizeText( $text ) );
132
133 wfProfileOut( __METHOD__ );
134 }
135 }
136
137 /**
138 * Placeholder class
139 *
140 * @ingroup Search
141 */
142 class SearchUpdateMyISAM extends SearchUpdate {
143 # Inherits everything
144 }