/** * Retrieves text from a page, or a cached copy unless $force is true * * @param bool $force Grab text from the API, don't use the cached copy (default: false) * @param string|int $section Section title or ID to retrieve * @return string|bool Page content */ public function get_text($force = false, $section = null) { pecho("Getting page content for {$this->title}..\n\n", PECHO_NOTICE); if (!$this->exists) { return null; } if (!is_null($section)) { if (empty($this->content)) { $this->content = $this->history(1, "older", true); $this->content = $this->content[0]['*']; } $sections = $this->wiki->apiQuery(array('action' => 'parse', 'page' => $this->title, 'prop' => 'sections')); if (!is_numeric($section)) { foreach ($sections['parse']['sections'] as $section3) { if ($section3['line'] == $section) { $section = $section3['number']; } } } if (!is_numeric($section)) { pecho("Warning: Section not found.\n\n", PECHO_WARN); return false; } $offsets = array('0' => '0'); if ($this->wiki->get_mw_version() < '1.16') { //FIXME: Implement proper notice suppression $ids = array(); foreach ($sections['parse']['sections'] as $section3) { $ids[$section3['line']] = $section3['number']; } $regex = '/^(=+)\\s*(.*?)\\s*(\\1)\\s*/m'; preg_match_all($regex, $this->content, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); foreach ($m as $id => $match) { $offsets[$id + 1] = $match[0][1]; } } else { foreach ($sections['parse']['sections'] as $section2) { $offsets[$section2['number']] = $section2['byteoffset']; } } if (intval($section) != count($offsets) - 1) { $length = $offsets[$section + 1] - $offsets[$section]; } if (isset($length)) { $substr = mb_substr($this->content, $offsets[$section], $length); } else { $substr = mb_substr($this->content, $offsets[$section]); } return $substr; } else { if (!$force && $this->content !== null) { return $this->content; } $this->content = $this->history(1, "older", true); $this->content = $this->content[0]['*']; return $this->content; } }