Update formatting on includes/deferred/
[lhc/web/wiklou.git] / includes / deferred / 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 * Page id being updated
34 * @var int
35 */
36 private $id = 0;
37
38 /**
39 * Title we're updating
40 * @var Title
41 */
42 private $title;
43
44 /**
45 * Content of the page (not text)
46 * @var Content|false
47 */
48 private $content;
49
50 /**
51 * Constructor
52 *
53 * @param int $id Page id to update
54 * @param Title|string $title Title of page to update
55 * @param Content|string|false $c Content of the page to update.
56 * If a Content object, text will be gotten from it. String is for back-compat.
57 * Passing false tells the backend to just update the title, not the content
58 */
59 public function __construct( $id, $title, $c = false ) {
60 if ( is_string( $title ) ) {
61 $nt = Title::newFromText( $title );
62 } else {
63 $nt = $title;
64 }
65
66 if ( $nt ) {
67 $this->id = $id;
68 // is_string() check is back-compat for ApprovedRevs
69 if ( is_string( $c ) ) {
70 $this->content = new TextContent( $c );
71 } else {
72 $this->content = $c ?: false;
73 }
74 $this->title = $nt;
75 } else {
76 wfDebug( "SearchUpdate object created with invalid title '$title'\n" );
77 }
78 }
79
80 /**
81 * Perform actual update for the entry
82 */
83 public function doUpdate() {
84 global $wgDisableSearchUpdate;
85
86 if ( $wgDisableSearchUpdate || !$this->id ) {
87 return;
88 }
89
90 wfProfileIn( __METHOD__ );
91
92 $page = WikiPage::newFromId( $this->id, WikiPage::READ_LATEST );
93 $indexTitle = Title::indexTitle( $this->title->getNamespace(), $this->title->getText() );
94
95 foreach ( SearchEngine::getSearchTypes() as $type ) {
96 $search = SearchEngine::create( $type );
97 if ( !$search->supports( 'search-update' ) ) {
98 continue;
99 }
100
101 $normalTitle = $search->normalizeText( $indexTitle );
102
103 if ( $page === null ) {
104 $search->delete( $this->id, $normalTitle );
105 continue;
106 } elseif ( $this->content === false ) {
107 $search->updateTitle( $this->id, $normalTitle );
108 continue;
109 }
110
111 $text = $search->getTextFromContent( $this->title, $this->content );
112 if ( !$search->textAlreadyUpdatedForIndex() ) {
113 $text = self::updateText( $text );
114 }
115
116 # Perform the actual update
117 $search->update( $this->id, $normalTitle, $search->normalizeText( $text ) );
118 }
119
120 wfProfileOut( __METHOD__ );
121 }
122
123 /**
124 * Clean text for indexing. Only really suitable for indexing in databases.
125 * If you're using a real search engine, you'll probably want to override
126 * this behavior and do something nicer with the original wikitext.
127 */
128 public static function updateText( $text ) {
129 global $wgContLang;
130
131 # Language-specific strip/conversion
132 $text = $wgContLang->normalizeForSearch( $text );
133 $lc = SearchEngine::legalSearchChars() . '&#;';
134
135 wfProfileIn( __METHOD__ . '-regexps' );
136 $text = preg_replace( "/<\\/?\\s*[A-Za-z][^>]*?>/",
137 ' ', $wgContLang->lc( " " . $text . " " ) ); # Strip HTML markup
138 $text = preg_replace( "/(^|\\n)==\\s*([^\\n]+)\\s*==(\\s)/sD",
139 "\\1\\2 \\2 \\2\\3", $text ); # Emphasize headings
140
141 # Strip external URLs
142 $uc = "A-Za-z0-9_\\/:.,~%\\-+&;#?!=()@\\x80-\\xFF";
143 $protos = "http|https|ftp|mailto|news|gopher";
144 $pat = "/(^|[^\\[])({$protos}):[{$uc}]+([^{$uc}]|$)/";
145 $text = preg_replace( $pat, "\\1 \\3", $text );
146
147 $p1 = "/([^\\[])\\[({$protos}):[{$uc}]+]/";
148 $p2 = "/([^\\[])\\[({$protos}):[{$uc}]+\\s+([^\\]]+)]/";
149 $text = preg_replace( $p1, "\\1 ", $text );
150 $text = preg_replace( $p2, "\\1 \\3 ", $text );
151
152 # Internal image links
153 $pat2 = "/\\[\\[image:([{$uc}]+)\\.(gif|png|jpg|jpeg)([^{$uc}])/i";
154 $text = preg_replace( $pat2, " \\1 \\3", $text );
155
156 $text = preg_replace( "/([^{$lc}])([{$lc}]+)]]([a-z]+)/",
157 "\\1\\2 \\2\\3", $text ); # Handle [[game]]s
158
159 # Strip all remaining non-search characters
160 $text = preg_replace( "/[^{$lc}]+/", " ", $text );
161
162 # Handle 's, s'
163 #
164 # $text = preg_replace( "/([{$lc}]+)'s /", "\\1 \\1's ", $text );
165 # $text = preg_replace( "/([{$lc}]+)s' /", "\\1s ", $text );
166 #
167 # These tail-anchored regexps are insanely slow. The worst case comes
168 # when Japanese or Chinese text (ie, no word spacing) is written on
169 # a wiki configured for Western UTF-8 mode. The Unicode characters are
170 # expanded to hex codes and the "words" are very long paragraph-length
171 # monstrosities. On a large page the above regexps may take over 20
172 # seconds *each* on a 1GHz-level processor.
173 #
174 # Following are reversed versions which are consistently fast
175 # (about 3 milliseconds on 1GHz-level processor).
176 #
177 $text = strrev( preg_replace( "/ s'([{$lc}]+)/", " s'\\1 \\1", strrev( $text ) ) );
178 $text = strrev( preg_replace( "/ 's([{$lc}]+)/", " s\\1", strrev( $text ) ) );
179
180 # Strip wiki '' and '''
181 $text = preg_replace( "/''[']*/", " ", $text );
182 wfProfileOut( __METHOD__ . '-regexps' );
183
184 return $text;
185 }
186 }